[C++] Image related question?

Samael88

Evil always finds a way
Reaction score
181
Hi everyone.

It sure was a long time since I posted here, but I have run into another problem when developing my game:)

I think that I get the basic builds of a program and such, but I can't figure out a good way to load my images:eek:

Or more precicely, when to load them.

My build so far is I got a game loop like this:

Code:
while [quit == false] //main loop
{
    //1.
    while[mainmenu == true] //main
    {  
        //2.
        //handle main menu functions here..
        mainmenufunc();
    }
}


1. My first choise is to declare them as globals, and then load them in the loop. at the 1. and clean them at the end.

2. My second choise is load them whenever I enter a loop, and thus have a secondary bool to check if the loop has ran once, and another to check if it should cleanup. The variables are also declared as globals for this to work.

3. A third alternative would be to load them in the respective function like this:

Code:
mainmenufunc()
{
   static bool ran_once = false;
   static bool exit = false;
   if (ran_once == false)
   {
      ran_once == true;
      //declare images and load them.
   }

    //do main menu stuff
    if (exit == true)
    {
       //cleanup the images 
    }
}

I can't really figure out which one to use, and perhaps there is a better alternative that I have not thought of yet. Help, anyone?
 

Samael88

Evil always finds a way
Reaction score
181
It's like this, since I don't have many images yet, all of them works fine.
But I am affraid that it will be to much if the variables are declared in the function later on, since it will be quite a few images to load and such..
 

swe_suffer

New Member
Reaction score
20
Hmm.. if with "load" you mean load the specific image files then I'd say 1, since they probably will be used in more than one loop.. (or enter the loop more than once)

And, what framework do you work with? (as I presume you haven't written your own..)
 

Samael88

Evil always finds a way
Reaction score
181
I'm using SDL, since it is easy to use for a beginner:)

I figured I did this:

Code:
newgame_func()
{
   //load images here.
   while(newgame == false)
    {
       //use images and such here.
    }
   //clean images here.
}

And then just call it from the main loop using an if to check which one of the functions to enter.

And since it is 2D I also figured that the things I need to have in more than one place I will have as globals anyway:)

I am currently considering the possibility to change the entire build at this stage since I think I might have stumbled upon a better way to do things for now:)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top