I am learning to build programs in c++ and am stuck at something basic. I use SDL2 to get inputs from and to deal with screens etc. I have defined an object "Program" and an object "EventHandler". The "EventHandler" handles all events (sends the events to lower level objects), but the "EventHandler" should also be able to create a new window, thus to access "Program".
This means I guess that "EventHandler" should be on the same level as "Program" and they should both be able to communicate with each other. Can this be done in c++ and how? Maybe there is some other more logical way in doing this.
The code below does obviously not work because of the order in which the classes are defined, and my selfmade "&this" to send the address of "program" is wrong, but it gives a nice overview of what I am trying to do.
//handles all events, is in contact with and same level as Program
class EventHandler {
private:
Program *program = NULL;
SDL_Event e;
//inputarrays
const Uint8 *currentKeyStates;
int mouseX = 0;
int mouseY = 0;
bool mousemotion = false;
int mouseButtons[4] = {0, 0, 0, 0};
public:
EventHandler(Program *p) {
program = p;
}
void handleEvents() {
while(SDL_PollEvent(&e) != 0) {
}
}
};
class Program {
private:
EventHandler *eh = NULL;
std::vector<Window> windows;
public:
bool running;
Program() {
//Here the most basic form of the program is written
//For this program we will use SDL2
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//This program uses 1 window
Window window(200, 200, 1200, 1000, "");
windows.push_back(window);
//Adds an event handler
eh = new EventHandler(&this);
//now simply run the program
run();
}
void run() {
running = true;
//while (running) {
//}
SDL_Delay(2000);
delete eh;
//Quit SDL subsystems
SDL_Quit();
}
};
int main( int argc, char* args[]) {
Program program;
return 0;
}