diff --git a/CMakeLists.txt b/CMakeLists.txt index a7e5140..745d441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ file(GLOB_RECURSE SourceFiles ${EngineDIR}/* ${EngineDIR}/renderengine/* ${EngineDIR}/input/* + ${EngineDIR}/resourcemanager/* ) add_executable(${BuildDIR}/${EXEName} ${SourceFiles}) diff --git a/bin/crumpet-engine b/bin/crumpet-engine index ea66599..a5516ff 100755 Binary files a/bin/crumpet-engine and b/bin/crumpet-engine differ diff --git a/sdl2-config.cmake b/sdl2-config.cmake deleted file mode 100644 index 4c5eaa6..0000000 --- a/sdl2-config.cmake +++ /dev/null @@ -1,10 +0,0 @@ -set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include") - -# Support both 32 and 64 bit builds -if (${CMAKE_SIZEOF_VOID_P} MATCHES 8) - set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2main.lib") -else () - set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2main.lib") -endif () - -string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES) diff --git a/src/crumpet-engine/crumpet-engine.h b/src/crumpet-engine/crumpet-engine.h index 5e2b6dd..0bdd7c4 100644 --- a/src/crumpet-engine/crumpet-engine.h +++ b/src/crumpet-engine/crumpet-engine.h @@ -1,8 +1,5 @@ #pragma once -#include -#include - #include "game.h" #undef main diff --git a/src/crumpet-engine/game.cpp b/src/crumpet-engine/game.cpp index c437934..121a0d1 100644 --- a/src/crumpet-engine/game.cpp +++ b/src/crumpet-engine/game.cpp @@ -1,7 +1,9 @@ #include "game.h" Game::Game() - : input(&renderer) { + : input(&renderer) + , resourceManager(&renderer) + , textureManager(resourceManager.textureManager) { } diff --git a/src/crumpet-engine/game.h b/src/crumpet-engine/game.h index 61d234a..d5dcbfe 100644 --- a/src/crumpet-engine/game.h +++ b/src/crumpet-engine/game.h @@ -6,12 +6,15 @@ #include #include "renderengine/renderer.h" +#include "resourcemanager/resourcemanager.h" #include "input/input.h" class Game { public: Game(); + ResourceManger resourceManager; + TextureManager textureManager; Renderer renderer; Input input; diff --git a/src/crumpet-engine/rect.cpp b/src/crumpet-engine/rect.cpp new file mode 100644 index 0000000..c29189f --- /dev/null +++ b/src/crumpet-engine/rect.cpp @@ -0,0 +1,170 @@ +#include "rect.h" + +Rect::Rect() + : rect(new SDL_Rect{ 0, 0, 0, 0 }) { + Clear(); +} + +Rect::Rect(int x, int y, int w, int h) + : rect(new SDL_Rect{ x, y, w, h }) { + SetRect(x, y, w, h); +} + +void Rect::Clear() { + SetRect(0, 0, 0, 0); +} + +SDL_Rect* Rect::ToSDLRect() { + rect->x = x; + rect->y = y; + rect->w = w; + rect->h = h; + + return rect; +} + +std::string Rect::ToString() { + std::string res = "("; + res += std::to_string(x); + res += ", "; + res += std::to_string(y); + res += ", "; + res += std::to_string(w); + res += ", "; + res += std::to_string(h); + res += ")"; + return res; +} + +bool Rect::Intersects(Rect* rect) { + int leftA = x; + int rightA = x + w; + int topA = y; + int bottomA = y + h; + + int leftB = rect->x; + int rightB = rect->x + rect->w; + int topB = rect->y; + int bottomB = rect->y + rect->h; + + if (bottomA <= topB) return false; + if (topA >= bottomB) return false; + if (rightA <= leftB) return false; + if (leftA >= rightB) return false; + + return true; +} + +// bool Rect::Intersects(int x, int y, int w, int h) { +// return Intersects(&CreateRect(x, y, w, h)); +// } + +bool Rect::Contains(Rect* rect) { + return (rect->x >= x && rect->Right() <= (x + w) && rect->y >= y && rect->Bottom() <= (y + h)); +} + +bool Rect::Contains(Vec2* point) { + return (point->x >= x && point->x <= (x + w) && point->y >= y && point->y <= (y + h)); +} + +bool Rect::Contains(int x, int y, int w, int h) { + Rect tempRect(x, y, w, h); + return Contains(&tempRect); +} + +Vec2* Rect::Position() { + Vec2* res = new Vec2(x, y); + return res; +} + +Vec2* Rect::Center() { + Vec2* res = new Vec2(x + (w / 2), y + (h / 2)); + return res; +} + +int Rect::CenterX() { + return (x + (w / 2)); +} + +int Rect::CenterY() { + return (y + (h / 2)); +} + +int Rect::Left() { + return x; +} + +int Rect::Right() { + return (x + w); +} + +int Rect::Top() { + return y; +} + +int Rect::Bottom() { + return y + h; +} + +int Rect::Perimiter() { + return (w + w + h + h); +} + +int Rect::Area() { + return (w + h); +} + +int Rect::GetX() { + return x; +} + +int Rect::GetY() { + return y; +} + +int Rect::GetW() { + return w; +} + +int Rect::GetH() { + return h; +} + +void Rect::SetRect(int x, int y, int w, int h) { + this->x = x; + this->y = y; + this->w = w; + this->h = h; + + this->rect->x = x; + this->rect->y = y; + this->rect->w = w; + this->rect->h = h; +} + +void Rect::SetSize(Vec2* size) { + this->x = size->x; + this->y = size->y; +} + +void Rect::SetPos(Vec2* pos) { + this->w = pos->x; + this->h = pos->y; +} + +void Rect::Translate(Vec2* offset) { + this->x += offset->x; + this->y += offset->y; +} + +void Rect::TranslateX(int x) { + this->x += x; +} + +void Rect::TranslateY(int y) { + this->y += y; +} + +Rect::~Rect() { + rect = NULL; +} diff --git a/src/crumpet-engine/rect.h b/src/crumpet-engine/rect.h new file mode 100644 index 0000000..5125b50 --- /dev/null +++ b/src/crumpet-engine/rect.h @@ -0,0 +1,70 @@ +#pragma once + +#include +#include +#include "math.h" + +class Rect { +public: + Rect(); + Rect(int x, int y, int w, int h); + void Clear(); + + static Rect CreateRect(int x, int y, int w, int h) { + Rect tempRect(x, y, w, h); + return tempRect; + } + + Rect operator+(Rect* rect) { + return Rect(this->x + rect->x, this->y + this->x, w, h); + } + Rect operator-(Rect* rect) { + return Rect(this->x - rect->x, this->y - this->x, w, h); + } + bool operator==(const Rect* rect) { + return (x == rect->x && y == rect->y && w == rect->w && h == rect->h); + } + bool operator!=(const Rect* rect) { + return !(x == rect->x && y == rect->y && w == rect->w && h == rect->h); + } + + SDL_Rect* ToSDLRect(); + std::string ToString(); + + bool Intersects(Rect* rect); + // bool Intersects(int x, int y, int w, int h); + + bool Contains(Rect* rect); + bool Contains(Vec2* point); + bool Contains(int x, int y, int w, int h); + + Vec2* Position(); + Vec2* Center(); + int CenterX(); + int CenterY(); + + int Left(); + int Right(); + int Top(); + int Bottom(); + int Perimiter(); + int Area(); + + int GetX(); + int GetY(); + int GetW(); + int GetH(); + + void SetRect(int x, int y, int w, int h); + void SetSize(Vec2* size); + void SetPos(Vec2* pos); + void Translate(Vec2* offset); + void TranslateX(int x); + void TranslateY(int y); + + int x, y, w, h; + + virtual ~Rect(); +private: + SDL_Rect* rect; +}; diff --git a/src/crumpet-engine/resourcemanager/resourcemanager.h b/src/crumpet-engine/resourcemanager/resourcemanager.h new file mode 100644 index 0000000..b486ee6 --- /dev/null +++ b/src/crumpet-engine/resourcemanager/resourcemanager.h @@ -0,0 +1,10 @@ +#pragma once + +#include "texturemanager.h" + +class ResourceManger { +public: + ResourceManger(Renderer* renderer) : textureManager(renderer) {}; + + TextureManager textureManager; +}; diff --git a/src/crumpet-engine/resourcemanager/texturemanager.cpp b/src/crumpet-engine/resourcemanager/texturemanager.cpp new file mode 100644 index 0000000..97e4e98 --- /dev/null +++ b/src/crumpet-engine/resourcemanager/texturemanager.cpp @@ -0,0 +1,46 @@ +#include "texturemanager.h" + +TextureManager::TextureManager(Renderer* renderer) { + m_renderer = renderer; +} + +bool TextureManager::registerTexture(std::string textureSource, std::string textureName) { + SDL_Texture* texture; + SDL_Surface* loadSurface = IMG_Load(textureSource.c_str()); + if (loadSurface == NULL) { + std::cout << "ERROR LOADING SURFACE " << SDL_GetError() << std::endl; + return false; + } + texture = SDL_CreateTextureFromSurface(m_renderer->SDLRenderer, loadSurface); + if (texture == NULL) { + std::cout << "ERROR LOADING TEXTURE " << SDL_GetError() << std::endl; + return false; + } + SDL_FreeSurface(loadSurface); + + m_textureSources[textureName] = textureSource; + m_registerdTextures[textureName] = texture; + + return true; +} + +void TextureManager::unregisterTexture(std::string textureName) { + SDL_DestroyTexture(m_registerdTextures[textureName]); + + delete &m_textureSources[textureName]; + delete &m_registerdTextures[textureName]; +} + +SDL_Texture* TextureManager::getTexture(std::string textureName) { + return m_registerdTextures[textureName]; +} + +std::string TextureManager::getTextureSource(std::string textureName) { + return m_textureSources[textureName]; +} + +TextureManager::~TextureManager() { + for (std::map::iterator it = m_registerdTextures.begin(); it != m_registerdTextures.end(); it++) { + SDL_DestroyTexture(m_registerdTextures[it->first]); + } +} diff --git a/src/crumpet-engine/resourcemanager/texturemanager.h b/src/crumpet-engine/resourcemanager/texturemanager.h new file mode 100644 index 0000000..ecb1812 --- /dev/null +++ b/src/crumpet-engine/resourcemanager/texturemanager.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "../renderengine/renderer.h" + +class TextureManager { +public: + TextureManager(Renderer* renderer); + + bool registerTexture(std::string textureSource, std::string textureName); + void unregisterTexture(std::string textureName); + SDL_Texture* getTexture(std::string textureName); + std::string getTextureSource(std::string textureName); + + virtual ~TextureManager(); +private: + std::map m_textureSources; + std::map m_registerdTextures; + Renderer* m_renderer; +}; diff --git a/src/main.cpp b/src/main.cpp index 9e1008f..6b9d44a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,21 +4,12 @@ int main(int argc, char** argv) { Game game; game.renderer.createWindow("Crumpet Engine", 600, 400, SCREEN_MODE_VSYNC); - SDL_Texture* texture; - SDL_Surface* loadSurface = IMG_Load("./resources/mario.png"); - if (loadSurface == NULL) { - std::cout << "ERROR LOADING SURFACE " << SDL_GetError() << std::endl; - } - texture = SDL_CreateTextureFromSurface(game.renderer.SDLRenderer, loadSurface); - if (texture == NULL) { - std::cout << "ERROR LOADING TEXTURE " << SDL_GetError() << std::endl; - } - SDL_FreeSurface(loadSurface); + game.textureManager.registerTexture("./resources/mario.png", "mario"); while (!game.renderer.isWindowClosed()) { game.renderer.clear(); game.input.poll(); - SDL_RenderCopy(game.renderer.SDLRenderer, texture, NULL, NULL); + SDL_RenderCopy(game.renderer.SDLRenderer, game.textureManager.getTexture("mario"), NULL, NULL); game.renderer.update(); } }