Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

variable scope?

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what's wrong with this code? static int yes; if ( yes == 0 ) { cGraphics Graphics(m_Wnd); Graphics.Init(); Graphics.SetBufferSize(1024, 768); Graphics.CreateDevice(); cTexture Texture; Texture.Init(&Graphics); Texture.Load("Back.bmp", 0); cSprite Sprite; Sprite.Init(&Graphics); yes = 1; } whenever i try to access the variable "Graphics" from outside the if statment, it doesn't work... how would i be able to declare it once, but still have it only declared once without having to put an if statement?

Share this post


Link to post
Share on other sites
Advertisement
Since Graphics is declared within those braces, it only exists within those braces and will be freed when that section of code is completed. If you want it to exist outside of that scope, then you need to declare it outside of that scope. I'm guessing you're pretty new to C++ and I'd recommend that you spend a while studying it before you jump into DirectX.

BTW, this question has nothing to do with DirectX and really isn't appropriate here.

Share this post


Link to post
Share on other sites
How about this? :


static int yes;
cGraphics* Graphics;
cTexture* Texture;
cSprite* Sprite;

if ( yes == 0 )
{
cGraphics = new Graphics(m_Wnd);
Graphics->Init();
Graphics->SetBufferSize(1024, 768);
Graphics->CreateDevice();

cTexture = new Texture;
Texture->Init(&Graphics);
Texture->Load("Back.bmp", 0);

cSprite = new Sprite;
Sprite->Init(&Graphics);

yes = 1;
}

//Access the objects here
Graphics->RenderSpriteWithTexture(Sprite, Texture);

//Clean Up
delete Texture;
delete Sprite;
delete Graphics;



Hope that helps

Share this post


Link to post
Share on other sites
well, the only problem with that code is see, all that code is in the cApp::Frame() function, and will be executed every frame. I want to be able to initialize everything (either before the VERY first frame and be able to see it in the frame function) or within the frame function and have it just do a check to see if it's been initiallized.

As for me being a noobie, i've been coding in C++ for a while, but i had to stop for a few months to learn DirectX, i spent all that time reading and haven't had time to practice C++ so i forgot some concepts :-P

And sry for putting it in DirectX forum, i wasn't thinking, i just thought it was appropriate cuz i have my Engine which handles directX code in there (an example that i've been coding for a while, i made my own little engine a while ago :-P)

Share this post


Link to post
Share on other sites
i think i have an idea for my own question, i will make those variables member variables, that way they will be visible to the whole class AND to all functinos :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!