[Add] Allow for star map zooming.

This commit is contained in:
Allanis 2013-03-10 18:53:15 +00:00
parent c96c1ec4f0
commit dac9bc367e

View File

@ -6,16 +6,17 @@
#include "map.h" #include "map.h"
#define WINDOW_WIDTH 550 #define WINDOW_WIDTH 550
#define WINDOW_HEIGHT 400 #define WINDOW_HEIGHT 440
#define MAP_WIDTH (WINDOW_WIDTH-150) #define MAP_WIDTH (WINDOW_WIDTH-150)
#define MAP_HEIGHT (WINDOW_HEIGHT-60) #define MAP_HEIGHT (WINDOW_HEIGHT-100)
#define BUTTON_WIDTH 60 #define BUTTON_WIDTH 60
#define BUTTON_HEIGHT 40 #define BUTTON_HEIGHT 40
static int map_wid = 0; static int map_wid = 0;
static double map_xpos = 0.; static double map_zoom = 1.; // Zoom of the map.
static double map_xpos = 0.; // Map position.
static double map_ypos = 0.; static double map_ypos = 0.;
static int map_selected = 0; static int map_selected = 0;
@ -33,6 +34,7 @@ static void map_close(char* str);
static void map_update(void); static void map_update(void);
static void map_render(double bx, double by, double w, double h); 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_mouse(SDL_Event* event, double mx, double my);
static void map_buttonZoom(char* str);
// Open the map window. // Open the map window.
void map_open(void) { void map_open(void) {
@ -62,12 +64,15 @@ void map_open(void) {
window_addText(map_wid, -20, -110-gl_smallFont.h-5, 80, 100, 0, "txtPlanets", window_addText(map_wid, -20, -110-gl_smallFont.h-5, 80, 100, 0, "txtPlanets",
&gl_smallFont, &cBlack, NULL); &gl_smallFont, &cBlack, NULL);
window_addCust(map_wid, 20, 20, MAP_WIDTH, MAP_HEIGHT, window_addCust(map_wid, 20, -40, MAP_WIDTH, MAP_HEIGHT,
"cstMap", 1, map_render, map_mouse); "cstMap", 1, map_render, map_mouse);
window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnClose", "Close", map_close); "btnClose", "Close", map_close);
window_addButton(map_wid, 40, 20, 30, 30, "btnZoomIn", "+", map_buttonZoom);
window_addButton(map_wid, 80, 20, 30, 30, "btnZoomOut", "-", map_buttonZoom);
map_update(); map_update();
} }
@ -130,8 +135,8 @@ static void map_render(double bx, double by, double w, double h) {
glColour* col; glColour* col;
r = 5.; r = 5.;
x = bx - map_xpos + w/2; x = (bx - map_xpos + w/2) * 1.; // Map zoom.
y = by - map_ypos + h/2; y = (by - map_ypos + h/2) * 1.; // Map zoom.
// Background // Background
COLOUR(cBlack); COLOUR(cBlack);
glBegin(GL_QUADS); glBegin(GL_QUADS);
@ -149,8 +154,8 @@ static void map_render(double bx, double by, double w, double h) {
if(sys == cur_system) COLOUR(cRadar_targ); if(sys == cur_system) COLOUR(cRadar_targ);
else if(sys->nplanets == 0) COLOUR(cInert); else if(sys->nplanets == 0) COLOUR(cInert);
else COLOUR(cYellow); else COLOUR(cYellow);
gl_drawCircleInRect(x + sys->pos.x, gl_drawCircleInRect(x + sys->pos.x*map_zoom,
y + sys->pos.y, y + sys->pos.y*map_zoom,
r, bx, by, w, h); r, bx, by, w, h);
// Draw the hyperspace paths. // Draw the hyperspace paths.
@ -166,18 +171,18 @@ static void map_render(double bx, double by, double w, double h) {
else col = &cInert; else col = &cInert;
glBegin(GL_LINE_STRIP); glBegin(GL_LINE_STRIP);
ACOLOUR(*col, 0.); ACOLOUR(*col, 0.);
tx = x + sys->pos.x; tx = x + sys->pos.x * map_zoom;
ty = y + sys->pos.y; ty = y + sys->pos.y * map_zoom;
if(!((tx < bx) || (tx > bx + w) || (ty < by) || (ty > by+h))) if(!((tx < bx) || (tx > bx + w) || (ty < by) || (ty > by+h)))
glVertex2d(tx, ty); glVertex2d(tx, ty);
COLOUR(*col); COLOUR(*col);
tx += (systems_stack[sys->jumps[j]].pos.x - sys->pos.x)/2.; tx += (systems_stack[sys->jumps[j]].pos.x - sys->pos.x)/2. * map_zoom;
ty += (systems_stack[sys->jumps[j]].pos.y - sys->pos.y)/2.; ty += (systems_stack[sys->jumps[j]].pos.y - sys->pos.y)/2. * map_zoom;
if(!((tx < bx) || (tx > bx+w) || (ty < by) || (ty > by+h))) if(!((tx < bx) || (tx > bx+w) || (ty < by) || (ty > by+h)))
glVertex2d(tx, ty); glVertex2d(tx, ty);
ACOLOUR(*col, 0.); ACOLOUR(*col, 0.);
tx += (systems_stack[sys->jumps[j]].pos.x - sys->pos.x)/2.; tx = x + systems_stack[sys->jumps[j]].pos.x * map_zoom;
ty += (systems_stack[sys->jumps[j]].pos.y - sys->pos.y)/2.; ty = y + systems_stack[sys->jumps[j]].pos.y * map_zoom;
if(!((tx < bx) || (tx > bx+w) || (ty < by) || (ty > by+h))) if(!((tx < bx) || (tx > bx+w) || (ty < by) || (ty > by+h)))
glVertex2d(tx, ty); glVertex2d(tx, ty);
glEnd(); glEnd();
@ -187,7 +192,8 @@ static void map_render(double bx, double by, double w, double h) {
// Selected planet. // Selected planet.
sys = &systems_stack[map_selected]; sys = &systems_stack[map_selected];
COLOUR(cRed); COLOUR(cRed);
gl_drawCircleInRect(x + sys->pos.x, y + sys->pos.y, r+3., bx, by, w, h); gl_drawCircleInRect(x + sys->pos.x*map_zoom, y + sys->pos.y*map_zoom,
r+3., bx, by, w, h);
} }
// Map event handling. // Map event handling.
@ -195,7 +201,7 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
int i, j; int i, j;
double x, y, t; double x, y, t;
t = 13.*13.; // Threshold. t = 13.*15.; // Threshold.
mx -= MAP_WIDTH/2 - map_xpos; mx -= MAP_WIDTH/2 - map_xpos;
my -= MAP_HEIGHT/2 - map_ypos; my -= MAP_HEIGHT/2 - map_ypos;
@ -205,8 +211,8 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
// Selecting star system. // Selecting star system.
if(event->button.button == SDL_BUTTON_LEFT) { if(event->button.button == SDL_BUTTON_LEFT) {
for(i = 0; i < systems_nstack; i++) { for(i = 0; i < systems_nstack; i++) {
x = systems_stack[i].pos.x; x = systems_stack[i].pos.x * map_zoom;
y = systems_stack[i].pos.y; y = systems_stack[i].pos.y * map_zoom;
if((pow2(mx-x)+pow2(my-y)) < t) { if((pow2(mx-x)+pow2(my-y)) < t) {
map_selected = i; map_selected = i;
@ -241,3 +247,10 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
} }
} }
static void map_buttonZoom(char* str) {
if(strcmp(str, "btnZoomIn")==0)
map_zoom = MIN(2., map_zoom+0.5);
else if(strcmp(str, "btnZoomOut")==0)
map_zoom = MAX(0.5, map_zoom-0.5);
}