0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-13 01:33:43 +00:00

Compare commits

..

No commits in common. "5dfe42195f98364f43cfb136351bbbaa837a0421" and "aa83280b6a5c5d24bd327d2c4ce65ef4798e663b" have entirely different histories.

8 changed files with 56 additions and 40 deletions

View File

@ -40,6 +40,7 @@ public:
void clean(); void clean();
bool running() const; bool running() const;
void addTile(unsigned long id, int x, int y);
/* static */ SDL_Renderer* renderer = nullptr; /* static */ SDL_Renderer* renderer = nullptr;
/* static */ SDL_Event event; /* static */ SDL_Event event;
/* static */ CollisionHandler* collisionHandler; /* static */ CollisionHandler* collisionHandler;

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
#include <map>
#include <string>
class Game; class Game;
class Map class Map
{ {
@ -21,6 +18,5 @@ public:
* \return Boolean for success * \return Boolean for success
* *
*/ */
static void loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */); static bool loadMap(const char* path, int sizeX, int sizeY, Game* game /* backreference */);
static void addTile(unsigned long id, int x, int y, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict);
}; };

24
include/TextureDict.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <map>
#include <string>
#include "AssetManager.h"
class TextureDict
{
public:
const std::map<int, std::string> tileDictionary = {
{1, "assets/water.png"},
{2, "assets/dirt.png"},
{3, "assets/grass.png"},
{7, "assets/grass_water_left.png"},
{9, "assets/grass_water_right.png"}
};
std::map<PowerupType, std::string> powerupDictionary = {
{PowerupType::HEART, "assets/heart_powerup.png"},
{PowerupType::WALKINGSPEED, "assets/movement_speed_powerup.png"},
{PowerupType::SHOOTINGSPEED, "assets/atk_speed_powerup.png"}
};
};

View File

@ -1,10 +1,9 @@
#pragma once #pragma once
#include <SDL.h> #include <SDL.h>
#include <string>
#include <map>
#include "Component.h" #include "Component.h"
#include "TextureDict.h"
class SpriteComponent; class SpriteComponent;
class TransformComponent; class TransformComponent;
@ -14,20 +13,15 @@ class TileComponent : public Component
public: public:
TransformComponent* transform; TransformComponent* transform;
SpriteComponent* sprite; SpriteComponent* sprite;
TextureDict textureDict;
SDL_Rect tileRect; SDL_Rect tileRect;
int tileID; int tileID;
const char* path; const char* path;
TileComponent() = default; TileComponent() = default;
TileComponent(int x, int y, int w, int h, int id, const std::map<int, std::pair<std::string, bool>>* textureDict); TileComponent(int x, int y, int w, int h, int id);
~TileComponent() = default; ~TileComponent() = default;
void init() override; void init() override;
bool hasCollision(){return this->collision;}
std::string getName(){return this->tileName;}
private:
bool collision;
std::string tileName;
}; };

View File

