Help with SDL sprite animation using SDL_Event

What I'm trying to do is have a function that returns the correct animation based on the key input of the user. This is a few snips of what I have.

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
  //the declaration of the function, located in a class called cHero
  SDL_Texture* GetAnimation(SDL_Event* movementEvent);

//this is the function
SDL_Texture* cHero::GetAnimation(SDL_Event* movementEvent)
{
	switch (movementEvent->type)
	{
	case SDLK_LEFT:
		facingLeft = true;
		animation = runLeftTexture;
		frames = (ticks / 100) % 6;
		break;
	case SDLK_RIGHT:
		facingLeft = false;
		animation = runRightTexture;
		frames = (ticks / 100) % 6;
		break;
	case SDLK_UP:
		break;
	case SDLK_DOWN:
		break;
	default:
		break;
	}

	return animation;
}

//and here is the gameloop
void cGameLoop::MainLoop()
{
	while (!quit && setup->GetMainEvent()->type != SDL_QUIT)
	{
		setup->Begin();

		keyEvent = new SDL_Event;

		hero->GetAnimation(keyEvent);
		
		setup->End();
	}
}


I know i'm asking a lot at once but that's because i'm not quite sure where I went wrong. can anyone help? or at least steer me in the right direction. I don't even know where begin. I got confused somewhere along the way :/

