Graphic bug I can't understand!

Previous topic - Next topic

François Vanzeveren

Dear all,

I need your help to explain me the bug I found.

The attached file is the source of the game I wrote "SimOniZ".
THe attached version has no problem... after I discovered a workaround for the bug.

In the source files, I declares global constants for the sprite ids of the grafix components.
e.g. in the play.gbas file
Code (glbasic) Select

// grafix components
GLOBAL GFX_GREEN% = 0 ;  // Green button
GLOBAL GFX_YELLOW% = 1 ;
GLOBAL GFX_BLUE% = 2 ;
GLOBAL GFX_RED% = 3 ;
GLOBAL GFX_PAUSE% = 4 ; // Pause button
GLOBAL GFX_STATUS% = 5 ;
GLOBAL GFX_FONT15% = 100 ;


To reproduce the bug, open the project and edit the credits.gbas file
Code (glbasic) Select
//-----------------------------------------------------------------------------
// C O N S T A N T S
//-----------------------------------------------------------------------------

// menu states
GLOBAL MS_NONE% = 0;
GLOBAL MS_BACK% = 1 ;

// grafix components
GLOBAL GFX_BACK% = 0 ; // Bouton "Back"
GLOBAL GFX_FONT15% = 100 ;


Replace the line
Code (glbasic) Select
GLOBAL GFX_FONT15% = 100 ;
by
Code (glbasic) Select
GLOBAL GFX_FONT15% = 1 ;

This change generates the following problem:
* in the play screen, the yellow button is not displayed
* in the pause screen, the abort button is not displayed

Doing the same change in the highscores.gbas does not cause the bug!

I am completely lost.

Thanks for the time you will spent to explain me what is going wrong!

François

[attachment deleted by admin]

okee

#1
why do you have a semicolon after every statement ?

Isn't Global exactly that? Global across all the files in your project
so if you declare Global variables in your main project file the other
files in your project will have access to them. You were declaring
GLOBAL GFX_FONT15%  = 1 in one file and
GLOBAL GFX_HIGHSCORES%    = 1 ; in another file

If 1 is being used for the id of an image then the last declaration
that the compiler reads will be given the id 1,
The best bet is declare all your global variables in your main file
and give each of the image variables a unique number
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

François Vanzeveren

Hi,

Defined in the main file globals are accessible anywhere, but defined in a source file they can only be accessed in that file.

Furthermore, everytime I call the main routine of a source file, I loadanim the grafix components, overriding the previous anim with the same id.

Finally, why  does the green button (GFX_GREEN% = 0) not show the same bug?

okee

QuoteDefined in the main file globals are accessible anywhere, but defined in a source file they can only be accessed in that file.
I just did a test but I'm using GLB v 8.0 and a global declared in an included file is available to the main file.
So I think if you declare a global in any file it's global across all
but if you declare as local (outside a function or sub) it's just available withing that file
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

François Vanzeveren

#4
Defining
Code (glbasic) Select
LOCAL GFX_FONT15% = 1 ;

returns

Code (glbasic) Select


_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.6.973 SN:126b5585 - 3D, NET
"credits.gbas"(17) error : command not inside function or sub


I use GLB 8 too

If I write
Code (glbasic) Select

// grafix components
GLOBAL GFX_BACK% = 1 ; // Bouton "Back"
GLOBAL GFX_FONT15% = 0 ;


Then it is the green button that shows the bug, not the yello  :S

okee

Ok sorry didn't test Local :) yeah that gives an error outside a function in an included file
but Global is ok

main file
Code (glbasic) Select

SETSCREEN 800,600,1
GLOBAL Name$ = "Mary"

WHILE KEY(1) <> 1
editname()
PRINT Name$,100,100
PRINT SurName$,100,150
SHOWSCREEN

WEND


included file
Code (glbasic) Select
GLOBAL SurName$ = "Krypton"

FUNCTION editname:

Name$ = Name$ + "s"

ENDFUNCTION
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

François Vanzeveren

Quote from: okee on 2010-Jul-28
QuoteDefined in the main file globals are accessible anywhere, but defined in a source file they can only be accessed in that file.
I just did a test but I'm using GLB v 8.0 and a global declared in an included file is available to the main file.
So I think if you declare a global in any file it's global across all
but if you declare as local (outside a function or sub) it's just available withing that file

I made the test

Main file:
Code (glbasic) Select

GLOBAL a;

a = 1;

GOSUB test2 ;

PRINT a, 100, 100;

PRINT b, 200, 100;

SHOWSCREEN;

MOUSEWAIT;


Source file test2.gbas
Code (glbasic) Select

GLOBAL b;

SUB test2:
b = 2 ;
ENDSUB


Code (glbasic) Select


_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.6.973 SN:126b5585 - 3D, NET
"test.gbas"(17) error : variable is not explicitly defined : b



I conclude that b is "global" only within test2.gbas...

okee

Hmm... it will work if you give it a value ?
GLOBAL b = 2
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

François Vanzeveren

GOOD! You are right!

Code (glbasic) Select

GLOBAL b = 3;

SUB test2:
b = 2 ;
PRINT a, 250, 300 ;
ENDSUB


In the main file, b equals 2

Weird!!!!

okee

I think it would be worth giving all your GFX variables a unique number
I'd say that would solve your problem.
P.S the semi colon ?  is that a habit from pascal programming ?
didn't even know GLB could take them :)
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

François Vanzeveren

Quote from: okee on 2010-Jul-28
I think it would be worth giving all your GFX variables a unique number
I'd say that would solve your problem.
P.S the semi colon ?  is that a habit from pascal programming ?
didn't even know GLB could take them :)

The semi-colon, an old habit of the C time :)

MrTAToad

You should be using GENSPRITE for loading sprites to guarantee a number that is free.  That will also prevent a sprite being replaced by loading into an index that already has a sprite.

In addition, if you are going to use a variable just for storing these indexes, you might as well use CONSTANT for extra coding protection.


François Vanzeveren

THanks to okee and MrTAToad...

By the way, what do you think about the code structure put in place in my project? Do you have suggestions to improve the structure? I tried by a complete code refactoring of SimOniZ to reach a kind of game "template" easily maintainable and reusable for other projects.

Thanks!

MrTAToad

I would do all the loading of sprites etc in one place, rather than split over the many files.

Apart from that it is nicely laid out.

Schranz0r

GLOBAL b = 3 WORKS, Ocean ! You can do that....


NEVER call functions with GLOBAL:

GLOBAL b = foo() //<----- BAM, doesen't work!

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard