From 6c4ecd35da17e07b743d4ef75e234383317d22ac Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 18 Jan 2025 21:22:32 +0500 Subject: feat: new editor (wip) + using Camera2D --- src/editor.c | 26 +++++++++++++++++++++++++ src/editor.h | 13 +++++++++++++ src/level.h | 5 +++++ src/main.c | 62 ++++++++++++++++++++---------------------------------------- 4 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 src/editor.c create mode 100644 src/editor.h diff --git a/src/editor.c b/src/editor.c new file mode 100644 index 0000000..64f1673 --- /dev/null +++ b/src/editor.c @@ -0,0 +1,26 @@ +#include "editor.h" + +#include "raylib.h" + +void SE_DrawEditor(Editor *editor, Camera2D *camera) { + Vector2 mousePos = GetScreenToWorld2D(GetMousePosition(), *camera); + float zoom = camera->zoom; + + for (int x = 0; x < editor->level->width; x++) { + for (int y = 0; y < editor->level->height; y++) { + float rx = x * zoom, ry = y * zoom; + Color innerColor = RAYWHITE; + Color borderColor = LIGHTGRAY; + + // recolor the tile if the cursor above the tile + if ((rx < mousePos.x && mousePos.x < rx + zoom) && + (ry < mousePos.y && mousePos.y < ry + zoom)) { + innerColor = SKYBLUE; + borderColor = BLUE; + } + + DrawRectangle(rx, ry, zoom, zoom, innerColor); + DrawRectangleLines(rx, ry, zoom, zoom, borderColor); + } + } +} diff --git a/src/editor.h b/src/editor.h new file mode 100644 index 0000000..0d7450e --- /dev/null +++ b/src/editor.h @@ -0,0 +1,13 @@ +#include + +#include "level.h" + +typedef struct { +} EditorState; + +typedef struct { + Level *level; + EditorState state; +} Editor; + +void SE_DrawEditor(Editor *editor, Camera2D *camera); diff --git a/src/level.h b/src/level.h index fe6ce1d..35e8cb8 100644 --- a/src/level.h +++ b/src/level.h @@ -1,3 +1,6 @@ +#ifndef __LEVEL_H__ +#define __LEVEL_H__ + typedef struct { int x, y; } Point; @@ -14,3 +17,5 @@ typedef struct { Level *SE_CreateLevel(int width, int height); void SE_RenderLevel(Level *level, int zoomScale); void SE_FreeLevel(Level *level); + +#endif diff --git a/src/main.c b/src/main.c index a64fb0d..36ceb19 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ #include -#include +#include "editor.h" #include "level.h" #include "raylib.h" @@ -9,57 +9,37 @@ int main() { InitWindow(800, 600, "sillyeditor"); SetTargetFPS(60); - int gridSize = 32; - Level *level = SE_CreateLevel(30, 30); + Editor editor = {level, {}}; - // creating a dummy side - Side *side = malloc(sizeof(Side)); - side->a = (Point){1, 1}; - side->b = (Point){3, 4}; - level->sides[0] = side; + Camera2D camera = {0}; + camera.target = (Vector2){0.0f, 0.0f}; + camera.offset = (Vector2){0.0f, 0.0f}; + camera.rotation = 0.0f; + camera.zoom = 4.0f; while (!WindowShouldClose()) { - // Change the grid size if (GetMouseWheelMove() != 0.0) { - gridSize += (int)(GetMouseWheelMove() * 4.0); - } - - BeginDrawing(); - ClearBackground(RAYWHITE); - DrawText("hi world!", GetScreenWidth() / 2 - 16 * 4, - GetScreenHeight() / 2 - 16, 32, BLACK); - - SE_RenderLevel(level, gridSize); + camera.zoom += (int)GetMouseWheelMove(); - // Drawing grid - for (int x = 0; x <= level->width; x++) { - DrawLine(x * gridSize, 0, x * gridSize, GetScreenHeight(), LIGHTGRAY); + if (camera.zoom > 6.0f) + camera.zoom = 6.0f; + else if (camera.zoom < 4.0f) + camera.zoom = 4.0f; } - for (int y = 0; y <= level->height; y++) { - DrawLine(0, y * gridSize, GetScreenWidth(), y * gridSize, LIGHTGRAY); + if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { + Vector2 mousePos = GetMouseDelta(); + camera.target.x -= mousePos.x / 5.0f; + camera.target.y -= mousePos.y / 5.0f; } - // Drawing points - for (int x = 0; x < level->width; x++) { - for (int y = 0; y < level->height; y++) { - Color color = GRAY; - - for (int i = 0; i < level->height * level->width; i++) { - Side *side = level->sides[i]; - if (side == NULL) continue; - - if ((x == side->a.x && y == side->a.y) || - (x == side->b.x && y == side->b.y)) { - color = RED; - break; - } - } + BeginDrawing(); + ClearBackground(RAYWHITE); - DrawCircle(x * gridSize, y * gridSize, 4, color); - } - } + BeginMode2D(camera); + SE_DrawEditor(&editor, &camera); + EndMode2D(); EndDrawing(); } -- cgit v1.2.3