@ -4,6 +4,7 @@
#include "SoundManager.h" #include "SoundManager.h"
#include "ProjectileComponent.h" #include "ProjectileComponent.h"
#include "Game.h" #include "Game.h"
#include "TextureDict.h"
#include "TransformComponent.h" #include "TransformComponent.h"
@ -49,6 +50,7 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
} }
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) { void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) {
TextureDict textureDict;
auto& powerups(man->addEntity()); auto& powerups(man->addEntity());
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects

View File

@ -145,6 +145,10 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
if (this->isRunning == false) return; if (this->isRunning == false) return;
map = new Map(); map = new Map();
if (!map->loadMap("assets/SDL_map_test.txt", 25, 20, this)) {
std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl;
SDL_ClearError();
};
//adding textures to the library in AssetManager //adding textures to the library in AssetManager
@ -332,6 +336,14 @@ void Game::clean()
std::cout << "Game Cleaned!" << std::endl; std::cout << "Game Cleaned!" << std::endl;
} }
void Game::addTile(unsigned long id, int x, int y) // tile entity
{
auto& tile(manager.addEntity());
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id);
if (id == 1) tile.addComponent<ColliderComponent>("water");
tile.addGroup((size_t)Entity::GroupLabel::MAPTILES);
}
bool Game::running() const bool Game::running() const
{ {
return isRunning; return isRunning;

View File

@ -3,14 +3,13 @@
#include <cctype> #include <cctype>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <utility> #include <string>
#include "Constants.h" #include "Constants.h"
#include "Game.h" #include "Game.h"
#include "SDL_error.h" #include "SDL_error.h"
#include "TileComponent.h"
void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */) bool Map::loadMap(const char* path, int sizeX, int sizeY, Game* game /* backreference */)
{ {
std::string tileIDstr; std::string tileIDstr;
char singleChar = 0; char singleChar = 0;
@ -19,24 +18,23 @@ void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std:
if (!mapFile.is_open()) { if (!mapFile.is_open()) {
SDL_SetError("Error loading map: Couldn't open map file!"); SDL_SetError("Error loading map: Couldn't open map file!");
std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; return false;
SDL_ClearError();
} }
int x = 0, y = 0; // needed outside for-loop for error handling int x = 0, y = 0; // needed outside for-loop for error handling
bool success = true;
for (; !mapFile.eof(); mapFile.get(singleChar)) for (; !mapFile.eof(); mapFile.get(singleChar))
{ {
if (singleChar == ',' || singleChar == '\n') { if (singleChar == ',' || singleChar == '\n') {
if (tileIDstr.empty()) if (tileIDstr.empty())
continue; continue;
Map::addTile(std::stoi(tileIDstr), x * TILE_SIZE, y * TILE_SIZE, game, textureDict); game->addTile(std::stoi(tileIDstr), x * TILE_SIZE, y * TILE_SIZE);
tileIDstr.clear(); tileIDstr.clear();
x++; x++;
if (singleChar == '\n') { if (singleChar == '\n') {
if (x != sizeX) { if (x != sizeX) {
SDL_SetError("Error loading map: specified x size doesn't match map file!"); SDL_SetError("Error loading map: specified x size doesn't match map file!");
std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; success = false;
SDL_ClearError();
} }
x = 0; x = 0;
y++; y++;
@ -49,18 +47,10 @@ void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std:
} }
if (y != sizeY) { if (y != sizeY) {
SDL_SetError("Error loading map: specified y size doesn't match map file!"); SDL_SetError("Error loading map: specified y size doesn't match map file!");
std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; success = false;
SDL_ClearError();
} }
mapFile.close(); mapFile.close();
}
void Map::addTile(unsigned long id, int x, int y, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict) // tile entity return success;
{
auto& tile(game->manager.addEntity());
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id, textureDict);
if(tile.getComponent<TileComponent>().hasCollision()) tile.addComponent<ColliderComponent>(tile.getComponent<TileComponent>().getName().data());
tile.addGroup((size_t)Entity::GroupLabel::MAPTILES);
} }

View File

@ -7,7 +7,7 @@
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "TileComponent.h" #include "TileComponent.h"
TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map<int, std::pair<std::string, bool>>* textureDict) TileComponent::TileComponent(int x, int y, int w, int h, int id)
{ {
this->tileRect.x = x; this->tileRect.x = x;
this->tileRect.y = y; this->tileRect.y = y;
@ -15,15 +15,12 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map<
this->tileRect.h = h; this->tileRect.h = h;
tileID = id; tileID = id;
auto it = textureDict->find(tileID); //every id has its own distinct texture (in texturedict.h) auto it = textureDict.tileDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h)
if (it == textureDict->end()) { if (it == textureDict.tileDictionary.end()) {
std::cout << "it end" << std::endl; std::cout << "it end" << std::endl;
return; return;
} }
this->path = it->second.data();
this->collision = it->second.second;
this->tileName = it->second.first;
this->path = it->second.first.data();
} }
void TileComponent::init() void TileComponent::init()