Help making classes

I know this is asking a lot but it will really help me learn. So I have trouble making classes because I flub up information exchange and stuff like that. Basically, this is what I have, all in one file except for the includes of iostream and some sdl includes. I was hoping you guys could help me split it into classes. That way I can have a better idea of how to split my work up in the future.
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
// this is my main.h file
#ifndef HARVESTERODYSSEY_MAIN_H_
#define HARVESTERODYSSEY_MAIN_H_

#include <iostream>

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

#endif//HARVESTERODYSSEY_MAIN_H_

// and this is the main.cpp
#include "Main.h"

int main(int argc, char *argv[])
{
	// represents if the user has or has not requested to quit the program
	bool quit = false;

	// the main event for the program
	SDL_Event event;

	// unsure of these
	SDL_Init(SDL_INIT_VIDEO);
	IMG_Init(IMG_INIT_PNG);

	// creates a window and renderer. the renderer is everything displayed in the window
	SDL_Window * window = SDL_CreateWindow("Harvester Odyssey", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1200, 800, 0);
	SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

	// sprite for running left
	SDL_Surface * runLeftImage = IMG_Load("runLeftAnimation.png");
	SDL_Texture * runLeftTexture = SDL_CreateTextureFromSurface(renderer, runLeftImage);

	// sprite for running right
	SDL_Surface * runRightImage = IMG_Load("runRightAnimation.png");
	SDL_Texture * runRightTexture = SDL_CreateTextureFromSurface(renderer, runRightImage);

	// sprite for standing left
	SDL_Surface * idleLeftImage = IMG_Load("idleLeftAnimation.png");
	SDL_Texture * idleLeftTexture = SDL_CreateTextureFromSurface(renderer, idleLeftImage);

	// sprite for standing right
	SDL_Surface * idleRightImage = IMG_Load("idleRightAnimation.png");
	SDL_Texture * idleRightTexture = SDL_CreateTextureFromSurface(renderer, idleRightImage);

	// sprite for jumping right
	SDL_Surface * jumpRightImage = IMG_Load("jumpRightAnimation.png");
	SDL_Texture * jumpRightTexture = SDL_CreateTextureFromSurface(renderer, jumpRightImage);

	// sprite for jumping left
	SDL_Surface * jumpLeftImage = IMG_Load("jumpLeftAnimation.png");
	SDL_Texture * jumpLeftTexture = SDL_CreateTextureFromSurface(renderer, jumpLeftImage);

	// represents the main character sprite
	SDL_Texture * animation = idleRightTexture;

	// represents if the sprite was last facing left or not
	bool facingLeft = false;

	// sets the background color of the renderer to gray
	SDL_SetRenderDrawColor(renderer, 75, 75, 75, 75);

	//clears the renderer of everything except the background color
	SDL_RenderClear(renderer);

	// the x and y position of there the sprite will be drawn
	int xCoord = 10;
	int yCoord = 700;

	// gets the time passed since the program started and stores it in this variable
	unsigned int time = SDL_GetTicks();

	// the main game loop which runs as long as the user doesn't request to quit the program
	while (!quit)
	{
		// recieves the next event in the que
		SDL_PollEvent(&event);

		// swich that closes the program if the user requests to do so
		switch (event.type)
		{
		case SDL_QUIT:
			quit = true;
			break;
		default:
			break;
		}

		
		// ANIMATION AND MOVEMENT MANAGMENT //

		// makes the sprite face the appropriate direction when no buttons are being pressed
		if (facingLeft == true)
		{
			animation = idleLeftTexture;
		}
		else
		{
			animation = idleRightTexture;
		}

		// this sets the number of frames and frame rate of the sprite
		Uint32 ticks = SDL_GetTicks();
		Uint32 sprite = (ticks / 100) % 1;
		
		// used to check the state of the keyboard
		const Uint8 *keyState = SDL_GetKeyboardState(NULL);

		// adjusts animation and movement based on key input
		if (keyState[SDL_SCANCODE_RIGHT])
		{
			facingLeft = false;
			animation = runRightTexture;
			sprite = (ticks / 100) % 6;
			if (SDL_GetTicks() > time + 4)
			{
				++xCoord;
				time = SDL_GetTicks();
			}
		}
		else if (keyState[SDL_SCANCODE_LEFT])
		{
			facingLeft = true;
			animation = runLeftTexture;
			sprite = (ticks / 100) % 6;
			if (SDL_GetTicks() > time + 4)
			{
				--xCoord;
				time = SDL_GetTicks();
			}
		}
		if (keyState[SDL_SCANCODE_UP])
		{
			if (facingLeft == true)
			{
				animation = jumpLeftTexture;
			}
			else
			{
				animation = jumpRightTexture;
			}
			sprite = (ticks / 100) % 4;
		}

		// controls which clip of the sprite sheet is used
		SDL_Rect srcrect = { sprite * 50, 0, 50, 75 };
		// controls the destination of the sprite sheet to be rendered
		SDL_Rect dstrect = { xCoord, yCoord, 50, 75 };

		// clears the renderer to the background color
		SDL_RenderClear(renderer);
		// prints the proper animation to the proper location based on the above information
		SDL_RenderCopy(renderer, animation, &srcrect, &dstrect);
		// shows renderer within the window
		SDL_RenderPresent(renderer);
	}

	// DESTROYS EVERYTHING, PREVENTING INFORMATION LEAKS

	SDL_DestroyTexture(runLeftTexture);
	SDL_FreeSurface(runLeftImage);

	SDL_DestroyTexture(runRightTexture);
	SDL_FreeSurface(runRightImage);

	SDL_DestroyTexture(idleLeftTexture);
	SDL_FreeSurface(idleLeftImage);

	SDL_DestroyTexture(idleRightTexture);
	SDL_FreeSurface(idleRightImage);

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);

	IMG_Quit();
	SDL_Quit();

	// terminates the program
	return 0;
}


