From 355ccfd65e92adda18e0d2161d0b53f380477924 Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 17 Mar 2014 01:56:39 +0000 Subject: [PATCH] [Add] 2x speed modifier by manipulating dt modifier. [Fix] Fixed double free on keybindings opening. --- src/input.c | 14 ++++++++++++-- src/lephisto.c | 14 ++++++++++---- src/options.c | 4 ++-- src/pause.c | 10 +++++++++- src/pause.h | 3 +++ src/player.c | 4 ++++ 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/input.c b/src/input.c index f10a479..d0160c5 100644 --- a/src/input.c +++ b/src/input.c @@ -48,7 +48,7 @@ const char* keybindNames[] = { /* Communication. */ "hail", /* Misc. */ - "mapzoomin", "mapzoomout", "screenshot", "pause", "menu", "info", + "mapzoomin", "mapzoomout", "screenshot", "pause", "speed", "menu", "info", "end" /* Must terminate at the end. */ }; @@ -79,17 +79,19 @@ const char* keybindDescription[] = { "Clears your escorts commands.", /* Space navigation. */ "Initializes the autonavigation system.", - "Cycles through hyperspace targets.", + "Cycles through planet targets.", "Attempt to land on your targetted planet or targets nearest landable planet. \ Requests for landing if you don't have permission yet.", "Cycles through hyperspace targets.", "Opens the Star Map.", + "Attempt to jump to your hyperspace target.", /* Communication. */ "Attempts to initialize communication with your taretted ship.", /* Misc. */ "Zooms in on your radar.", "Zooms out on your radar.", "Takes a screenshot.", + "Toggles 2x speed modifier.", "Pauses the game.", "Opens the small ingame menu.", "Opens the information menu." @@ -145,6 +147,7 @@ void input_setDefault(void) { input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, KMOD_NONE, 0); input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, KMOD_NONE, 0); input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, KMOD_NONE, 0); + input_setKeybind("speed", KEYBIND_KEYBOARD, SDLK_BACKQUOTE, KMOD_NONE, 0); input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, KMOD_NONE, 0); input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, KMOD_NONE, 0); } @@ -447,6 +450,13 @@ static void input_key(int keynum, double value, int kabs) { } else pause_game(); } } + /* Toggle speed mode. */ + else if(KEY("speed")) { + if(value == KEY_PRESS) { + if(dt_mod == 1.) pause_setSpeed(2.); + else pause_setSpeed(1.); + } + } /* Opens a menu. */ else if(KEY("menu")) { if(value == KEY_PRESS) menu_small(); diff --git a/src/lephisto.c b/src/lephisto.c index b4236b0..fa00116 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -452,9 +452,11 @@ static void fps_control(void) { unsigned int t; double delay; - /* dt in ms/1000. */ + /* dt in s. */ t = SDL_GetTicks(); - cur_dt = (double)(t-time)/1000.; + cur_dt = (double)(t - time); /* Get the elapsed ms. */ + cur_dt *= dt_mod; /* Apply the modifier. */ + cur_dt /= 1000.; /* Convert to seconds. */ time = t; if(paused) SDL_Delay(10); /* Drop paused FPS to be nice to the CPU. */ @@ -476,7 +478,7 @@ static const double fps_min = 1./50.0; static void update_all(void) { double tmpdt; - if(cur_dt > 0.25) { /* Slow timers down and rerun calculations */ + if(cur_dt > 0.25*dt_mod) { /* Slow timers down and rerun calculations */ pause_delay((unsigned int)cur_dt*1000.); return; } @@ -578,8 +580,12 @@ static void display_fps(const double dt) { x = 10.; y = (double)(gl_screen.h - 20); - if(show_fps) + if(show_fps) { gl_print(NULL, x, y, NULL, "%3.2f", fps); + y -= gl_defFont.h + 5.; + } + if(dt_mod != 1.) + gl_print(NULL, x, y, NULL, "%3.1fx", dt_mod); } /** diff --git a/src/options.c b/src/options.c index 7fb61fe..ace4c66 100644 --- a/src/options.c +++ b/src/options.c @@ -51,7 +51,7 @@ void opt_menuKeybinds(void) { /* Create the list. */ for(i = 0; strcmp(keybindNames[i], "end"); i++); - str = malloc(sizeof(char*) * (i-1)); + str = malloc(sizeof(char*) * i); for(j = 0; j < i; j++) { str[j] = malloc(sizeof(char) * 64); key = input_getKeybind(keybindNames[j], &type, &mod, &reverse); @@ -75,7 +75,7 @@ void opt_menuKeybinds(void) { } } window_addList(wid, 20, -40, 200, KEYBINDS_HEIGHT-60, "lstKeybinds", - str, i-1, 0, menuKeybinds_update); + str, i, 0, menuKeybinds_update); /* Update the list. */ menuKeybinds_update(wid, NULL); diff --git a/src/pause.c b/src/pause.c index c46020b..23bab3b 100644 --- a/src/pause.c +++ b/src/pause.c @@ -12,7 +12,8 @@ #include "pilot.h" #include "pause.h" -int paused = 0; /* Are we paused. */ +int paused = 0; /* Are we paused. */ +double dt_mod = 1.; /**< dt modifier. */ /* From pilot.c */ extern Pilot** pilot_stack; @@ -46,6 +47,13 @@ void pause_delay(unsigned int delay) { pilot_delay(delay); } +/** + * @brief Adjust the game's dt modifier. + */ +void pause_setSpeed(double mod) { + dt_mod = mod; +} + /* Pilots pausing/unpausing. */ static void pilot_pause(void) { int i, j; diff --git a/src/pause.h b/src/pause.h index b39d9f0..643d876 100644 --- a/src/pause.h +++ b/src/pause.h @@ -1,6 +1,9 @@ #pragma once extern int paused; +extern double dt_mod; + +void pause_setSpeed(double mod); void pause_game(void); void unpause_game(void); diff --git a/src/player.c b/src/player.c index deb2bba..72f0041 100644 --- a/src/player.c +++ b/src/player.c @@ -1551,6 +1551,10 @@ void player_abortAutonav(char* reason) { /* Get rid of acceleration. */ player_accelOver(); + /* Drop out of possible different speed modes. */ + if(dt_mod != 1.) + pause_setSpeed(1.); + /* Break possible hyperspacing. */ if(pilot_isFlag(player, PILOT_HYP_PREP)) { pilot_hyperspaceAbort(player);