-
Notifications
You must be signed in to change notification settings - Fork 55
/
Overlay.cpp
405 lines (349 loc) · 11.3 KB
/
Overlay.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "Overlay.h"
#include "TrackingModes.h"
#include "Hooks.h"
#include "GUI.h"
#include "modules/DFSDL.h"
#include "df/init.h"
#include "df/viewscreen_dwarfmodest.h"
//#include "df/viewscreen_dungeonmodest.h"
#include "df/adventurest.h"
using namespace std;
using namespace DFHack;
using namespace df::enums;
void Overlay::ReadTileLocations()
{
//fontx = df::global::init->font.small_font_dispx;
//fonty = df::global::init->font.small_font_dispy;
//actualWindowSize(width, height);
//DFHack::DFSDL_Surface * dfsurf = (DFHack::DFSDL_Surface *) SDL_GetVideoSurface();
//offsetx = fontx+((dfsurf->w) % fontx)/2;
//offsety = fontx+((dfsurf->h) % fonty)/2;
//if (!df::global::gamemode || *df::global::gamemode == game_mode::ADVENTURE)
//{
// //Adventure mode doesn't have a single-tile border around it.
// offsetx = offsetx - fontx;
// offsety = offsety - fonty;
//}
//ssState.ScreenW = fontx*width;
//ssState.ScreenH = fonty*height;
}
void Overlay::CheckViewscreen()
{
df::viewscreen * vs = Gui::getCurViewscreen();
virtual_identity * id = virtual_identity::get(vs);
if (id == &df::viewscreen_dwarfmodest::_identity)
{
good_viewscreen = true;
}
/* FIXME when adventure is back.
else if (id == &df::viewscreen_dungeonmodest::_identity)
{
if (!df::global::adventure)
good_viewscreen = true;
else if (df::global::adventure->menu == df::ui_advmode_menu::Default)
good_viewscreen = true;
else if (df::global::adventure->menu == df::ui_advmode_menu::ConversationAddress)
good_viewscreen = true;
else
good_viewscreen = false;
}
*/
else
{
good_viewscreen = false;
}
}
bool Overlay::PaintingOverTileAt(int32_t x, int32_t y)
{
if (!df::global::gamemode || *df::global::gamemode == game_mode::ADVENTURE)
return x >= 0 && x <= width && y >= 0 && y <= height;
else
return x > 0 && x <= width && y > 0 && y <= height;
}
void Overlay::set_to_null()
{
/*
screen = NULL;
screentexpos = NULL;
screentexpos_addcolor = NULL;
screentexpos_grayscale = NULL;
screentexpos_cf = NULL;
screentexpos_cbr = NULL;
screen_old = NULL;
screentexpos_old = NULL;
screentexpos_addcolor_old = NULL;
screentexpos_grayscale_old = NULL;
screentexpos_cf_old = NULL;
screentexpos_cbr_old = NULL;
*/
}
void Overlay::copy_from_inner()
{
/*
screen = parent->screen;
screentexpos = parent->screentexpos;
screentexpos_addcolor = parent->screentexpos_addcolor;
screentexpos_grayscale = parent->screentexpos_grayscale;
screentexpos_cf = parent->screentexpos_cf;
screentexpos_cbr = parent->screentexpos_cbr;
screen_old = parent->screen_old;
screentexpos_old = parent->screentexpos_old;
screentexpos_addcolor_old = parent->screentexpos_addcolor_old;
screentexpos_grayscale_old = parent->screentexpos_grayscale_old;
screentexpos_cf_old = parent->screentexpos_cf_old;
screentexpos_cbr_old = parent->screentexpos_cbr_old;
*/
}
void Overlay::copy_to_inner()
{
/*
parent->screen = screen;
parent->screentexpos = screentexpos;
parent->screentexpos_addcolor = screentexpos_addcolor;
parent->screentexpos_grayscale = screentexpos_grayscale;
parent->screentexpos_cf = screentexpos_cf;
parent->screentexpos_cbr = screentexpos_cbr;
parent->screen_old = screen_old;
parent->screentexpos_old = screentexpos_old;
parent->screentexpos_addcolor_old = screentexpos_addcolor_old;
parent->screentexpos_grayscale_old = screentexpos_grayscale_old;
parent->screentexpos_cf_old = screentexpos_cf_old;
parent->screentexpos_cbr_old = screentexpos_cbr_old;
*/
}
Overlay::Overlay(renderer* parent) : parent(parent)
{
{
CoreSuspender suspend;
//parent->zoom(df::zoom_commands::zoom_reset);
good_viewscreen = false;
ReadTileLocations();
copy_from_inner();
}
front_mutex = al_create_mutex();
int32_t flags = al_get_new_bitmap_flags();
if(al_get_current_display() != NULL){
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_ALPHA_TEST);
}
front = al_create_bitmap(0,0);
if(al_get_current_display() != NULL){
al_set_new_bitmap_flags(al_get_bitmap_flags(al_get_backbuffer(al_get_current_display())));
}
back = al_create_bitmap(0,0);
al_set_new_bitmap_flags(flags);
Flip();
};
Overlay::~Overlay()
{
{
CoreSuspender suspend;
df::global::enabler->renderer = parent;
}
al_destroy_mutex(front_mutex);
al_destroy_bitmap(front);
al_destroy_bitmap(back);
front = back = NULL;
if(al_get_current_display() == NULL){
al_set_target_bitmap(NULL);
} else {
al_set_target_bitmap(al_get_backbuffer(al_get_current_display()));
}
}
void Overlay::Flip()
{
//do the starting timer stuff
clock_t starttime = clock();
al_lock_mutex(front_mutex);
{
al_unlock_bitmap(front);
if(al_get_bitmap_width(front) != ssState.ScreenW
|| al_get_bitmap_height(front) != ssState.ScreenH){
al_destroy_bitmap(front);
int32_t flags = al_get_new_bitmap_flags();
if(al_get_current_display() != NULL){
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_ALPHA_TEST);
}
front = al_create_bitmap(ssState.ScreenW, ssState.ScreenH);
al_set_new_bitmap_flags(flags);
}
al_set_target_bitmap(front);
al_draw_bitmap(back, 0, 0, 0);
front_data = al_lock_bitmap(front,
al_get_bitmap_format(front), ALLEGRO_LOCK_READONLY);
}
front_updated = true;
al_unlock_mutex(front_mutex);
if(al_get_bitmap_width(back) != ssState.ScreenW
|| al_get_bitmap_height(back) != ssState.ScreenH){
al_destroy_bitmap(back);
int32_t flags = al_get_new_bitmap_flags();
if(al_get_current_display() != NULL){
al_set_new_bitmap_flags(al_get_bitmap_flags(al_get_backbuffer(al_get_current_display())));
}
back = al_create_bitmap(ssState.ScreenW, ssState.ScreenH);
al_set_new_bitmap_flags(flags);
}
al_set_target_bitmap(back);
//do the ending timer stuff
clock_t donetime = clock();
ssTimers.overlay_time = (donetime - starttime)*0.1 + ssTimers.overlay_time*0.9;
}
bool Overlay::GoodViewscreen()
{
return good_viewscreen;
}
void Overlay::update_tile(int32_t x, int32_t y)
{
//don't update tiles we are painting overtop of
if(!PaintingOverTileAt(x,y)){
copy_to_inner();
parent->update_tile(x,y);
}
}
void Overlay::update_all()
{
copy_to_inner();
parent->update_all();
front_updated = true;
}
void Overlay::render()
{
copy_to_inner();
al_lock_mutex(front_mutex);
CheckViewscreen();
ReadTileLocations();
if(good_viewscreen){
if(front_data != NULL && front_updated){
//allegro sometimes gives a negative pitch, which SDL doesn't understand, so take care of that case
int neg = 1;
int dataoffset = 0;
if(front_data->pitch < 0){
neg = -1;
dataoffset = (al_get_bitmap_height(front) - 1)*front_data->pitch;
}
//get the SDL surface information so we can do a blit
DFHack::DFSDL_Surface * dfsurf = (DFHack::DFSDL_Surface *) DFHack::DFSDL::DFSDL_GetVideoSurface();
DFHack::DFSDL_Surface * sssurf = (DFHack::DFSDL_Surface *) DFHack::DFSDL::DFSDL_CreateRGBSurfaceFrom( ((char*) front_data->data) + dataoffset,
al_get_bitmap_width(front), al_get_bitmap_height(front), 8*front_data->pixel_size, neg*front_data->pitch, 0, 0, 0, 0);
DFSDL_Rect src;
src.x = 0;
src.y = 0;
src.w = ssState.ScreenW;
src.h = ssState.ScreenH;
DFSDL_Rect pos;
pos.x = offsetx;
pos.y = offsety;
pos.w = 0;
pos.h = 0;
//do the blit
DFHack::DFSDL::DFSDL_UpperBlit(sssurf, &src, dfsurf, &pos);
DFHack::DFSDL::DFSDL_FreeSurface(sssurf);
}
front_updated = false;
} else {
width = 0;
height = 0;
}
al_unlock_mutex(front_mutex);
parent->render();
}
void Overlay::set_fullscreen()
{
copy_to_inner();
parent->set_fullscreen();
copy_from_inner();
}
void Overlay::zoom(df::zoom_commands z)
{
copy_to_inner();
parent->zoom(df::zoom_commands::zoom_reset);
//parent->zoom(z);
copy_from_inner();
//switch (z) {
//case df::zoom_commands::zoom_fullscreen:
// //???
// break;
//case df::zoom_commands::zoom_in:
// ssConfig.zoom++;
// ssConfig.scale = pow(2.0f, ssConfig.zoom);
// break;
//case df::zoom_commands::zoom_out:
// ssConfig.zoom--;
// ssConfig.scale = pow(2.0f, ssConfig.zoom);
// break;
//case df::zoom_commands::zoom_reset:
// ssConfig.zoom = 5;
// ssConfig.scale = pow(2.0f, ssConfig.zoom);
// break;
//case df::zoom_commands::zoom_resetgrid:
// //???
// break;
//}
}
void Overlay::resize(int32_t w, int32_t h)
{
copy_to_inner();
parent->resize(w,h);
copy_from_inner();
}
void Overlay::grid_resize(int32_t w, int32_t h)
{
copy_to_inner();
parent->grid_resize(w,h);
copy_from_inner();
}
bool Overlay::get_mouse_coords(int32_t* x, int32_t* y)
{
/*FIXME: Find a new replacement for the overlay mode.
//PrintMessage("in:(%i,%i)\n",*x,*y);
bool ret = parent->get_mouse_coords(x,y);
//PrintMessage("out:(%i,%i)\n",*x,*y);
//if we are painting over the tile, then we need to reroute through stonesense
if(ret && PaintingOverTileAt(*x,*y)){
int xpx, ypx, xpos, ypos, zpos;
SDL_GetMouseState(&xpx, &ypx);
xpx = xpx - offsetx;
ypx = ypx - offsety;
//first figure out which tile in the segment it came from
ScreenToPoint(xpx,ypx,xpos,ypos,zpos);
//then remove the segment rotation
correctForRotation(
xpos, ypos,
(4 - ssState.Rotation) % 4,
ssState.Size.x, ssState.Size.y);
//add on the segment offset
xpos = xpos + ssState.Position.x;
ypos = ypos + ssState.Position.y;
//zpos = zpos + ssState.Position.z - 1;
//remove the offset of the df window
int dfviewx, dfviewy, dfviewz;
Gui::getViewCoords(dfviewx, dfviewy, dfviewz);
xpos = xpos - dfviewx + 1;
ypos = ypos - dfviewy + 1;
//zpos = zpos - dfviewz;
if (!df::global::gamemode || *df::global::gamemode == game_mode::ADVENTURE)
{
//Adventure mode doesn't have a single-tile border around it.
xpos = xpos - 1;
ypos = ypos - 1;
}
//check to see if this new loaction is within the area we are painting over
//since we don't want to accidentally click somewhere in the interface
if(PaintingOverTileAt(xpos,ypos)) {
*x = xpos;
*y = ypos;
return true;
} else {
*x = -1;
*y = -1;
return false;
}
}
return ret;
*/
return false;
}
bool Overlay::uses_opengl()
{
return parent->uses_opengl();
}