As you see it's starting to become unmanageable as one file and I always seem to break something when I split stuff into classes, so any help would be much appreciated
Last edited on
Feel free to chime in, totally ok
closed account (SECMoG1T)
it's starting to become unmanageable
I would recommend that you make some functions to contain related codes
N then you just call them in main, you can put them in a file and # include them.
Good plan muchacho
You haven't shown your application objects, but really, you need to encalsulate your stuff into object. I guess, wrapping SDL2 would help a bit. I've done that for you, but placed the code in one file as it's easier to post. You should move the objects out into seperate files; the main program becomes much smaller.
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>

#include <string>

namespace SDLWrapper
{
	class Window
	{
		SDL_Window *handle;

	public:
		Window();
		Window(const std::string& name, int x, int y); // you can add the other flags here instead of hard coding them
		~Window();

		Window(const Window&) = delete;
		Window& operator=(const Window&) = delete;
		Window(Window&&);
		Window& operator=(Window&&);

		operator SDL_Window*();
	};

	class Renderer
	{
		SDL_Renderer* handle;

	public:
		Renderer(Window& window);
		~Renderer();

		Renderer(const Renderer&) = delete;
		Renderer& operator=(const Renderer&) = delete;

		Renderer(Renderer&&);
		Renderer& operator=(Renderer&&);

		operator SDL_Renderer*();
	};

	class Surface
	{
		SDL_Surface* handle;

	public:
		Surface(const std::string& filename);
		~Surface();

		Surface(const Surface&) = delete;
		Surface& operator=(const Surface&) = delete;

		Surface(Surface&&);
		Surface& operator=(Surface&&);

		operator SDL_Surface*();
	};

	class Texture
	{
		SDL_Texture* handle;

	public:
		Texture(Renderer& renderer, Surface& surface);
		~Texture();

		Texture(const Texture&) = delete;
		Texture& operator=(const Texture&) = delete;

		Texture(Texture&&);
		Texture& operator=(Texture&&);

		operator SDL_Texture*();
	};

	//-----------------------------------------------------------------------

	Window::Window() :
		handle(nullptr)
	{
	}

