From 57a609228d297262986de56fcf057cf5812f73cb Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Fri, 29 Dec 2023 23:47:32 +0700 Subject: [PATCH] Draw tiled map and a blue knight with basic movement keys --- main.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index 01419ac..7c0fb7a 100644 --- a/main.c +++ b/main.c @@ -1,24 +1,103 @@ #include "raylib.h" -int main(void) -{ - const int screenWidth = 800; - const int screenHeight = 450; +typedef struct { + Vector2 knight_pos; +} GameState; - InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); +void Init(GameState *state); +// { +// } + +void Draw(GameState *state); +// { +// ClearBackground(RAYWHITE); +// DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); +// } + +int main(void) { + const int screen_width = 1600; + const int screen_height = 1080; + + InitWindow(screen_width, screen_height, "raylib [core] example - basic window"); + + int monitor = GetCurrentMonitor(); + int monitor_width = GetMonitorWidth(monitor); + int monitor_height = GetMonitorHeight(monitor); + SetWindowPosition(monitor_width / 2 - screen_width / 2, + monitor_height / 2 - screen_height / 2); SetTargetFPS(60); - while (!WindowShouldClose()) - { + + GameState state = {0}; + Camera2D cam = {0}; + + // Vector2 offset; // Camera offset (displacement from target) + // Vector2 target; // Camera target (rotation and zoom origin) + // float rotation; // Camera rotation in degrees + cam.zoom = 1.0f; + + Texture2D ground = LoadTexture("assets/Terrain/Ground/Tilemap_Flat.png"); + Texture2D blue_knight_warrior = + LoadTexture("assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png"); + + int framesCounter = 0; + int knight_speed = 10.1f; + while (!WindowShouldClose()) { + framesCounter++; + + if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) { + state.knight_pos.x += knight_speed; + } + if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) { + state.knight_pos.x -= knight_speed; + } + if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) { + state.knight_pos.y -= knight_speed; + } + if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) { + state.knight_pos.y += knight_speed; + } + BeginDrawing(); - - ClearBackground(RAYWHITE); - - DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); - + { + BeginMode2D(cam); + { + ClearBackground((Color){ 100, 149, 237, 255 }); + // Draw(&state); + int size = 32; + int topx = 300; + int topy = 32; + for (int col = 0; col < size; col++) { + for (int row = 0; row < size; row++) { + int atlas_col = 0; + int atlas_row = 0; + if (col == size - 1) { + atlas_col = 5; + } else if (col > 0) { + atlas_col = (col % 4) + 1; + } + if (row == size - 1) { + atlas_row = 5; + } else if (row > 0) { + atlas_row = (row % 4) + 1; + } + Vector2 pos = {32 * col + topx, 32 * row + topy}; + Rectangle src_rect = {32 * atlas_col, 32 * atlas_row, 32, 32}; + DrawTextureRec(ground, src_rect, pos, WHITE); + } + } + DrawTextureRec(blue_knight_warrior, + (Rectangle){0, 0, 1152 / 6, 1536 / 8}, + state.knight_pos, + // (Vector2){100, 100}, + WHITE); + } + EndMode2D(); + } EndDrawing(); } + UnloadTexture(ground); CloseWindow(); return 0; }