GLBasic forum

Main forum => GLBasic - en => Topic started by: François Vanzeveren on 2010-Jul-28

Title: Graphic bug I can't understand!
Post by: François Vanzeveren on 2010-Jul-28
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]
Title: Re: Graphic bug I can't understand!
Post by: okee on 2010-Jul-28
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
Title: Re: Graphic bug I can't understand!
Post by: François Vanzeveren on 2010-Jul-28
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?
Title: Re: Graphic bug I can't understand!
Post by: okee on 2010-Jul-28
Quote
Defined 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
Title: Re: Graphic bug I can't understand!
Post by: François Vanzeveren on 2010-Jul-28
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
Title: Re: Graphic bug I can't understand!
Post by: okee on 2010-Jul-28
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
Title: Re: Graphic bug I can't understand!
Post by: François Vanzeveren on 2010-Jul-28
Quote
Defined 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...
Title: Re: Graphic bug I can't understand!
Post by: okee on 2010-Jul-28
Hmm... it will work if you give it a value ?
GLOBAL b = 2
Title: Re: Graphic bug I can't understand!
Post by: François Vanzeveren on 2010-Jul-28
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!!!!
Title: Re: Graphic bug I can't understand!
Post by: 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 :)
Title: Re: Graphic bug I can't understand!
Post by: François Vanzeveren 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 :)
Title: Re: Graphic bug I can't understand!
Post by: MrTAToad on 2010-Jul-29
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.

Title: Re: Graphic bug I can't understand!
Post by: François Vanzeveren on 2010-Jul-29
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!
Title: Re: Graphic bug I can't understand!
Post by: MrTAToad on 2010-Jul-29
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.
Title: Re: Graphic bug I can't understand!
Post by: Schranz0r on 2010-Jul-29
GLOBAL b = 3 WORKS, Ocean ! You can do that....


NEVER call functions with GLOBAL:

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

Title: Re: Graphic bug I can't understand!
Post by: Moru on 2010-Jul-29
It has been working now for a while again so I started using it.
Title: Re: Graphic bug I can't understand!
Post by: MrTAToad on 2010-Jul-29
Quote
don't do that in GLB (assigning a value when initialising/defining variables) as this is only guaranteed to work when used in conjunction with the RGB() function...
Constants should be used for things like that...
Title: Re: Graphic bug I can't understand!
Post by: Kitty Hello on 2010-Jul-29
You can assign GLOBAL a = foo(), but you are not able to use strings or any of the GLBasic commands inside. The problem is, that this foo() is called in the constructor _before_ the GLBasic engine is set up, whereas
GLOBAL g
g=foo() // this assignement is done during runtime.
Title: Re: Graphic bug I can't understand!
Post by: MrTAToad on 2010-Jul-29
You can assign GLOBAL a = foo(), but you are not able to use strings or any of the GLBasic commands inside. The problem is, that this foo() is called in the constructor _before_ the GLBasic engine is set up, whereas
GLOBAL g
g=foo() // this assignement is done during runtime.
I tried that, but the program didn't actually run :)

Code: GLBasic [Select]
GLOBAL a%=foo()

DEBUG a%
END

FUNCTION foo%:
        RETURN 12
ENDFUNCTION
Title: Re: Graphic bug I can't understand!
Post by: okee on 2010-Jul-29
Regarding the scope of Global Local
From tests this is what i gathered:

2 files in a project
main.gbas //the main code file
include.gbas //included file

Global  declared in main.gbas is available to main.gbas and include.gbas
Global declared in include.gbas must have an assignment otherwise the compiler throws variable is not explicitly defined error
once declared with an assignment it's available to main.gbas and include.gbas

Local declared outside a function/sub in main.gbas is only available to functions/subs in main.gbas
Local outside a function/sub in include.gbas  is not allowed

Also using GENSPRITE() as described in the manual
Code: GLBasic [Select]
GLOBAL gImgFoo = GENSPRITE(); LOADSPRITE "foo.png", gImgFoo
used to cause problems when declaring and assigning has this changed ?
or do we still have to do
Code: GLBasic [Select]
GLOBAL gImgFoo
gImgFoo = GENSPRITE()


okee
Title: Re: Graphic bug I can't understand!
Post by: MrTAToad on 2010-Jul-29
RGB is the only command that be used with variable initialisation.
Title: Re: Graphic bug I can't understand!
Post by: matchy on 2010-Jul-29
Oh yeah! Even this works...
Code: GLBasic [Select]
CONSTANT rnd_color=RGB(RND(255),RND(255),RND(255))
CONSTANT rnd_red=RND(255)