Hi, all. Before I begin I am new to C++.

Ok. I'm trying to create a simple 2D game in C++ using SDL, however I have hit upon a small problem. When I try to close the game window nothing happens. I think it's because when I create a new object to change the boolean value of 'running' in my Loop class, it doesn't change the original set in Loop.cpp but the new copy created only for my new object's instance, right?

Here is some sample code (see lines 20 and 82):

// Loop.h
class Loop {

    public:

        void loop(); // Start the game's loop
        bool running; // Game is running or not

};

// Loop.cpp

#include "Loop.h"
#include "GameEngine.h"
#include "EventHandler.h"

// Start game loop
void Loop::loop(){

    running = true;

    Events events;

    // Check for all updates
    while(running == true){ // While game is running

        events.readInput();
    }

    // Game loop has stopped, start shutdown
    stopGameEngine();

}

// EventHandler.h

#include "SDL/SDL.H"

class EventHandler {

    public:

        void readInput();  // Read the input and store the information obtained (must be called before any other events function)

    private:

        SDL_Event event; // SDL_Event structure stores event infomation

};

// EventHandler.cpp

#include "EventHandler.h"
#include "Loop.h"
#include "game_engine.h"

// Get input and store information (must be called before any other events function)
void Events::readInput(){

    // Look for events
    while (SDL_PollEvent(&event)) {
            if (event.type == SDL_KEYDOWN) { // Key pressed
                    switch (event.key.keysym.sym) {
                        case SDLK_UP: // Up key pressed
                            break;
                        case SDLK_DOWN: //Down key pressed
                            break;
                        case SDLK_LEFT: // Left key pressed
                            break;
                        case SDLK_RIGHT: // Right key pressed
                            break;

                        default:
                            // Do nothing
                            break;
                            }
            }
            //If the user has pressed X out the window
            else if(event.type == SDL_QUIT) {
                    //Quit the program
                    Loop loop;
                    loop.running = false;
            }
    }
}

So my question is, how can I change the original value of 'running' from EventHandler.cpp?

Thanks.

Edited 2 Years Ago by champmanking

Yes, it is an instance variable, and only affects the instance you set it in. Make it a class static variable, and that should do what you want.

Like this?

// Loop.cpp

bool Loop::running = true; // in place of running = true; in previous code

// Loop.h
class Loop {
public:
       static bool running; // Game is running or not
};

// EventHandler.cpp

else if(event.type == SDL_QUIT) {
      //Quit the program
      Loop loop;
      loop.running = false;
      // or Loop::running = false; instead of using object
     }
}

Are there any other ways?

By the way, works perfectly, thanks.

Edited 2 Years Ago by champmanking

This question has already been answered. Start a new discussion instead.