SDL OOP problem

Hello. This is my first try to make my SDL program object oriented. it runs and render the background just fine. But it dosnt respond to any event. Nothing happends when i press the X button. the game is running in game class. I also have a base class which contains some constants like screen width,height and such. It felt unecessary to post base.

Hope you can solve this, excuse my bad english!
Please if you got any constructive critic to me please type it. I am new to SDL.

Edit: The X button works fine. Just stupid fail by me.

But i got another error that i really dont understand. All the keys in my key array is always true. I cant find the solution for this problem.

EDIT2: Now im even more confused. when i use this code
1
2
3
4
if(!keys[SDLK_ESCAPE])
{
  running = false;
}


The program quits when i release the key. im tottaly confused


Here is game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include "base.h"

class game : base
{
public:
	game(void);
	bool init();
	void GameLoop();
	void HandleEvents();
	void Update();
	void Render();
	~game(void);
private:
	SDL_Surface *screen,*BackGround;
	SDL_Rect BackGroundRect;
	bool running;
	bool keys[272];
	SDL_Event event;
};


here is game.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "game.h"


game::game(void)
{
	if(this->init() == false)
	{
		exit(0);
	}
	running = true;
	BackGround = Load_Image("SpaceBackGround.png");
	this->GameLoop();
	
}

void game::GameLoop()
{
	Uint32 start,now;
	while(running)
	{
		start = SDL_GetTicks();
		this->HandleEvents();
		
		this->Update();

		this->Render();

		now = SDL_GetTicks();
		if(1000/FPS > now - start)
		{
			SDL_Delay(1000/FPS - (now - start));
		}
	}
}

void game::HandleEvents()
{
	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{
		case SDL_QUIT:
			running = false;
			break;
		case SDL_KEYDOWN:
			keys[event.key.keysym.sym] = true;
			break;
		case SDL_KEYUP:
			keys[event.key.keysym.sym] = false;
			break;
		}
	}
}

void game::Update()
{

}

void game::Render()
{
	SDL_BlitSurface(BackGround,NULL,screen,NULL);
	SDL_Flip(screen);
}

game::~game(void)
{
	SDL_FreeSurface(BackGround);
}

bool game::init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		cout << "SDL could not initialize" << endl;
		return false;
	}else{
		cout << "SDL initialized" << endl;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
	if(screen != NULL)
	{
		cout << "Screen initialized" << endl;
	}else{
		cout << "Screen could not initialize" << endl;
		return false;
	}
	return true;

}
Last edited on
The keys array is too small to store all possible key values. Either make the array big enough to store all the keys
bool keys[SDLK_LAST];
or add if statements to the cases in the HandleEvents function to check so that the key value is below 272
if (event.key.keysym.sym < 272) {keys[event.key.keysym.sym] = true;}.

I don't see that you initialize the keys array anywhere. Make sure to initialize all the elements in the array to false before using it.

I mentioned in another thread that I recommended you to use SDL_GetKeyState. I still think that is better because the problem you try to battle here is already solved by SDL. You just need to call SDL_GetKeyState to get the array and it's ready to use.
Last edited on
Topic archived. No new replies allowed.