From 1623663c6e2e8f33f84b37b9f6bd968c8bc6c92d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 14 Dec 2024 12:00:54 +0100 Subject: WIP demo --- src/crepe/api/Camera.cpp | 4 +- src/crepe/api/Camera.h | 9 +- src/crepe/facade/SDLContext.cpp | 28 ++-- src/example/AITest.cpp | 90 ------------ src/example/CMakeLists.txt | 6 +- src/example/button.cpp | 54 ------- src/example/demo.cpp | 24 +++ src/example/game.cpp | 293 ------------------------------------- src/example/rendering_particle.cpp | 73 --------- src/example/replay.cpp | 84 ----------- 10 files changed, 42 insertions(+), 623 deletions(-) delete mode 100644 src/example/AITest.cpp delete mode 100644 src/example/button.cpp create mode 100644 src/example/demo.cpp delete mode 100644 src/example/game.cpp delete mode 100644 src/example/rendering_particle.cpp delete mode 100644 src/example/replay.cpp (limited to 'src') diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 19a3296..9befc3f 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -6,10 +6,8 @@ using namespace crepe; -Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, - const Data & data) +Camera::Camera(game_object_id_t id, const vec2 & viewport_size, const Data & data) : Component(id), - screen(screen), viewport_size(viewport_size), data(data) { dbg_trace(); diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 54d9a73..48f2ff2 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -30,7 +30,7 @@ public: * zoom < 1 --> zoom out * zoom > 1 --> zoom in */ - double zoom = 1; + float zoom = 1.0; //! offset postion from the game object transform component vec2 postion_offset; @@ -40,20 +40,15 @@ public: /** * \brief Constructs a Camera with the specified ID and background color. * \param id Unique identifier for the camera component. - * \param screen is the actual screen size in pixels * \param viewport_size is the view of the world in game units * \param data the camera component data */ - Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, - const Camera::Data & data); + Camera(game_object_id_t id, const vec2 & viewport_size, const Data & data); ~Camera(); // dbg_trace only public: Camera::Data data; - //! screen the display size in pixels ( output resolution ) - const ivec2 screen; - //! viewport is the area of the world visible through the camera (in world units) const vec2 viewport_size; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index fccc15f..18f40f9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -279,13 +279,13 @@ void SDLContext::draw(const RenderContext & ctx) { } void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { - const Camera::Data & cam_data = cam.data; + const Config & config = Config::get_instance(); // resize window int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); - if (w != cam.screen.x || h != cam.screen.y) { - SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); + if (w != config.window_settings.default_size.x || h != config.window_settings.default_size.y) { + SDL_SetWindowSize(this->game_window.get(), config.window_settings.default_size.x, config.window_settings.default_size.y); } vec2 & zoomed_viewport = this->cam_aux_data.zoomed_viewport; @@ -294,28 +294,28 @@ void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { this->cam_aux_data.cam_pos = new_pos; zoomed_viewport = cam.viewport_size * cam_data.zoom; - float screen_aspect = static_cast(cam.screen.x) / cam.screen.y; + float screen_aspect = static_cast(config.window_settings.default_size.x) / config.window_settings.default_size.y; float viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing - float scale = cam.screen.y / zoomed_viewport.y; + float scale = config.window_settings.default_size.y / zoomed_viewport.y; float adj_width = zoomed_viewport.x * scale; - float bar_width = (cam.screen.x - adj_width) / 2; - this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y}; - this->black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; + float bar_width = (config.window_settings.default_size.x - adj_width) / 2; + this->black_bars[0] = {0, 0, bar_width, (float) config.window_settings.default_size.y}; + this->black_bars[1] = {(config.window_settings.default_size.x - bar_width), 0, bar_width, (float) config.window_settings.default_size.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; } else { // letterboxing - float scale = cam.screen.x / (cam.viewport_size.x * cam_data.zoom); + float scale = config.window_settings.default_size.x / (cam.viewport_size.x * cam_data.zoom); float adj_height = cam.viewport_size.y * scale; - float bar_height = (cam.screen.y - adj_height) / 2; - this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; + float bar_height = (config.window_settings.default_size.y - adj_height) / 2; + this->black_bars[0] = {0, 0, (float) config.window_settings.default_size.x, bar_height}; this->black_bars[1] - = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; + = {0, (config.window_settings.default_size.y - bar_height), (float) config.window_settings.default_size.x, bar_height}; bar_size = {0, bar_height}; render_scale.x = render_scale.y = scale; @@ -327,8 +327,8 @@ void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { SDL_Rect bg = { .x = 0, .y = 0, - .w = cam.screen.x, - .h = cam.screen.y, + .w = config.window_settings.default_size.x, + .h = config.window_settings.default_size.y, }; // fill bg color diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp deleted file mode 100644 index 93ba500..0000000 --- a/src/example/AITest.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -class Script1 : public Script { - bool shutdown(const ShutDownEvent & event) { - // Very dirty way of shutting down the game - throw "ShutDownEvent"; - return true; - } - - bool mousemove(const MouseMoveEvent & event) { - /*RefVector aivec = this->get_components(); - AI & ai = aivec.front().get(); - ai.flee_target - = vec2{static_cast(event.mouse_x), static_cast(event.mouse_y)};*/ - return true; - } - - void init() { - subscribe( - [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); - subscribe( - [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); }); - } -}; - -class Scene1 : public Scene { -public: - void load_scene() override { - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - - GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1); - GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1); - - Asset img{"asset/texture/test_ap43.png"}; - - Sprite & test_sprite = game_object1.add_component( - img, Sprite::Data{ - .color = Color::MAGENTA, - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 2, - .order_in_layer = 2, - .size = {0, 100}, - .angle_offset = 0, - .position_offset = {0, 0}, - }); - - AI & ai = game_object1.add_component(3000); - // ai.arrive_on(); - // ai.flee_on(); - ai.path_follow_on(); - ai.make_oval_path(500, 1000, {0, -1000}, 1.5708, true); - ai.make_oval_path(1000, 500, {0, 500}, 4.7124, false); - game_object1.add_component(Rigidbody::Data{ - .mass = 0.1f, - .max_linear_velocity = 40, - }); - game_object1.add_component().set_script(); - - game_object2.add_component(ivec2{1080, 720}, vec2{5000, 5000}, - Camera::Data{ - .bg_color = Color::WHITE, - .zoom = 1, - }); - } - - string get_name() const override { return "Scene1"; } -}; - -int main() { - LoopManager engine; - engine.add_scene(); - engine.start(); - - return 0; -} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 1bc31d8..f35f38f 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,8 +16,4 @@ function(add_example target_name) add_dependencies(examples ${target_name}) endfunction() -add_example(rendering_particle) -add_example(game) -add_example(button) -add_example(replay) -add_example(AITest) +add_example(demo) diff --git a/src/example/button.cpp b/src/example/button.cpp deleted file mode 100644 index 00bdc28..0000000 --- a/src/example/button.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr; - RenderSystem sys{mgr}; - EventManager & event_mgr = EventManager::get_instance(); - InputSystem input_sys{mgr}; - AnimatorSystem asys{mgr}; - GameObject camera_obj = mgr.new_object("", "", vec2{1000, 1000}, 0, 1); - camera_obj.add_component(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); - - GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - auto s2 = Texture("asset/texture/test_ap43.png"); - bool button_clicked = false; - auto & sprite2 = button_obj.add_component( - s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100); - std::function on_click = [&]() { std::cout << "button clicked" << std::endl; }; - std::function on_enter = [&]() { std::cout << "enter" << std::endl; }; - std::function on_exit = [&]() { std::cout << "exit" << std::endl; }; - auto & button - = button_obj.add_component