Need help with simple program
Hello, I need some help with a very simple program.
int main()
{
GameInit();
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
GameMain();
}
return 0;
}
That part works fine, what I want to do next is to make an object in GameInit() and be able to access it in GameMain().
int GameInit()
{
Sprite TankUp(4);
return 0;
}
int GameMain()
{
TankUp.Update();
return 0;
}
class Sprite
{
public:
int frame;
Sprite (int);
void Update();
};
Sprite::Sprite(int f)
{
frame=f;
}
void Sprite::Update()
{
frame++;
}
Right now this wont compile. How can I make it so GameMain can access the sprite TankUp. This may be really basic, but I dont know how to do it, maybe its not allowed.
quote:Original post by asdasd12345You one more curly brace.int main(){ GameInit(); int done=0; while(done == 0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { GameMain(); } return 0; }}int GameInit(){ Sprite TankUp(4); return 0;}int GameMain(){ TankUp.Update(); return 0;}class Sprite{public:int frame;Sprite (int);void Update();};Sprite::Sprite(int f){ frame=f;}void Sprite::Update(){ frame++;}
The simplest option would be to make Tankup a global variable (place the initialization line outside of any function, preferably just after your include directives). If you are a beginner, this would definitely be your best bet. Look up variable scoping for more information.
Also make sure that you fix the error that alnite discovered.
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
[edited by - Thunder_Hawk on January 23, 2004 9:03:22 PM]
Also make sure that you fix the error that alnite discovered.
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
[edited by - Thunder_Hawk on January 23, 2004 9:03:22 PM]
Hm..some more problems than I expected:
TankUp(4) is local to GameInit(). You must make it global. put
TankUp is an array. Specify an index.
[edited by - alnite on January 23, 2004 9:03:24 PM]
int GameInit(){ Sprite TankUp(4); return 0;}
TankUp(4) is local to GameInit(). You must make it global. put
Sprite TankUp(4)
to the topmost, just before main()
, after #includes
. An array is denoted by []
, not ()
.int GameMain(){ TankUp.Update(); return 0;}
TankUp is an array. Specify an index.
[edited by - alnite on January 23, 2004 9:03:24 PM]
I was told maybe I read it in a book , that its better to keep away from using globals, so thats not really the case then?
Tankup isn''t an array (that''s a constructor argument specifying the first frame).
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
for SDL your main MUST MUST MUST MUST consults faq and says MUST be declared as
[Mercury Software] [Google!] [ Look I DONT Follow Trends ]
int main (int argc, char *argv[])
[Mercury Software] [Google!] [ Look I DONT Follow Trends ]
heres from the faq
[Mercury Software] [Google!] [ Look I DONT Follow Trends ]
Make sure that you are declaring main() as: #include "SDL.h" //or whatever my SDL and most peoples SDL is #include <SDL/SDL.h> i dont knw y sdl.h would be in every progs dir.int main(int argc, char *argv[])
[Mercury Software] [Google!] [ Look I DONT Follow Trends ]
quote:Original post by Thunder_HawkAh I see. my mistake. Sorry.
Tankup isn''t an array (that''s a constructor argument specifying the first frame).
Globals aren''t entirely evil. They just tend to make larger programs more confusing, and it cracks the fundamental of ''independence-ism''. If one of your functions uses a global, that function can''t run if the global is removed.
Here''s another alternative:
int main(){ Sprite TankUp(4); // code goes here GameMain( TankUp );}int GameMain( Sprite& tank ){ tank.Update();}
of course just doing
class Game {
Sprite TankUp;
Update();
Main();
};
would also solve that problem, even though i sometimes think its kind of silly to wrap your whole program in a class just for the sake of "clean" coding and being able to say "ha, see? now they arent global anymore". but as you dont need more than one Game you could make everything static, effectively its all global again and not different from placing it in a namespace, except being able to regulate access to different members via private and public. making the constructor private avoids creating instances.
of course now someone will come up and tell you about singletons, another concept i find highly overrated and too often used without thinking (just like quaternions *cough*).
class Game {
Sprite TankUp;
Update();
Main();
};
would also solve that problem, even though i sometimes think its kind of silly to wrap your whole program in a class just for the sake of "clean" coding and being able to say "ha, see? now they arent global anymore". but as you dont need more than one Game you could make everything static, effectively its all global again and not different from placing it in a namespace, except being able to regulate access to different members via private and public. making the constructor private avoids creating instances.
of course now someone will come up and tell you about singletons, another concept i find highly overrated and too often used without thinking (just like quaternions *cough*).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement