diff options
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 14 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 12 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 2 | ||||
-rw-r--r-- | src/crepe/util/AbsoluutPosition.cpp | 20 | ||||
-rw-r--r-- | src/crepe/util/AbsoluutPosition.h | 14 | ||||
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 27 |
7 files changed, 70 insertions, 21 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f350250..d352adc 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -28,6 +28,7 @@ #include "SDLContext.h" #include "Texture.h" #include "types.h" +#include "util/AbsoluutPosition.h" using namespace crepe; using namespace std; @@ -148,8 +149,7 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset; - vec2 screen_pos = (ctx.pos + data.position_offset - cam_aux_data.cam_pos - + (cam_aux_data.zoomed_viewport) / 2) + vec2 screen_pos = (ctx.pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; @@ -195,7 +195,7 @@ void SDLContext::draw_text(const RenderText & data) { const Text & text = data.text; const Font & font = data.font; - const Transform & transform = data.transform; + vec2 absoluut_pos = AbsoluutPosition::get_position(data.transform, data.text.offset); std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>> font_surface; std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> font_texture; @@ -220,8 +220,8 @@ void SDLContext::draw_text(const RenderText & data) { font_texture = {tmp_font_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; - vec2 size = text.dimensions * cam_aux_data.render_scale * transform.scale; - vec2 screen_pos = (transform.position + text.offset - cam_aux_data.cam_pos + vec2 size = text.dimensions * cam_aux_data.render_scale * data.transform.scale; + vec2 screen_pos = (absoluut_pos + text.offset - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; @@ -233,8 +233,8 @@ void SDLContext::draw_text(const RenderText & data) { .h = size.y, }; - SDL_RenderCopyExF(this->game_renderer.get(), font_texture.get(), NULL, &dstrect, transform.rotation, NULL, - SDL_FLIP_NONE); + SDL_RenderCopyExF(this->game_renderer.get(), font_texture.get(), NULL, &dstrect, + data.transform.rotation, NULL, SDL_FLIP_NONE); } void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index bf2bfd3..ba09530 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -21,6 +21,7 @@ #include "RenderSystem.h" #include "types.h" +#include "util/AbsoluutPosition.h" using namespace crepe; using namespace std; @@ -99,8 +100,7 @@ void RenderSystem::render_text() { } } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { - +bool RenderSystem::render_particle(const Sprite & sprite, const Transform & tm){ ComponentManager & mgr = this->mediator.component_manager; SDLContext & ctx = this->mediator.sdl_context; ResourceManager & resource_manager = this->mediator.resource_manager; @@ -122,9 +122,9 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .texture = res, - .pos = p.position, + .pos = AbsoluutPosition::get_position(tm, em.data.offset + sprite.data.position_offset), .angle = p.angle, - .scale = scale, + .scale = tm.scale, }); } } @@ -138,7 +138,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .texture = res, - .pos = tm.position, + .pos = AbsoluutPosition::get_position(tm, sprite.data.position_offset), .angle = tm.rotation, .scale = tm.scale, }); @@ -158,7 +158,7 @@ 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, transform.scale); + bool rendered_particles = this->render_particle(sprite, transform); if (rendered_particles) continue; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index d5385eb..762b11f 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -53,7 +53,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 double & scale); + bool render_particle(const Sprite & sprite, const Transform & tm); /** * \brief renders a sprite with a Transform component on the screen * diff --git a/src/crepe/util/AbsoluutPosition.cpp b/src/crepe/util/AbsoluutPosition.cpp new file mode 100644 index 0000000..296cc09 --- /dev/null +++ b/src/crepe/util/AbsoluutPosition.cpp @@ -0,0 +1,20 @@ +#include "AbsoluutPosition.h" + +using namespace crepe; + +vec2 AbsoluutPosition::get_position(const Transform & transform, const vec2 & offset) { + // Get the rotation in radians + float radians1 = transform.rotation * (M_PI / 180.0); + + // Calculate total offset with scale + vec2 total_offset = offset * transform.scale; + + // Rotate + float rotated_total_offset_x1 + = total_offset.x * cos(radians1) - total_offset.y * sin(radians1); + float rotated_total_offset_y1 + = total_offset.x * sin(radians1) + total_offset.y * cos(radians1); + + // Final positions considering scaling and rotation + return (transform.position + vec2(rotated_total_offset_x1, rotated_total_offset_y1)); +} diff --git a/src/crepe/util/AbsoluutPosition.h b/src/crepe/util/AbsoluutPosition.h new file mode 100644 index 0000000..30a7f93 --- /dev/null +++ b/src/crepe/util/AbsoluutPosition.h @@ -0,0 +1,14 @@ +#pragma once + +#include "api/Transform.h" + +#include "types.h" + +namespace crepe { + +class AbsoluutPosition { +public: + static vec2 get_position(const Transform & transform, const vec2 & offset); +}; + +} // namespace crepe diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index 94ed906..b4b9221 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,6 +1,7 @@ target_sources(crepe PUBLIC LogColor.cpp Log.cpp + AbsoluutPosition.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -11,5 +12,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Proxy.hpp OptionalRef.h OptionalRef.hpp + AbsoluutPosition.h ) diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 4430d75..9cf25d7 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -23,11 +23,11 @@ using namespace std; class TestScene : public Scene { public: void load_scene() { - GameObject game_object = new_object("", "", vec2{0, 0}, 0, 1); + GameObject game_object = new_object("", "", vec2{0, 0}, 45, 1); Color color(255, 255, 255, 255); - Asset img{"asset/spritesheet/spritesheet_test.png"}; + Asset img{"asset/texture/square.png"}; Sprite & test_sprite = game_object.add_component<Sprite>( img, Sprite::Data{ @@ -35,17 +35,30 @@ public: .flip = Sprite::FlipSettings{false, false}, .sorting_in_layer = 2, .order_in_layer = 2, - .size = {0, 0}, + .size = {1, 1}, .angle_offset = 0, - .position_offset = {0, 0}, + .position_offset = {0, 1}, }); - auto & cam = game_object.add_component<Camera>(ivec2{1280, 720}, vec2{4000, 4000}, + Sprite & test_sprite1 + = game_object.add_component<Sprite>(img, Sprite::Data{ + .color = color, + .size = {1, 1}, + .position_offset = {0, -1}, + }); + + auto & cam = game_object.add_component<Camera>(ivec2{1280, 720}, vec2{5, 5}, Camera::Data{ .bg_color = Color::WHITE, }); - game_object.add_component<Text>(vec2{4000, 400}, vec2{0, 0}, "ComicSansMS", - Text::Data{.text_color = Color::RED}, "TEST test"); + + /* + game_object.add_component<Text>(vec2{1, 1}, vec2{0, -0.5}, "ComicSansMS", + Text::Data{.text_color = Color::RED}, "test TEST"); + + game_object.add_component<Text>(vec2{1, 1}, vec2{0, 0.5}, "ComicSansMS", + Text::Data{.text_color = Color::BLACK}, "TEST test"); + */ } string get_name() const { return "TestScene"; }; |