diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/crepe/api/Color.cpp | 53 | ||||
-rw-r--r-- | src/crepe/api/Color.h | 34 | ||||
-rw-r--r-- | src/crepe/api/Point.h | 14 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 28 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 13 | ||||
-rw-r--r-- | src/crepe/api/game.cpp | 26 | ||||
-rw-r--r-- | src/crepe/api/game.h | 16 | ||||
-rw-r--r-- | src/crepe/api/spritesheet.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/spritesheet.h | 4 | ||||
-rw-r--r-- | src/crepe/core/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/crepe/core/renderSystem.cpp | 37 | ||||
-rw-r--r-- | src/crepe/core/renderSystem.h | 13 | ||||
-rw-r--r-- | src/crepe/facade/SdlContext.cpp | 145 | ||||
-rw-r--r-- | src/crepe/facade/SdlContext.h | 19 | ||||
-rw-r--r-- | src/crepe/facade/Texture.cpp | 6 | ||||
-rw-r--r-- | src/crepe/facade/Texture.h | 8 | ||||
-rw-r--r-- | src/dummy_rendering.cpp | 16 |
20 files changed, 349 insertions, 105 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d29a54..a9193fd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,6 +33,6 @@ install( FILE_SET HEADERS DESTINATION include/crepe ) -add_executable(dummy_rm dummy_resource_manager.cpp) +add_executable(dummy_rm dummy_rendering.cpp) #add_executable(dummy_rm dummy_audio.cpp) target_link_libraries(dummy_rm PUBLIC crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 69e67ac..05f14d1 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -8,4 +8,5 @@ add_subdirectory(api) add_subdirectory(util) add_subdirectory(fabricator) add_subdirectory(facade) +add_subdirectory(core) diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 96b55cf..dcac0ae 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC resource_manager.cpp Resource.cpp game.cpp + Color.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,4 +16,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES resource_manager.h Component.h AudioSource.h + Sprite.h + Color.h + Transform.h + Point.h ) diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp new file mode 100644 index 0000000..c73ce6c --- /dev/null +++ b/src/crepe/api/Color.cpp @@ -0,0 +1,53 @@ + + +#include "Color.h" + + +using namespace crepe::api; + +Color Color::white = Color(255,255,255,0); +Color Color::red = Color(255,0,0,0); +Color Color::green = Color(0,255,0,0); +Color Color::blue = Color(0,0,255,0); +Color Color::black = Color(0,0,0,0); +Color Color::cyan = Color(0,255,255,0); +Color Color::yellow = Color(255,255,0,0); +Color Color::magenta= Color(255,0,255,0); + +Color::Color(double red, double green, double blue, double alpha){ + this->a = alpha; + this->r = red; + this->g = green; + this->b = blue; +}; + +const Color& Color::get_white(){ + return Color::white; +}; + +const Color& Color::get_red(){ + return Color::red; +}; +const Color& Color::get_green(){ + return Color::green; +}; +const Color& Color::get_blue(){ + return Color::blue; +}; + +const Color& Color::get_black(){ + return Color::black; +}; + +const Color& Color::get_cyan(){ + return Color::cyan; +}; + +const Color& Color::get_yellow(){ + return Color::yellow; +}; + +const Color& Color::get_magenta(){ + return Color::magenta; +}; + diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h new file mode 100644 index 0000000..207434e --- /dev/null +++ b/src/crepe/api/Color.h @@ -0,0 +1,34 @@ +#pragma once + +namespace crepe::api { + +class Color { + +public: + Color(double red, double green, double blue, double alpha); + static const Color & get_white(); + static const Color & get_red(); + static const Color & get_green(); + static const Color & get_blue(); + static const Color & get_cyan(); + static const Color & get_magenta(); + static const Color & get_yellow(); + static const Color & get_black(); + +private: + double r; + double g; + double b; + double a; + + static Color white; + static Color red; + static Color green; + static Color blue; + static Color cyan; + static Color magenta; + static Color yellow; + static Color black; +}; + +} // namespace crepe::api diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h new file mode 100644 index 0000000..463aa7c --- /dev/null +++ b/src/crepe/api/Point.h @@ -0,0 +1,14 @@ +#pragma once + + + +namespace crepe::api { + +class Point { +public: + double x; + double y; +}; + + +} diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h new file mode 100644 index 0000000..84eeb83 --- /dev/null +++ b/src/crepe/api/Sprite.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Component.h" +#include "api/Color.h" +#include "facade/Texture.h" +#include <cstdint> + + +namespace crepe::api { + +struct flip_settings{ + bool flipX : 1; + bool flipY : 1; +}; +class Sprite : public Component { + +public: + Sprite(crepe::Texture& image, const Color& color, const flip_settings& flip ) : sprite_image(&image), color(color), flip(flip){} + crepe::Texture* sprite_image; + Color color; + flip_settings flip; + uint8_t sortingLayer; + uint8_t orderInLayer; + + +}; + +} diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h new file mode 100644 index 0000000..d4dfafc --- /dev/null +++ b/src/crepe/api/Transform.h @@ -0,0 +1,13 @@ +#pragma once + +#include "api/Component.h" +#include "api/Point.h" +namespace crepe::api { + +class Transform : public Component { +public: + Point position; // Translation (shift) + double rotation; // Rotation, in radians + double scale; // Multiplication factoh +}; +} // namespace crepe::api diff --git a/src/crepe/api/game.cpp b/src/crepe/api/game.cpp index 02a0132..01920a9 100644 --- a/src/crepe/api/game.cpp +++ b/src/crepe/api/game.cpp @@ -2,17 +2,29 @@ #include "game.h" -#include "api/spritesheet.h" +#include "core/renderSystem.h" #include "facade/SdlContext.h" -#include "facade/Texture.h" -#include <vector> -void game::render(std::vector<crepe::Texture*> & draw, std::vector<crepe::api::Spritesheet*> & ss){ - auto& ctx = crepe::SdlContext::get_instance(); - - ctx.loop(*draw[0], *ss[0]); +Engine::Engine(int windowHeight, int window_with){ + crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); } +void Engine::loop() { + + bool running = true; + crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); + RenderSystem rendering; + + while (running) { + ctx.handleEvents(running); + + ctx.clearScreen(); + + rendering.render(); + + ctx.presentScreen(); + } +} diff --git a/src/crepe/api/game.h b/src/crepe/api/game.h index 7cde954..64027fa 100644 --- a/src/crepe/api/game.h +++ b/src/crepe/api/game.h @@ -1,15 +1,17 @@ #pragma once -#include "api/spritesheet.h" -#include "facade/Texture.h" -#include <vector> -class game { +class Engine{ public: - game(){} - ~game(){} + Engine(int windowWith, int windowHeight); + ~Engine() = default; - void render(std::vector<crepe::Texture*>&, std::vector<crepe::api::Spritesheet*>&); + void loop(); + + +private: + int window_height; + int window_width; }; diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp index 93a2b65..7f5da38 100644 --- a/src/crepe/api/spritesheet.cpp +++ b/src/crepe/api/spritesheet.cpp @@ -30,12 +30,6 @@ void Spritesheet::select_sprite(const int x, const int y){ m_clip.y = y * m_clip.h; } -void Spritesheet::draw_selected_sprite(const int x, const int y){ - auto& ctx = SdlContext::get_instance(); - SDL_Rect tmp = { x, y, m_clip.w, m_clip.h}; - SDL_RenderCopy(ctx.m_game_renderer, this->m_spritesheet, &this->m_clip, &tmp); -} - void Spritesheet::load(std::unique_ptr<api::Resource> res, const int row, const int col){ auto& ctx = SdlContext::get_instance(); diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h index 7f46296..503dcef 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -24,14 +24,10 @@ private: void load(std::unique_ptr<api::Resource> res, const int row, const int col);; SDL_Texture* get_texture() const; - - private: SDL_Texture* m_spritesheet; SDL_Rect m_clip; - - friend class SdlContext; }; } diff --git a/src/crepe/core/CMakeLists.txt b/src/crepe/core/CMakeLists.txt new file mode 100644 index 0000000..c44f0f6 --- /dev/null +++ b/src/crepe/core/CMakeLists.txt @@ -0,0 +1,8 @@ +target_sources(crepe PUBLIC + renderSystem.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + renderSystem.h +) + diff --git a/src/crepe/core/renderSystem.cpp b/src/crepe/core/renderSystem.cpp new file mode 100644 index 0000000..a06aeba --- /dev/null +++ b/src/crepe/core/renderSystem.cpp @@ -0,0 +1,37 @@ + + + +#include "renderSystem.h" +#include <vector> + +#include "api/Color.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include "facade/SdlContext.h" +#include "facade/Texture.h" + +using namespace crepe::api; + + +static crepe::Texture player("../asset/texture/img.png"); + + +void RenderSystem::render(){ + + Sprite sprite(player, Color::get_red(), {1,1}); + Transform transform ={ + .position = {0,0}, + .rotation = 0, + .scale = 1, + }; + + // this will get changed to ecs getter of componets + crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); + + ctx.draw(sprite, transform); + /* + for(const auto& S : test_objects){ + ctx.draw(S, const api::Transform &) + } + */ +} diff --git a/src/crepe/core/renderSystem.h b/src/crepe/core/renderSystem.h new file mode 100644 index 0000000..9011b30 --- /dev/null +++ b/src/crepe/core/renderSystem.h @@ -0,0 +1,13 @@ + +#pragma once + + + +class RenderSystem { + +public: + RenderSystem() = default; + ~RenderSystem() = default; + + void render(); +}; diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp index 7e2d79f..b2043e5 100644 --- a/src/crepe/facade/SdlContext.cpp +++ b/src/crepe/facade/SdlContext.cpp @@ -2,54 +2,88 @@ #include "SdlContext.h" #include "SDL_rect.h" -#include "api/spritesheet.h" +#include "api/Sprite.h" +#include "api/Transform.h" #include "facade/Texture.h" #include "util/log.h" #include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> #include <SDL2/SDL_render.h> #include <SDL2/SDL_surface.h> #include <SDL2/SDL_video.h> -#include <SDL2/SDL_image.h> +#include <cstddef> #include <iostream> -#include <iterator> #include <ostream> using namespace crepe; - -SdlContext& SdlContext::get_instance(){ +SdlContext & SdlContext::get_instance() { static SdlContext instance; return instance; } +void SdlContext::handleEvents(bool & running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} +void SdlContext::clearScreen() { SDL_RenderClear(this->m_game_renderer); } + +void SdlContext::presentScreen() { SDL_RenderPresent(this->m_game_renderer); } -SdlContext::SdlContext(){ - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; - return; - } +void SdlContext::draw(const api::Sprite & sprite, const api::Transform& transform) { + static SDL_RendererFlip renderFlip + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flipX) + | (SDL_FLIP_VERTICAL * sprite.flip.flipY)); - m_game_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN); - if (!m_game_window) { - std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; - } + // needs maybe camera for position + static SDL_Rect dstrect = { + .x = static_cast<int>(transform.position.x), + .y = static_cast<int>(transform.position.y), + .w = static_cast<int>(sprite.sprite_image->get_rect().w * transform.scale), + .h = static_cast<int>(sprite.sprite_image->get_rect().h * transform.scale), + }; - m_game_renderer = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); - if (!m_game_renderer) { - std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl; + SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->get_texture(), + &sprite.sprite_image->get_rect(), &dstrect, 0, NULL, renderFlip); +} + +SdlContext::SdlContext() { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() + << std::endl; + return; + } + + m_game_window + = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_SHOWN); + if (!m_game_window) { + std::cerr << "Window could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + } + + m_game_renderer + = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); + if (!m_game_renderer) { + std::cerr << "Renderer could not be created! SDL_Error: " + << SDL_GetError() << std::endl; SDL_DestroyWindow(m_game_window); - return; - } - int imgFlags = IMG_INIT_PNG; - if( !( IMG_Init( imgFlags ) & imgFlags ) ) - { - std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; - } + return; + } + int imgFlags = IMG_INIT_PNG; + if (!(IMG_Init(imgFlags) & imgFlags)) { + std::cout << "SDL_image could not initialize! SDL_image Error: " + << IMG_GetError() << std::endl; + } + SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); } -SdlContext::~SdlContext(){ - if(m_game_renderer) - SDL_DestroyRenderer(m_game_renderer); +SdlContext::~SdlContext() { + if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); if (m_game_window) { SDL_DestroyWindow(m_game_window); @@ -58,69 +92,46 @@ SdlContext::~SdlContext(){ SDL_Quit(); } -SDL_Texture* SdlContext::setTextureFromPath(const char* path, SDL_Rect& clip, const int row, const int col){ +SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, + const int row, const int col) { dbg_trace(); - - SDL_Surface* tmp = IMG_Load(path); + + SDL_Surface * tmp = IMG_Load(path); if (!tmp) { std::cerr << "Error surface " << IMG_GetError << std::endl; } - clip.w = tmp->w / col; - clip.h = tmp->h / row; + clip.w = tmp->w / col; + clip.h = tmp->h / row; - SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + SDL_Texture * CreatedTexture + = SDL_CreateTextureFromSurface(m_game_renderer, tmp); if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; } SDL_FreeSurface(tmp); return CreatedTexture; } -SDL_Texture* SdlContext::setTextureFromPath(const char* path){ +SDL_Texture * SdlContext::setTextureFromPath(const char * path) { dbg_trace(); - - SDL_Surface* tmp = IMG_Load(path); + + SDL_Surface * tmp = IMG_Load(path); if (!tmp) { std::cerr << "Error surface " << IMG_GetError << std::endl; } - SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + SDL_Texture * CreatedTexture + = SDL_CreateTextureFromSurface(m_game_renderer, tmp); if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; } SDL_FreeSurface(tmp); return CreatedTexture; } - -void SdlContext::loop(const Texture& texture, api::Spritesheet& ss){ - SDL_RenderClear(m_game_renderer); - bool quit = false; - SDL_Event event; - - while (!quit) { - Uint32 ticks = SDL_GetTicks(); - int sprite = (ticks / 100) % 4; - ss.select_sprite(sprite, 0); - - while (SDL_PollEvent(&event) != NULL) { - switch (event.type) { - case SDL_QUIT: - quit = true; - break; - } - - } - - SDL_RenderClear(m_game_renderer); - SDL_RenderCopy(m_game_renderer, texture.get_texture(), NULL, NULL); - ss.draw_selected_sprite(10, 10); - SDL_RenderPresent(m_game_renderer); - } -} - - diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h index 329a374..c8f1304 100644 --- a/src/crepe/facade/SdlContext.h +++ b/src/crepe/facade/SdlContext.h @@ -1,23 +1,28 @@ #pragma once #include "SDL_rect.h" -#include "Texture.h" -#include "api/spritesheet.h" +#include "api/Sprite.h" +#include "api/Transform.h" #include <SDL2/SDL_render.h> #include <SDL2/SDL_video.h> namespace crepe { -class Texture; -class Spritesheet; class SdlContext { public: - void loop(const Texture& , api::Spritesheet&); + + void handleEvents(bool& running); + void clearScreen(); + void presentScreen(); + void draw(const api::Sprite&, const api::Transform&); // singleton static SdlContext & get_instance(); + SDL_Texture* setTextureFromPath(const char*); + SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); + private: SdlContext(); virtual ~SdlContext(); @@ -27,12 +32,8 @@ private: SdlContext & operator=(const SdlContext &) = delete; SdlContext & operator=(SdlContext &&) = delete; - SDL_Texture* setTextureFromPath(const char*); - SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); private: - friend class Texture; - friend class api::Spritesheet; SDL_Window* m_game_window; SDL_Renderer* m_game_renderer; diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp index 220ef2e..b4e3aa8 100644 --- a/src/crepe/facade/Texture.cpp +++ b/src/crepe/facade/Texture.cpp @@ -27,9 +27,13 @@ Texture::~Texture(){ void Texture::load(std::unique_ptr<api::Resource> res) { dbg_trace(); SdlContext& ctx = SdlContext::get_instance(); - m_texture = ctx.setTextureFromPath(res->canonical()); + m_texture = ctx.setTextureFromPath(res->canonical(), srcrect, 1, 1); } SDL_Texture* Texture::get_texture() const{ return m_texture; } + +SDL_Rect& Texture::get_rect() { + return srcrect; +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index a5fcca9..db2f1f9 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,13 +1,15 @@ #pragma once +#include "SDL_rect.h" #include "api/baseResource.h" -#include "facade/SdlContext.h" #include "api/Resource.h" #include <SDL2/SDL_render.h> #include <memory> namespace crepe { + + class Texture : public api::BaseResource{ public: @@ -16,13 +18,13 @@ public: ~Texture(); SDL_Texture* get_texture() const; + SDL_Rect& get_rect() ; private: void load(std::unique_ptr<api::Resource> res); private: SDL_Texture* m_texture; - - friend class SdlContext; + SDL_Rect srcrect; }; } // namespace crepe diff --git a/src/dummy_rendering.cpp b/src/dummy_rendering.cpp new file mode 100644 index 0000000..9bbf92d --- /dev/null +++ b/src/dummy_rendering.cpp @@ -0,0 +1,16 @@ + + + + + + + + +#include "api/game.h" +int main(){ + + Engine engine(800,600); + + engine.loop(); + +} |