Rect classes and basic texture management with SDL_image
This commit is contained in:
@@ -33,6 +33,7 @@ file(GLOB_RECURSE SourceFiles
|
||||
${EngineDIR}/*
|
||||
${EngineDIR}/renderengine/*
|
||||
${EngineDIR}/input/*
|
||||
${EngineDIR}/resourcemanager/*
|
||||
)
|
||||
|
||||
add_executable(${BuildDIR}/${EXEName} ${SourceFiles})
|
||||
|
||||
Binary file not shown.
@@ -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)
|
||||
@@ -1,8 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#include "game.h"
|
||||
|
||||
#undef main
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "game.h"
|
||||
|
||||
Game::Game()
|
||||
: input(&renderer) {
|
||||
: input(&renderer)
|
||||
, resourceManager(&renderer)
|
||||
, textureManager(resourceManager.textureManager) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,15 @@
|
||||
#include <map>
|
||||
|
||||
#include "renderengine/renderer.h"
|
||||
#include "resourcemanager/resourcemanager.h"
|
||||
#include "input/input.h"
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
|
||||
ResourceManger resourceManager;
|
||||
TextureManager textureManager;
|
||||
Renderer renderer;
|
||||
Input input;
|
||||
|
||||
|
||||
170
src/crumpet-engine/rect.cpp
Normal file
170
src/crumpet-engine/rect.cpp
Normal file
@@ -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;
|
||||
}
|
||||
70
src/crumpet-engine/rect.h
Normal file
70
src/crumpet-engine/rect.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <SDL2/SDL.h>
|
||||
#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;
|
||||
};
|
||||
10
src/crumpet-engine/resourcemanager/resourcemanager.h
Normal file
10
src/crumpet-engine/resourcemanager/resourcemanager.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "texturemanager.h"
|
||||
|
||||
class ResourceManger {
|
||||
public:
|
||||
ResourceManger(Renderer* renderer) : textureManager(renderer) {};
|
||||
|
||||
TextureManager textureManager;
|
||||
};
|
||||
46
src/crumpet-engine/resourcemanager/texturemanager.cpp
Normal file
46
src/crumpet-engine/resourcemanager/texturemanager.cpp
Normal file
@@ -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<std::string, SDL_Texture*>::iterator it = m_registerdTextures.begin(); it != m_registerdTextures.end(); it++) {
|
||||
SDL_DestroyTexture(m_registerdTextures[it->first]);
|
||||
}
|
||||
}
|
||||
25
src/crumpet-engine/resourcemanager/texturemanager.h
Normal file
25
src/crumpet-engine/resourcemanager/texturemanager.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#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<std::string, std::string> m_textureSources;
|
||||
std::map<std::string, SDL_Texture*> m_registerdTextures;
|
||||
Renderer* m_renderer;
|
||||
};
|
||||
13
src/main.cpp
13
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user