-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimap.cpp
executable file
·137 lines (116 loc) · 4.15 KB
/
minimap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "main.hpp"
const int minimapX=13,
minimapY=582;
const int minimapWidth=188,
minimapHeight=176;
void ScreenView::generateMinimap()
{
unsigned sizeX = model->getSizeX(),
sizeY = model->getSizeY();
SDL_Surface *minimap = SDL_CreateRGBSurface(
SDL_SWSURFACE, sizeX, sizeY, 32,
textureFormat.Rmask, textureFormat.Gmask, textureFormat.Bmask, textureFormat.Amask);
unsigned char *pos = (unsigned char*)minimap->pixels;
for(unsigned yi=0; yi<sizeY; yi++)
for(unsigned xi=0; xi<sizeX; xi++)
{
Color c = model->getTile(xi,yi)->image().averageColor();
pos[0] = c.r;
pos[1] = c.g;
pos[2] = c.b;
pos[3] = 255;
pos += 4;
}
glGenTextures(1, &minimapTexID);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, minimapTexID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, //type of texture
4, //channels
sizeX, sizeY, //dimensions
GL_RGBA, //format
GL_UNSIGNED_BYTE, //channel type
minimap->pixels); //data
SDL_FreeSurface(minimap);
}
void ScreenView::drawMinimap()
{
// Draw terrain
glBindTexture(GL_TEXTURE_2D, minimapTexID);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(minimapX, minimapY );
glTexCoord2f(1.0, 0.0); glVertex2f(minimapX+minimapWidth, minimapY );
glTexCoord2f(1.0, 1.0); glVertex2f(minimapX+minimapWidth, minimapY+minimapHeight);
glTexCoord2f(0.0, 1.0); glVertex2f(minimapX, minimapY+minimapHeight);
glEnd();
}
void ScreenView::drawMinimapBox()
{
// Draw view rectangle
float boxTop, boxLeft;
float boxWidth, boxHeight;
if(mapView) {
boxLeft = minimapX + (float)minimapWidth * mapCameraX/model->getSizeX();
boxTop = minimapY + (float)minimapHeight * mapCameraY/model->getSizeY(),
boxWidth = (float)minimapWidth * (float)screenWidth / (float)mapViewTileSize / (float)model->getSizeX(),
boxHeight = (float)minimapHeight * (float)screenMainHeight / (float)mapViewTileSize / (float)model->getSizeY();
} else {
boxTop = minimapY + (float)minimapHeight * cameraY/model->getSizeY(),
boxLeft = minimapX + (float)minimapWidth * cameraX/model->getSizeX();
boxWidth = (float)minimapWidth * (float)screenWidth / (float)tileSize / (float)model->getSizeX(),
boxHeight = (float)minimapHeight * (float)screenMainHeight / (float)tileSize / (float)model->getSizeY();
}
if(boxWidth > minimapWidth) boxWidth = minimapWidth;
if(boxHeight > minimapHeight) boxHeight = minimapHeight;
float boxBottom = boxTop + boxHeight,
boxRight = boxLeft + boxWidth;
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f(0.0, 1.0, 0.0);
glLineWidth(1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(boxLeft, boxTop);
glVertex2f(boxRight, boxTop);
glVertex2f(boxRight, boxBottom);
glVertex2f(boxLeft, boxBottom);
glEnd();
}
bool ScreenView::isMinimapArea(int x, int y)
{
return x>=minimapX && y>=minimapY && x<(minimapX+minimapWidth) && y<(minimapY+minimapHeight);
}
bool ScreenView::mouseDownMinimap(int x, int y, int button, int mod)
{
if(button == SDL_BUTTON_LEFT)
{
draggingMinimap = true;
mouseDragMinimap(x, y);
}
else if(button == SDL_BUTTON_RIGHT)
{
float w_x=(x-minimapX)*((double)model->getSizeX()/minimapWidth),
w_y=(y-minimapY)*((double)model->getSizeY()/minimapWidth);
if(mod & KMOD_SHIFT) {
issueCommand(selection, Command::attack, w_x, w_y);
} else if(mod & KMOD_CTRL) {
mapView = !mapView;
centerCameraAt(w_x, w_y);
} else {
issueCommand(selection, Command::move, w_x, w_y);
}
}
return true;
}
void ScreenView::mouseDragMinimap(int x, int y)
{
if(draggingMinimap) {
centerCameraAt( (x-minimapX)*((double)model->getSizeX()/minimapWidth),
(y-minimapY)*((double)model->getSizeY()/minimapWidth) );
clipCamera();
}
}
void ScreenView::mouseUpMinimap(int x, int y, int button)
{
draggingMinimap = false;
}