From 67d36df9711844a432a4c744c620f0d4d8c886d9 Mon Sep 17 00:00:00 2001 From: shweet Date: Fri, 15 Aug 2025 13:52:46 -0400 Subject: [PATCH] Canvas size fix --- src/canvas.cpp | 21 +++++++++------------ src/canvas.h | 6 +++--- src/imgui.cpp | 21 ++++++++++++--------- src/preview.cpp | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/canvas.cpp b/src/canvas.cpp index 1eb6116..998cbf7 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -7,11 +7,12 @@ static void _canvas_texture_free(Canvas* self) if (self->texture != 0) glDeleteTextures(1, &self->texture); } -static void _canvas_texture_init(Canvas* self, const vec2& size) +static void _canvas_texture_init(Canvas* self, const ivec2& size) { _canvas_texture_free(self); self->size = size; + self->previousSize = size; glGenFramebuffers(1, &self->fbo); @@ -19,7 +20,7 @@ static void _canvas_texture_init(Canvas* self, const vec2& size) glGenTextures(1, &self->texture); glBindTexture(GL_TEXTURE_2D, self->texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (s32)self->size.x, (s32)self->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self->size.x, self->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -27,13 +28,13 @@ static void _canvas_texture_init(Canvas* self, const vec2& size) glGenRenderbuffers(1, &self->rbo); glBindRenderbuffer(GL_RENDERBUFFER, self->rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, (s32)self->size.x, (s32)self->size.y); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, self->size.x, self->size.y); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, self->rbo); glBindFramebuffer(GL_FRAMEBUFFER, 0); } -void canvas_init(Canvas* self, const vec2& size) +void canvas_init(Canvas* self, const ivec2& size) { // Axis glGenVertexArrays(1, &self->axisVAO); @@ -109,8 +110,9 @@ void canvas_init(Canvas* self, const vec2& size) mat4 canvas_transform_get(Canvas* self, vec2 pan, f32 zoom, OriginType origin) { f32 zoomFactor = PERCENT_TO_UNIT(zoom); - mat4 projection = glm::ortho(0.0f, self->size.x, 0.0f, self->size.y, -1.0f, 1.0f); + mat4 projection = glm::ortho(0.0f, (f32)self->size.x, 0.0f, (f32)self->size.y, -1.0f, 1.0f); mat4 view = mat4{1.0f}; + vec2 size = vec2(self->size.x, self->size.y); switch (origin) { @@ -118,7 +120,7 @@ mat4 canvas_transform_get(Canvas* self, vec2 pan, f32 zoom, OriginType origin) view = glm::translate(view, vec3(pan, 0.0f)); break; default: - view = glm::translate(view, vec3((self->size * 0.5f) + pan, 0.0f)); + view = glm::translate(view, vec3((size * 0.5f) + pan, 0.0f)); break; } @@ -140,13 +142,8 @@ void canvas_viewport_set(Canvas* self) void canvas_texture_set(Canvas* self) { - static vec2 previousSize = {-1, -1}; - - if (previousSize != self->size) - { + if (self->previousSize != self->size) _canvas_texture_init(self, self->size); - previousSize = self->size; - } } void canvas_grid_draw(Canvas* self, GLuint& shader, mat4& transform, ivec2& size, ivec2& offset, vec4& color) diff --git a/src/canvas.h b/src/canvas.h index 73ab316..5221cd5 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -12,7 +12,6 @@ #define CANVAS_GRID_DEFAULT 32 #define CANVAS_LINE_LENGTH (FLT_MAX * 0.001f) -static const vec2 CANVAS_GRID_SIZE = {3200, 1600}; static const vec2 CANVAS_PIVOT_SIZE = {8, 8}; static const vec2 CANVAS_SCALE_DEFAULT = {1.0f, 1.0f}; @@ -45,10 +44,11 @@ struct Canvas GLuint textureEBO{}; GLuint textureVAO{}; GLuint textureVBO{}; - vec2 size{}; + ivec2 size{}; + ivec2 previousSize{}; }; -void canvas_init(Canvas* self, const vec2& size); +void canvas_init(Canvas* self, const ivec2& size); mat4 canvas_transform_get(Canvas* self, vec2 pan, f32 zoom, OriginType origin); void canvas_clear(vec4& color); void canvas_bind(Canvas* self); diff --git a/src/imgui.cpp b/src/imgui.cpp index 2993238..554f78d 100644 --- a/src/imgui.cpp +++ b/src/imgui.cpp @@ -2069,14 +2069,15 @@ static void _imgui_animation_preview(Imgui* self) static s32& tool = self->settings->tool; static f32& zoom = self->settings->previewZoom; static vec2& pan = self->settings->previewPan; - static vec2& size = self->preview->canvas.size; + static ivec2& size = self->preview->canvas.size; static vec2 mousePos{}; static vec2 previewPos{}; static ImVec2 previewScreenPos{}; std::string mousePositionString = std::format(IMGUI_POSITION_FORMAT, (s32)mousePos.x, (s32)mousePos.y); - _imgui_begin(IMGUI_ANIMATION_PREVIEW, self); + IMGUI_BEGIN_OR_RETURN(IMGUI_ANIMATION_PREVIEW, self); + _imgui_begin_child(IMGUI_CANVAS_GRID_CHILD, self); _imgui_checkbox(IMGUI_CANVAS_GRID, self, self->settings->previewIsGrid); ImGui::SameLine(); @@ -2140,9 +2141,10 @@ static void _imgui_animation_preview(Imgui* self) previewPos = vec2(ImGui::GetCursorPos()); previewScreenPos = vec2(ImGui::GetCursorScreenPos()); - size = ImGui::GetContentRegionAvail(); + vec2 imageSize = ImGui::GetContentRegionAvail(); + size = ivec2(imageSize); preview_draw(self->preview); - ImGui::Image(self->preview->canvas.texture, size); + ImGui::Image(self->preview->canvas.texture, imageSize); if (self->settings->previewIsTriggers) { @@ -2169,7 +2171,7 @@ static void _imgui_animation_preview(Imgui* self) _imgui_end(); // IMGUI_ANIMATION_PREVIEW - mousePos = (vec2((ImGui::GetMousePos()) - (ImGui::GetWindowPos() + previewPos)) - (size * 0.5f) - pan) / PERCENT_TO_UNIT(zoom); + mousePos = (vec2((ImGui::GetMousePos()) - (ImGui::GetWindowPos() + previewPos)) - (imageSize * 0.5f) - pan) / PERCENT_TO_UNIT(zoom); const bool isLeft = ImGui::IsKeyPressed(IMGUI_INPUT_LEFT); const bool isRight = ImGui::IsKeyPressed(IMGUI_INPUT_RIGHT); @@ -2255,11 +2257,11 @@ static void _imgui_spritesheet_editor(Imgui* self) static ivec2& gridOffset = self->settings->editorGridOffset; static vec2& pan = self->settings->editorPan; static f32& zoom = self->settings->editorZoom; - static vec2& size = self->editor->canvas.size; + static ivec2& size = self->editor->canvas.size; std::string mousePositionString = std::format(IMGUI_POSITION_FORMAT, (s32)mousePos.x, (s32)mousePos.y); - _imgui_begin(IMGUI_SPRITESHEET_EDITOR, self); + IMGUI_BEGIN_OR_RETURN(IMGUI_SPRITESHEET_EDITOR, self); _imgui_begin_child(IMGUI_CANVAS_GRID_CHILD, self); _imgui_checkbox(IMGUI_CANVAS_GRID, self, self->settings->editorIsGrid); @@ -2287,9 +2289,10 @@ static void _imgui_spritesheet_editor(Imgui* self) _imgui_end_child(); // IMGUI_CANVAS_VISUAL_CHILD ImVec2 editorPos = ImGui::GetCursorPos(); - size = ImGui::GetContentRegionAvail(); + vec2 imageSize = ImGui::GetContentRegionAvail(); + size = ivec2(imageSize); editor_draw(self->editor); - ImGui::Image(self->editor->canvas.texture, size); + ImGui::Image(self->editor->canvas.texture, imageSize); if (ImGui::IsItemHovered()) self->pendingCursor = TOOL_CURSORS[tool]; diff --git a/src/preview.cpp b/src/preview.cpp index 47e2dfc..086177e 100644 --- a/src/preview.cpp +++ b/src/preview.cpp @@ -29,7 +29,7 @@ void preview_tick(Preview* self) { if (self->isRender) { - vec2& size = self->canvas.size; + ivec2& size = self->canvas.size; u32 framebufferPixelCount = size.x * size.y * TEXTURE_CHANNELS; std::vector framebufferPixels(framebufferPixelCount); Texture frameTexture;