From 119649353ca1cf1abb78f0c99d66829b4311f4d2 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 29 Sep 2018 10:42:47 +0100 Subject: [PATCH] MathHelper and entity abstractions --- crumpet-engine/crumpet-engine.vcxproj | 2 + crumpet-engine/crumpet-engine.vcxproj.filters | 4 ++ crumpet-engine/entity.cpp | 19 ++++++- crumpet-engine/entity.h | 29 ++++++++-- crumpet-engine/main.cpp | 4 +- crumpet-engine/mathHelper.cpp | 1 + crumpet-engine/mathHelper.h | 55 +++++++++++++++++++ 7 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 crumpet-engine/mathHelper.cpp create mode 100644 crumpet-engine/mathHelper.h diff --git a/crumpet-engine/crumpet-engine.vcxproj b/crumpet-engine/crumpet-engine.vcxproj index b877e5e..b177328 100644 --- a/crumpet-engine/crumpet-engine.vcxproj +++ b/crumpet-engine/crumpet-engine.vcxproj @@ -128,11 +128,13 @@ + + diff --git a/crumpet-engine/crumpet-engine.vcxproj.filters b/crumpet-engine/crumpet-engine.vcxproj.filters index b42a6be..d0999d8 100644 --- a/crumpet-engine/crumpet-engine.vcxproj.filters +++ b/crumpet-engine/crumpet-engine.vcxproj.filters @@ -5,6 +5,7 @@ + @@ -21,5 +22,8 @@ headers + + headers + \ No newline at end of file diff --git a/crumpet-engine/entity.cpp b/crumpet-engine/entity.cpp index b951f92..579967b 100644 --- a/crumpet-engine/entity.cpp +++ b/crumpet-engine/entity.cpp @@ -1,9 +1,9 @@ #include "entity.h" -Entity::Entity(std::string name, SDL_Renderer* SDLRenderer, EntityType mode) { +Entity::Entity(std::string name, SDL_Renderer* SDLRenderer, RenderType mode) { this->m_name = name; this->m_SDLRenderer = SDLRenderer; - this->RenderType = mode; + this->Rendertype = mode; } bool Entity::LoadTexture(std::string path) { @@ -18,12 +18,25 @@ bool Entity::LoadTexture(std::string path) { std::cout << "Unable to create texture from:" << (PATH + path).c_str() << " SDL ERROR: " << SDL_GetError() << std::endl; return false; } + SDL_FreeSurface(loadedSurface); return true; } +void Entity::SetVecPoints(std::vector polyPoints) { + this->m_polyPoints = polyPoints; +} + +void Entity::AddVecPoint(Vec2 point) { + this->m_polyPoints.push_back(point); +} + +void Entity::SetPolyDrawType(PolyDrawType type) { + this->Drawtype = type; +} + void Entity::Render() { - if (RenderType == EntityType::MODE_TEXTURE) + if (Rendertype == RenderType::MODE_TEXTURE) SDL_RenderCopy(m_SDLRenderer, m_texture, NULL, NULL); // if (RenderType == EntityType::MODE_POLYGON) diff --git a/crumpet-engine/entity.h b/crumpet-engine/entity.h index 07f4336..7d359f2 100644 --- a/crumpet-engine/entity.h +++ b/crumpet-engine/entity.h @@ -2,33 +2,50 @@ #include #include +#include #include #include +#include "mathHelper.h" -enum struct EntityType { +enum struct RenderType { MODE_DEFAULT, MODE_TEXTURE, MODE_POLYGON }; +enum struct PolyDrawType { + DRAW_DEFAULT, + DRAW_FILLED_RECT, + DRAW_OUTLINE_RECT, + DRAW_LINES +}; + class Entity { public: - Entity(std::string name, SDL_Renderer* SDLRenderer, EntityType mode); // Texture overload + Entity(std::string name, SDL_Renderer* SDLRenderer, RenderType mode); // Texture overload // Entity(); // Polygon overload - EntityType RenderType = EntityType::MODE_DEFAULT; + RenderType Rendertype = RenderType::MODE_DEFAULT; + PolyDrawType Drawtype = PolyDrawType::DRAW_DEFAULT; bool LoadTexture(std::string path); + + void SetVecPoints(std::vector polyPoints); + void AddVecPoint(Vec2 point); + void SetPolyDrawType(PolyDrawType type); + void Render(); virtual ~Entity(); private: std::string m_name; - // std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine"; - std::string PATH = "E:/Games/crumpet-engine"; - + std::vector m_polyPoints; SDL_Texture* m_texture; + + std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine"; + // std::string PATH = "E:/Games/crumpet-engine"; + SDL_Renderer* m_SDLRenderer; }; diff --git a/crumpet-engine/main.cpp b/crumpet-engine/main.cpp index aef2201..5c52d93 100644 --- a/crumpet-engine/main.cpp +++ b/crumpet-engine/main.cpp @@ -8,10 +8,10 @@ int main(int argc, char** argv) { Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 0, 60); - Entity mario("mario", game.SDLRenderer, EntityType::MODE_TEXTURE); + Entity mario("mario", game.SDLRenderer, RenderType::MODE_TEXTURE); mario.LoadTexture("/resources/mario.png"); - Entity box("box", game.SDLRenderer, EntityType::MODE_POLYGON); + Entity box("box", game.SDLRenderer, RenderType::MODE_POLYGON); while (!game.IsDisplayClosed()) { diff --git a/crumpet-engine/mathHelper.cpp b/crumpet-engine/mathHelper.cpp new file mode 100644 index 0000000..d2e7dbf --- /dev/null +++ b/crumpet-engine/mathHelper.cpp @@ -0,0 +1 @@ +#include "mathHelper.h" diff --git a/crumpet-engine/mathHelper.h b/crumpet-engine/mathHelper.h new file mode 100644 index 0000000..891b121 --- /dev/null +++ b/crumpet-engine/mathHelper.h @@ -0,0 +1,55 @@ +#pragma once + +struct Vec3 { + Vec3(float x, float y, float z) { + this->x = x; + this->y = y; + this->z = z; + } + + float x; + float y; + float z; +}; + +struct Vec2 { + Vec2(float x, float y) { + this->x = x; + this->y = y; + } + + float x; + float y; +}; + +/* struct Vec2SInt16 { + Vec2SInt16(Sint16 x, Sint16 y) { + this->x = x; + this->y = y; + } + + Sint16 x; + Sint16 y; +}; + +struct Vec2UInt16 { + Vec2UInt16(Uint16 x, Uint16 y) { + this->x = x; + this->y = y; + } + + Uint16 x; + Uint16 y; +}; + +struct RectVect { + RectVect(Vec2SInt16 pos, Vec2UInt16 size) + : pos(1, 1), + size(1, 1) { + this->pos = pos; + this->size = size; + } + + Vec2SInt16 pos; + Vec2UInt16 size; +}; */