Archived

This topic is now archived and is closed to further replies.

asdasd12345

Need help with simple program

Recommended Posts

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.

Share this post


Link to post
Share on other sites
quote:
Original post by asdasd12345

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++;
}

You one more curly brace.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Hm..some more problems than I expected:

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]

Share this post


Link to post
Share on other sites
Tankup isn''t an array (that''s a constructor argument specifying the first frame).

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
quote:
Original post by Thunder_Hawk
Tankup isn''t an array (that''s a constructor argument specifying the first frame).
Ah I see. my mistake. Sorry.

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();
}

Share this post


Link to post
Share on other sites
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*).

Share this post


Link to post
Share on other sites
quote:
Original post by Trienco
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*).


What''s this about misuse of quaternions? *puts away quaternion shaded rendering code*

... sorry about being stupid...

Share this post


Link to post
Share on other sites