diff --git a/include/conf.h b/include/conf.h index c750632..9d02f55 100644 --- a/include/conf.h +++ b/include/conf.h @@ -2,13 +2,26 @@ /* Copyright (C) 2021 KikooDX */ #pragma once -static const int game_window_width = 396; -static const int game_window_height = 224; -static const int picker_window_width = 256; -static const int picker_window_height = 256; -static const int draw_offset_x = -2; -static const int draw_offset_y = -8; -static const int tile_width = 16; -static const int tile_height = 16; +#include /* for the color struct */ + +#define TILE_WIDTH 16 /* in pixels */ +#define TILE_HEIGHT 16 +#define TILESET_WIDTH 16 /* in tiles */ +#define TILESET_HEIGHT 16 +#define TILESET_MIN_WIDTH_PX (TILE_WIDTH * TILESET_WIDTH) +#define TILESET_MIN_HEIGHT_PX (TILE_HEIGHT * TILESET_HEIGHT) + static const int editor_target_fps = 60; static const int picker_target_fps = 30; +static const Color unselected_tile_color = {80, 80, 80, 255}; + +static const int editor_window_width = 396; +static const int editor_window_height = 224; +static const int draw_offset_x = -2; +static const int draw_offset_y = -8; + +#define PICKER_PADDING 4 +static const int picker_window_width = + (TILE_WIDTH + PICKER_PADDING) * TILESET_WIDTH + PICKER_PADDING; +static const int picker_window_height = + (TILE_HEIGHT + PICKER_PADDING) * TILESET_HEIGHT + PICKER_PADDING; diff --git a/include/tile_picker/draw.h b/include/tile_picker/draw.h index 3041a68..968d4d5 100644 --- a/include/tile_picker/draw.h +++ b/include/tile_picker/draw.h @@ -4,4 +4,4 @@ #include -void tileset_draw(Texture2D tileset); +void tileset_draw(Texture2D tileset, int selected_tile); diff --git a/src/editing_area/draw.c b/src/editing_area/draw.c index 1c9f5bd..415f001 100644 --- a/src/editing_area/draw.c +++ b/src/editing_area/draw.c @@ -14,13 +14,13 @@ void level_draw(struct Level level, Texture2D tileset) for (y = 0; y < level.height; y += 1) { const int tile = level.data[x + y * level.width]; - Rectangle tile_rect = { - (int)(tile % tileset.width) * tile_width, - (int)(tile / tileset.width) * tile_width, - tile_width, tile_height}; - Vector2 tile_pos = { - x * tile_width + draw_offset_x, - y * tile_height + draw_offset_y}; + const Rectangle tile_rect = { + (int)(tile % TILESET_WIDTH) * TILE_WIDTH, + (int)(tile / TILESET_HEIGHT) * TILE_WIDTH, + TILE_WIDTH, TILE_HEIGHT}; + const Vector2 tile_pos = { + x * TILE_WIDTH + draw_offset_x, + y * TILE_HEIGHT + draw_offset_y}; if (tile) DrawTextureRec(tileset, tile_rect, tile_pos, WHITE); diff --git a/src/editing_area/main.c b/src/editing_area/main.c index a43923c..6f8858f 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -7,6 +7,7 @@ #include "shared_data.h" #include #include +#include #include #include @@ -17,14 +18,15 @@ int editing_area_main(int argc, char **argv, level.data = NULL; /* initialize raylib */ - InitWindow(game_window_width, game_window_height, + InitWindow(editor_window_width, editor_window_height, "SLE main window"); SetTargetFPS(editor_target_fps); /* load textures */ const Texture2D tileset = LoadTexture(argv[1]); - /* only proceed if tileset is well loaded */ - if (tileset.width > 0) { + /* only proceed if tileset is large enough */ + if (tileset.width >= TILESET_MIN_WIDTH_PX && + tileset.height >= TILESET_MIN_HEIGHT_PX) { /* load level */ level_read(&level, argv[2]); @@ -40,7 +42,12 @@ int editing_area_main(int argc, char **argv, /* save level */ level_write(level, argv[2]); - } + } else + fprintf(stderr, + "ERROR: tileset size is invalid, expected at " + "least [%d ; %d], got [%d ; %d]\n", + TILESET_MIN_WIDTH_PX, TILESET_MIN_HEIGHT_PX, + tileset.width, tileset.height); /* deinit */ level_free(&level); diff --git a/src/main.c b/src/main.c index 0877886..34767ce 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,7 @@ int main(int argc, char **argv) (struct SharedData *)create_shared_memory( sizeof(struct SharedData)); shared_data->end_child = false; - shared_data->selected_tile = 1; + shared_data->selected_tile = 69; pid_t child_process; /* set log level */ diff --git a/src/tile_picker/draw.c b/src/tile_picker/draw.c index 63edc8e..f027377 100644 --- a/src/tile_picker/draw.c +++ b/src/tile_picker/draw.c @@ -2,9 +2,31 @@ /* Copyright (C) 2021 KikooDX */ #include "tile_picker/draw.h" +#include "conf.h" #include -void tileset_draw(Texture2D tileset) +void tileset_draw(Texture2D tileset, int selected_tile) { - DrawTexture(tileset, 0, 0, WHITE); + int x; + int y; + /* draw tiles */ + for (x = 0; x < TILESET_WIDTH; x += 1) { + for (y = 0; y < TILESET_HEIGHT; y += 1) { + const Rectangle tile_rect = { + x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, + TILE_HEIGHT}; + const Vector2 tile_pos = { + x * (TILE_WIDTH + PICKER_PADDING) + + PICKER_PADDING, + y * (TILE_HEIGHT + PICKER_PADDING) + + PICKER_PADDING}; + /* apply tint if tile isn't selected */ + Color tint = unselected_tile_color; + if (x + y * TILESET_WIDTH == selected_tile) + tint = WHITE; + /* draw the tile */ + DrawTextureRec(tileset, tile_rect, tile_pos, + tint); + } + } } diff --git a/src/tile_picker/main.c b/src/tile_picker/main.c index 3533523..0990735 100644 --- a/src/tile_picker/main.c +++ b/src/tile_picker/main.c @@ -24,7 +24,8 @@ int tile_picker_main(int argc, char **argv, BeginDrawing(); ClearBackground(BLACK); - tileset_draw(tileset); + tileset_draw(tileset, + shared_data->selected_tile); EndDrawing(); }