aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-23 19:57:44 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-23 19:57:44 +0200
commitedbb6c892299e3b7f93638abcc9e55b2cfce2358 (patch)
treed9c306af19636f5c757397ec1c1a157a93c12f0c /src/crepe
parent1b96c6e3c57b9d1dc5fb02cbd24b625d7f7f5b05 (diff)
parenteaa05e7a981b0f581f5393882e4753d9294a3dba (diff)
merge with niels/rendering
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/Asset.cpp3
-rw-r--r--src/crepe/CMakeLists.txt4
-rw-r--r--src/crepe/GameObject.hpp15
-rw-r--r--src/crepe/RenderSystem.cpp41
-rw-r--r--src/crepe/RenderSystem.h18
-rw-r--r--src/crepe/SdlContext.cpp149
-rw-r--r--src/crepe/SdlContext.h50
-rw-r--r--src/crepe/api/AssetManager.cpp23
-rw-r--r--src/crepe/api/AssetManager.h50
-rw-r--r--src/crepe/api/AudioSource.cpp1
-rw-r--r--src/crepe/api/CMakeLists.txt8
-rw-r--r--src/crepe/api/Color.cpp53
-rw-r--r--src/crepe/api/Color.h34
-rw-r--r--src/crepe/api/Point.h14
-rw-r--r--src/crepe/api/Sprite.cpp16
-rw-r--r--src/crepe/api/Sprite.h24
-rw-r--r--src/crepe/api/Texture.cpp32
-rw-r--r--src/crepe/api/Texture.h32
-rw-r--r--src/crepe/api/Transform.cpp13
-rw-r--r--src/crepe/api/Transform.h23
20 files changed, 575 insertions, 28 deletions
diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp
index 15ddc27..3cbed0b 100644
--- a/src/crepe/Asset.cpp
+++ b/src/crepe/Asset.cpp
@@ -5,7 +5,8 @@
using namespace crepe;
Asset::Asset(const std::string & src) {
- this->src = std::filesystem::canonical(src);
+ //this->src = std::filesystem::canonical(src);
+ this->src = src;
this->file = std::ifstream(this->src, std::ios::in | std::ios::binary);
}
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index df3adf0..afaa74c 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -12,6 +12,8 @@ target_sources(crepe PUBLIC
PhysicsSystem.cpp
CollisionSystem.cpp
Collider.cpp
+ SdlContext.cpp
+ RenderSystem.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -29,6 +31,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
PhysicsSystem.h
CollisionSystem.h
Collider.h
+ SdlContext.h
+ RenderSystem.h
)
add_subdirectory(api)
diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp
new file mode 100644
index 0000000..8cd1abe
--- /dev/null
+++ b/src/crepe/GameObject.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "GameObject.h"
+
+#include "ComponentManager.h"
+
+namespace crepe {
+
+template <typename T, typename... Args>
+T & GameObject::add_component(Args &&... args) {
+ auto & mgr = ComponentManager::get_instance();
+ return mgr.add_component<T>(id, std::forward<Args>(args)...);
+}
+
+} // namespace crepe
diff --git a/src/crepe/RenderSystem.cpp b/src/crepe/RenderSystem.cpp
new file mode 100644
index 0000000..6aae3bb
--- /dev/null
+++ b/src/crepe/RenderSystem.cpp
@@ -0,0 +1,41 @@
+
+
+#include "RenderSystem.h"
+#include "ComponentManager.h"
+#include "SdlContext.h"
+#include "api/Sprite.h"
+#include "api/Transform.h"
+#include "util/log.h"
+#include <cstddef>
+#include <functional>
+#include <vector>
+
+using namespace crepe;
+using namespace crepe::api;
+
+RenderSystem::RenderSystem() { dbg_trace(); }
+
+RenderSystem::~RenderSystem() { dbg_trace(); }
+
+RenderSystem& RenderSystem::get_instance(){
+ static RenderSystem instance;
+ return instance;
+}
+
+void RenderSystem::update() {
+
+ ComponentManager& mgr = ComponentManager::get_instance();
+
+ std::vector<std::reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>();
+ std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>();
+
+ SdlContext& render = SdlContext::get_instance();
+ render.clear_screen();
+
+ for (size_t i = 0; i < sprites.size(); ++i) {
+ render.draw(sprites[i].get(), transforms[i].get());
+ }
+
+ render.present_screen();
+
+}
diff --git a/src/crepe/RenderSystem.h b/src/crepe/RenderSystem.h
new file mode 100644
index 0000000..5e86dce
--- /dev/null
+++ b/src/crepe/RenderSystem.h
@@ -0,0 +1,18 @@
+
+#pragma once
+
+#include "System.h"
+
+namespace crepe {
+
+class RenderSystem : public System {
+
+public:
+ static RenderSystem & get_instance();
+ void update();
+
+private:
+ RenderSystem();
+ ~RenderSystem();
+};
+} // namespace crepe
diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp
new file mode 100644
index 0000000..cc5148c
--- /dev/null
+++ b/src/crepe/SdlContext.cpp
@@ -0,0 +1,149 @@
+
+
+#include "SdlContext.h"
+
+#include "api/Sprite.h"
+#include "api/Texture.h"
+#include "api/Transform.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 <cmath>
+#include <cstddef>
+#include <iostream>
+
+using namespace crepe;
+
+SdlContext & SdlContext::get_instance() {
+ static SdlContext instance;
+ return instance;
+}
+
+void SdlContext::handle_events(bool & running) {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ if (event.type == SDL_QUIT) {
+ running = false;
+ }
+ }
+}
+
+SdlContext::~SdlContext() {
+ dbg_trace();
+
+ if (m_game_renderer != nullptr) SDL_DestroyRenderer(m_game_renderer);
+
+ if (m_game_window != nullptr) {
+ SDL_DestroyWindow(m_game_window);
+ }
+
+ IMG_Quit();
+ SDL_Quit();
+}
+
+void SdlContext::clear_screen() { SDL_RenderClear(this->m_game_renderer); }
+
+SdlContext::SdlContext() {
+ dbg_trace();
+
+ 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_HIDDEN);
+ 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 img_flags = IMG_INIT_PNG;
+ if (!(IMG_Init(img_flags) & img_flags)) {
+ std::cout << "SDL_image could not initialize! SDL_image Error: "
+ << IMG_GetError() << std::endl;
+ }
+}
+void SdlContext::present_screen() { SDL_RenderPresent(this->m_game_renderer); }
+
+void SdlContext::draw(const api::Sprite & sprite,
+ const api::Transform & transform) {
+
+ static SDL_RendererFlip render_flip
+ = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
+ | (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
+
+ int w, h;
+ SDL_QueryTexture(sprite.sprite_image->m_texture, NULL, NULL, &w, &h);
+ // needs maybe camera for position
+ SDL_Rect dstrect = {
+ .x = static_cast<int>(transform.position.x),
+ .y = static_cast<int>(transform.position.y),
+ .w = static_cast<int>(w * transform.scale),
+ .h = static_cast<int>(h * transform.scale),
+ };
+
+ double degrees = transform.rotation * 180 / M_PI;
+ SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->m_texture,
+ NULL, &dstrect, degrees, NULL, render_flip);
+}
+
+/*
+SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip,
+ const int row, const int col) {
+ dbg_trace();
+
+ 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;
+
+ SDL_Texture * CreatedTexture
+ = SDL_CreateTextureFromSurface(m_game_renderer, tmp);
+
+ if (!CreatedTexture) {
+ std::cerr << "Error could not create texture " << IMG_GetError
+ << std::endl;
+ }
+ SDL_FreeSurface(tmp);
+
+ return CreatedTexture;
+}
+*/
+
+SDL_Texture * SdlContext::texture_from_path(const char * path) {
+ dbg_trace();
+
+ SDL_Surface * tmp = IMG_Load(path);
+ if (!tmp) {
+ std::cerr << "Error surface " << IMG_GetError << std::endl;
+ }
+ SDL_Texture * created_texture
+ = SDL_CreateTextureFromSurface(m_game_renderer, tmp);
+
+ if (!created_texture) {
+ std::cerr << "Error could not create texture " << IMG_GetError
+ << std::endl;
+ }
+ SDL_FreeSurface(tmp);
+
+ return created_texture;
+}
diff --git a/src/crepe/SdlContext.h b/src/crepe/SdlContext.h
new file mode 100644
index 0000000..97adfa2
--- /dev/null
+++ b/src/crepe/SdlContext.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include "RenderSystem.h"
+#include "api/Sprite.h"
+#include "api/Transform.h"
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_video.h>
+
+namespace crepe::api {
+class Texture;
+}
+
+namespace crepe {
+
+class SdlContext {
+
+public:
+ // singleton
+ static SdlContext & get_instance();
+ SdlContext(const SdlContext &) = delete;
+ SdlContext(SdlContext &&) = delete;
+ SdlContext & operator=(const SdlContext &) = delete;
+ SdlContext & operator=(SdlContext &&) = delete;
+
+ //TODO decide events wouter?
+
+private:
+ void handle_events(bool & running);
+
+private:
+ SdlContext();
+ virtual ~SdlContext();
+
+private:
+ friend class api::Texture;
+ SDL_Texture * texture_from_path(const char *);
+ //SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col);
+
+private:
+ friend class RenderSystem;
+ void draw(const api::Sprite &, const api::Transform &);
+ void clear_screen();
+ void present_screen();
+
+private:
+ SDL_Window * m_game_window = nullptr;
+ SDL_Renderer * m_game_renderer = nullptr;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp
new file mode 100644
index 0000000..f6cc369
--- /dev/null
+++ b/src/crepe/api/AssetManager.cpp
@@ -0,0 +1,23 @@
+
+
+#include "AssetManager.h"
+#include "util/log.h"
+
+
+using namespace crepe::api;
+
+AssetManager& AssetManager::get_instance(){
+ static AssetManager instance;
+ return instance;
+}
+
+
+AssetManager::~AssetManager(){
+ dbg_trace();
+ this->asset_cache.clear();
+}
+
+AssetManager::AssetManager(){
+ dbg_trace();
+}
+
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h
new file mode 100644
index 0000000..1b8b86f
--- /dev/null
+++ b/src/crepe/api/AssetManager.h
@@ -0,0 +1,50 @@
+#pragma once
+
+
+
+#include <any>
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <utility>
+
+
+namespace crepe::api{
+
+class AssetManager{
+
+
+private:
+ std::unordered_map< std::string, std::any> asset_cache;
+
+private:
+ AssetManager();
+ virtual ~AssetManager();
+
+public:
+ AssetManager(const AssetManager &) = delete;
+ AssetManager(AssetManager &&) = delete;
+ AssetManager &operator=(const AssetManager &) = delete;
+ AssetManager &operator=(AssetManager &&) = delete;
+
+
+ static AssetManager& get_instance();
+
+
+public:
+ template<typename asset>
+ std::shared_ptr<asset> cache(const std::string& file_path, bool reload = false){
+ auto it = asset_cache.find(file_path);
+
+ if (!reload && it != asset_cache.end()) {
+ return std::any_cast<std::shared_ptr<asset>>(it->second);
+ }
+
+ std::shared_ptr<asset> new_asset = std::make_shared<asset>(file_path.c_str());
+
+ asset_cache[file_path] = new_asset;
+
+ return new_asset;
+ }
+};
+}
diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp
index bb067dc..10b3b49 100644
--- a/src/crepe/api/AudioSource.cpp
+++ b/src/crepe/api/AudioSource.cpp
@@ -3,6 +3,7 @@
#include "AudioSource.h"
#include "../Sound.h"
+#include <memory>
using namespace crepe::api;
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index ef01594..f6de96e 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -8,6 +8,9 @@ target_sources(crepe PUBLIC
Force.cpp
ParticleEmitter.cpp
Transform.cpp
+ Color.cpp
+ Texture.cpp
+ AssetManager.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -21,5 +24,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Force.h
ParticleEmitter.h
Transform.h
+ Point.h
+ Color.h
+ Texture.h
+ AssetManager.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.cpp b/src/crepe/api/Sprite.cpp
index 4eda466..d9e26ab 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -1,8 +1,18 @@
-#include <string>
+
#include "Sprite.h"
+#include "api/Texture.h"
+#include "util/log.h"
+#include <memory>
+#include <utility>
-using namespace crepe::api;
using namespace std;
+using namespace crepe;
+using namespace crepe::api;
+
+Sprite::Sprite(shared_ptr<Texture> image, const Color & color,
+ const flip_settings & flip) : color(color), flip(flip), sprite_image(image) {
+ dbg_trace();
+}
-Sprite::Sprite(uint32_t gameObjectId, string path) : Component(gameObjectId), path(path) {}
+Sprite::~Sprite() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index c06e76c..920f91e 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,16 +1,30 @@
#pragma once
-#include <string>
+#include "Component.h"
+#include "api/Color.h"
+#include "api/Texture.h"
+#include <SDL2/SDL_rect.h>
+#include <cstdint>
+#include <memory>
-#include "../Component.h"
namespace crepe::api {
+struct flip_settings{
+ bool flip_x: 1;
+ bool flip_y : 1;
+};
class Sprite : public Component {
+
public:
- Sprite(uint32_t gameObjectId,std::string path);
+ Sprite(std::shared_ptr<Texture> image, const Color& color, const flip_settings& flip );
+ ~Sprite();
+ std::shared_ptr<Texture> sprite_image;
+ Color color;
+ flip_settings flip;
+ uint8_t sorting_in_layer;
+ uint8_t order_in_layer;
- std::string path;
};
-} // namespace crepe::api
+}
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
new file mode 100644
index 0000000..ba06c6d
--- /dev/null
+++ b/src/crepe/api/Texture.cpp
@@ -0,0 +1,32 @@
+
+
+#include "Asset.h"
+#include "SdlContext.h"
+#include "util/log.h"
+
+#include "Texture.h"
+#include <SDL2/SDL_render.h>
+
+using namespace crepe::api;
+
+Texture::Texture(std::unique_ptr<Asset> res) {
+ dbg_trace();
+ this->load(std::move(res));
+}
+
+Texture::Texture(const char * src) {
+ dbg_trace();
+ this->load(std::make_unique<Asset>(src));
+}
+
+Texture::~Texture() {
+ dbg_trace();
+ if (this->m_texture != nullptr) {
+ SDL_DestroyTexture(m_texture);
+ }
+}
+void Texture::load(std::unique_ptr<Asset> res) {
+ SdlContext & ctx = SdlContext::get_instance();
+ m_texture = ctx.texture_from_path(res->canonical());
+
+}
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
new file mode 100644
index 0000000..b376b44
--- /dev/null
+++ b/src/crepe/api/Texture.h
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "Asset.h"
+#include <SDL2/SDL_render.h>
+#include <memory>
+
+
+namespace crepe {
+ class SdlContext;
+}
+
+namespace crepe::api {
+
+class Texture {
+
+public:
+ Texture(const char * src);
+ Texture(std::unique_ptr<Asset> res);
+ ~Texture();
+
+
+private:
+ void load(std::unique_ptr<Asset> res);
+
+
+private:
+ SDL_Texture * m_texture = nullptr;
+
+ friend class crepe::SdlContext;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp
index 2c39523..c83461f 100644
--- a/src/crepe/api/Transform.cpp
+++ b/src/crepe/api/Transform.cpp
@@ -1,6 +1,13 @@
+
+
#include "Transform.h"
+#include "api/Point.h"
+#include "util/log.h"
-using namespace crepe;
+using namespace crepe::api;
-Transform::Transform(uint32_t gameObjectId,Position position, int rotation, int scale)
- : Component(gameObjectId), postion(postion), rotation(rotation), scale(scale) {}
+Transform::Transform(Point & point, double rot, double scale)
+ : position(point), rotation(rot), scale(scale) {
+ dbg_trace();
+}
+Transform::~Transform() { dbg_trace(); }
diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h
index 3e8d142..a34ebb1 100644
--- a/src/crepe/api/Transform.h
+++ b/src/crepe/api/Transform.h
@@ -1,22 +1,15 @@
#pragma once
#include "Component.h"
-
-namespace crepe {
-
-struct Position
-{
- int x;
- int y;
-};
-
+#include "api/Point.h"
+namespace crepe::api {
class Transform : public Component {
public:
- Transform(uint32_t gameObjectId,Position position, int rotation, int scale);
- Position postion;
- int rotation;
- int scale;
+ Transform(Point&, double, double);
+ ~Transform();
+ Point position; // Translation (shift)
+ double rotation; // Rotation, in radians
+ double scale; // Multiplication factoh
};
-
-} // namespace crepe
+} // namespace crepe::api