replace context offset with Camera2D

This commit is contained in:
Luka Jankovic 2025-08-13 23:48:14 +02:00
parent 2026f8ac8d
commit af7d6dee00
11 changed files with 132 additions and 70 deletions

View file

@ -40,6 +40,7 @@ set(SOURCES
kubo_window.c kubo_window.c
kubo_context.c kubo_context.c
kubo_command.c kubo_command.c
kubo_camera.c
main.c main.c
) )

47
kubo_camera.c Normal file
View file

@ -0,0 +1,47 @@
/*
* Copyright Luka Jankovic 2025
*
* This file is part of Kubo.
*
* Kubo is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Kubo is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Kubo. If not, see <https://www.gnu.org/licenses/>.
*/
#include "kubo_camera.h"
void kubo_camera_zoom(Camera2D *camera) {
float wheel = GetMouseWheelMove();
if (wheel != 0) {
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), *camera);
camera->offset = GetMousePosition();
camera->target = mouseWorldPos;
float scale = 0.2f * wheel;
camera->zoom = Clamp(expf(logf(camera->zoom) + scale), 0.125f, 64.0f);
}
}
void kubo_camera_pan(Camera2D *camera) {
Vector2 delta = GetMouseDelta();
delta = Vector2Scale(delta, -1.0f / camera->zoom);
kubo_camera_shift(camera, delta);
}
void kubo_camera_shift(Camera2D *camera, Vector2 delta) {
camera->target = Vector2Add(camera->target, delta);
}
void kubo_camera_reset(Camera2D *camera) {
camera->target = (Vector2){.x = 0, .y = 0};
camera->offset = (Vector2){.x = 0, .y = 0};
camera->zoom = 1.0f;
}

30
kubo_camera.h Normal file
View file

