]> git.lizzy.rs Git - nothing.git/blobdiff - src/game.c
(#1193) Use 'menu' console command instead of 'Esc' key
[nothing.git] / src / game.c
index c13ad1342e4b3f47024fd41a507d59f32fd18f93..9c45c05d5c4d28cbd18da051b3d9cacfa5645445 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"
+#include "game/credits.h"
 
-static int game_render_cursor(const 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;
-
-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,
-    GAME_STATE_LEVEL_EDITOR,
-    GAME_STATE_SETTINGS,
-    GAME_STATE_QUIT
-} Game_state;
 
 typedef struct Game {
     Lt *lt;
@@ -52,20 +27,17 @@ typedef struct Game {
     Sprite_font *font;
     LevelPicker *level_picker;
     LevelEditor *level_editor;
+    Credits *credits;
     Level *level;
     Settings settings;
     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
 void game_switch_state(Game *game, Game_state state)
 {
     game->camera = create_camera(game->renderer, game->font);
@@ -107,6 +79,14 @@ Game *create_game(const char *level_folder,
         RETURN_LT(lt, NULL);
     }
 
+    game->credits = PUSH_LT(
+        lt,
+        create_credits(),
+        destroy_credits);
+    if (game->credits == NULL) {
+        RETURN_LT(lt, NULL);
+    }
+
     game->sound_samples = PUSH_LT(
         lt,
         create_sound_samples(
@@ -122,12 +102,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,
@@ -149,9 +129,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;
@@ -186,6 +163,12 @@ int game_render(const Game *game)
         }
     } break;
 
+    case GAME_STATE_CREDITS: {
+        if (credits_render(game->credits, &game->camera) < 0) {
+            return -1;
+        }
+    } break;
+
     case GAME_STATE_SETTINGS: {
         settings_render(&game->settings, &game->camera);
     } break;
@@ -199,7 +182,7 @@ int game_render(const Game *game)
         }
     }
 
-    if (game_render_cursor(game) < 0) {
+    if (cursor_render(&game->cursor, game->renderer) < 0) {
         return -1;
     }
 
@@ -215,6 +198,7 @@ int game_sound(Game *game)
         level_editor_sound(game->level_editor, game->sound_samples);
         return 0;
     case GAME_STATE_LEVEL_PICKER:
+    case GAME_STATE_CREDITS:
     case GAME_STATE_SETTINGS:
     case GAME_STATE_QUIT:
         return 0;
@@ -274,6 +258,12 @@ int game_update(Game *game, float delta_time)
         level_editor_update(game->level_editor, delta_time);
     } break;
 
+    case GAME_STATE_CREDITS: {
+        if (credits_update(game->credits, &game->camera, delta_time) < 0) {
+            return -1;
+        }
+    } break;
+
     case GAME_STATE_SETTINGS: {
         settings_update(&game->settings, &game->camera, delta_time);
         sound_samples_update_volume(
@@ -308,6 +298,10 @@ static int game_event_running(Game *game, const SDL_Event *event)
                     return -1;
                 }
 
+                level_disable_pause_mode(
+                    game->level,
+                    &game->camera,
+                    game->sound_samples);
                 camera_disable_debug_mode(&game->camera);
             } break;
 
@@ -334,13 +328,13 @@ static int game_event_level_picker(Game *game, const SDL_Event *event)
             if (game->level_editor == NULL) {
                 game->level_editor = PUSH_LT(
                     game->lt,
-                    create_level_editor(),
+                    create_level_editor(&game->cursor),
                     destroy_level_editor);
             } else {
                 game->level_editor = RESET_LT(
                     game->lt,
                     game->level_editor,
-                    create_level_editor());
+                    create_level_editor(&game->cursor));
             }
 
             if (game->level_editor == NULL) {
@@ -368,6 +362,10 @@ static int game_event_level_picker(Game *game, const SDL_Event *event)
             game_switch_state(game, GAME_STATE_LEVEL);
         } break;
 
+        case SDLK_i: {
+            game_switch_state(game, GAME_STATE_CREDITS);
+        } break;
+
         case SDLK_s: {
             game_switch_state(game, GAME_STATE_SETTINGS);
         } break;
@@ -375,7 +373,7 @@ static int game_event_level_picker(Game *game, const SDL_Event *event)
     } break;
     }
 
-    return level_picker_event(game->level_picker, event, &game->camera);
+    return level_picker_event(game->level_picker, event);
 }
 
 static int game_event_level_editor(Game *game, const SDL_Event *event)
@@ -416,11 +414,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);
@@ -451,9 +444,11 @@ int game_event(Game *game, const SDL_Event *event)
             switch (event->key.keysym.sym) {
             case SDLK_BACKQUOTE:
             case SDLK_c: {
-                SDL_StartTextInput();
-                game->console_enabled = 1;
-                console_slide_down(game->console);
+                if (event->key.keysym.mod == 0) {
+                    SDL_StartTextInput();
+                    game->console_enabled = 1;
+                    console_slide_down(game->console);
+                }
             } break;
             }
         } break;
@@ -471,6 +466,19 @@ int game_event(Game *game, const SDL_Event *event)
     case GAME_STATE_LEVEL_EDITOR:
         return game_event_level_editor(game, event);
 
+    case GAME_STATE_CREDITS: {
+        switch (event->type) {
+        case SDL_KEYDOWN: {
+            if (event->key.keysym.sym == SDLK_ESCAPE) {
+                game_switch_state(game, GAME_STATE_LEVEL_PICKER);
+                return 0;
+            }
+        } break;
+        }
+
+        return 0;
+    } break;
+
     case GAME_STATE_SETTINGS: {
         switch (event->type) {
         case SDL_KEYDOWN: {
@@ -492,9 +500,9 @@ int game_event(Game *game, const SDL_Event *event)
     return -1;
 }
 
-
-// TODO: get rid of keyboard_state (because it's a global var and can
-// be check anywhere anyway). And introduce *_joystick methods.
+// TODO(#1145): get rid of keyboard_state and introduce *_joystick methods
+//
+// keyboard_state is a global var and can be check anywhere anyway
 int game_input(Game *game,
                const Uint8 *const keyboard_state,
                SDL_Joystick *the_stick_of_joy)
@@ -508,6 +516,7 @@ int game_input(Game *game,
 
     switch (game->state) {
     case GAME_STATE_SETTINGS:
+    case GAME_STATE_CREDITS:
     case GAME_STATE_QUIT:
     case GAME_STATE_LEVEL_EDITOR:
         return 0;
@@ -527,24 +536,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);
@@ -553,13 +544,13 @@ int game_load_level(Game *game, const char *level_filename)
     if (game->level_editor == NULL) {
         game->level_editor = PUSH_LT(
             game->lt,
-            create_level_editor_from_file(level_filename),
+            create_level_editor_from_file(level_filename, &game->cursor),
             destroy_level_editor);
     } else {
         game->level_editor = RESET_LT(
             game->lt,
             game->level_editor,
-            create_level_editor_from_file(level_filename));
+            create_level_editor_from_file(level_filename, &game->cursor));
     }
 
     if (game->level_editor == NULL) {