Cube entity abstractions complete

This commit is contained in:
plane000
2018-09-29 13:50:30 +01:00
parent 119649353c
commit 6a470f4457
7 changed files with 115 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
#include "game.h"
Game::Game(std::string title, int width, int height, int targetFramerate, int targetUpdaterate)
: Renderer(title, width, height) {
: Renderer(title, width, height, targetFramerate) {
this->targetFramerate = targetFramerate;
this->targetUpdaterate = targetUpdaterate;

View File

@@ -1,9 +1,24 @@
#include "entity.h"
Entity::Entity(std::string name, SDL_Renderer* SDLRenderer, RenderType mode) {
Entity::Entity(std::string name, SDL_Renderer* SDLRenderer)
: m_rectPos(0, 0),
m_rectSize(0, 0),
m_col(0, 0, 0, 0) {
this->m_name = name;
this->m_SDLRenderer = SDLRenderer;
this->Rendertype = mode;
this->Rendertype = RenderType::MODE_TEXTURE;
}
Entity::Entity(std::string name, SDL_Renderer* SDLRenderer, PolyDrawType drawType)
: m_rectPos(0, 0),
m_rectSize(0, 0),
m_col(0, 0, 0, 0) {
this->m_name = name;
this->m_SDLRenderer = SDLRenderer;
this->Drawtype = drawType;
this->Rendertype = RenderType::MODE_POLYGON;
}
bool Entity::LoadTexture(std::string path) {
@@ -23,23 +38,27 @@ bool Entity::LoadTexture(std::string path) {
return true;
}
void Entity::SetVecPoints(std::vector<Vec2> polyPoints) {
this->m_polyPoints = polyPoints;
void Entity::SetDrawColour(Vec4 col) {
this->m_col = col;
// SDL_SetRenderDrawColor(m_SDLRenderer, col.x, col.y, col.z, col.w);
}
void Entity::AddVecPoint(Vec2 point) {
this->m_polyPoints.push_back(point);
}
void Entity::SetPolyDrawType(PolyDrawType type) {
this->Drawtype = type;
void Entity::SetRect(Vec2 pos, Vec2 size) {
m_rectPos = pos;
m_rectSize = size;
m_rect = { pos.x, pos.y, size.x, size.y };
}
void Entity::Render() {
if (Rendertype == RenderType::MODE_TEXTURE)
SDL_RenderCopy(m_SDLRenderer, m_texture, NULL, NULL);
// if (RenderType == EntityType::MODE_POLYGON)
if (Rendertype == RenderType::MODE_POLYGON) {
SDL_SetRenderDrawColor(m_SDLRenderer, m_col.x, m_col.y, m_col.z, m_col.w);
if (Drawtype == PolyDrawType::DRAW_FILLED_RECT)
SDL_RenderFillRect(m_SDLRenderer, &m_rect);
if (Drawtype == PolyDrawType::DRAW_OUTLINE_RECT)
SDL_RenderDrawRect(m_SDLRenderer, &m_rect);
}
}
Entity::~Entity() {

View File

@@ -22,29 +22,38 @@ enum struct PolyDrawType {
class Entity {
public:
Entity(std::string name, SDL_Renderer* SDLRenderer, RenderType mode); // Texture overload
// Entity(); // Polygon overload
Entity(std::string name, SDL_Renderer* SDLRenderer); // Texture overload
Entity(std::string name, SDL_Renderer* SDLRenderer, PolyDrawType drawType); // Polygon overload
RenderType Rendertype = RenderType::MODE_DEFAULT;
PolyDrawType Drawtype = PolyDrawType::DRAW_DEFAULT;
bool LoadTexture(std::string path);
void SetVecPoints(std::vector<Vec2> polyPoints);
void SetDrawColour(Vec4 col);
void SetRect(Vec2 pos, Vec2 size);
void SetVecPoints(std::vector<Vec2> linePoints);
void AddVecPoint(Vec2 point);
void SetPolyDrawType(PolyDrawType type);
void Render();
virtual ~Entity();
private:
std::string m_name;
std::vector<Vec2> m_polyPoints;
Vec2 m_rectPos;
Vec2 m_rectSize;
SDL_Rect m_rect;
Vec4 m_col;
std::vector<Vec2> m_linePoints;
SDL_Texture* m_texture;
std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
// std::string PATH = "E:/Games/crumpet-engine";
// std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
std::string PATH = "E:/Games/crumpet-engine";
SDL_Renderer* m_SDLRenderer;
};

View File

@@ -8,17 +8,27 @@
int main(int argc, char** argv) {
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 0, 60);
Entity mario("mario", game.SDLRenderer, RenderType::MODE_TEXTURE);
Entity mario("mario", game.SDLRenderer);
mario.LoadTexture("/resources/mario.png");
Entity box("box", game.SDLRenderer, RenderType::MODE_POLYGON);
Entity box("box", game.SDLRenderer, PolyDrawType::DRAW_FILLED_RECT);
box.SetDrawColour(Vec4(0xFF, 0x00, 0x00, 0xFF));
box.SetRect(Vec2(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4), Vec2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2));
Entity outlineBox("bigger green box", game.SDLRenderer, PolyDrawType::DRAW_OUTLINE_RECT);
outlineBox.SetDrawColour(Vec4(0x00, 0xFF, 0x00, 0xFF));
outlineBox.SetRect(Vec2(SCREEN_WIDTH / 6, SCREEN_HEIGHT / 6), Vec2(SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT * 2 / 3));
while (!game.IsDisplayClosed()) {
game.PollEvents();
game.RenderClear();
game.RenderEntity(&mario);
game.RenderEntity(&box);
game.RenderEntity(&outlineBox);
game.RenderUpdate();
}

View File

@@ -1,27 +1,66 @@
#pragma once
struct Vec4 {
Vec4(int x, int y, int z, int w) {
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
int x, y, z, w;
};
struct Vec4f {
Vec4f(float x, float y, float z, float w) {
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
float x, y, z, w;
};
struct Vec3 {
Vec3(float x, float y, float z) {
Vec3(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
float x;
float y;
float z;
int x, y, z;
};
struct Vec3f {
Vec3f(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
float x, y, z;
};
struct Vec2 {
Vec2(float x, float y) {
Vec2(int x, int y) {
this->x = x;
this->y = y;
}
float x;
float y;
int x, y;
};
struct Vec2f {
Vec2f(float x, float y) {
this->x = x;
this->y = y;
}
float x, y;
};
/* struct Vec2SInt16 {
Vec2SInt16(Sint16 x, Sint16 y) {
this->x = x;

View File

@@ -1,14 +1,18 @@
#include "renderer.h"
#include "entity.h"
Renderer::Renderer(std::string title, int width, int height) {
Renderer::Renderer(std::string title, int width, int height, int targetFramerate) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << "SDL could not initialize, SDL ERROR: " << SDL_GetError() << std::endl;
}
m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
SDLRenderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (targetFramerate == 0)
SDLRenderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);
if (targetFramerate == 1)
SDLRenderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
isClosed = false;
}

View File

@@ -7,7 +7,7 @@
class Renderer {
public:
Renderer(std::string title, int width, int height);
Renderer(std::string title, int width, int height, int targetFramerate);
SDL_Renderer *SDLRenderer;
void RenderEntity(Entity* entity);