]> git.lizzy.rs Git - nothing.git/commitdiff
(#1129) Extract Cursor entity
authorrexim <reximkut@gmail.com>
Sat, 16 Nov 2019 16:57:16 +0000 (23:57 +0700)
committerrexim <reximkut@gmail.com>
Sat, 16 Nov 2019 16:57:16 +0000 (23:57 +0700)
CMakeLists.txt
src/game.c
src/game.h
src/game/level/level_editor/rect_layer.c
src/ui/cursor.c [new file with mode: 0644]
src/ui/cursor.h [new file with mode: 0644]

index 7391fac5f83ea29ef23f4f5207ca5c1f58320a01..aebaed534151c94967483ae347c9156957f5b11c 100644 (file)
@@ -122,6 +122,8 @@ add_executable(nothing
   src/sdl/renderer.h
   src/sdl/texture.h
   src/sdl/texture.c
+  src/ui/cursor.h
+  src/ui/cursor.c
   src/ui/console.c
   src/ui/console.h
   src/ui/console_log.c
index 05001c7741c0f2c21ab59fde06123f04a05b15c3..d10eec5afc807b2e8864bb8a9f438025323c0154 100644 (file)
 #include "system/nth_alloc.h"
 #include "ui/console.h"
 #include "ui/edit_field.h"
+#include "ui/cursor.h"
 #include "system/str.h"
 #include "sdl/texture.h"
 #include "game/level/level_editor/background_layer.h"
 #include "game/level/level_editor.h"
 #include "game/settings.h"
 
-static int game_render_cursor(const Game *game);
-
-const char *cursor_style_tex_files[CURSOR_STYLE_N] = {
-    "./assets/images/cursor.bmp",
-    "./assets/images/cursor-resize-vert.bmp",
-    "./assets/images/cursor-resize-horis.bmp",
-    "./assets/images/cursor-resize-diag1.bmp",
-    "./assets/images/cursor-resize-diag2.bmp"
-};
-
 typedef enum Game_state {
     GAME_STATE_LEVEL = 0,
     GAME_STATE_LEVEL_PICKER,
@@ -47,12 +38,9 @@ typedef struct Game {
     Sound_samples *sound_samples;
     Camera camera;
     SDL_Renderer *renderer;
-    SDL_Texture *cursor_texs[CURSOR_STYLE_N];
-    Cursor_Style cursor_style;
     Console *console;
+    Cursor cursor;
     int console_enabled;
-    int cursor_x;
-    int cursor_y;
 } Game;
 
 static
@@ -112,12 +100,12 @@ Game *create_game(const char *level_folder,
     game->renderer = renderer;
 
     for (Cursor_Style style = 0; style < CURSOR_STYLE_N; ++style) {
-        game->cursor_texs[style] = PUSH_LT(
+        game->cursor.texs[style] = PUSH_LT(
             lt,
             texture_from_bmp(cursor_style_tex_files[style], renderer),
             SDL_DestroyTexture);
         if (SDL_SetTextureBlendMode(
-                game->cursor_texs[style],
+                game->cursor.texs[style],
                 SDL_ComposeCustomBlendMode(
                     SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR,
                     SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR,
@@ -139,9 +127,6 @@ Game *create_game(const char *level_folder,
     }
     game->console_enabled = 0;
 
-    game->cursor_x = 0;
-    game->cursor_y = 0;
-
     game_switch_state(game, GAME_STATE_LEVEL_PICKER);
 
     return game;
@@ -189,7 +174,7 @@ int game_render(const Game *game)
         }
     }
 
-    if (game_render_cursor(game) < 0) {
+    if (cursor_render(&game->cursor, game->renderer) < 0) {
         return -1;
     }
 
@@ -406,11 +391,6 @@ int game_event(Game *game, const SDL_Event *event)
         return 0;
     } break;
 
-    case SDL_MOUSEMOTION: {
-        game->cursor_x = event->motion.x;
-        game->cursor_y = event->motion.y;
-    } break;
-
     case SDL_KEYDOWN: {
         if (event->key.keysym.sym == SDLK_q && event->key.keysym.mod & KMOD_CTRL) {
             game_switch_state(game, GAME_STATE_QUIT);
@@ -517,24 +497,6 @@ int game_over_check(const Game *game)
     return game->state == GAME_STATE_QUIT;
 }
 
-// Private Functions
-
-static int game_render_cursor(const Game *game)
-{
-    trace_assert(game);
-
-    SDL_Rect src = {0, 0, 32, 32};
-    SDL_Rect dest = {game->cursor_x, game->cursor_y, 32, 32};
-    if (SDL_RenderCopy(
-            game->renderer,
-            game->cursor_texs[game->cursor_style],
-            &src, &dest) < 0) {
-        return -1;
-    }
-
-    return 0;
-}
-
 int game_load_level(Game *game, const char *level_filename)
 {
     trace_assert(game);
@@ -578,10 +540,3 @@ int game_load_level(Game *game, const char *level_filename)
 
     return 0;
 }
-
-void game_set_cursor(Game *game, Cursor_Style style)
-{
-    trace_assert(game);
-    trace_assert(style < CURSOR_STYLE_N);
-    game->cursor_style = style;
-}
index d2640af0bf4daf470d6d0ed510cc687599cf842e..b1141320efb9a411c26a38328382fd5bcd962956 100644 (file)
@@ -7,16 +7,6 @@
 
 typedef struct Game Game;
 
-typedef enum {
-    CURSOR_STYLE_POINTER = 0,
-    CURSOR_STYLE_RESIZE_VERT,
-    CURSOR_STYLE_RESIZE_HORIS,
-    CURSOR_STYLE_RESIZE_DIAG1,
-    CURSOR_STYLE_RESIZE_DIAG2,
-
-    CURSOR_STYLE_N
-} Cursor_Style;
-
 Game *create_game(const char *platforms_file_path,
                     const char *sound_sample_files[],
                     size_t sound_sample_files_count,
@@ -35,6 +25,5 @@ int game_input(Game *game,
 int game_over_check(const Game *game);
 
 int game_load_level(Game *game, const char *filepath);
-void game_set_cursor(Game *game, Cursor_Style style);
 
 #endif  // GAME_H_
index 681f6907f4b6b9164fb1647b056c012d3cf29c0f..d76aa6cdeb3feea86b6cc2b9b6173a8191859aa2 100644 (file)
@@ -997,12 +997,6 @@ int rect_layer_event(RectLayer *layer,
     } break;
     }
 
-    if (layer->state == RECT_LAYER_RESIZE) {
-        game_set_cursor(game, CURSOR_STYLE_RESIZE_VERT);
-    } else {
-        game_set_cursor(game, CURSOR_STYLE_POINTER);
-    }
-
     switch (layer->state) {
     case RECT_LAYER_IDLE:
         return rect_layer_event_idle(layer, event, camera, undo_history);
diff --git a/src/ui/cursor.c b/src/ui/cursor.c
new file mode 100644 (file)
index 0000000..897780d
--- /dev/null
@@ -0,0 +1,22 @@
+#include "system/stacktrace.h"
+#include "cursor.h"
+
+int cursor_render(const Cursor *cursor, SDL_Renderer *renderer)
+{
+    trace_assert(cursor);
+    trace_assert(renderer);
+
+    int cursor_x, cursor_y;
+    SDL_GetMouseState(&cursor_x, &cursor_y);
+
+    const SDL_Rect src = {0, 0, 32, 32};
+    const SDL_Rect dest = {cursor_x, cursor_y, 32, 32};
+    if (SDL_RenderCopy(
+            renderer,
+            cursor->texs[cursor->style],
+            &src, &dest) < 0) {
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/src/ui/cursor.h b/src/ui/cursor.h
new file mode 100644 (file)
index 0000000..738cf50
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef CURSOR_H_
+#define CURSOR_H_
+
+#include <SDL2/SDL.h>
+
+typedef enum {
+    CURSOR_STYLE_POINTER = 0,
+    CURSOR_STYLE_RESIZE_VERT,
+    CURSOR_STYLE_RESIZE_HORIS,
+    CURSOR_STYLE_RESIZE_DIAG1,
+    CURSOR_STYLE_RESIZE_DIAG2,
+
+    CURSOR_STYLE_N
+} Cursor_Style;
+
+static const char * const cursor_style_tex_files[CURSOR_STYLE_N] = {
+    "./assets/images/cursor.bmp",
+    "./assets/images/cursor-resize-vert.bmp",
+    "./assets/images/cursor-resize-horis.bmp",
+    "./assets/images/cursor-resize-diag1.bmp",
+    "./assets/images/cursor-resize-diag2.bmp"
+};
+
+typedef struct {
+    SDL_Texture *texs[CURSOR_STYLE_N];
+    Cursor_Style style;
+} Cursor;
+
+int cursor_render(const Cursor *cursor, SDL_Renderer *renderer);
+
+#endif  // CURSOR_H_