	Window::Window(const std::string& name, int x, int y) :
		handle(SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, x, y, 0))
	{
	}

	Window::~Window()
	{
		if (handle)
			SDL_DestroyWindow(handle);
	}

	Window::Window(Window&& n) :
		handle(n.handle)
	{
		handle = nullptr;
	}

	Window& Window::operator=(Window&& n)
	{
		if (this != &n)
		{
			if (handle)
				SDL_DestroyWindow(handle);
			handle = n.handle;
			n.handle = nullptr;
		}
		return *this;
	}

	Window::operator SDL_Window*()
	{
		return handle;
	}

	//-----------------------------------------------------------------------

	Renderer::Renderer(Window& window) :
		handle(SDL_CreateRenderer(window, -1, 0))
	{
	}

	Renderer::~Renderer()
	{
		if (handle)
			SDL_DestroyRenderer(handle);
	}

	Renderer::Renderer(Renderer&& n) :
		handle(n.handle)
	{
		n.handle = nullptr;
	}

	Renderer& Renderer::operator=(Renderer&& n)
	{
		if (this != &n)
		{
			if (handle)
				SDL_DestroyRenderer(handle);
			handle = n.handle;
			n.handle = nullptr;
		}
		return *this;
	}

	Renderer::operator SDL_Renderer*()
	{
		return handle;
	}

	//-----------------------------------------------------------------------

	Surface::Surface(const std::string& filename) :
		handle(IMG_Load(filename.c_str()))
	{
	}

	Surface::~Surface()
	{
		if (handle)
			SDL_FreeSurface(handle);
	}

	Surface::Surface(Surface&& n) :
		handle(n.handle)
	{
		n.handle = nullptr;
	}

	Surface& Surface::operator=(Surface&& n)
	{
		if (this != &n)
		{
			if (handle)
				SDL_FreeSurface(handle);
			handle = n.handle;
			n.handle = nullptr;
		}
		return *this;
	}

	Surface::operator SDL_Surface*()
	{
		return handle;
	}

	//-----------------------------------------------------------------------

	Texture::Texture(Renderer& renderer, Surface& surface) :
		handle(SDL_CreateTextureFromSurface(renderer, surface))
	{
	}

	Texture::~Texture()
	{
		if (handle)
			SDL_DestroyTexture(handle);
	}

	Texture::Texture(Texture&& n) :
		handle(n.handle)
	{
		n.handle = nullptr;
	}

	Texture& Texture::operator=(Texture&& n)
	{
		if (this != &n)
		{
			if (handle)
				SDL_DestroyTexture(handle);
			handle = n.handle;
			n.handle = nullptr;
		}
		return *this;
	}

	Texture::operator SDL_Texture*()
	{
		return handle;
	}
}