@ -0,0 +1,30 @@
/*
* Copyright Luka Jankovic 2025
*
* This file is part of Kubo.
*
* Kubo is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Kubo is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Kubo. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef KUBO_CAMERA_H
#define KUBO_CAMERA_H
#include <raylib.h>
#include <raymath.h>
void kubo_camera_zoom(Camera2D *camera);
void kubo_camera_pan(Camera2D *camera);
void kubo_camera_shift(Camera2D *camera, Vector2 delta);
void kubo_camera_reset(Camera2D *camera);
#endif

View file

@ -38,9 +38,6 @@ void kubo_context_init(struct kubo_context *context) {
context->exit_pending = false; context->exit_pending = false;
context->state = kubo_context_states[KUBO_CONTEXT_NORMAL]; context->state = kubo_context_states[KUBO_CONTEXT_NORMAL];
context->offset_x = 0;
context->offset_y = 0;
} }
void kubo_context_cleanup(struct kubo_context *context) { void kubo_context_cleanup(struct kubo_context *context) {
@ -105,9 +102,6 @@ void kubo_context_delete_wall(struct kubo_context *context) {
void kubo_context_input_up(struct kubo_context *context) { void kubo_context_input_up(struct kubo_context *context) {
switch (context->state.id) { switch (context->state.id) {
case KUBO_CONTEXT_NORMAL:
context->offset_y -= KUBO_CONTEXT_OFFSET_JMP;
break;
case KUBO_CONTEXT_WALL_SELECT: case KUBO_CONTEXT_WALL_SELECT:
select_next_wall(context); select_next_wall(context);
break; break;
@ -118,9 +112,6 @@ void kubo_context_input_up(struct kubo_context *context) {
void kubo_context_input_down(struct kubo_context *context) { void kubo_context_input_down(struct kubo_context *context) {
switch (context->state.id) { switch (context->state.id) {
case KUBO_CONTEXT_NORMAL:
context->offset_y += KUBO_CONTEXT_OFFSET_JMP;
break;
case KUBO_CONTEXT_WALL_SELECT: case KUBO_CONTEXT_WALL_SELECT:
select_prev_wall(context); select_prev_wall(context);
break; break;
@ -131,9 +122,6 @@ void kubo_context_input_down(struct kubo_context *context) {
void kubo_context_input_left(struct kubo_context *context) { void kubo_context_input_left(struct kubo_context *context) {
switch (context->state.id) { switch (context->state.id) {
case KUBO_CONTEXT_NORMAL:
context->offset_x -= KUBO_CONTEXT_OFFSET_JMP;
break;
case KUBO_CONTEXT_WALL_SELECT: case KUBO_CONTEXT_WALL_SELECT:
select_prev_wall(context); select_prev_wall(context);
break; break;
@ -144,9 +132,6 @@ void kubo_context_input_left(struct kubo_context *context) {
void kubo_context_input_right(struct kubo_context *context) { void kubo_context_input_right(struct kubo_context *context) {
switch (context->state.id) { switch (context->state.id) {
case KUBO_CONTEXT_NORMAL:
context->offset_x += KUBO_CONTEXT_OFFSET_JMP;
break;
case KUBO_CONTEXT_WALL_SELECT: case KUBO_CONTEXT_WALL_SELECT:
select_next_wall(context); select_next_wall(context);
break; break;
@ -155,17 +140,6 @@ void kubo_context_input_right(struct kubo_context *context) {
} }
} }
void kubo_context_reset_offset(struct kubo_context *context) {
switch (context->state.id) {
case KUBO_CONTEXT_NORMAL:
context->offset_x = 0;
context->offset_y = 0;
break;
default:
break;
}
}
static void select_next_wall(struct kubo_context *context) { static void select_next_wall(struct kubo_context *context) {
if (!context->walls.count) { if (!context->walls.count) {
return; return;

View file

@ -51,9 +51,6 @@ struct kubo_context {
struct kubo_wall_arr walls; struct kubo_wall_arr walls;
struct kubo_context_state_data state; struct kubo_context_state_data state;
int offset_x;
int offset_y;
// KUBO_CONTEXT_WALL_SELECT // KUBO_CONTEXT_WALL_SELECT
size_t wall_select_index; size_t wall_select_index;
}; };
@ -74,6 +71,4 @@ void kubo_context_input_down(struct kubo_context *context);
void kubo_context_input_left(struct kubo_context *context); void kubo_context_input_left(struct kubo_context *context);
void kubo_context_input_right(struct kubo_context *context); void kubo_context_input_right(struct kubo_context *context);
void kubo_context_reset_offset(struct kubo_context *context);
#endif #endif

View file

@ -18,22 +18,22 @@
#include "kubo_input.h" #include "kubo_input.h"
static void key_input(struct kubo_context *context); static void key_input(struct kubo_context *context, Camera2D *camera);
static void char_input(struct kubo_context *context); static void char_input(struct kubo_context *context);
static void handle_cmd_input(struct kubo_context *context); static void handle_cmd_input(struct kubo_context *context);
void kubo_input_handle(struct kubo_context *context) { void kubo_input_handle(struct kubo_context *context, Camera2D *camera) {
char_input(context); char_input(context);
if (context->state.id == KUBO_CONTEXT_COMMAND) { if (context->state.id == KUBO_CONTEXT_COMMAND) {
handle_cmd_input(context); handle_cmd_input(context);
} }
key_input(context); key_input(context, camera);
} }
static void key_input(struct kubo_context *context) { static void key_input(struct kubo_context *context, Camera2D *camera) {
int key_code = GetKeyPressed(); int key_code = GetKeyPressed();
switch (key_code) { switch (key_code) {
@ -52,21 +52,37 @@ static void key_input(struct kubo_context *context) {
case KEY_RIGHT: case KEY_RIGHT:
case KEY_L: case KEY_L:
if (context->state.id == KUBO_CONTEXT_NORMAL) {
kubo_camera_shift(camera,
(Vector2){.x = KUBO_INPUT_CAMERA_SHIFT, .y = 0});
}
kubo_context_input_right(context); kubo_context_input_right(context);
break; break;
case KEY_UP: case KEY_UP:
case KEY_K: case KEY_K:
if (context->state.id == KUBO_CONTEXT_NORMAL) {
kubo_camera_shift(camera,
(Vector2){.x = 0, .y = KUBO_INPUT_CAMERA_SHIFT});
}
kubo_context_input_up(context); kubo_context_input_up(context);
break; break;
case KEY_LEFT: case KEY_LEFT:
case KEY_H: case KEY_H:
if (context->state.id == KUBO_CONTEXT_NORMAL) {
kubo_camera_shift(camera,
(Vector2){.x = -KUBO_INPUT_CAMERA_SHIFT, .y = 0});
}
kubo_context_input_left(context); kubo_context_input_left(context);
break; break;
case KEY_DOWN: case KEY_DOWN:
case KEY_J: case KEY_J:
if (context->state.id == KUBO_CONTEXT_NORMAL) {
kubo_camera_shift(camera,
(Vector2){.x = 0, .y = -KUBO_INPUT_CAMERA_SHIFT});
}
kubo_context_input_down(context); kubo_context_input_down(context);
break; break;
@ -75,7 +91,7 @@ static void key_input(struct kubo_context *context) {
break; break;
case KEY_SPACE: case KEY_SPACE:
kubo_context_reset_offset(context); kubo_camera_reset(camera);
break; break;
default: default:

View file

@ -19,9 +19,14 @@
#ifndef KUBO_INPUT_H #ifndef KUBO_INPUT_H
#define KUBO_INPUT_H #define KUBO_INPUT_H
#include "kubo_context.h" #include <raylib.h>
#include "kubo_command.h"
void kubo_input_handle(struct kubo_context *context); #include "kubo_camera.h"
#include "kubo_command.h"
#include "kubo_context.h"
#define KUBO_INPUT_CAMERA_SHIFT 50
void kubo_input_handle(struct kubo_context *context, Camera2D *camera);
#endif #endif

View file

@ -40,8 +40,7 @@ Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index) {
return kubo_vector2_arr_get(&wall->vertices, index); return kubo_vector2_arr_get(&wall->vertices, index);
} }
void kubo_wall_render(struct kubo_wall *wall, bool select, int offset_x, void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera) {
int offset_y) {
Color wall_color = Color wall_color =
(select && wall->state == KUBO_WALL_SELECTED) ? BLUE : BLACK; (select && wall->state == KUBO_WALL_SELECTED) ? BLUE : BLACK;
@ -50,15 +49,12 @@ void kubo_wall_render(struct kubo_wall *wall, bool select, int offset_x,
for (size_t i = 0; i < wall->vertices.count; i++) { for (size_t i = 0; i < wall->vertices.count; i++) {
points[i] = kubo_vector2_arr_get(&wall->vertices, i); points[i] = kubo_vector2_arr_get(&wall->vertices, i);
points[i].x += offset_x;
points[i].y += offset_y;
} }
DrawSplineLinear(points, wall->vertices.count, 10.f, wall_color); DrawSplineLinear(points, wall->vertices.count, 10.f, wall_color);
if (wall->state == KUBO_WALL_DRAWING) { if (wall->state == KUBO_WALL_DRAWING) {
Vector2 mouse = GetMousePosition(); Vector2 mouse = GetScreenToWorld2D(GetMousePosition(), *camera);
Vector2 mouse_points[] = {points[wall->vertices.count - 1], mouse}; Vector2 mouse_points[] = {points[wall->vertices.count - 1], mouse};
DrawSplineLinear(mouse_points, 2, 10.f, wall_color); DrawSplineLinear(mouse_points, 2, 10.f, wall_color);

View file

@ -47,7 +47,6 @@ void kubo_wall_cleanup(struct kubo_wall *wall);
bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec); bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec);
Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index); Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index);
void kubo_wall_render(struct kubo_wall *wall, bool select, int offset_x, void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera);
int offset_y);
#endif #endif

View file

@ -18,12 +18,12 @@
#include "kubo_window.h" #include "kubo_window.h"
Camera2D camera = {0};
static void window_render(struct kubo_context *context); static void window_render(struct kubo_context *context);
static void window_left_mouse(struct kubo_context *context); static void window_left_mouse(struct kubo_context *context);
static void window_right_mouse(struct kubo_context *context); static void window_right_mouse(struct kubo_context *context);
static void pan(struct kubo_context *context);
static void new_wall_click(struct kubo_context *context); static void new_wall_click(struct kubo_context *context);
static void new_wall_end(struct kubo_context *context); static void new_wall_end(struct kubo_context *context);
@ -33,6 +33,8 @@ void kubo_window_init() {
EnableEventWaiting(); EnableEventWaiting();
SetExitKey(0); SetExitKey(0);
SetTargetFPS(KUBO_TARGET_FPS); SetTargetFPS(KUBO_TARGET_FPS);
camera.zoom = 1.0f;
} }
void kubo_window_cleanup() { CloseWindow(); } void kubo_window_cleanup() { CloseWindow(); }
@ -43,18 +45,26 @@ bool kubo_window_should_close(struct kubo_context *context) {
void kubo_window_tick(struct kubo_context *context) { void kubo_window_tick(struct kubo_context *context) {
window_render(context); window_render(context);
pan(context);
kubo_camera_zoom(&camera);
if ((context->state.id == KUBO_CONTEXT_NORMAL &&
IsMouseButtonDown(MOUSE_BUTTON_LEFT)) ||
IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) {
kubo_camera_pan(&camera);
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
window_left_mouse(context); window_left_mouse(context);
} }
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
window_right_mouse(context); window_right_mouse(context);
} }
kubo_input_handle(context); kubo_input_handle(context, &camera);
} }
static void window_render(struct kubo_context *context) { static void window_render(struct kubo_context *context) {
BeginDrawing(); BeginDrawing();
BeginMode2D(camera);
ClearBackground(WHITE); ClearBackground(WHITE);
for (size_t i = 0; i < context->walls.count; i++) { for (size_t i = 0; i < context->walls.count; i++) {
@ -62,9 +72,11 @@ static void window_render(struct kubo_context *context) {
assert(wall != NULL); assert(wall != NULL);
kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT, kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT,
context->offset_x, context->offset_y); &camera);
} }
EndMode2D();
kubo_bar_render(context); kubo_bar_render(context);
EndDrawing(); EndDrawing();
@ -94,15 +106,6 @@ static void window_right_mouse(struct kubo_context *context) {
} }
} }
static void pan(struct kubo_context *context) {
if (context->state.id == KUBO_CONTEXT_NORMAL &&
IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
Vector2 delta = GetMouseDelta();
context->offset_x += delta.x;
context->offset_y += delta.y;
}
}
static void new_wall_click(struct kubo_context *context) { static void new_wall_click(struct kubo_context *context) {
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context); struct kubo_wall *current_wall = kubo_context_get_pending_wall(context);
@ -110,14 +113,9 @@ static void new_wall_click(struct kubo_context *context) {
return; return;
} }
int bar_limit = GetScreenHeight() - KUBO_BAR_HEIGHT; Vector2 new_pos = GetScreenToWorld2D(GetMousePosition(), camera);
Vector2 new_pos = GetMousePosition(); kubo_wall_add_vertex(current_wall, new_pos);
new_pos.x -= context->offset_x; current_wall->state = KUBO_WALL_DRAWING;
new_pos.y -= context->offset_y;
if (new_pos.y < bar_limit) {
kubo_wall_add_vertex(current_wall, new_pos);
current_wall->state = KUBO_WALL_DRAWING;
}
} }
static void new_wall_end(struct kubo_context *context) { static void new_wall_end(struct kubo_context *context) {

View file

@ -21,9 +21,10 @@
#include <raylib.h> #include <raylib.h>
#include "kubo_bar.h"
#include "kubo_camera.h"
#include "kubo_context.h" #include "kubo_context.h"
#include "kubo_input.h" #include "kubo_input.h"
#include "kubo_bar.h"
#define KUBO_WINDOW_WIDTH 1000 #define KUBO_WINDOW_WIDTH 1000
#define KUBO_WINDOW_HEIGHT 800 #define KUBO_WINDOW_HEIGHT 800