diff --git a/crumpet-engine/Game.cpp b/crumpet-engine/Game.cpp index 7a3d9bf..8b4b779 100644 --- a/crumpet-engine/Game.cpp +++ b/crumpet-engine/Game.cpp @@ -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) diff --git a/crumpet-engine/crumpet-engine.vcxproj b/crumpet-engine/crumpet-engine.vcxproj index 4cee162..b877e5e 100644 --- a/crumpet-engine/crumpet-engine.vcxproj +++ b/crumpet-engine/crumpet-engine.vcxproj @@ -80,7 +80,7 @@ ./lib;%(AdditionalLibraryDirectories) - SDL2.lib;SDL2main.lib;SDL2test.lib;%(AdditionalDependencies) + SDL2.lib;SDL2main.lib;SDL2test.lib;SDL2_image.lib;%(AdditionalDependencies) @@ -126,14 +126,14 @@ - + - + diff --git a/crumpet-engine/crumpet-engine.vcxproj.filters b/crumpet-engine/crumpet-engine.vcxproj.filters index f980d56..b42a6be 100644 --- a/crumpet-engine/crumpet-engine.vcxproj.filters +++ b/crumpet-engine/crumpet-engine.vcxproj.filters @@ -2,9 +2,9 @@ - - + + @@ -12,14 +12,14 @@ + + headers + headers headers - - headers - \ No newline at end of file diff --git a/crumpet-engine/entity.cpp b/crumpet-engine/entity.cpp index 781b01e..b951f92 100644 --- a/crumpet-engine/entity.cpp +++ b/crumpet-engine/entity.cpp @@ -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() { } diff --git a/crumpet-engine/entity.h b/crumpet-engine/entity.h index 8ff4152..07f4336 100644 --- a/crumpet-engine/entity.h +++ b/crumpet-engine/entity.h @@ -1,19 +1,34 @@ #pragma once -#include "game.h" +#include +#include +#include +#include + +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; }; diff --git a/crumpet-engine/game.h b/crumpet-engine/game.h index 05dd1f2..fcbd1d5 100644 --- a/crumpet-engine/game.h +++ b/crumpet-engine/game.h @@ -1,19 +1,14 @@ #pragma once #include +#include #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; }; diff --git a/crumpet-engine/include/SDL_image.h b/crumpet-engine/include/SDL_image.h new file mode 100644 index 0000000..f3148a7 --- /dev/null +++ b/crumpet-engine/include/SDL_image.h @@ -0,0 +1,161 @@ +/* + SDL_image: An example image loading library for use with SDL + Copyright (C) 1997-2018 Sam Lantinga + + 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_ */ diff --git a/crumpet-engine/lib/SDL2_image.lib b/crumpet-engine/lib/SDL2_image.lib new file mode 100644 index 0000000..3fc6a5a Binary files /dev/null and b/crumpet-engine/lib/SDL2_image.lib differ diff --git a/crumpet-engine/main.cpp b/crumpet-engine/main.cpp index c67caca..aef2201 100644 --- a/crumpet-engine/main.cpp +++ b/crumpet-engine/main.cpp @@ -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; diff --git a/crumpet-engine/renderer.cpp b/crumpet-engine/renderer.cpp index aa4fa35..54a61ed 100644 --- a/crumpet-engine/renderer.cpp +++ b/crumpet-engine/renderer.cpp @@ -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; } diff --git a/crumpet-engine/renderer.h b/crumpet-engine/renderer.h index e782d93..434713d 100644 --- a/crumpet-engine/renderer.h +++ b/crumpet-engine/renderer.h @@ -5,8 +5,6 @@ #include #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; diff --git a/resources/mario.png b/resources/mario.png new file mode 100644 index 0000000..85758d2 Binary files /dev/null and b/resources/mario.png differ