Improved entity class

This commit is contained in:
plane000
2018-09-28 20:01:32 +01:00
parent 08a93a0901
commit 42b17bfc23
12 changed files with 227 additions and 53 deletions

View File

@@ -1,7 +1,7 @@
#include "game.h"
Game::Game(std::string title, int width, int height, int targetFramerate, int targetUpdaterate)
: m_display(title, width, height) {
: Renderer(title, width, height) {
this->targetFramerate = targetFramerate;
this->targetUpdaterate = targetUpdaterate;
@@ -9,22 +9,6 @@ Game::Game(std::string title, int width, int height, int targetFramerate, int ta
std::cout << "Engine initialized" << std::endl;
}
void Game::UpdateDisplay() {
m_display.Update();
}
void Game::CloseDisplay() {
m_display.Close();
}
bool Game::IsDisplayClosed() {
return m_display.IsClosed();
}
Renderer Game::GetDisplay() {
return m_display;
}
void Game::PollEvents() {
while (SDL_PollEvent(&m_event) != 0)
if (m_event.type == SDL_QUIT)

View File

@@ -80,7 +80,7 @@
</ClCompile>
<Link>
<AdditionalLibraryDirectories>./lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2test.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2test.lib;SDL2_image.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -126,14 +126,14 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="entity.cpp" />
<ClCompile Include="renderer.cpp" />
<ClCompile Include="game.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="renderer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="entity.h" />
<ClInclude Include="renderer.h" />
<ClInclude Include="game.h" />
<ClInclude Include="renderer.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -2,9 +2,9 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="game.cpp" />
<ClCompile Include="renderer.cpp" />
<ClCompile Include="entity.cpp" />
<ClCompile Include="renderer.cpp" />
<ClCompile Include="game.cpp" />
</ItemGroup>
<ItemGroup>
<Filter Include="headers">
@@ -12,14 +12,14 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="entity.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="game.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="renderer.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="entity.h">
<Filter>headers</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,26 +1,34 @@
#include "entity.h"
Entity::Entity(std::string name, SDL_Renderer* SDLRenderer) {
Entity::Entity(std::string name, SDL_Renderer* SDLRenderer, EntityType mode) {
this->m_name = name;
this->m_SDLRenderer = SDLRenderer;
this->RenderType = mode;
}
bool Entity::LoadTexture(std::string path) {
SDL_Surface* loadedSurface; // TODO: IMG_Load(path.c_str());
SDL_Surface* loadedSurface = IMG_Load((PATH + path).c_str());
if (loadedSurface == NULL) {
std::cout << "Unable to load image from:" << path << " IMG ERROR: "; // TODO: << IMG_GetError() << std::endl;
std::cout << "Unable to load image from:" << (PATH + path).c_str() << " IMG ERROR: " << IMG_GetError() << std::endl;
return false;
}
m_texture = SDL_CreateTextureFromSurface(m_SDLRenderer, loadedSurface);
if (m_texture == NULL) {
std::cout << "Unable to create texture from:" << path << " SDL ERROR: " << SDL_GetError() << std::endl;
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::Render() {
if (RenderType == EntityType::MODE_TEXTURE)
SDL_RenderCopy(m_SDLRenderer, m_texture, NULL, NULL);
// if (RenderType == EntityType::MODE_POLYGON)
}
Entity::~Entity() {
}

View File

@@ -1,19 +1,34 @@
#pragma once
#include "game.h"
#include <string>
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
enum struct EntityType {
MODE_DEFAULT,
MODE_TEXTURE,
MODE_POLYGON
};
class Entity {
public:
Entity(std::string name, SDL_Renderer* SDLRenderer); // Texture overload
Entity(std::string name, SDL_Renderer* SDLRenderer, EntityType mode); // Texture overload
// Entity(); // Polygon overload
EntityType RenderType = EntityType::MODE_DEFAULT;
bool LoadTexture(std::string path);
void Render();
virtual ~Entity();
private:
std::string m_name;
SDL_Renderer* m_SDLRenderer;
// std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
std::string PATH = "E:/Games/crumpet-engine";
SDL_Texture* m_texture;
SDL_Renderer* m_SDLRenderer;
};

View File

@@ -1,19 +1,14 @@
#pragma once
#include <string>
#include <SDL.h>
#include "renderer.h"
#include "entity.h"
class Game {
class Game : public Renderer {
public:
Game(std::string title, int width, int height, int targetFramerate, int targetUpdaterate);
void UpdateDisplay();
void CloseDisplay();
bool IsDisplayClosed();
Renderer GetDisplay();
void PollEvents();
virtual ~Game();
@@ -24,6 +19,5 @@ private:
int framerate; // Current framerate
int updaterate; // Current updaterate
Renderer m_display;
SDL_Event m_event;
};

View File

@@ -0,0 +1,161 @@
/*
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* A simple library to load images of various formats as SDL surfaces */
#ifndef SDL_IMAGE_H_
#define SDL_IMAGE_H_
#include "SDL.h"
#include "SDL_version.h"
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif
/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
*/
#define SDL_IMAGE_MAJOR_VERSION 2
#define SDL_IMAGE_MINOR_VERSION 0
#define SDL_IMAGE_PATCHLEVEL 3
/* This macro can be used to fill a version structure with the compile-time
* version of the SDL_image library.
*/
#define SDL_IMAGE_VERSION(X) \
{ \
(X)->major = SDL_IMAGE_MAJOR_VERSION; \
(X)->minor = SDL_IMAGE_MINOR_VERSION; \
(X)->patch = SDL_IMAGE_PATCHLEVEL; \
}
/**
* This is the version number macro for the current SDL_image version.
*/
#define SDL_IMAGE_COMPILEDVERSION \
SDL_VERSIONNUM(SDL_IMAGE_MAJOR_VERSION, SDL_IMAGE_MINOR_VERSION, SDL_IMAGE_PATCHLEVEL)
/**
* This macro will evaluate to true if compiled with SDL_image at least X.Y.Z.
*/
#define SDL_IMAGE_VERSION_ATLEAST(X, Y, Z) \
(SDL_IMAGE_COMPILEDVERSION >= SDL_VERSIONNUM(X, Y, Z))
/* This function gets the version of the dynamically linked SDL_image library.
it should NOT be used to fill a version structure, instead you should
use the SDL_IMAGE_VERSION() macro.
*/
extern DECLSPEC const SDL_version * SDLCALL IMG_Linked_Version(void);
typedef enum
{
IMG_INIT_JPG = 0x00000001,
IMG_INIT_PNG = 0x00000002,
IMG_INIT_TIF = 0x00000004,
IMG_INIT_WEBP = 0x00000008
} IMG_InitFlags;
/* Loads dynamic libraries and prepares them for use. Flags should be
one or more flags from IMG_InitFlags OR'd together.
It returns the flags successfully initialized, or 0 on failure.
*/
extern DECLSPEC int SDLCALL IMG_Init(int flags);
/* Unloads libraries loaded with IMG_Init */
extern DECLSPEC void SDLCALL IMG_Quit(void);
/* Load an image from an SDL data source.
The 'type' may be one of: "BMP", "GIF", "PNG", etc.
If the image format supports a transparent pixel, SDL will set the
colorkey for the surface. You can enable RLE acceleration on the
surface afterwards by calling:
SDL_SetColorKey(image, SDL_RLEACCEL, image->format->colorkey);
*/
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type);
/* Convenience functions */
extern DECLSPEC SDL_Surface * SDLCALL IMG_Load(const char *file);
extern DECLSPEC SDL_Surface * SDLCALL IMG_Load_RW(SDL_RWops *src, int freesrc);
#if SDL_VERSION_ATLEAST(2,0,0)
/* Load an image directly into a render texture.
*/
extern DECLSPEC SDL_Texture * SDLCALL IMG_LoadTexture(SDL_Renderer *renderer, const char *file);
extern DECLSPEC SDL_Texture * SDLCALL IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc);
extern DECLSPEC SDL_Texture * SDLCALL IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type);
#endif /* SDL 2.0 */
/* Functions to detect a file type, given a seekable source */
extern DECLSPEC int SDLCALL IMG_isICO(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isCUR(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isBMP(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isGIF(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isJPG(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isLBM(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isPCX(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isPNG(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isPNM(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isSVG(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isTIF(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isXCF(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isXPM(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isXV(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isWEBP(SDL_RWops *src);
/* Individual loading functions */
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadICO_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadCUR_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadBMP_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadGIF_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadLBM_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPCX_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNG_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNM_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadSVG_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTGA_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTIF_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXCF_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXPM_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXV_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadWEBP_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArray(char **xpm);
/* Individual saving functions */
extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char *file);
extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst);
extern DECLSPEC int SDLCALL IMG_SaveJPG(SDL_Surface *surface, const char *file, int quality);
extern DECLSPEC int SDLCALL IMG_SaveJPG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality);
/* We'll use SDL for reporting errors */
#define IMG_SetError SDL_SetError
#define IMG_GetError SDL_GetError
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
#include "close_code.h"
#endif /* SDL_IMAGE_H_ */

Binary file not shown.

View File

@@ -8,13 +8,18 @@
int main(int argc, char** argv) {
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 0, 60);
Entity mario("mario", game.SDLRenderer, EntityType::MODE_TEXTURE);
mario.LoadTexture("/resources/mario.png");
Entity box("box", game.SDLRenderer, EntityType::MODE_POLYGON);
while (!game.IsDisplayClosed()) {
game.PollEvents();
game.UpdateDisplay();
game.RenderClear();
game.RenderEntity(&mario);
game.RenderUpdate();
}
return 0;

View File

@@ -1,4 +1,5 @@
#include "renderer.h"
#include "entity.h"
Renderer::Renderer(std::string title, int width, int height) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
@@ -7,21 +8,28 @@ Renderer::Renderer(std::string title, int width, int height) {
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);
SDL_SetRenderDrawColor(SDLRenderer, 66, 134, 244, 255);
isClosed = false;
}
void Renderer::Update() {
SDL_RenderClear(SDLRenderer);
void Renderer::RenderEntity(Entity* entity) {
entity->Render();
}
void Renderer::RenderUpdate() {
SDL_SetRenderDrawColor(SDLRenderer, 66, 134, 244, 255);
SDL_RenderPresent(SDLRenderer);
}
bool Renderer::IsClosed() {
void Renderer::RenderClear() {
SDL_RenderClear(SDLRenderer);
}
bool Renderer::IsDisplayClosed() {
return isClosed;
}
void Renderer::Close() {
void Renderer::CloseDisplay() {
isClosed = true;
}

View File

@@ -5,8 +5,6 @@
#include <SDL.h>
#include "entity.h"
#define PATH "C:/Users/Ben/Desktop/crumpet-engine"
class Renderer {
public:
Renderer(std::string title, int width, int height);
@@ -14,10 +12,11 @@ public:
SDL_Renderer *SDLRenderer;
void RenderEntity(Entity* entity);
void Update();
void RenderUpdate();
void RenderClear();
bool IsClosed();
void Close();
bool IsDisplayClosed();
void CloseDisplay();
virtual ~Renderer();
private:
SDL_Window *m_window;

BIN
resources/mario.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB