kubo/kubo_window.c
2025-07-26 23:03:27 +02:00

119 lines
3.3 KiB
C

/*
* 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_window.h"
static void window_render(struct kubo_context *context);
static void window_left_mouse(struct kubo_context *context);
static void window_right_mouse(struct kubo_context *context);
static void new_wall_click(struct kubo_context *context);
static void new_wall_end(struct kubo_context *context);
void kubo_window_init() {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(KUBO_WINDOW_WIDTH, KUBO_WINDOW_HEIGHT, "Kubo");
EnableEventWaiting();
SetExitKey(0);
SetTargetFPS(60);
}
void kubo_window_cleanup() { CloseWindow(); }
bool kubo_window_should_close(struct kubo_context *context) {
return context->exit_pending || WindowShouldClose();
}
void kubo_window_tick(struct kubo_context *context) {
window_render(context);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
window_left_mouse(context);
}
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
window_right_mouse(context);
}
kubo_input_handle(context);
}
static void window_render(struct kubo_context *context) {
BeginDrawing();
ClearBackground(WHITE);
for (size_t i = 0; i < context->walls.count; i++) {
struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i);
assert(wall != NULL);
kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT,
context->offset_x, context->offset_y);
}
kubo_bar_render(context);
EndDrawing();
}
static void window_left_mouse(struct kubo_context *context) {
switch (context->state.id) {
case KUBO_CONTEXT_WALL_NEW:
new_wall_click(context);
break;
case KUBO_CONTEXT_WALL_SELECT:
break;
default:
break;
}
}
static void window_right_mouse(struct kubo_context *context) {
switch (context->state.id) {
case KUBO_CONTEXT_WALL_NEW:
new_wall_end(context);
break;
case KUBO_CONTEXT_WALL_SELECT:
break;
default:
break;
}
}
static void new_wall_click(struct kubo_context *context) {
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context);
if (!current_wall) {
return;
}
int bar_limit = GetScreenHeight() - KUBO_BAR_HEIGHT;
Vector2 new_pos = GetMousePosition();
new_pos.x -= context->offset_x;
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) {
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context);
if (!current_wall) {
return;
}
current_wall->state = KUBO_WALL_DONE;
}