From e37d373214ecac153de5ed4ee51256a252cd75b1 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 6 Jun 2014 16:52:35 +0100 Subject: [PATCH] [Change] Just some code cleanup. --- src/player.c | 118 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 48 deletions(-) diff --git a/src/player.c b/src/player.c index 50f6eb4..e18774d 100644 --- a/src/player.c +++ b/src/player.c @@ -87,30 +87,30 @@ static char** player_lstack = NULL; /**< Names of the planet the ships are a static int player_nstack = 0; /**< Number of ships player has. */ /* Player global properties. */ -char* player_name = NULL; /**< Player name. */ -double player_crating = 0; /**< Player combar rating. */ +char* player_name = NULL; /**< Player name. */ +double player_crating = 0; /**< Player combar rating. */ unsigned int player_flags = 0; /**< Player flags. */ /* Input.c */ -double player_left = 0.; /**< Player left turn velocity from input.c. */ -double player_right = 0.; /**< Player right turn velocity from input.c */ +double player_left = 0.; /**< Player left turn velocity from input.c. */ +double player_right = 0.; /**< Player right turn velocity from input.c */ static double player_acc = 0.; /**< Accel velocity from input. */ /* Internal */ -int planet_target = -1; /**< Targetted planet. -1 is none. */ +int planet_target = -1; /**< Targetted planet. -1 is none. */ int hyperspace_target = -1; /**< Target hyperspace route. -1 is none. */ /* For death etc. */ -static unsigned int player_timer = 0; /**< For death and such. */ +static double player_timer = 0; /**< For death and such. */ static Vec2 player_cam; /**< Again, for death etc. */ /* For interference. */ -static int interference_layer = 0; /**< Layer of the current interference. */ -double interference_alpha = 0.; /**< Alpha of the current interference layer. */ -static double interference_t = 0.; /**< Interference timer to control transitions. */ +static int interference_layer = 0; /**< Layer of the current interference. */ +double interference_alpha = 0.; /**< Alpha of the current interference layer. */ +static double interference_t = 0.; /**< Interference timer to control transitions. */ -static int* missions_done = NULL; /**< Saves position of completed missions. */ -static int missions_mdone = 0; /**< Memory size of completed missions. */ -static int missions_ndone = 0; /**< Number of completed missions. */ +static int* missions_done = NULL; /**< Saves position of completed missions. */ +static int missions_mdone = 0; /**< Memory size of completed missions. */ +static int missions_ndone = 0; /**< Number of completed missions. */ /* Pilot stuff for GUI. */ extern Pilot** pilot_stack; @@ -197,8 +197,8 @@ typedef struct GUI_ { } GUI; static GUI gui = { - .gfx_frame = NULL, - .gfx_targetPilot = NULL, + .gfx_frame = NULL, + .gfx_targetPilot = NULL, .gfx_targetPlanet = NULL }; /**< The GUI. */ @@ -208,7 +208,7 @@ double gui_yoff = 0.; /**< Y offset that GUI introduces. */ /* Messages. */ #define MSG_SIZE_MAX 120 /**< Max message length. */ -int msg_timeout = 5000; /**< How long it takes for a message to timeout. */ +double msg_timeout = 5.; /**< How long it takes for a message to timeout. */ int msg_max = 5; /**< Max messages on screen. */ /** @@ -217,10 +217,10 @@ int msg_max = 5; /**< Max messages on screen. */ * @brief On screen player message. */ typedef struct Msg_ { - char str[MSG_SIZE_MAX]; /**< The message. */ - unsigned int t; /**< Time of creation. */ + char str[MSG_SIZE_MAX]; /**< The message. */ + double t; /**< Time related to creation. */ } Msg; -static Msg* msg_stack; /**< Stack of messages, will be of mesg_max size. */ +static Msg* msg_stack; /**< Stack of messages, will be of msg_max size. */ /* External. */ extern void pilot_render(const Pilot* pilot); /* Extern is in Pilot.* */ @@ -243,6 +243,7 @@ static void rect_parse(const xmlNodePtr parent, static int gui_parse(const xmlNodePtr parent, const char* name); static void gui_cleanup(void); /* Render GUI. */ +static void gui_renderMessages(double dt); static void gui_renderPilot(const Pilot* p); static void gui_renderHealth(const glColour* c, const Rect* r, const glTexture* tex, const double w); @@ -540,7 +541,8 @@ void player_swapShip(char* shipname) { player_stack[i] = player; for(j = 0; j < pilot_nstack; j++) /* Find pilot in stack to swap. */ if(pilot_stack[j] == player) { - pilot_stack[j] = player = ship; + player = ship; + pilot_stack[j] = ship; break; } gl_bindCamera(&player->solid->pos); /* Let's not forget the camera. */ @@ -626,7 +628,7 @@ void player_cleanup(void) { memset(msg_stack[i].str, '\0', MSG_SIZE_MAX); /* Clean up the stack. */ - if(player_stack != NULL) { + if(player_nstack > 0) { for(i = 0; i < player_nstack; i++) { pilot_free(player_stack[i]); free(player_lstack[i]); @@ -640,7 +642,7 @@ void player_cleanup(void) { } /* Clean up missions. */ - if(missions_done != NULL) { + if(missions_ndone > 0) { free(missions_done); missions_done = NULL; missions_ndone = 0; @@ -726,7 +728,7 @@ void player_message(const char* fmt, ...) { vsprintf(msg_stack[0].str, fmt, ap); va_end(ap); - msg_stack[0].t = SDL_GetTicks() + msg_timeout; + msg_stack[0].t = msg_timeout; } /** @@ -881,24 +883,22 @@ static int can_jump = 0; /**< Store whether or not the player is able to jump. * */ void player_renderGUI(double dt) { int i, j; - double x, y; + double x; char str[10]; Pilot* p; - glColour* c, c2; + glColour* c; glFont* f; StarSystem* sys; - unsigned int t; int quantity, delay; - t = SDL_GetTicks(); - - /* Pilot is dead or being created, just render her and stop. */ + /* If player is dead, just render the cinematic mode. */ if(player_isFlag(PLAYER_DESTROYED) || player_isFlag(PLAYER_CREATING) || - pilot_isFlag(player, PILOT_DEAD)) { + ((player != NULL) && pilot_isFlag(player, PILOT_DEAD))) { if(player_isFlag(PLAYER_DESTROYED)) { + player_timer -= dt; if(!toolkit && !player_isFlag(PLAYER_CREATING) && - (t > player_timer)) { + (player_timer < 0.)) { menu_death(); } } @@ -1155,23 +1155,8 @@ void player_renderGUI(double dt) { NULL, "%d", pilot_cargoFree(player)); /* Messages. */ - x = gui.msg.x; - y = gui.msg.y + (double)(gl_defFont.h * msg_max)*1.2; - c2.r = c2.g = c2.b = 1.; - for(i = 0; i < msg_max; i++) { - y -= (double)gl_defFont.h*1.2; - if(msg_stack[msg_max-i-1].str[0] != '\0') { - if(msg_stack[msg_max-i-1].t < t) - msg_stack[msg_max-i-1].str[0] = '\0'; - else { - if(msg_stack[msg_max-i-1].t - msg_timeout/2 < t) - c2.a = (double)(msg_stack[msg_max-i-1].t - t) / (double)(msg_timeout/2); - else - c2.a = 1.; - gl_print(NULL, x, y, &c2, "%s", msg_stack[msg_max-i-1].str); - } - } - } + gui_renderMessages(dt); + /* Hyperspace FLASH BANG!!! */ if(pilot_isFlag(player, PILOT_HYPERSPACE) && (player->ptimer < HYPERSPACE_FADEOUT)) { @@ -1188,6 +1173,43 @@ void player_renderGUI(double dt) { } } +/** + * @brief Renders the players messages on screen. + * @param dt Current delta tick. + */ +static void gui_renderMessages(double dt) { + double x, y; + glColour c; + int i; + + x = gui.msg.x; + y = gui.msg.y + (double)(gl_defFont.h*msg_max)*1.2; + c.r = c.g = c.b = 1.; + + for(i = msg_max-1; i >= 0; i--) { + y -= (double)gl_defFont.h*1.2; + + /* Only handle non-NULL messages. */ + if(msg_stack[i].str[0] != '\0') { + /* Decrement timer. */ + msg_stack[i].t -= dt; + + /* Set to NULL if timer is up. */ + if(msg_stack[i].t < 0.) + msg_stack[i]..str[0] = '\0'; + + /* Draw with variable alpha. */ + else { + if(msg_stack[i].t - msg_timeout/2 < 0.) + c.a = msg_stack[i].t / (msg_timeout/2.); + else + c.a = 1.; + gl_print(NULL, x, y, &c, "%s", msg_stack[i].str); + } + } + } +} + /** * @brief Render interference if needed. * @param dt Current deltatick. @@ -2425,7 +2447,7 @@ void player_destroyed(void) { vectcpy(&player_cam, &player->solid->pos); gl_bindCamera(&player_cam); player_setFlag(PLAYER_DESTROYED); - player_timer = SDL_GetTicks() + 5000; + player_timer = 5.; } /**