diff options
Diffstat (limited to 'src')
40 files changed, 762 insertions, 75 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 446433c..b60a0cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_BUILD_TYPE Debug) project(crepe C CXX) find_package(SDL2 REQUIRED) +find_package(SDL2_image REQUIRED) find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) @@ -20,7 +21,8 @@ target_include_directories(crepe target_link_libraries(crepe PRIVATE soloud - PRIVATE SDL2 + PUBLIC SDL2 + PUBLIC SDL2_image ) add_subdirectory(crepe) diff --git a/src/build.sh b/src/build.sh new file mode 100755 index 0000000..05d086c --- /dev/null +++ b/src/build.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# creates the build dir and runs CMake with Ninja +cmake -B build -G Ninja + +ninja -C build + +ninja -C build/ rendering +ninja -C build/ asset_manager 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 ef2584e..497423e 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -6,14 +6,11 @@ target_sources(crepe PUBLIC Particle.cpp ParticleEmitter.cpp ParticleSystem.cpp - SDLApp.cpp + SdlContext.cpp ComponentManager.cpp Component.cpp - GameObject.cpp - Collider.cpp - Rigidbody.cpp - Sprite.cpp ScriptSystem.cpp + RenderSystem.cpp PhysicsSystem.cpp Transform.cpp Force.cpp @@ -27,21 +24,17 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ParticleEmitter.h ParticleSystem.h Particle.h - SDLApp.h + SdlContext.h ComponentManager.h ComponentManager.hpp Component.h - GameObject.h - GameObject.hpp - Collider.h - Rigidbody.h - Sprite.h System.h ScriptSystem.h PhysicsSystem.h Transform.h Force.h CollisionSystem.h + RenderSystem.h ) add_subdirectory(api) diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp deleted file mode 100644 index 36eb886..0000000 --- a/src/crepe/GameObject.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "GameObject.h" -#include "Transform.h" - -using namespace crepe; - -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - int layer) - : id(id), name(name), tag(tag), active(true), layer(layer) -{ - Position pos = {0,0}; - this->add_component<Transform>(pos, 0, 0); -} 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/Sprite.cpp b/src/crepe/Sprite.cpp deleted file mode 100644 index 7f5bca7..0000000 --- a/src/crepe/Sprite.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include <string> - -#include "Sprite.h" - -using namespace crepe; -using namespace std; - -Sprite::Sprite(uint32_t gameObjectId, string path) : Component(gameObjectId), path(path) {} diff --git a/src/crepe/Sprite.h b/src/crepe/Sprite.h deleted file mode 100644 index 8c517ed..0000000 --- a/src/crepe/Sprite.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include <string> - -#include "Component.h" - -namespace crepe { - -class Sprite : public Component { -public: - Sprite(uint32_t gameObjectId,std::string path); - - std::string path; -}; - -} // 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 b512d27..b246cc9 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,6 +1,8 @@ +#include <memory> + #include "AudioSource.h" -#include "../Sound.h" +#include "Sound.h" #include <memory> using namespace crepe::api; diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 2d26cda..7980212 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -2,8 +2,8 @@ #include <memory> -#include "Asset.h" -#include "Component.h" +#include "../Asset.h" +#include "../Component.h" namespace crepe { class Sound; diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index 063d225..74788ec 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -1,7 +1,6 @@ #include "../util/log.h" #include "BehaviorScript.h" -#include "Script.h" using namespace crepe::api; diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6ce6798..25d3cc5 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -2,7 +2,7 @@ #include <memory> -#include "Component.h" +#include "../Component.h" #include "Script.h" namespace crepe { diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 6b337be..37c194d 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -2,11 +2,30 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp Script.cpp + Color.cpp + Texture.cpp + Sprite.cpp + Transform.cpp + AssetManager.cpp + GameObject.cpp + Collider.cpp + Rigidbody.cpp + Sprite.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES # AudioSource.h BehaviorScript.h Script.h + GameObject.h + GameObject.hpp + Collider.h + Rigidbody.h + Sprite.h + Point.h + Transform.h + Color.h + Sprite.h + Texture.h + AssetManager.h ) - diff --git a/src/crepe/Collider.cpp b/src/crepe/api/Collider.cpp index 311ffb3..c3e4929 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/api/Collider.cpp @@ -1,5 +1,5 @@ #include "Collider.h" -using namespace crepe; +using namespace crepe::api; Collider::Collider(uint32_t gameObjectId) : Component(gameObjectId){} diff --git a/src/crepe/Collider.h b/src/crepe/api/Collider.h index cfc044c..1d540de 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/api/Collider.h @@ -1,8 +1,8 @@ #pragma once -#include "Component.h" +#include "../Component.h" -namespace crepe { +namespace crepe::api { class Collider : public Component { public: @@ -11,4 +11,4 @@ public: int size; }; -} // namespace crepe +} // namespace crepe::api 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/GameObject.cpp b/src/crepe/api/GameObject.cpp new file mode 100644 index 0000000..b167187 --- /dev/null +++ b/src/crepe/api/GameObject.cpp @@ -0,0 +1,7 @@ +#include "GameObject.h" + +using namespace crepe::api; +using namespace std; + +GameObject::GameObject(uint32_t id, string name, string tag, int layer) + : id(id), name(name), tag(tag), active(true), layer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/api/GameObject.h index b5d6399..57508c5 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,7 +3,7 @@ #include <cstdint> #include <string> -namespace crepe { +namespace crepe::api { class GameObject { public: @@ -19,6 +19,6 @@ public: int layer; }; -} // namespace crepe +} // namespace crepe::api #include "GameObject.hpp" diff --git a/src/crepe/GameObject.hpp b/src/crepe/api/GameObject.hpp index 8cd1abe..8295ea3 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/api/GameObject.hpp @@ -1,10 +1,10 @@ #pragma once -#include "GameObject.h" +#include "../ComponentManager.h" -#include "ComponentManager.h" +#include "GameObject.h" -namespace crepe { +namespace crepe::api { template <typename T, typename... Args> T & GameObject::add_component(Args &&... args) { @@ -12,4 +12,4 @@ T & GameObject::add_component(Args &&... args) { return mgr.add_component<T>(id, std::forward<Args>(args)...); } -} // namespace crepe +} // 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/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index a610e55..30a2cff 100644 --- a/src/crepe/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -1,6 +1,6 @@ #include "Rigidbody.h" -using namespace crepe; +using namespace crepe::api; Rigidbody::Rigidbody(uint32_t gameObjectId,int mass, int gravityScale, BodyType bodyType) : Component(gameObjectId), mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} diff --git a/src/crepe/Rigidbody.h b/src/crepe/api/Rigidbody.h index a03346f..548650a 100644 --- a/src/crepe/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -21,4 +21,4 @@ public: BodyType body_type; }; -} // namespace crepe +} // namespace crepe::api diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp new file mode 100644 index 0000000..d9e26ab --- /dev/null +++ b/src/crepe/api/Sprite.cpp @@ -0,0 +1,18 @@ + + +#include "Sprite.h" +#include "api/Texture.h" +#include "util/log.h" +#include <memory> +#include <utility> + +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() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h new file mode 100644 index 0000000..920f91e --- /dev/null +++ b/src/crepe/api/Sprite.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Component.h" +#include "api/Color.h" +#include "api/Texture.h" +#include <SDL2/SDL_rect.h> +#include <cstdint> +#include <memory> + + +namespace crepe::api { + +struct flip_settings{ + bool flip_x: 1; + bool flip_y : 1; +}; +class Sprite : public Component { + +public: + 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; + +}; + +} 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 new file mode 100644 index 0000000..c83461f --- /dev/null +++ b/src/crepe/api/Transform.cpp @@ -0,0 +1,13 @@ + + +#include "Transform.h" +#include "api/Point.h" +#include "util/log.h" + +using namespace crepe::api; + +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 new file mode 100644 index 0000000..a34ebb1 --- /dev/null +++ b/src/crepe/api/Transform.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Component.h" +#include "api/Point.h" +namespace crepe::api { + +class Transform : public Component { +public: + Transform(Point&, double, double); + ~Transform(); + Point position; // Translation (shift) + double rotation; // Rotation, in radians + double scale; // Multiplication factoh +}; +} // namespace crepe::api diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 256d87e..368aa2e 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -1,3 +1,6 @@ +# all examples +add_custom_target(examples) + # add_example(target_name [SOURCES...]) function(add_example target_name) # if SOURCES is not specified @@ -10,12 +13,15 @@ function(add_example target_name) add_executable(${target_name} EXCLUDE_FROM_ALL ${sources}) target_link_libraries(${target_name} PUBLIC crepe) + add_dependencies(examples ${target_name}) endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) +add_example(rendering) +add_example(asset_manager) +add_example(particle) +add_example(Physics) add_example(particle) add_example(Physics) -target_link_libraries(particle PUBLIC SDL2) -target_link_libraries(Physics PUBLIC SDL2) diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp new file mode 100644 index 0000000..9b41c2f --- /dev/null +++ b/src/example/asset_manager.cpp @@ -0,0 +1,35 @@ + + +#include <crepe/Sound.h> +#include <crepe/api/AssetManager.h> +#include <crepe/api/Texture.h> + +using namespace crepe; +using namespace crepe::api; +int main() { + + // this needs to be called before the asset manager otherwise the destructor of sdl is not in the right order + { + Texture test("../asset/texture/img.png"); + } + + auto & mgr = AssetManager::get_instance(); + + { + auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); + + auto img = mgr.cache<Texture>("../asset/texture/img.png"); + auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); + } + + { + auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); + + auto img = mgr.cache<Texture>("../asset/texture/img.png"); + auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); + } +} diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 3ba5b5a..ae6c765 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -6,14 +6,17 @@ #include <cassert> #include <chrono> -#include <crepe/Collider.h> #include <crepe/Component.h> #include <crepe/ComponentManager.h> -#include <crepe/GameObject.h> -#include <crepe/Rigidbody.h> -#include <crepe/Sprite.h> + +#include <crepe/api/Collider.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> + #include <crepe/util/log.h> +using namespace crepe::api; using namespace crepe; using namespace std; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp new file mode 100644 index 0000000..34d9f66 --- /dev/null +++ b/src/example/rendering.cpp @@ -0,0 +1,74 @@ + + +#include <crepe/ComponentManager.h> +#include <crepe/GameObject.h> +#include <crepe/RenderSystem.h> +#include <crepe/util/log.h> + +#include <crepe/api/Color.h> +#include <crepe/api/Point.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Texture.h> +#include <crepe/api/Transform.h> +#include <crepe/api/AssetManager.h> + +#include <chrono> +#include <memory> + +using namespace std; +using namespace crepe; +using namespace crepe::api; + +int main() { + + dbg_trace(); + + auto obj = GameObject(0, "name", "tag", 0); + auto obj1= GameObject(0, "name", "tag", 0); + auto obj2 = GameObject(0, "name", "tag", 0); + + auto& mgr = AssetManager::get_instance(); + // Normal adding components + { + Color color(0, 0, 0, 0); + Point point = { + .x = 0, + .y = 0, + }; + obj.add_component<Transform>(point, 1, 1); + obj.add_component<Sprite>( + make_shared<Texture>("../asset/texture/img.png"), color, + flip_settings{true, true}); + } + { + Color color(0, 0, 0, 0); + Point point = { + .x = 500, + .y = 0, + }; + obj.add_component<Transform>(point, 0, 0.1); + auto img = mgr.cache<Texture>("../asset/texture/second.png"); + obj.add_component<Sprite>(img, color, + flip_settings{true, true}); + } + { + Color color(0, 0, 0, 0); + Point point = { + .x = 800, + .y = 0, + }; + //obj.add_component<Transform>(point, 0, 0.1); + auto img = mgr.cache<Texture>("../asset/texture/second.png"); + obj.add_component<Sprite>(img, color, + flip_settings{true, true}); + } + + + + + auto & sys = crepe::RenderSystem::get_instance(); + auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { + sys.update(); + } +} diff --git a/src/example/script.cpp b/src/example/script.cpp index cc585db..6e5563c 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -4,11 +4,11 @@ */ #include <crepe/ComponentManager.h> -#include <crepe/GameObject.h> #include <crepe/ScriptSystem.h> #include <crepe/util/log.h> #include <crepe/api/BehaviorScript.h> +#include <crepe/api/GameObject.h> #include <crepe/api/Script.h> using namespace crepe; diff --git a/src/readme.md b/src/readme.md index a4a71e7..a8ffc51 100644 --- a/src/readme.md +++ b/src/readme.md @@ -18,7 +18,8 @@ running the build command: $ ninja -C build test_main ``` -Each source file in the example/ folder corresponds to a CMake target as well: +Each source file in the example/ folder corresponds to a CMake target as well +(all examples can be built at once by specifying the `examples` target): ``` $ ninja -C build audio_internal components_internal |