Add more useless commentary

This commit is contained in:
Ulysse Cura 2024-10-17 18:23:58 +02:00
parent 7f5984b229
commit a206123854
3 changed files with 28 additions and 10 deletions

View File

@ -34,6 +34,7 @@ class ChannelManager {
return it != channels.end() ? it->second : false; return it != channels.end() ? it->second : false;
} }
// Verify if all given channels are in corresponding state
bool areChannelsInState(const unordered_map<int, bool> &states) bool areChannelsInState(const unordered_map<int, bool> &states)
{ {
for(const auto &state : states) for(const auto &state : states)

View File

@ -34,6 +34,7 @@ Manager Game::entityManager;
Entity &player(Game::entityManager.addEntity()); Entity &player(Game::entityManager.addEntity());
// Game constructor
Game::~Game() Game::~Game()
{ {
if(renderer != NULL) SDL_DestroyRenderer(renderer); if(renderer != NULL) SDL_DestroyRenderer(renderer);
@ -44,8 +45,10 @@ Game::~Game()
cerr << "Game cleaned.\n"; cerr << "Game cleaned.\n";
} }
// Init SDL, IMG...
bool Game::Init() bool Game::Init()
{ {
// Init SDL
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{ {
cerr << "Erreur SDL_Init : " << SDL_GetError() << '\n'; cerr << "Erreur SDL_Init : " << SDL_GetError() << '\n';
@ -53,6 +56,7 @@ bool Game::Init()
} }
cerr << "SDL initialised successfully.\n"; cerr << "SDL initialised successfully.\n";
// Init IMG
if(IMG_Init(IMG_INIT_PNG) == 0) if(IMG_Init(IMG_INIT_PNG) == 0)
{ {
cerr << "Erreur IMG_Init : " << IMG_GetError() << '\n'; cerr << "Erreur IMG_Init : " << IMG_GetError() << '\n';
@ -60,6 +64,7 @@ bool Game::Init()
} }
cerr << "IMG initialised successfully.\n"; cerr << "IMG initialised successfully.\n";
// Create Window
m_window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); m_window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if(m_window == NULL) if(m_window == NULL)
{ {
@ -68,8 +73,9 @@ bool Game::Init()
} }
cerr << "Window created successfully.\n"; cerr << "Window created successfully.\n";
// Disable unused events
SDL_EventType disableEvents[] = { SDL_EventType disableEvents[] = {
// Événements liés aux manettes (joysticks) // Joysticks events
SDL_JOYAXISMOTION, SDL_JOYAXISMOTION,
SDL_JOYBALLMOTION, SDL_JOYBALLMOTION,
SDL_JOYHATMOTION, SDL_JOYHATMOTION,
@ -78,7 +84,7 @@ bool Game::Init()
SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED,
SDL_JOYDEVICEREMOVED, SDL_JOYDEVICEREMOVED,
// Événements liés aux contrôleurs (game controllers) // Game controllers events
SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERAXISMOTION,
SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONDOWN,
SDL_CONTROLLERBUTTONUP, SDL_CONTROLLERBUTTONUP,
@ -86,13 +92,13 @@ bool Game::Init()
SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMOVED,
SDL_CONTROLLERDEVICEREMAPPED, SDL_CONTROLLERDEVICEREMAPPED,
// Événements de glisser-déposer (drag & drop) // Drag & drop events
SDL_DROPFILE, SDL_DROPFILE,
SDL_DROPTEXT, SDL_DROPTEXT,
SDL_DROPBEGIN, SDL_DROPBEGIN,
SDL_DROPCOMPLETE, SDL_DROPCOMPLETE,
// Événements liés aux appareils tactiles // Tactile events
SDL_FINGERMOTION, SDL_FINGERMOTION,
SDL_FINGERDOWN, SDL_FINGERDOWN,
SDL_FINGERUP, SDL_FINGERUP,
@ -100,15 +106,15 @@ bool Game::Init()
SDL_DOLLARGESTURE, SDL_DOLLARGESTURE,
SDL_DOLLARRECORD, SDL_DOLLARRECORD,
// Événements liés aux capteurs // Sensor event
SDL_SENSORUPDATE, SDL_SENSORUPDATE,
// Événements utilisateurs (custom) // User event
SDL_USEREVENT SDL_USEREVENT
}; };
for(const SDL_EventType &disableEvent : disableEvents) SDL_EventState(disableEvent, SDL_DISABLE); for(const SDL_EventType &disableEvent : disableEvents) SDL_EventState(disableEvent, SDL_DISABLE);
// Create renderer
renderer = SDL_CreateRenderer(m_window, -1, 0); renderer = SDL_CreateRenderer(m_window, -1, 0);
if(renderer == NULL) if(renderer == NULL)
{ {
@ -117,8 +123,8 @@ bool Game::Init()
} }
cerr << "Renderer created successfully\n\n"; cerr << "Renderer created successfully\n\n";
// Load initial map and tileset
tileMap.LoadTileset("ressources/tileset/tileset.png"); tileMap.LoadTileset("ressources/tileset/tileset.png");
tileMap.LoadTileMap("ressources/maps/dungeon entry.json"); tileMap.LoadTileMap("ressources/maps/dungeon entry.json");
TransformComponent *p_transform = &player.addComponent<TransformComponent>(tileMap.getPlayerInitPos(), Vector2D<int>(32, 32), 2, 3); TransformComponent *p_transform = &player.addComponent<TransformComponent>(tileMap.getPlayerInitPos(), Vector2D<int>(32, 32), 2, 3);

View File

@ -23,18 +23,21 @@
using std::string, std::exception, std::vector, std::cerr, std::runtime_error; using std::string, std::exception, std::vector, std::cerr, std::runtime_error;
using json = nlohmann::json; using json = nlohmann::json;
// Load a map from memory
void TileMap::LoadTileMap(const string &path) void TileMap::LoadTileMap(const string &path)
{ {
// Load the map data from memory
json *mapData; json *mapData;
mapData = Game::mapManager.LoadMap(path); mapData = Game::mapManager.LoadMap(path);
// Get tilemap width and height same for world
tileMapWidth = mapData->at("TileMapWidth").get<int>(); tileMapWidth = mapData->at("TileMapWidth").get<int>();
tileMapHeight = mapData->at("TileMapHeight").get<int>(); tileMapHeight = mapData->at("TileMapHeight").get<int>();
worldWidth = tileMapWidth * TILE_SIZE * TILEMAP_SCALE; worldWidth = tileMapWidth * TILE_SIZE * TILEMAP_SCALE;
worldHeight = tileMapHeight * TILE_SIZE * TILEMAP_SCALE; worldHeight = tileMapHeight * TILE_SIZE * TILEMAP_SCALE;
// Clear layers and hitboxes and resize them
m_tilesLayer1.clear(); m_tilesLayer1.clear();
m_tilesLayer2.clear(); m_tilesLayer2.clear();
m_tilesLayer3.clear(); m_tilesLayer3.clear();
@ -47,6 +50,7 @@ void TileMap::LoadTileMap(const string &path)
hitboxes.resize(tileMapWidth * tileMapHeight); hitboxes.resize(tileMapWidth * tileMapHeight);
// Load layers and hitboxes from map data
string layers[] {"Layer 1","Layer 2","Layer 3", "Hitboxes"}; string layers[] {"Layer 1","Layer 2","Layer 3", "Hitboxes"};
for(const string &layer : layers) for(const string &layer : layers)
@ -84,7 +88,6 @@ void TileMap::LoadTileMap(const string &path)
} }
m_nextMaps.clear(); m_nextMaps.clear();
for(auto it {mapData->at("NextMaps").begin()}; it < mapData->at("NextMaps").end(); it++) for(auto it {mapData->at("NextMaps").begin()}; it < mapData->at("NextMaps").end(); it++)
{ {
NextMap nextMap; NextMap nextMap;
@ -95,6 +98,7 @@ void TileMap::LoadTileMap(const string &path)
m_nextMaps.emplace(it->at("Number").get<int>(), nextMap); m_nextMaps.emplace(it->at("Number").get<int>(), nextMap);
} }
// Erase lasts entities and load news if there are some
std::size_t numberOfEntity {Game::entityManager.getNumberOfEntity()}; std::size_t numberOfEntity {Game::entityManager.getNumberOfEntity()};
for(std::size_t i {1}; i < numberOfEntity; i++) Game::entityManager.erase(i); for(std::size_t i {1}; i < numberOfEntity; i++) Game::entityManager.erase(i);
@ -157,6 +161,7 @@ void TileMap::LoadTileMap(const string &path)
} }
} }
// Texture renderer creating
Game::textureRenderer = SDL_CreateTexture(Game::renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, worldWidth, worldHeight); Game::textureRenderer = SDL_CreateTexture(Game::renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, worldWidth, worldHeight);
if(Game::textureRenderer == NULL) if(Game::textureRenderer == NULL)
{ {
@ -165,6 +170,7 @@ void TileMap::LoadTileMap(const string &path)
} }
} }
// Load the corresponding next map from memory
void TileMap::LoadNextMap(int nextMapNumber) void TileMap::LoadNextMap(int nextMapNumber)
{ {
NextMap nextMap = m_nextMaps.at(nextMapNumber); NextMap nextMap = m_nextMaps.at(nextMapNumber);
@ -174,6 +180,7 @@ void TileMap::LoadNextMap(int nextMapNumber)
m_playerInitPos = nextMap.playerInitPos * TILE_SIZE * TILEMAP_SCALE; m_playerInitPos = nextMap.playerInitPos * TILE_SIZE * TILEMAP_SCALE;
} }
// Load a tileset
void TileMap::LoadTileset(const string &path) void TileMap::LoadTileset(const string &path)
{ {
m_tileset = Game::textureManager.LoadTexture(path); m_tileset = Game::textureManager.LoadTexture(path);
@ -184,6 +191,7 @@ void TileMap::LoadTileset(const string &path)
m_tilesetHeight /= TILE_SIZE; m_tilesetHeight /= TILE_SIZE;
} }
// Draw corresponding layer
void TileMap::draw(int layer) void TileMap::draw(int layer)
{ {
if(layer == 1) if(layer == 1)
@ -205,6 +213,7 @@ void TileMap::draw(int layer)
} }
} }
// Draw the layer given
void TileMap::m_draw(const vector<TileID> &tiles) void TileMap::m_draw(const vector<TileID> &tiles)
{ {
SDL_Rect visibleTilesR; SDL_Rect visibleTilesR;
@ -230,11 +239,13 @@ void TileMap::m_draw(const vector<TileID> &tiles)
} }
} }
// Get loaded player inital position
Vector2D<float> TileMap::getPlayerInitPos() Vector2D<float> TileMap::getPlayerInitPos()
{ {
return m_playerInitPos; return m_playerInitPos;
} }
// Destroy texture renderer
TileMap::~TileMap() TileMap::~TileMap()
{ {
if(Game::textureRenderer != NULL) SDL_DestroyTexture(Game::textureRenderer); if(Game::textureRenderer != NULL) SDL_DestroyTexture(Game::textureRenderer);