From a597b0059f30e9d56331c6ece34fdcabb4028616 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 9 Dec 2024 20:15:41 +0100 Subject: making rendering fonts and UI --- src/crepe/facade/SDLContext.cpp | 43 +++++++++++++++++++++++++++++++---------- src/crepe/facade/SDLContext.h | 10 +++++++++- 2 files changed, 42 insertions(+), 11 deletions(-) (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6becf60..297763d 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -62,6 +64,10 @@ SDLContext::SDLContext() { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } + + if (TTF_Init() != 0) { + throw runtime_error("SDLContext: TTF could not initialize!"); + } } SDLContext::~SDLContext() { @@ -73,6 +79,7 @@ SDLContext::~SDLContext() { // TODO: how are we going to ensure that these are called from the same // thread that SDL_Init() was called on? This has caused problems for me // before. + TTF_Quit(); IMG_Quit(); SDL_Quit(); } @@ -281,10 +288,9 @@ void SDLContext::draw(const RenderContext & ctx) { &dstrect, angle, NULL, render_flip); } -SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { +SDLContext::CameraValues & SDLContext::set_camera(const Camera & cam) { 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 +298,9 @@ 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->camera_val.zoomed_viewport; + vec2 & bar_size = this->camera_val.bar_size; + vec2 & render_scale = this->camera_val.render_scale; zoomed_viewport = cam.viewport_size * cam_data.zoom; float screen_aspect = static_cast(cam.screen.x) / cam.screen.y; @@ -337,7 +343,7 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); - return ret_cam; + return this->camera_val; } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } @@ -366,6 +372,19 @@ SDLContext::texture_from_path(const std::string & path) { return img_texture; } +std::unique_ptr> +SDLContext::font_from_path(const std::string & path) { + + TTF_Font * lib_font = TTF_OpenFont(path.c_str(), 72); + if (!lib_font) { + throw runtime_error(format("SDLContext: font cannot be load from {}", path)); + } + std::unique_ptr> font; + font = {lib_font, [](TTF_Font * f){}}; + + return font; +} + ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); @@ -377,7 +396,11 @@ void SDLContext::delay(int ms) const { SDL_Delay(ms); } std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; + const CameraValues & cam = this->camera_val; 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{ @@ -401,7 +424,7 @@ std::vector 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: { @@ -410,21 +433,21 @@ std::vector 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 e232511..b847c72 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -188,6 +189,9 @@ private: */ ivec2 get_size(const Texture & ctx); + std::unique_ptr> + font_from_path(const std::string & path); + private: //! Will use draw,clear_screen, present_screen, camera. friend class RenderSystem; @@ -198,6 +202,8 @@ private: */ void draw(const RenderContext & ctx); + void draw_text(const + //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -208,7 +214,7 @@ private: * \brief sets the background of the camera (will be adjusted in future PR) * \param camera Reference to the Camera object. */ - CameraValues set_camera(const Camera & camera); + CameraValues & set_camera(const Camera & camera); private: //! the data needed to construct a sdl dst rectangle @@ -254,6 +260,8 @@ private: //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; + + CameraValues camera_val; }; } // namespace crepe -- cgit v1.2.3 From 045c263f5bc432ff0758dd0928e4a0f5e5eed85d Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 11 Dec 2024 15:46:17 +0100 Subject: updated the code so that get_events() calculates pixel units to game units --- src/crepe/facade/SDLContext.cpp | 43 +++++++++------------------------------ src/crepe/facade/SDLContext.h | 27 ++++++++++++------------ src/crepe/system/RenderSystem.cpp | 23 +++++++++------------ src/crepe/system/RenderSystem.h | 10 +++------ 4 files changed, 37 insertions(+), 66 deletions(-) (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 297763d..8597f06 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -64,10 +63,6 @@ SDLContext::SDLContext() { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } - - if (TTF_Init() != 0) { - throw runtime_error("SDLContext: TTF could not initialize!"); - } } SDLContext::~SDLContext() { @@ -79,7 +74,6 @@ SDLContext::~SDLContext() { // TODO: how are we going to ensure that these are called from the same // thread that SDL_Init() was called on? This has caused problems for me // before. - TTF_Quit(); IMG_Quit(); SDL_Quit(); } @@ -249,14 +243,12 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { size.y = data.size.x / ctx.sprite.aspect_ratio; } - const CameraValues & cam = ctx.cam; - - size *= cam.render_scale * ctx.img_scale * data.scale_offset; + size *= cam_aux_data.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; + = (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, @@ -276,7 +268,6 @@ void SDLContext::draw(const RenderContext & ctx) { SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, - .cam = ctx.cam, .pos = ctx.pos, .img_scale = ctx.scale, }); @@ -288,7 +279,7 @@ void SDLContext::draw(const RenderContext & ctx) { &dstrect, 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; // resize window @@ -298,9 +289,10 @@ SDLContext::CameraValues & SDLContext::set_camera(const Camera & cam) { SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } - vec2 & zoomed_viewport = this->camera_val.zoomed_viewport; - vec2 & bar_size = this->camera_val.bar_size; - vec2 & render_scale = this->camera_val.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(cam.screen.x) / cam.screen.y; @@ -342,8 +334,6 @@ SDLContext::CameraValues & SDLContext::set_camera(const Camera & cam) { // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); - - return this->camera_val; } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } @@ -372,19 +362,6 @@ SDLContext::texture_from_path(const std::string & path) { return img_texture; } -std::unique_ptr> -SDLContext::font_from_path(const std::string & path) { - - TTF_Font * lib_font = TTF_OpenFont(path.c_str(), 72); - if (!lib_font) { - throw runtime_error(format("SDLContext: font cannot be load from {}", path)); - } - std::unique_ptr> font; - font = {lib_font, [](TTF_Font * f){}}; - - return font; -} - ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); @@ -396,7 +373,7 @@ void SDLContext::delay(int ms) const { SDL_Delay(ms); } std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; - const CameraValues & cam = this->camera_val; + 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; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index b847c72..6d50ab0 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -33,7 +32,7 @@ class InputSystem; class SDLContext { public: //! data that the camera component cannot hold - struct CameraValues { + struct CameraAuxiliaryData { //! zoomed in viewport in game_units vec2 zoomed_viewport; @@ -63,7 +62,6 @@ public: //! rendering data needed to render on screen struct RenderContext { const Sprite & sprite; - const CameraValues & cam; const vec2 & pos; const double & angle; const double & scale; @@ -189,9 +187,6 @@ private: */ ivec2 get_size(const Texture & ctx); - std::unique_ptr> - font_from_path(const std::string & path); - private: //! Will use draw,clear_screen, present_screen, camera. friend class RenderSystem; @@ -202,8 +197,6 @@ private: */ void draw(const RenderContext & ctx); - void draw_text(const - //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -211,16 +204,19 @@ private: void present_screen(); /** - * \brief sets the background of the camera (will be adjusted in future PR) - * \param camera Reference to the Camera object. + * \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); private: //! the data needed to construct a sdl dst rectangle struct DestinationRectangleData { const Sprite & sprite; - const CameraValues & cam; const vec2 & pos; const double & img_scale; }; @@ -261,7 +257,12 @@ private: //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; - CameraValues camera_val; + /** + * \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/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 74fed26..1ce9059 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -11,6 +11,7 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" +#include "types.h" #include "RenderSystem.h" @@ -27,7 +28,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 cameras = mgr.get_components_by_type(); @@ -38,9 +39,9 @@ SDLContext::CameraValues & RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id(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"); } @@ -67,8 +68,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; @@ -88,7 +88,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came ctx.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = cam, .pos = p.position, .angle = p.angle, .scale = scale, @@ -97,12 +96,10 @@ 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; ctx.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = cam, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, @@ -111,7 +108,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 sprites = mgr.get_components_by_type(); RefVector sorted_sprites = this->sort(sprites); @@ -121,10 +118,10 @@ void RenderSystem::render() { const Transform & transform = mgr.get_components_by_id(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 de26aa8..8e61072 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -2,8 +2,6 @@ #include -#include "facade/SDLContext.h" - #include "System.h" #include "types.h" @@ -37,7 +35,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 +50,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 +58,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 -- cgit v1.2.3 From 9b337ae01e4f3efc6ad3be5af33e3df8e9224d71 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 11 Dec 2024 15:47:49 +0100 Subject: make format --- src/crepe/facade/SDLContext.cpp | 10 +++++----- src/example/rendering_particle.cpp | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8597f06..7676cfd 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,8 +1,8 @@ #include -#include #include #include #include +#include #include #include #include @@ -245,10 +245,10 @@ 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) - * cam_aux_data.render_scale - - size / 2 + cam_aux_data.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, diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index b57ba04..48e02ba 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -71,21 +71,21 @@ public: anim.set_anim(2); anim.pause(); - auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, + auto & cam = game_object.add_component(ivec2{720, 1280}, vec2{400, 400}, Camera::Data{ .bg_color = Color::WHITE, }); - function on_click = [&](){ cout << "button clicked" << std::endl; }; - function on_enter = [&](){ cout << "enter" << std::endl; }; - function on_exit = [&](){ cout << "exit" << std::endl; }; + function on_click = [&]() { cout << "button clicked" << std::endl; }; + function on_enter = [&]() { cout << "enter" << std::endl; }; + function on_exit = [&]() { cout << "exit" << std::endl; }; - auto & button = game_object.add_component