int main(int argc, char *argv[])
{
	using namespace SDLWrapper;

	// represents if the user has or has not requested to quit the program
	bool quit = false;

	// the main event for the program
	SDL_Event event;

	// unsure of these
	SDL_Init(SDL_INIT_VIDEO);
	IMG_Init(IMG_INIT_PNG);

	// creates a window and renderer. the renderer is everything displayed in the window
	Window window("Harvester Odyssey", 1200, 800);
	Renderer renderer(window);

	// sprite for running left
	Surface runLeftImage("runLeftAnimation.png");
	Texture runLeftTexture(renderer, runLeftImage);
	// sprite for running right
	Surface runRightImage("runRightAnimation.png");
	Texture runRightTexture(renderer, runRightImage);
	// sprite for standing left
	Surface idleLeftImage("idleLeftAnimation.png");
	Texture idleLeftTexture(renderer, idleLeftImage);
	// sprite for standing right
	Surface idleRightImage("idleRightAnimation.png");
	Texture idleRightTexture(renderer, idleRightImage);
	// sprite for jumping right
	Surface jumpRightImage("jumpRightAnimation.png");
	Texture jumpRightTexture(renderer, jumpRightImage);
	// sprite for jumping left
	Surface jumpLeftImage("jumpLeftAnimation.png");
	Texture jumpLeftTexture(renderer, jumpLeftImage);

	// represents the main character sprite
	SDL_Texture * animation = idleRightTexture;

	// represents if the sprite was last facing left or not
	bool facingLeft = false;

	// sets the background color of the renderer to gray
	SDL_SetRenderDrawColor(renderer, 75, 75, 75, 75);

	//clears the renderer of everything except the background color
	SDL_RenderClear(renderer);

	// the x and y position of there the sprite will be drawn
	int xCoord = 10;
	int yCoord = 700;

	// gets the time passed since the program started and stores it in this variable
	unsigned int time = SDL_GetTicks();

	// the main game loop which runs as long as the user doesn't request to quit the program
	while (!quit)
	{
		// recieves the next event in the que
		SDL_PollEvent(&event);

		// swich that closes the program if the user requests to do so
		switch (event.type)
		{
		case SDL_QUIT:
			quit = true;
			break;
		default:
			break;
		}

		// ANIMATION AND MOVEMENT MANAGMENT //
		// makes the sprite face the appropriate direction when no buttons are being pressed
		if (facingLeft == true)
		{
			animation = idleLeftTexture;
		}
		else
		{
			animation = idleRightTexture;
		}

		// this sets the number of frames and frame rate of the sprite
		Uint32 ticks = SDL_GetTicks();
		Uint32 sprite = (ticks / 100) % 1;
		
		// used to check the state of the keyboard
		const Uint8 *keyState = SDL_GetKeyboardState(NULL);

		// adjusts animation and movement based on key input
		if (keyState[SDL_SCANCODE_RIGHT])
		{
			facingLeft = false;
			animation = runRightTexture;
			sprite = (ticks / 100) % 6;
			if (SDL_GetTicks() > time + 4)
			{
				++xCoord;
				time = SDL_GetTicks();
			}
		}
		else if (keyState[SDL_SCANCODE_LEFT])
		{
			facingLeft = true;
			animation = runLeftTexture;
			sprite = (ticks / 100) % 6;
			if (SDL_GetTicks() > time + 4)
			{
				--xCoord;
				time = SDL_GetTicks();
			}
		}
		if (keyState[SDL_SCANCODE_UP])
		{
			if (facingLeft == true)
			{
				animation = jumpLeftTexture;
			}
			else
			{
				animation = jumpRightTexture;
			}
			sprite = (ticks / 100) % 4;
		}

		// controls which clip of the sprite sheet is used
		SDL_Rect srcrect = { sprite * 50, 0, 50, 75 };
		// controls the destination of the sprite sheet to be rendered
		SDL_Rect dstrect = { xCoord, yCoord, 50, 75 };

		// clears the renderer to the background color
		SDL_RenderClear(renderer);
		// prints the proper animation to the proper location based on the above information
		SDL_RenderCopy(renderer, animation, &srcrect, &dstrect);
		// shows renderer within the window
		SDL_RenderPresent(renderer);
	}

	IMG_Quit();
	SDL_Quit();

	// terminates the program
	return 0;
}
Ah fk yes. That's what I'm talking about. I'm gonna pour over this and learn much. Thank you friend.
closed account (SECMoG1T)
@turtlesavage and @kbw where can I learn this please . Thanks you.
Personally what I've learned I learned from a combination of sources on the internet and asking questions on this site.

For instance, if you go on youtube you can search c++ rpg tutorial and there is a tutorial called "lets make an rpg" I believe. And there are countless others. Just do as many tutorials as you can, ask questions, and connect dots.
https://www.youtube.com/watch?v=8YltubRNx0k&list=PLHM_A02NtaaVey-4Ezh7p6bbOsv-DKA-0&index=15

I don't know how much you know already but if you know the basics and want to learn a bit of game stuff you can go through this tutorial. start with part 1 obviously. And there are many more on youtube.
closed account (SECMoG1T)
Thank you very much @turtlesavage, l am totally newb but am trying to get better , av learnt basics in c++ but av no idea about user interfaces, graphics, games such, I just make some boring consoles though I know everyone started here, I wanna fasten my pace, I'll follow your advice to see where a can get , (i know you alredy good at this by now so I can follow your steps ) i'll also try find some more resources, thenks again for your time.
Last edited on
Topic archived. No new replies allowed.