diff --git a/src/map.c b/src/map.c index 7371372..30cbb22 100644 --- a/src/map.c +++ b/src/map.c @@ -10,16 +10,16 @@ #include "mission.h" #include "map.h" +#define MAP_WDWNAME "Star Map" #define WINDOW_WIDTH 650 #define WINDOW_HEIGHT 540 #define MAP_WIDTH (WINDOW_WIDTH-150) #define MAP_HEIGHT (WINDOW_HEIGHT-100) -#define BUTTON_WIDTH 60 -#define BUTTON_HEIGHT 40 +#define BUTTON_WIDTH 60 +#define BUTTON_HEIGHT 30 -static int map_wid = 0; static double map_zoom = 1.; /* Zoom of the map. */ static double map_xpos = 0.; /* Map position. */ static double map_ypos = 0.; @@ -37,17 +37,20 @@ extern int systems_nstack; extern int planet_target; extern int hyperspace_target; -static void map_update(void); +static void map_update(unsigned int wid); static int map_inPath(StarSystem* sys); static void map_render(double bx, double by, double w, double h); -static void map_mouse(SDL_Event* event, double mx, double my); -static void map_buttonZoom(char* str); +static void map_mouse(unsigned int wid, SDL_Event* event, double mx, double my); +static void map_buttonZoom(unsigned int wid, char* str); static void map_selectCur(void); /* Open the map window. */ void map_open(void) { - if(map_wid) { - map_close(); + unsigned int wid; + + wid = window_get(MAP_WDWNAME); + if(wid > 0) { + window_destroy(wid); return; } @@ -62,7 +65,7 @@ void map_open(void) { if(map_selected == -1) map_selectCur(); - map_wid = window_create("Star Map", -1, -1, WINDOW_WIDTH, WINDOW_HEIGHT); + wid = window_create("Star Map", -1, -1, WINDOW_WIDTH, WINDOW_HEIGHT); /* * SIDE TEXT @@ -87,43 +90,43 @@ void map_open(void) { */ /* System name. */ - window_addText(map_wid, -20, -20, 100, 20, 1, "txtSysname", + window_addText(wid, -20, -20, 100, 20, 1, "txtSysname", &gl_defFont, &cDConsole, systems_stack[map_selected].name); /* Faction. */ - window_addText(map_wid, -20, -60, 90, 20, 0, "txtSFaction", + window_addText(wid, -20, -60, 90, 20, 0, "txtSFaction", &gl_smallFont, &cDConsole, "Faction:"); - window_addText(map_wid, -20, -60-gl_smallFont.h-5, 80, 100, 0, "txtFaction", + window_addText(wid, -20, -60-gl_smallFont.h-5, 80, 100, 0, "txtFaction", &gl_smallFont, &cBlack, NULL); /* Standing. */ - window_addText(map_wid, -20, -100, 90, 20, 0, "txtSStanding", + window_addText(wid, -20, -100, 90, 20, 0, "txtSStanding", &gl_smallFont, &cDConsole, "Standing:"); - window_addText(map_wid, -20, -100-gl_smallFont.h-5, 80, 100, 0, "txtStanding", + window_addText(wid, -20, -100-gl_smallFont.h-5, 80, 100, 0, "txtStanding", &gl_smallFont, &cBlack, NULL); /* Planets. */ - window_addText(map_wid, -20, -140, 90, 20, 0, "txtSPlanets", + window_addText(wid, -20, -140, 90, 20, 0, "txtSPlanets", &gl_smallFont, &cDConsole, "Planets:"); - window_addText(map_wid, -20, -140-gl_smallFont.h-5, 80, 100, 0, "txtPlanets", + window_addText(wid, -20, -140-gl_smallFont.h-5, 80, 100, 0, "txtPlanets", &gl_smallFont, &cBlack, NULL); /* Services. */ - window_addText(map_wid, -20, -180, 90, 20, 0, "txtSServices", + window_addText(wid, -20, -180, 90, 20, 0, "txtSServices", &gl_smallFont, &cDConsole, "Services:"); - window_addText(map_wid, -20, -180-gl_smallFont.h-5, 80, 100, 0, "txtServices", + window_addText(wid, -20, -180-gl_smallFont.h-5, 80, 100, 0, "txtServices", &gl_smallFont, &cBlack, NULL); /* Close button. */ - window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, + window_addButton(wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnClose", "Close", (void(*)(char*))map_close); /* The map itself. */ - window_addCust(map_wid, 20, -40, MAP_WIDTH, MAP_HEIGHT, + window_addCust(wid, 20, -40, MAP_WIDTH, MAP_HEIGHT, "cstMap", 1, map_render, map_mouse); /* @@ -132,23 +135,16 @@ void map_open(void) { * [+] [-] Nebulae, Asteroids, Interference. */ /* Zoom buttons. */ - window_addButton(map_wid, 40, 20, 30, 30, "btnZoomIn", "+", map_buttonZoom); - window_addButton(map_wid, 80, 20, 30, 30, "btnZoomOut", "-", map_buttonZoom); + window_addButton(wid, 40, 20, 30, 30, "btnZoomIn", "+", map_buttonZoom); + window_addButton(wid, 80, 20, 30, 30, "btnZoomOut", "-", map_buttonZoom); /* Situation text. */ - window_addText(map_wid, 140, 10, WINDOW_WIDTH-80-30-30, 30, 0, + window_addText(wid, 140, 10, WINDOW_WIDTH-80-30-30, 30, 0, "txtSystemStatus", &gl_smallFont, &cBlack, NULL); - map_update(); + map_update(wid); } -void map_close(void) { - if(map_wid) { - window_destroy(map_wid); - map_wid = 0; - } -} - -static void map_update(void) { +static void map_update(unsigned int wid) { int i; StarSystem* sys; int f, y, h, standing, nstanding; @@ -156,7 +152,7 @@ static void map_update(void) { char buf[128]; /* Needs map to update. */ - if(map_wid <= 0) + if(!map_isOpen()) return; sys = &systems_stack[map_selected]; @@ -164,29 +160,29 @@ static void map_update(void) { /* Right text. */ if(!sys_isKnown(sys)) { /* System isn't known. Erase all. */ /* Right text. */ - window_modifyText(map_wid, "txtSysname", "Unknown"); - window_modifyText(map_wid, "txtFaction", "Unknown"); + window_modifyText(wid, "txtSysname", "Unknown"); + window_modifyText(wid, "txtFaction", "Unknown"); /* Standing. */ - window_moveWidget(map_wid, "txtSStanding", -20, -100); - window_moveWidget(map_wid, "txtStanding", -20, -100-gl_smallFont.h-5); - window_modifyText(map_wid, "txtStanding", "Unknown"); + window_moveWidget(wid, "txtSStanding", -20, -100); + window_moveWidget(wid, "txtStanding", -20, -100-gl_smallFont.h-5); + window_modifyText(wid, "txtStanding", "Unknown"); /* Planets. */ - window_moveWidget(map_wid, "txtSPlanets", -20, -140); - window_moveWidget(map_wid, "txtPlanets", -20, -140-gl_smallFont.h-5); - window_modifyText(map_wid, "txtPlanets", "Unknown"); + window_moveWidget(wid, "txtSPlanets", -20, -140); + window_moveWidget(wid, "txtPlanets", -20, -140-gl_smallFont.h-5); + window_modifyText(wid, "txtPlanets", "Unknown"); /* Services. */ - window_moveWidget(map_wid, "txtSServices", -20, -180); - window_moveWidget(map_wid, "txtServices", -20, -180-gl_smallFont.h-5); - window_modifyText(map_wid, "txtServices", "Unknown"); + window_moveWidget(wid, "txtSServices", -20, -180); + window_moveWidget(wid, "txtServices", -20, -180-gl_smallFont.h-5); + window_modifyText(wid, "txtServices", "Unknown"); /* Bottom text. */ - window_modifyText(map_wid, "txtSystemStatus", NULL); + window_modifyText(wid, "txtSystemStatus", NULL); return; } /* System is known. */ - window_modifyText(map_wid, "txtSysname", sys->name); + window_modifyText(wid, "txtSysname", sys->name); standing = 0; nstanding = 0; @@ -204,10 +200,10 @@ static void map_update(void) { } } if(f == -1) { - window_modifyText(map_wid, "txtFaction", "NA"); - window_moveWidget(map_wid, "txtSStanding", -20, -100); - window_moveWidget(map_wid, "txtStanding", -20, -100-gl_smallFont.h-5); - window_modifyText(map_wid, "txtStanding", "NA"); + window_modifyText(wid, "txtFaction", "NA"); + window_moveWidget(wid, "txtSStanding", -20, -100); + window_moveWidget(wid, "txtStanding", -20, -100-gl_smallFont.h-5); + window_modifyText(wid, "txtStanding", "NA"); y = -100; } else { if(i == sys->nplanets) @@ -215,20 +211,20 @@ static void map_update(void) { snprintf(buf, 100, "%s", faction_longname(f)); /* Modify the text. */ - window_modifyText(map_wid, "txtFaction", buf); - window_modifyText(map_wid, "txtStanding", + window_modifyText(wid, "txtFaction", buf); + window_modifyText(wid, "txtStanding", faction_getStanding(standing / nstanding)); /* Lower text if needed. */ h = gl_printHeight(&gl_smallFont, 80, buf); y = -100 - (h - gl_smallFont.h); - window_moveWidget(map_wid, "txtSStanding", -20, y); - window_moveWidget(map_wid, "txtStanding", -20, y-gl_smallFont.h-5); + window_moveWidget(wid, "txtSStanding", -20, y); + window_moveWidget(wid, "txtStanding", -20, y-gl_smallFont.h-5); } /* Get planets. */ if(sys->nplanets == 0) - window_modifyText(map_wid, "txtPlanets", "None"); + window_modifyText(wid, "txtPlanets", "None"); else { buf[0] = '\0'; if(sys->nplanets > 0) @@ -237,17 +233,17 @@ static void map_update(void) { strcat(buf, ",\n"); strcat(buf, sys->planets[i]->name); } - window_modifyText(map_wid, "txtPlanets", buf); + window_modifyText(wid, "txtPlanets", buf); } y -= 40; - window_moveWidget(map_wid, "txtSPlanets", -20, y); - window_moveWidget(map_wid, "txtPlanets", -20, y-gl_smallFont.h-5); + window_moveWidget(wid, "txtSPlanets", -20, y); + window_moveWidget(wid, "txtPlanets", -20, y-gl_smallFont.h-5); /* Get the services. */ h = gl_printHeight(&gl_smallFont, 80, buf); y -= 40 +(h - gl_smallFont.h); - window_moveWidget(map_wid, "txtSServices", -20, y); - window_moveWidget(map_wid, "txtServices", -20, y-gl_smallFont.h-5); + window_moveWidget(wid, "txtSServices", -20, y); + window_moveWidget(wid, "txtServices", -20, y-gl_smallFont.h-5); services = 0; for(i = 0; i < sys->nplanets; i++) services |= sys->planets[i]->services; @@ -260,7 +256,7 @@ static void map_update(void) { strcat(buf, "Shipyard\n"); if(buf[0] == '\0') strcat(buf, "None"); - window_modifyText(map_wid, "txtServices", buf); + window_modifyText(wid, "txtServices", buf); /* System status. */ buf[0] = '\0'; @@ -279,7 +275,7 @@ static void map_update(void) { strcat(buf, " Light"); strcat(buf, " Nebulae"); } - window_modifyText(map_wid, "txtSystemStatus", buf); + window_modifyText(wid, "txtSystemStatus", buf); } /** @@ -289,7 +285,7 @@ static void map_update(void) { * @return 0 if map is closed, non-zero if it's open. */ int map_isOpen(void) { - return (map_wid > 0); + return window_exists(MAP_WDWNAME); } /* Return 1 if sys is part of the map_path. */ @@ -411,7 +407,7 @@ static void map_render(double bx, double by, double w, double h) { } /* Map event handling. */ -static void map_mouse(SDL_Event* event, double mx, double my) { +static void map_mouse(unsigned int wid, SDL_Event* event, double mx, double my) { int i, j; double x, y, t; StarSystem* sys; @@ -424,9 +420,9 @@ static void map_mouse(SDL_Event* event, double mx, double my) { switch(event->type) { case SDL_MOUSEBUTTONDOWN: if(event->button.button == SDL_BUTTON_WHEELUP) - map_buttonZoom("btnZoomOut"); + map_buttonZoom(0, "btnZoomOut"); else if(event->button.button == SDL_BUTTON_WHEELDOWN) - map_buttonZoom("btnZoomIn"); + map_buttonZoom(0, "btnZoomIn"); /* Selecting star system. */ else { @@ -460,7 +456,7 @@ static void map_mouse(SDL_Event* event, double mx, double my) { break; } } - map_update(); + map_update(wid); break; } } @@ -482,7 +478,9 @@ static void map_mouse(SDL_Event* event, double mx, double my) { } } -static void map_buttonZoom(char* str) { +static void map_buttonZoom(unsigned int wid, char* str) { + (void)wid; + if(strcmp(str, "btnZoomIn")==0) { map_zoom += (map_zoom >= 1.) ? 0.5 : 0.25; map_zoom = MIN(2.5, map_zoom); @@ -494,6 +492,17 @@ static void map_buttonZoom(char* str) { } } +/** + * @brief Closes the map. + */ +void map_close(void) { + unsigned int wid; + + wid = window_get(MAP_WDWNAME); + if(wid > 0) + window_destroy(wid); +} + /* Set the map to sane defaults. */ void map_clear(void) { map_zoom = 1.; @@ -568,11 +577,14 @@ void map_jump(void) { * @param sys System to select. */ void map_select(StarSystem* sys) { + unsigned int wid; + + wid = window_get(MAP_WDWNAME); if(sys == NULL) map_selectCur(); else map_selected = sys - systems_stack; - map_update(); + map_update(wid); } /* A* Algorithm fo shortest path finding. */