Draw tiled map and a blue knight with basic movement keys

This commit is contained in:
Joseph Ferano 2023-12-29 23:47:32 +07:00
parent 0f9ed29a01
commit 57a609228d

103
main.c
View File

@ -1,24 +1,103 @@
#include "raylib.h" #include "raylib.h"
int main(void) typedef struct {
{ Vector2 knight_pos;
const int screenWidth = 800; } GameState;
const int screenHeight = 450;
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); 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(); BeginDrawing();
{
ClearBackground(RAYWHITE); BeginMode2D(cam);
{
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); 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(); EndDrawing();
} }
UnloadTexture(ground);
CloseWindow(); CloseWindow();
return 0; return 0;
} }