1
1
Fork 0
Browse Source

pause menu

emscriptem
alistair 3 years ago
parent
commit
3a2a6acb2b
  1. 3
      src/audio.c
  2. 4
      src/controlscheme.c
  3. 3
      src/controlscheme.h
  4. 81
      src/draw.c
  5. 49
      src/game.c
  6. 14
      src/main.c

3
src/audio.c

@ -9,6 +9,7 @@ struct game_sounds game_sounds; @@ -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) { @@ -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;
}

4
src/controlscheme.c

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "controlscheme.h"
#include <SDL2/SDL_scancode.h>
struct InputMap input_map;
@ -10,5 +11,8 @@ void get_input_map(void) { @@ -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;
}

3
src/controlscheme.h

@ -17,6 +17,9 @@ struct InputMap { @@ -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;

81
src/draw.c

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "draw.h"
#include "game.h"
#include "types.h"
#include <SDL2/SDL_mutex.h>
#include <SDL2/SDL_render.h>
#include <time.h>
@ -530,10 +531,25 @@ int draw_end_screen(SDL_Renderer *ren) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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;*/

49
src/game.c

@ -20,7 +20,8 @@ player_st *glob_player; @@ -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() { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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;
}

14
src/main.c

@ -50,26 +50,21 @@ void redraw(struct SDL_Renderer * ren) { @@ -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) { @@ -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) { @@ -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:

Loading…
Cancel
Save