diff --git a/CMakeLists.txt b/CMakeLists.txt index ee30305..b6a534f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,4 +40,10 @@ set(SOURCES add_executable(kubo ${SOURCES}) +if(MSVC) + target_compile_options(kubo PRIVATE /W4 /WX) +else() + target_compile_options(kubo PRIVATE -Wall -Wextra -Werror) +endif() + target_link_libraries(kubo raylib) diff --git a/kubo_bar.c b/kubo_bar.c index ec9f05a..c269d54 100644 --- a/kubo_bar.c +++ b/kubo_bar.c @@ -18,16 +18,7 @@ #include "kubo_bar.h" -static inline int push_text(int x, int y, const char *text, Color bg_color) { - - int font_size = KUBO_BAR_HEIGHT - KUBO_BAR_PADDING; - int text_width = MeasureText(text, font_size); - - DrawRectangle(x - KUBO_BAR_PADDING, y, text_width + (2 * KUBO_BAR_PADDING), - KUBO_BAR_HEIGHT, bg_color); - DrawText(text, x, y + (KUBO_BAR_PADDING / 2), font_size, WHITE); - return x + text_width; -} +static inline int push_text(int x, int y, const char *text, Color bg_color); void kubo_bar_render(struct kubo_context *context) { const int bar_y = GetScreenHeight() - KUBO_BAR_HEIGHT; @@ -44,3 +35,14 @@ void kubo_bar_render(struct kubo_context *context) { KUBO_BAR_HEIGHT - KUBO_BAR_PADDING); } } + +static inline int push_text(int x, int y, const char *text, Color bg_color) { + int font_size = KUBO_BAR_HEIGHT - KUBO_BAR_PADDING; + int text_width = MeasureText(text, font_size); + + DrawRectangle(x - KUBO_BAR_PADDING, y, text_width + (2 * KUBO_BAR_PADDING), + KUBO_BAR_HEIGHT, bg_color); + DrawText(text, x, y + (KUBO_BAR_PADDING / 2), font_size, WHITE); + return x + text_width; +} + diff --git a/kubo_bar.h b/kubo_bar.h index 1eddab0..9b0ee83 100644 --- a/kubo_bar.h +++ b/kubo_bar.h @@ -29,6 +29,4 @@ void kubo_bar_render(struct kubo_context *context); -static inline int push_text(int x, int y, const char *text, Color bg_color); - #endif diff --git a/kubo_char_arr.h b/kubo_char_arr.h index d590a79..60f3ed9 100644 --- a/kubo_char_arr.h +++ b/kubo_char_arr.h @@ -23,11 +23,11 @@ #include "kubo_dynarray.h" -KUBO_DYNARRAY_REGISTER(kubo_char_arr, char); +KUBO_DYNARRAY_REGISTER(kubo_char_arr, char) static inline char *kubo_char_arr_build_str(struct kubo_char_arr *arr) { char *res = malloc(arr->count + 1); - for (int i = 0; i < arr->count; i++) { + for (size_t i = 0; i < arr->count; i++) { res[i] = kubo_char_arr_get(arr, i); } res[arr->count] = '\0'; diff --git a/kubo_context.c b/kubo_context.c index 1596416..ca2e290 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -24,6 +24,8 @@ const struct kubo_context_state_data kubo_context_states[] = { {.id = KUBO_CONTEXT_WALL_NEW, .label = "Wall New", .color = GREEN}, {.id = KUBO_CONTEXT_WALL_SELECT, .label = "Wall Select", .color = BLUE}}; +static inline void wall_select_refresh(struct kubo_context *context); + void kubo_context_init(struct kubo_context *context) { if (!context) { return; @@ -121,7 +123,7 @@ void kubo_context_select_prev_wall(struct kubo_context *context) { } static inline void wall_select_refresh(struct kubo_context *context) { - for (int i = 0; i < context->walls.count; i++) { + for (size_t i = 0; i < context->walls.count; i++) { struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i); wall->state = i == context->wall_select_index ? KUBO_WALL_SELECTED : KUBO_WALL_DONE; diff --git a/kubo_context.h b/kubo_context.h index 3f78a62..e495f38 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -27,7 +27,7 @@ #include "kubo_dynarray.h" #include "kubo_wall.h" -KUBO_DYNARRAY_REGISTER(kubo_wall_arr, struct kubo_wall *); +KUBO_DYNARRAY_REGISTER(kubo_wall_arr, struct kubo_wall *) enum kubo_context_state { KUBO_CONTEXT_NORMAL, @@ -67,6 +67,4 @@ struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context); void kubo_context_select_next_wall(struct kubo_context *context); void kubo_context_select_prev_wall(struct kubo_context *context); -static inline void wall_select_refresh(struct kubo_context *context); - #endif diff --git a/kubo_dynarray.h b/kubo_dynarray.h index 55e9c8c..893c0b5 100644 --- a/kubo_dynarray.h +++ b/kubo_dynarray.h @@ -71,6 +71,12 @@ arr->data[index] = new_val; \ } \ \ + static inline void name##_pop(struct name *arr) { \ + if (arr->count > 0) { \ + arr->count--; \ + } \ + } \ + \ static inline void name##_clear(struct name *arr) { \ free(arr->data); \ name##_init(arr); \ diff --git a/kubo_window.c b/kubo_window.c index d08b6cb..e7d0e06 100644 --- a/kubo_window.c +++ b/kubo_window.c @@ -18,14 +18,21 @@ #include "kubo_window.h" -void kubo_window_init(struct kubo_context *context) { +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); } -void kubo_window_cleanup(struct kubo_context *context) { CloseWindow(); } +void kubo_window_cleanup() { CloseWindow(); } bool kubo_window_should_close(struct kubo_context *context) { return context->exit_pending || WindowShouldClose(); @@ -39,14 +46,14 @@ void kubo_window_tick(struct kubo_context *context) { if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { window_right_mouse(context); } - window_key_input(context); + kubo_input_handle(context); } static void window_render(struct kubo_context *context) { BeginDrawing(); ClearBackground(WHITE); - for (int i = 0; i < context->walls.count; i++) { + for (size_t i = 0; i < context->walls.count; i++) { struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i); assert(wall != NULL); @@ -82,76 +89,6 @@ static void window_right_mouse(struct kubo_context *context) { } } -static void window_key_input(struct kubo_context *context) { - - window_char_input(context); - - int key_code = GetKeyPressed(); - - if (context->state.id == KUBO_CONTEXT_COMMAND) { - switch (key_code) { - case KEY_ESCAPE: - kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); - break; - case KEY_ENTER: - kubo_context_accept_cmd(context); - default: - break; - } - - return; - } - - if (!key_code) { - return; - } - - switch (key_code) { - case KEY_Q: - kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); - break; - - case KEY_W: - kubo_context_set_state(context, KUBO_CONTEXT_WALL_NEW); - break; - - case KEY_S: - kubo_context_set_state(context, KUBO_CONTEXT_WALL_SELECT); - break; - - case KEY_RIGHT: - case KEY_UP: - case KEY_L: - case KEY_K: - kubo_context_select_next_wall(context); - break; - - case KEY_LEFT: - case KEY_DOWN: - case KEY_J: - case KEY_H: - kubo_context_select_prev_wall(context); - break; - - default: - break; - } -} - -static void window_char_input(struct kubo_context *context) { - - int char_code; - do { - char_code = GetCharPressed(); - - if (char_code && context->state.id == KUBO_CONTEXT_COMMAND) { - kubo_char_arr_add(&context->command, char_code); - } else if (char_code == ':') { - kubo_context_set_state(context, KUBO_CONTEXT_COMMAND); - } - } while (char_code > 0); -} - static void new_wall_click(struct kubo_context *context) { struct kubo_wall *current_wall = kubo_context_get_pending_wall(context); diff --git a/kubo_window.h b/kubo_window.h index 3e45973..a8de428 100644 --- a/kubo_window.h +++ b/kubo_window.h @@ -20,28 +20,19 @@ #define KUBO_WINDOW_H #include -#include #include "kubo_context.h" +#include "kubo_input.h" #include "kubo_bar.h" #define KUBO_WINDOW_WIDTH 1000 #define KUBO_WINDOW_HEIGHT 800 -void kubo_window_init(struct kubo_context *context); -void kubo_window_cleanup(struct kubo_context *context); +void kubo_window_init(); +void kubo_window_cleanup(); bool kubo_window_should_close(struct kubo_context *context); void kubo_window_tick(struct kubo_context *context); -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 window_key_input(struct kubo_context *context); -static void window_char_input(struct kubo_context *context); - -static void new_wall_click(struct kubo_context *context); -static void new_wall_end(struct kubo_context *context); - #endif diff --git a/main.c b/main.c index 5592015..ef5b46d 100644 --- a/main.c +++ b/main.c @@ -4,18 +4,17 @@ #include "kubo_context.h" #include "kubo_window.h" -int main(int argc, char *argv[]) { - +int main() { struct kubo_context context; kubo_context_init(&context); - kubo_window_init(&context); + kubo_window_init(); while (!kubo_window_should_close(&context)) { kubo_window_tick(&context); } - kubo_window_cleanup(&context); + kubo_window_cleanup(); kubo_context_cleanup(&context); return 0;