From 3a2a6acb2b9fa9ba4c3397a48772a83655305943 Mon Sep 17 00:00:00 2001 From: alistair Date: Mon, 30 Nov 2020 21:52:18 +1000 Subject: [PATCH] pause menu --- src/audio.c | 3 +- src/controlscheme.c | 4 +++ src/controlscheme.h | 3 ++ src/draw.c | 81 +++++++++++++++++++++++++++++++++++++++++++-- src/game.c | 49 ++++++++++++++++++--------- src/main.c | 14 +++----- 6 files changed, 126 insertions(+), 28 deletions(-) diff --git a/src/audio.c b/src/audio.c index 1f8ebbe..7be6387 100644 --- a/src/audio.c +++ b/src/audio.c @@ -9,6 +9,7 @@ struct game_sounds game_sounds; Uint8 big_hum(unsigned int t); Uint8 func0(unsigned int t); +int mute = false; void fill_audio(void *func, Uint8 *stream, int len) { Uint8 (*wavegen)(unsigned int) = func; @@ -53,7 +54,7 @@ void start_audio(void) { void play_game_sound(Mix_Chunk *chunk, int len, int channel) { - if (Mix_Playing(channel)) { + if (mute || Mix_Playing(channel)) { return; } diff --git a/src/controlscheme.c b/src/controlscheme.c index adaba04..9a4c129 100644 --- a/src/controlscheme.c +++ b/src/controlscheme.c @@ -1,5 +1,6 @@ #include "controlscheme.h" +#include struct InputMap input_map; @@ -10,5 +11,8 @@ void get_input_map(void) { input_map.player_left = SDL_SCANCODE_A; input_map.player_rope = SDL_SCANCODE_E; input_map.player_pull_rope = SDL_SCANCODE_Q; + input_map.mute = SDL_SCANCODE_M; + input_map.pause = SDL_SCANCODE_ESCAPE; + input_map.quit = SDL_SCANCODE_Q; } diff --git a/src/controlscheme.h b/src/controlscheme.h index 67cc8f2..62657d3 100644 --- a/src/controlscheme.h +++ b/src/controlscheme.h @@ -17,6 +17,9 @@ struct InputMap { SDL_Scancode player_right; SDL_Scancode player_rope; SDL_Scancode player_pull_rope; + SDL_Scancode mute; + SDL_Scancode quit; + SDL_Scancode pause; }; extern struct InputMap input_map; diff --git a/src/draw.c b/src/draw.c index fa6e7b7..afd0434 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,5 +1,6 @@ #include "draw.h" #include "game.h" +#include "types.h" #include #include #include @@ -530,10 +531,25 @@ int draw_end_screen(SDL_Renderer *ren) { struct colour bg = scene.colours.bg; struct colour colour = scene.colours.fg1; + struct colour_pallete pal = scene.colours; SDL_SetRenderDrawColor(ren, bg.r, bg.g, bg.b,255); + SDL_RenderClear(ren); + + SDL_SetRenderDrawColor(ren, pal.fg2.r, pal.fg2.g, pal.fg2.b,255); + for (int i = width - 300; i < width; i++) { + SDL_RenderDrawLine(ren, i, height, width, height - i); + if (!(i % 240)) { + SDL_SetRenderDrawColor(ren, pal.fg3.r, pal.fg3.g, pal.fg3.b,255); + } + if (!(i % 350)) { + SDL_SetRenderDrawColor(ren, pal.fg1.r, pal.fg1.g, pal.fg1.b,255); + } + } + + int margin = height / 10; // Vect position = {.x = width - width / 10, .y = height - margin}; @@ -544,10 +560,9 @@ int draw_end_screen(SDL_Renderer *ren) { char time_string[250]; - char * end_str = "level over."; - Vect position = {.x = width/3, .y = height / 4}; + Vect position = {.x = width/4, .y = height / 4}; draw_text(ren, position, colour, end_str); position.y += 60; @@ -580,7 +595,7 @@ int draw_end_screen(SDL_Renderer *ren) { snprintf(time_string, 250, "NEW BEST TIME: %d:%.4f", minutes, seconds); } - Vect position2 = {.x = 3 * width/5, .y = 60 + height / 4}; + Vect position2 = {.x = position.x + width/3, .y = 60 + height / 4}; if (fastest > 0) { //position.y += 40; @@ -630,6 +645,59 @@ void draw_level_time(SDL_Renderer *ren, struct environment * e) { } +void draw_pause_screen(SDL_Renderer *ren, struct environment *e) { + + char *te[] = { "GAME PAUSED.", + "controls", + "________", + "pause/unpause: esc", + "grapple: left click", + "grapple and pull: right click", + "go to level: g", + "mute/unmute: m", + "quit: q" + }; + + struct colour textc = e->colours.fg1; + struct colour bg = e->colours.bg; + + SDL_SetRenderDrawColor(ren, bg.r, bg.g, bg.b, 255); + int boxwidth = 360; + int boxheight = 348; + int box_x = (2 * width / 3) - (boxwidth / 2); + int box_y = (height - boxheight) / 2; + SDL_Rect r = {.w = boxwidth, .h = boxheight, .x = box_x, .y = box_y}; + SDL_RenderFillRect(ren, &r); + + Vect p = {.x = box_x + 10, .y = box_y + 10}; + draw_text(ren, p, textc, te[0]); + p.y += 60; + + draw_text(ren, p, textc, te[1]); + p.y += 5; + draw_text(ren, p, textc, te[2]); + p.y += 35; + + draw_text(ren, p, textc, te[3]); + p.y += 40; + + draw_text(ren, p, textc, te[4]); + p.y += 40; + + draw_text(ren, p, textc, te[5]); + p.y += 40; + + draw_text(ren, p, textc, te[6]); + p.y += 40; + + draw_text(ren, p, textc, te[7]); + p.y += 40; + + draw_text(ren, p, textc, te[8]); + p.y += 40; + +} + void redraw_buffer(SDL_Renderer * ren) { static int mousex = 0; static int mousey = 0; @@ -709,6 +777,13 @@ void redraw_buffer(SDL_Renderer * ren) { draw_collision_poly(ren, &lplayer); draw_forces(ren, &lplayer); + if (game_paused) { + SDL_Rect r = {.x = 0, .y = 0, .w = width, .h = height}; + SDL_SetRenderDrawColor(ren, 0, 0, 0, 90); + SDL_RenderFillRect(ren, &r); + if (in_game) + draw_pause_screen(ren, &scene); + } /*if (newmousex != mousex || newmousey != mousey) {*/ /*mousey = newmousey;*/ /*mousex = newmousex;*/ diff --git a/src/game.c b/src/game.c index f7d66e3..817357c 100644 --- a/src/game.c +++ b/src/game.c @@ -20,7 +20,8 @@ player_st *glob_player; SDL_Renderer *debug_ren; bool in_game; -bool game_paused; +bool game_paused = false; +extern bool mute; int level; int quality = 100; long level_time; @@ -717,6 +718,8 @@ void next_level() { viewport_pos.y = player.physics->position.y - height / 2; SDL_UnlockMutex(player.physics->lock); + + Mix_HaltChannel(-1); } int get_floor_ceiling() { @@ -974,7 +977,9 @@ void add_rope(int mouse_x, int mouse_y) { if (player.physics->strings[0].attached) { return; } - + if (game_paused || !in_game) { + return; + } Vect mouse; mouse.x = mouse_x; @@ -1621,7 +1626,6 @@ void startgame(SDL_Renderer * ren) { viewport_pos.x = player.physics->position.x - width / 2; viewport_pos.y = player.physics->position.y - height / 2; - //get_room(); } @@ -1646,6 +1650,7 @@ void handle_input_event(SDL_Event event) { static bool mouse_down = false; switch (event.type) { case SDL_KEYDOWN: +#ifdef DEBUGMODE if (sc == input_map.player_up) { walk_player(0, -1); } if ( sc == input_map.player_left) { @@ -1654,19 +1659,31 @@ void handle_input_event(SDL_Event event) { walk_player(0, 1); } if (sc == input_map.player_right) { walk_player(1, 0); - } if (sc == input_map.player_pull_rope) { - pull_rope(900); } +#endif + if (sc == input_map.player_pull_rope) { + pull_rope(900); + } if (sc == input_map.mute) { + mute = !mute; + } if (sc == input_map.pause) { + game_paused = !game_paused; + } break; case SDL_KEYUP: if (event.key.keysym.scancode == input_map.player_rope) { delete_rope(); - } if (sc == input_map.player_up || sc == input_map.player_down + } + +#ifdef DEBUGMODE + if (sc == input_map.player_up || sc == input_map.player_down || sc == input_map.player_left || sc == input_map.player_right) { set_motor_newtons(player.physics, M_PLAYER_WALK, 0, 0); set_motor_status(player.physics, M_PLAYER_WALK, false); - } if (sc == input_map.player_pull_rope) { + } +#endif + + if (sc == input_map.player_pull_rope) { stop_pull_rope(); } if (event.key.keysym.scancode == SDL_SCANCODE_F10) { @@ -1712,27 +1729,29 @@ int get_rand(int seed) { int step(void) { - static int first_run = 0; if (!first_run) { start_audio(); // play_game_sound(game_sounds.background, -1, BC_MUSIC); first_run = 1; + game_paused = 1; } if (player.physics->position.x > world.uniques_index[ROOM_W]->room ->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) { return 1; } - if (player.physics->position.x > world.uniques_index[ROOM_W]->room - ->floor.items[3]->position.x) { - level_timer_update(false); - } - if (in_game && !game_paused) { - advance_things(); - } + if (!game_paused) { + if (player.physics->position.x > world.uniques_index[ROOM_W]->room + ->floor.items[3]->position.x) { + level_timer_update(false); + } + if (in_game) { + advance_things(); + } + } return 0; } diff --git a/src/main.c b/src/main.c index f731ff3..f3994df 100644 --- a/src/main.c +++ b/src/main.c @@ -50,26 +50,21 @@ void redraw(struct SDL_Renderer * ren) { int physics_loop(void *ptr) { game_paused = 1; - SDL_Delay(1000); - game_paused = 0; while (1) { if (step()) { // display end level screen - game_paused = true; - SDL_Delay(1000); - Mix_Pause(-1); in_game = false; + game_paused = true; + SDL_Delay(300); SDL_SemWait(resume); add_time(level_time); SDL_LockMutex(player.physics->lock); - game_paused = true; next_level(); in_game = true; SDL_UnlockMutex(player.physics->lock); SDL_Delay(1000); game_paused = 0; - Mix_Resume(-1); } } } @@ -83,6 +78,7 @@ int game(void) { logwrite(INFO, "Starting\n"); SDL_Window * win = make_window(); SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); + SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND); // | SDL_RENDERER_PRESENTVSYNC); queue_for_cleanup(win, WINDOW); @@ -122,10 +118,10 @@ int game(void) { close = 1; return 0; case SDL_KEYDOWN: - if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { + if (event.key.keysym.scancode == SDL_SCANCODE_Q) { return 0; } - if (!SDL_SemValue(resume)) { + if (!in_game && !SDL_SemValue(resume)) { SDL_SemPost(resume); } case SDL_KEYUP: