diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-12 19:48:18 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-12 19:48:18 +0100 |
commit | 8cb43ae9ce445c757274874934c30f815b3b6108 (patch) | |
tree | 47e2d09223995b122cab3be6af0a5d6dd09dfbbd /src/crepe | |
parent | f7b4866811c63ae24c366d9452e53d24e504336f (diff) | |
parent | fd403d038b017ec8976023471073329896035e36 (diff) |
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/gameloop
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Config.h | 2 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.h | 1 | ||||
-rw-r--r-- | src/crepe/api/Rigidbody.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 39 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 23 | ||||
-rw-r--r-- | src/crepe/system/AISystem.cpp | 10 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 131 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 23 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 11 |
9 files changed, 123 insertions, 121 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 6472270..ca2d3f1 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -53,7 +53,7 @@ struct Config final { * * Gravity value of game. */ - double gravity = 1; + float gravity = 10; } physics; //! default window settings diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 2915315..40e6b38 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -12,7 +12,6 @@ #include "../manager/SceneManager.h" #include "../system/System.h" - namespace crepe { /** * \brief Main game loop manager diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 40c6bf1..b08c8db 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -53,7 +53,7 @@ public: */ struct Data { //! objects mass - float mass = 0.0; + float mass = 1; /** * \brief Gravity scale factor. * @@ -79,7 +79,7 @@ public: //! Linear velocity of the object (speed and direction). vec2 linear_velocity; //! Maximum linear velocity of the object. This limits the object's speed. - vec2 max_linear_velocity = {INFINITY, INFINITY}; + float max_linear_velocity = INFINITY; //! Linear velocity coefficient. This scales the object's velocity for adjustment or damping. vec2 linear_velocity_coefficient = {1, 1}; //! \} diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d71a9c8..20bb030 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -2,6 +2,7 @@ #include <SDL2/SDL_blendmode.h> #include <SDL2/SDL_image.h> #include <SDL2/SDL_keycode.h> +#include <SDL2/SDL_pixels.h> #include <SDL2/SDL_rect.h> #include <SDL2/SDL_render.h> #include <SDL2/SDL_surface.h> @@ -232,15 +233,12 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { if (data.size.y == 0 && data.size.x != 0) { size.y = data.size.x / aspect_ratio; } + size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset; - const CameraValues & cam = ctx.cam; - - size *= cam.render_scale * ctx.img_scale * data.scale_offset; - - vec2 screen_pos - = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2) - * cam.render_scale - - size / 2 + cam.bar_size; + vec2 screen_pos = (ctx.pos + data.position_offset - cam_aux_data.cam_pos + + (cam_aux_data.zoomed_viewport) / 2) + * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; return SDL_FRect{ .x = screen_pos.x, @@ -269,7 +267,6 @@ void SDLContext::draw(const RenderContext & ctx) { SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, .texture = ctx.texture, - .cam = ctx.cam, .pos = ctx.pos, .img_scale = ctx.scale, }); @@ -281,10 +278,9 @@ void SDLContext::draw(const RenderContext & ctx) { angle, NULL, render_flip); } -SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { +void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { const Camera::Data & cam_data = cam.data; - CameraValues ret_cam; // resize window int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); @@ -292,9 +288,10 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } - vec2 & zoomed_viewport = ret_cam.zoomed_viewport; - vec2 & bar_size = ret_cam.bar_size; - vec2 & render_scale = ret_cam.render_scale; + vec2 & zoomed_viewport = this->cam_aux_data.zoomed_viewport; + vec2 & bar_size = this->cam_aux_data.bar_size; + vec2 & render_scale = this->cam_aux_data.render_scale; + this->cam_aux_data.cam_pos = new_pos; zoomed_viewport = cam.viewport_size * cam_data.zoom; float screen_aspect = static_cast<float>(cam.screen.x) / cam.screen.y; @@ -336,8 +333,6 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); - - return ret_cam; } std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> @@ -373,7 +368,11 @@ ivec2 SDLContext::get_size(const Texture & ctx) { std::vector<SDLContext::EventData> SDLContext::get_events() { std::vector<SDLContext::EventData> event_list; SDL_Event event; + const CameraAuxiliaryData & cam = this->cam_aux_data; while (SDL_PollEvent(&event)) { + ivec2 mouse_pos; + mouse_pos.x = (event.button.x - cam.bar_size.x) / cam.render_scale.x; + mouse_pos.y = (event.button.y - cam.bar_size.y) / cam.render_scale.y; switch (event.type) { case SDL_QUIT: event_list.push_back(EventData{ @@ -397,7 +396,7 @@ std::vector<SDLContext::EventData> SDLContext::get_events() { event_list.push_back(EventData{ .event_type = SDLContext::EventType::MOUSEDOWN, .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {event.button.x, event.button.y}, + .mouse_position = mouse_pos, }); break; case SDL_MOUSEBUTTONUP: { @@ -406,21 +405,21 @@ std::vector<SDLContext::EventData> SDLContext::get_events() { event_list.push_back(EventData{ .event_type = SDLContext::EventType::MOUSEUP, .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {event.button.x, event.button.y}, + .mouse_position = mouse_pos, }); } break; case SDL_MOUSEMOTION: { event_list.push_back( EventData{.event_type = SDLContext::EventType::MOUSEMOVE, - .mouse_position = {event.motion.x, event.motion.y}, + .mouse_position = mouse_pos, .rel_mouse_move = {event.motion.xrel, event.motion.yrel}}); } break; case SDL_MOUSEWHEEL: { event_list.push_back(EventData{ .event_type = SDLContext::EventType::MOUSEWHEEL, - .mouse_position = {event.motion.x, event.motion.y}, + .mouse_position = mouse_pos, // TODO: why is this needed? .scroll_direction = event.wheel.y < 0 ? -1 : 1, .scroll_delta = event.wheel.preciseY, diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 46b779f..bcadf87 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -24,7 +24,6 @@ class Texture; class Mediator; /** - * \class SDLContext * \brief Facade for the SDL library * * SDLContext is a singleton that handles the SDL window and renderer, provides methods for @@ -33,7 +32,7 @@ class Mediator; class SDLContext { public: //! data that the camera component cannot hold - struct CameraValues { + struct CameraAuxiliaryData { //! zoomed in viewport in game_units vec2 zoomed_viewport; @@ -64,7 +63,6 @@ public: struct RenderContext { const Sprite & sprite; const Texture & texture; - const CameraValues & cam; const vec2 & pos; const double & angle; const double & scale; @@ -193,18 +191,20 @@ public: void present_screen(); /** - * \brief sets the background of the camera (will be adjusted in future PR) - * \param camera Reference to the Camera object. - * \return camera data the component cannot store + * \brief calculates camera view settings. such as black_bars, zoomed_viewport, scaling and + * adjusting window size. + * + * \note only supports windowed mode. + * \param camera Reference to the current Camera object in the scene. + * \param new_pos new camera position from transform and offset */ - CameraValues set_camera(const Camera & camera); + void update_camera_view(const Camera & camera, const vec2 & new_pos); public: //! the data needed to construct a sdl dst rectangle struct DestinationRectangleData { const Sprite & sprite; const Texture & texture; - const CameraValues & cam; const vec2 & pos; const double & img_scale; }; @@ -233,6 +233,13 @@ private: //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; + + /** + * \cam_aux_data extra data that the component cannot hold. + * + * - this is defined in this class because get_events() needs this information aswell + */ + CameraAuxiliaryData cam_aux_data; }; } // namespace crepe diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index d231c7c..680dbb8 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -13,12 +13,10 @@ using namespace std::chrono; void AISystem::update() { const Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; - LoopTimerManager & timer = mediator.loop_timer; - RefVector<AI> ai_components = mgr.get_components_by_type<AI>(); LoopTimerManager & loop_timer = mediator.loop_timer; + RefVector<AI> ai_components = mgr.get_components_by_type<AI>(); - //TODO: Use fixed loop dt (this is not available at master at the moment) - duration_t dt = loop_timer.get_delta_time(); + float dt = loop_timer.get_scaled_fixed_delta_time().count(); // Loop through all AI components for (AI & ai : ai_components) { @@ -45,7 +43,7 @@ void AISystem::update() { // Calculate the acceleration (using the above calculated force) vec2 acceleration = force / rigidbody.data.mass; // Finally, update Rigidbody's velocity - rigidbody.data.linear_velocity += acceleration * duration_cast<seconds>(dt).count(); + rigidbody.data.linear_velocity += acceleration * dt; } } @@ -146,7 +144,7 @@ vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody, } float speed = distance / ai.arrive_deceleration; - speed = std::min(speed, rigidbody.data.max_linear_velocity.length()); + speed = std::min(speed, rigidbody.data.max_linear_velocity); vec2 desired_velocity = to_target * (speed / distance); return desired_velocity - rigidbody.data.linear_velocity; diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index ebf4439..3b3b8ab 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -5,88 +5,95 @@ #include "../api/Transform.h" #include "../api/Vector2.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" +#include "../manager/Mediator.h" #include "PhysicsSystem.h" using namespace crepe; void PhysicsSystem::update() { - ComponentManager & mgr = this->mediator.component_manager; + + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + LoopTimerManager & loop_timer = mediator.loop_timer; RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>(); - RefVector<Transform> transforms = mgr.get_components_by_type<Transform>(); + float dt = loop_timer.get_scaled_fixed_delta_time().count(); - double gravity = Config::get_instance().physics.gravity; + float gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { if (!rigidbody.active) continue; + Transform & transform + = mgr.get_components_by_id<Transform>(rigidbody.game_object_id).front().get(); switch (rigidbody.data.body_type) { case Rigidbody::BodyType::DYNAMIC: - for (Transform & transform : transforms) { - if (transform.game_object_id == rigidbody.game_object_id) { + if (transform.game_object_id == rigidbody.game_object_id) { + // Add gravity - // Add gravity - if (rigidbody.data.gravity_scale > 0) { - rigidbody.data.linear_velocity.y - += (rigidbody.data.mass * rigidbody.data.gravity_scale - * gravity); - } - // Add damping - if (rigidbody.data.angular_velocity_coefficient > 0) { - rigidbody.data.angular_velocity - *= rigidbody.data.angular_velocity_coefficient; - } - if (rigidbody.data.linear_velocity_coefficient.x > 0 - && rigidbody.data.linear_velocity_coefficient.y > 0) { - rigidbody.data.linear_velocity - *= rigidbody.data.linear_velocity_coefficient; - } + if (rigidbody.data.mass <= 0) { + throw std::runtime_error("Mass must be greater than 0"); + } - // Max velocity check - if (rigidbody.data.angular_velocity - > rigidbody.data.max_angular_velocity) { - rigidbody.data.angular_velocity - = rigidbody.data.max_angular_velocity; - } else if (rigidbody.data.angular_velocity - < -rigidbody.data.max_angular_velocity) { - rigidbody.data.angular_velocity - = -rigidbody.data.max_angular_velocity; - } + if (gravity <= 0) { + throw std::runtime_error("Config Gravity must be greater than 0"); + } - if (rigidbody.data.linear_velocity.x - > rigidbody.data.max_linear_velocity.x) { - rigidbody.data.linear_velocity.x - = rigidbody.data.max_linear_velocity.x; - } else if (rigidbody.data.linear_velocity.x - < -rigidbody.data.max_linear_velocity.x) { - rigidbody.data.linear_velocity.x - = -rigidbody.data.max_linear_velocity.x; - } + if (rigidbody.data.gravity_scale > 0 && !rigidbody.data.constraints.y) { + rigidbody.data.linear_velocity.y + += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity + * dt); + } + // Add coefficient rotation + if (rigidbody.data.angular_velocity_coefficient > 0) { + rigidbody.data.angular_velocity + *= std::pow(rigidbody.data.angular_velocity_coefficient, dt); + } - if (rigidbody.data.linear_velocity.y - > rigidbody.data.max_linear_velocity.y) { - rigidbody.data.linear_velocity.y - = rigidbody.data.max_linear_velocity.y; - } else if (rigidbody.data.linear_velocity.y - < -rigidbody.data.max_linear_velocity.y) { - rigidbody.data.linear_velocity.y - = -rigidbody.data.max_linear_velocity.y; - } + // Add coefficient movement horizontal + if (rigidbody.data.linear_velocity_coefficient.x > 0 + && !rigidbody.data.constraints.x) { + rigidbody.data.linear_velocity.x + *= std::pow(rigidbody.data.linear_velocity_coefficient.x, dt); + } - // Move object - if (!rigidbody.data.constraints.rotation) { - transform.rotation += rigidbody.data.angular_velocity; - transform.rotation = std::fmod(transform.rotation, 360.0); - if (transform.rotation < 0) { - transform.rotation += 360.0; - } - } - if (!rigidbody.data.constraints.x) { - transform.position.x += rigidbody.data.linear_velocity.x; - } - if (!rigidbody.data.constraints.y) { - transform.position.y += rigidbody.data.linear_velocity.y; + // Add coefficient movement horizontal + if (rigidbody.data.linear_velocity_coefficient.y > 0 + && !rigidbody.data.constraints.y) { + rigidbody.data.linear_velocity.y + *= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt); + } + + // Max velocity check + if (rigidbody.data.angular_velocity + > rigidbody.data.max_angular_velocity) { + rigidbody.data.angular_velocity = rigidbody.data.max_angular_velocity; + } else if (rigidbody.data.angular_velocity + < -rigidbody.data.max_angular_velocity) { + rigidbody.data.angular_velocity = -rigidbody.data.max_angular_velocity; + } + + // Set max velocity to maximum length + if (rigidbody.data.linear_velocity.length() + > rigidbody.data.max_linear_velocity) { + rigidbody.data.linear_velocity.normalize(); + rigidbody.data.linear_velocity *= rigidbody.data.max_linear_velocity; + } + + // Move object + if (!rigidbody.data.constraints.rotation) { + transform.rotation += rigidbody.data.angular_velocity * dt; + transform.rotation = std::fmod(transform.rotation, 360.0); + if (transform.rotation < 0) { + transform.rotation += 360.0; } } + if (!rigidbody.data.constraints.x) { + transform.position.x += rigidbody.data.linear_velocity.x * dt; + } + if (!rigidbody.data.constraints.y) { + transform.position.y += rigidbody.data.linear_velocity.y * dt; + } } break; case Rigidbody::BodyType::KINEMATIC: diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 51340fb..afd9548 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -15,6 +15,7 @@ #include "../manager/ResourceManager.h" #include "RenderSystem.h" +#include "types.h" using namespace crepe; using namespace std; @@ -29,7 +30,7 @@ void RenderSystem::present_screen() { ctx.present_screen(); } -SDLContext::CameraValues RenderSystem::update_camera() { +void RenderSystem::update_camera() { ComponentManager & mgr = this->mediator.component_manager; SDLContext & ctx = this->mediator.sdl_context; RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); @@ -40,9 +41,9 @@ SDLContext::CameraValues RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); - SDLContext::CameraValues cam_val = ctx.set_camera(cam); - cam_val.cam_pos = transform.position + cam.data.postion_offset; - return cam_val; + vec2 new_camera_pos = transform.position + cam.data.postion_offset; + ctx.update_camera_view(cam, new_camera_pos); + return; } throw std::runtime_error("No active cameras in current scene"); } @@ -69,8 +70,7 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, - const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { ComponentManager & mgr = this->mediator.component_manager; SDLContext & ctx = this->mediator.sdl_context; @@ -93,7 +93,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .texture = res, - .cam = cam, .pos = p.position, .angle = p.angle, .scale = scale, @@ -102,8 +101,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, - const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { SDLContext & ctx = this->mediator.sdl_context; ResourceManager & resource_manager = this->mediator.resource_manager; const Texture & res = resource_manager.get<Texture>(sprite.source); @@ -111,7 +109,6 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .texture = res, - .cam = cam, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, @@ -120,7 +117,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera void RenderSystem::render() { ComponentManager & mgr = this->mediator.component_manager; - const SDLContext::CameraValues & cam = this->update_camera(); + this->update_camera(); RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); RefVector<Sprite> sorted_sprites = this->sort(sprites); @@ -130,10 +127,10 @@ void RenderSystem::render() { const Transform & transform = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, cam, transform.scale); + bool rendered_particles = this->render_particle(sprite, transform.scale); if (rendered_particles) continue; - this->render_normal(sprite, cam, transform); + this->render_normal(sprite, transform); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index e270a6b..fc7b46e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -2,8 +2,6 @@ #include <cmath> -#include "facade/SDLContext.h" - #include "System.h" #include "types.h" @@ -14,7 +12,6 @@ class Sprite; class Transform; /** - * \class RenderSystem * \brief Manages rendering operations for all game objects. * * RenderSystem is responsible for rendering, clearing and presenting the screen, and @@ -37,7 +34,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - SDLContext::CameraValues update_camera(); + void update_camera(); //! Renders the whole screen void render(); @@ -52,8 +49,7 @@ private: * constructor is now protected i cannot make tmp inside * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, - const double & scale); + bool render_particle(const Sprite & sprite, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -61,8 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, - const Transform & tm); + void render_normal(const Sprite & sprite, const Transform & tm); /** * \brief sort a vector sprite objects with |