unfortunately I had it working but then I split it into classes and things got complex
Last edited on
You need to declare the variable animation:
1
2
3
4
5
6
7
8
SDL_Texture* cHero::GetAnimation(SDL_Event* movementEvent)
{
	SDL_Texture* animation = nullptr; // Note
	switch (movementEvent->type)
	{
	case SDLK_LEFT:
		facingLeft = true;
		animation = runLeftTexture;
You are leaking insane amounts of memory because you create a new SDL_Event object every loop iteration and never delete any of them.
Ok I think I'm not getting across what I mean to. I want to return the animation, which is declared in hero.h, but when I launch it essentially freezes and shuts down.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef FIRSTPLATFORMER_HERO_H_
#define FIRSTPLATFORMER_HERO_H_

#include "main.h"
#include "setup.h"

class cHero
{
public:
	cHero(SDL_Renderer* passedRenderer);
	~cHero();

	void Draw();

	SDL_Texture* GetAnimation(SDL_Event* movementEvent);

private:
	SDL_Texture* animation;

	bool facingLeft;

	SDL_Surface* runRightImage;
	SDL_Texture* runRightTexture;

	SDL_Surface* idleRightImage;
	SDL_Texture* idleRightTexture;

	SDL_Surface* runLeftImage;
	SDL_Texture* runLeftTexture;

	SDL_Surface* idleLeftImage;
	SDL_Texture* idleLeftTexture;

	SDL_Rect srcrect;
	SDL_Rect dstrect;

	SDL_Renderer* renderer;

	Uint32 frames;

	Uint32 ticks;
};

#endif//FIRSTPLATFORMER_HERO_H_

#include "hero.h"

cHero::cHero(SDL_Renderer* passedRenderer)
{
	renderer = passedRenderer;

	frames = 1;

	runRightImage = IMG_Load("runRightAnimation.png");
	runRightTexture = SDL_CreateTextureFromSurface(renderer, runRightImage);

	idleRightImage = IMG_Load("idleRightAnimation.png");
	idleRightTexture = SDL_CreateTextureFromSurface(renderer, idleRightImage);

	runLeftImage = IMG_Load("runLeftAnimation.png");
	runLeftTexture = SDL_CreateTextureFromSurface(renderer, runLeftImage);

	idleLeftImage = IMG_Load("idleLeftAnimation.png");
	idleLeftTexture = SDL_CreateTextureFromSurface(renderer, idleLeftImage);

	SDL_Rect srcrect = { frames * 50, 0, 50, 75 };
	SDL_Rect dstrect = { 10, 400, 50, 75 };

	Uint32 ticks = SDL_GetTicks();

	Uint32 frames = (ticks / 100) % 1;

	facingLeft = false;
}

cHero::~cHero()
{
	SDL_DestroyTexture(runRightTexture);
	SDL_FreeSurface(runRightImage);

	SDL_DestroyTexture(idleRightTexture);
	SDL_FreeSurface(idleRightImage);

	SDL_DestroyTexture(runLeftTexture);
	SDL_FreeSurface(runLeftImage);

	SDL_DestroyTexture(idleLeftTexture);
	SDL_FreeSurface(idleLeftImage);
}

void cHero::Draw()
{
	SDL_RenderCopy(renderer, animation, &srcrect, &dstrect);
}

SDL_Texture* cHero::GetAnimation(SDL_Event* movementEvent)
{
	switch (movementEvent->type)
	{
	case SDLK_LEFT:
		facingLeft = true;
		animation = runLeftTexture;
		frames = (ticks / 100) % 6;
		break;
	case SDLK_RIGHT:
		facingLeft = false;
		animation = runRightTexture;
		frames = (ticks / 100) % 6;
		break;
	case SDLK_UP:
		break;
	case SDLK_DOWN:
		break;
	default:
		break;
	}

	return animation;
}

#ifndef FIRSTPLATFORMER_GAMELOOP_H_
#define FIRSTPLATFORMER_GAMELOOP_H_

#include "setup.h"
#include "hero.h"

class cGameLoop
{
public:
	cGameLoop(void);
	~cGameLoop();

	void MainLoop();

private:
	bool quit;

	cSetup* setup;

	cHero* hero;
};

#endif//FIRSTPLATFORMER_GAMELOOP_H_

#include "main.h"
#include "gameloop.h"

cGameLoop::cGameLoop(void)
{
	quit = false;


	setup = new cSetup(&quit);
}

cGameLoop::~cGameLoop()
{
}

void cGameLoop::MainLoop()
{
	while (!quit && setup->GetMainEvent()->type != SDL_QUIT)
	{
		setup->Begin();

		hero->GetAnimation(setup->GetMainEvent());
		
		setup->End();
	}
}

#define FIRSTPLATFORMER_SETUP_H_

#include "main.h"

class cSetup
{
public:
	cSetup(bool* quit);
	~cSetup();

	void GameLoop();

	SDL_Renderer* GetRenderer();
	SDL_Event* GetMainEvent();

	void Begin();
	void End();

private:
	SDL_Event* mainEvent;

	SDL_Window* window;
	SDL_Renderer* renderer;

	
};

#endif//FIRSTPLATFORMER_SETUP_H_

#include "setup.h"

cSetup::cSetup(bool* quit)
{
	SDL_Init(SDL_INIT_VIDEO);

	window = SDL_CreateWindow("SDL2 Sprite Sheets", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 500, 0);
	renderer = SDL_CreateRenderer(window, -1, 0);

	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
	SDL_RenderClear(renderer);

	mainEvent = new SDL_Event;
}

cSetup::~cSetup()
{
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);

	delete mainEvent;

	IMG_Quit();
	SDL_Quit();
}

SDL_Renderer* cSetup::GetRenderer()
{
	return renderer;
}

SDL_Event* cSetup::GetMainEvent()
{
	return mainEvent;
}

void cSetup::Begin()
{
	SDL_PollEvent(mainEvent);
	SDL_RenderClear(renderer);
}

void cSetup::End()
{
	SDL_RenderPresent(renderer);
}

#ifndef FIRSTPLATFORMER_MAIN_H_
#define FIRSTPLATFORMER_MAIN_H_

#include <iostream>


#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>

#endif//FIRSTPLATFORMER_MAIN_H_

#include "main.h"
#include "gameloop.h"

int main(int argc, char *argv[])
{
	cGameLoop gameloop;
	gameloop.MainLoop();
	gameloop.~cGameLoop();

	return 0;
}


It builds just fine but gos to th "stopped working" message
Okay, so I figured out the problem lies somewhere in the hero header or cpp file
Don't call the cGameLoop destructor explicitly. It will be called automatically when the object goes out of scope (at the end of the main function).

Have you got anything to show up on the screen? Are you sure the textures loads successfully?

The ticks member variable in cHero is never initialized. Note that in the constructor you declare and initialize a local variable named ticks but the member variable with that name is left uninitialized.
Last edited on
Topic archived. No new replies allowed.