Hello, hello!
I have recently started coding a game using SDL2 and I am currently running head on into a wall.
Until I completed - today - my path-finding algorithm (A*) everything was pretty much under control. However I am starting to think that I'm doing something wrong with the organisation of my code. Indeed, I have several classes, a few of which are inherited classes. Most of them are just independent.
I will show you the list of the classes I have as well as what they do:
|
main.cpp //Calls Game class functions
|
Updates, renders, handle events
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
|
#ifndef GAME_H
#define GAME_H
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
using namespace std;
const int WIN_W = 1500;
const int WIN_H = 900;
class Game
{
private:
bool isRunning;
SDL_Window* window;
public:
Game();
~Game();
bool init(const char* title, int xpos, int ypos, int width, int height);
void handleEvents();
void update();
void render();
void clean();
bool running()
{ return isRunning; }
static SDL_Renderer* renderer;
};
#endif
|
initially loads textures and draws textures;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H
#include "Game.h"
extern SDL_Rect nullPos;
class TextureManager
{
private:
public:
TextureManager();
static SDL_Texture* loadTexture(const char* fileName);
static void drawTexture(SDL_Texture* tex, SDL_Rect srcRect, SDL_Rect destRect);
static int PIXEL_SHIFT_X;
static int PIXEL_SHIFT_Y;
};
#endif;
|
Stores a 2d array for the map level, initially loads the array, and draws the map in the game loop.
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
|
#ifndef MAP_H
#define MAP_H
#include "Game.h"
class Map
{
private:
SDL_Rect sourceR;
SDL_Rect destinationR;
//textures;
//SDL_Texture* dirt;
//SDL_Texture* grass;
//SDL_Texture* cobble;
SDL_Texture* blank;
SDL_Texture* grey;
SDL_Texture* floor01;
SDL_Texture* wall04;
SDL_Texture* roof01;
SDL_Texture* block01;
int map[20][20];
public:
Map();
~Map();
void loadMap(int arr[20][20]);
void drawMapFloor();
void drawMapWalls();
static int levelTest[20][20];
};
#endif
|
draws mouse pointer, return mouse coordinates, check if mouse is near the edges of the window, if so the screen scrolls.
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
|
#ifndef MOUSE_H
#define MOUSE_H
#include "Game.h"
class Player;
class Map;
class Mouse
{
private:
SDL_Rect mousePosition;
SDL_Rect srcRect;
SDL_Texture* mouseCursor;
SDL_Texture* mousePointer;
SDL_Texture* mouse;
public:
Mouse();
~Mouse();
void drawMousePointer(Map* map);
int* getMouseCoord();
void checkBorderScrolling(Player* player, Map* map);
int getMouseState();
};
#endif
|
Converts Cartesian coordinates to isometric and vice versa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#ifndef ISOCONVERSION_H
#define ISOCONVERSION_H
#include "Game.h"
SDL_Rect squareToIso(SDL_Rect dest, int column, int row, int shiftX, int shiftY);
SDL_Rect isoToSquare(SDL_Rect dest, int column, int row, int shiftX, int shiftY);
int* getSquareCoord(SDL_Rect rect, int shiftX, int shiftY);
SDL_Rect getIsoCoord(int row, int column, int shiftX, int shiftY);
#endif;
|
Holds the player Position, his destination, handles the player movement events and draws it.
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
|
#ifndef PLAYER_H
#define PLAYER_H
#include "Game.h"
#include "TextureManager.h"
#include "Mouse.h"
#include "Map.h"
class Player
{
private:
SDL_Rect playerSrcRect;
SDL_Rect playerPos;
SDL_Rect playerDestination;
SDL_Texture* playerTexture;
public:
Player();
~Player();
void playerEvents(int choice, Mouse* mouse);
void drawPlayer(Map* map);
void playerTravel(Map* map);
void setPosition();
void changePosition(int x, int y);
bool changeDestination;
};
#endif
|
Holds different variables and functions necessary for the path-finding algorithm
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
|
#ifndef ASTAR_H
#define ASTAR_H
#include "Game.h"
#include <vector>
#include "Map.h"
#include "IsoConversion.h"
struct square
{
int x;
int y;
int G;
int H;
int F;
};
class PathFinding
{
private:
static vector<square> openList;
static vector<square> closedList;
static square currentSquare;
static square sourceSquare;
static square destinationSquare;
static square adjacentSquares[4];
public:
PathFinding();
static vector<SDL_Rect> pathFinding_main(Map* map, SDL_Rect entityPos, SDL_Rect destinationPos);
static square openList_get_square_with_lowest_score();
static bool current_square_is_destination_square();
static bool closedList_has_destination_square();
static void get_adjacent_squares(Map* map);
static bool closedList_has_square(square s);
static bool openList_has_square(square s);
static void add_square_to_openList(square s);
static void add_square_to_closedList(square s);
static void remove_square_from_openList(square s);
static bool openList_is_empty();
static int get_manhattan_distance(int x, int destx, int y, int desty);
};
#endif
|
My issue comes from the implementation of the path-finding algorithm because it returns a vector of SDL_Rectangle which each holds x and y coordinates. They are sorted by the path-finding function in order, from the player position to its destination, each rectangle for the square where the player needs to be rendered next.
So my question is am I indeed mis-organising my code and class dependencies, in which case do you have any advice/source on how to structure a game code?
The code I currently have is just for one level so I would also be interested to know how the game would be structured considering there would be several levels each including loading a new map.
Also what do you think about the return type of my path-finding function
vector<SDL_Rect> pathFinding_main(Map* map, SDL_Rect entityPos, SDL_Rect destinationPos)
?
Should it return something else, and where in the code would it be best to place it?
Thank you a lot in advance!
Any advice, comment is welcome!
Regards,
Hoogo.
EDIT: Typos.
I can provide the code for the CPP files too if necessary.