forked from Enigma-Game/Enigma
-
Notifications
You must be signed in to change notification settings - Fork 2
/
video.cc
1052 lines (900 loc) · 37.8 KB
/
video.cc
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2002,2004 Daniel Heck
* Copyright (C) 2007 Ronald Lamprecht
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// This file contains the code for initializing the video hardware and for
// various visual effects in the widest sense. This includes
//
// a) managing and rendering the mouse cursor,
// b) changing between windowed and fullscreen mode,
// c) fading between different screen using one of a number of
// predefined effects.
//
// The code in this file is independent of the game. For game-specific display
// code, refer to file display.cc
#include "video.hh"
#include "ecl_video.hh"
#include "ecl_system.hh"
#include "lua.hh"
#include "main.hh"
#include "options.hh"
#include "resource_cache.hh"
#include <cassert>
#include <cstdio>
#include <sstream>
#include "config.h"
#ifdef __vita__
#include <vita2d.h>
#include "psp2_input.h"
#define SDL_PollEvent PSP2_PollEvent
#endif
#define SCREEN ecl::Screen::get_instance()
using namespace std;
using namespace ecl;
using namespace video;
using namespace enigma;
/* -------------------- Video Engine -------------------- */
namespace {
class Video_SDL {
SDL_Surface *sdlScreen;
string caption;
ecl::Screen *screen;
bool initialized;
public:
Video_SDL();
~Video_SDL();
bool init(int w, int h, int bpp, bool fullscreen);
void toggle_fullscreen();
void set_fullscreen(bool on_off);
bool is_fullscreen() const;
void set_caption(const char *str);
const string &get_caption() const { return caption; }
ecl::Screen *get_screen() { return screen; }
};
Video_SDL::Video_SDL() : sdlScreen(0), screen(0), initialized(false) {
}
Video_SDL::~Video_SDL() {
SDL_WM_GrabInput(SDL_GRAB_OFF);
delete screen;
}
void Video_SDL::set_caption(const char *str) {
caption = str;
if (initialized)
SDL_WM_SetCaption(str, 0);
}
bool Video_SDL::init(int w, int h, int bpp, bool fullscreen) {
#ifndef MACOSX
static bool firstInit = true;
if (firstInit) {
// Set the caption icon -- due to SDL doc it has to be set before first SDL_SetVideoMode()
// !!
// In praxis this SetIcon does not work for Linux, nor is it necessary for OSX.
// Just XP with selected "WindowsXP Design" needs this SetIcon.
// See video::Init() for icon set for Linux and other Windows versions
// Mac icon is set via Makefile
firstInit = false;
std::string iconpath;
ecl::Surface *es = NULL;
if (app.resourceFS->findFile("gfx/enigma_marble.png",
iconpath)) { // cannot use ecl::findImageFile !
es = ecl::LoadImage(iconpath.c_str());
if (es) {
SDL_WM_SetIcon(es->get_surface(), NULL);
}
}
}
#endif
SDL_WM_SetCaption(caption.c_str(), 0);
Uint32 flags = SDL_SWSURFACE;
if (fullscreen)
flags |= SDL_FULLSCREEN;
// Try to initialize vide mode, return error code on failure
sdlScreen = 0;
bpp = SDL_VideoModeOK(w, h, bpp, flags);
if (bpp == 0)
return false;
#ifdef __vita__
vita2d_texture_set_alloc_memblock_type(SCE_KERNEL_MEMBLOCK_TYPE_USER_RW);
sdlScreen = SDL_SetVideoMode(w, h, 16, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
float sh = (float) 544;
float sw = (float)w*((float)544/(float)h);
int x = (960-sw)/2;
int y = (544-sh)/2;
SDL_SetVideoModeScaling(x, y, sw, sh);
//This requires a recent SDL-Vita branch SDL12 for example
//https://github.com/rsn8887/SDL-Vita/tree/SDL12
//to compile
SDL_SetVideoModeBilinear(1);
SDL_SetVideoModeSync(1);
#else
sdlScreen = SDL_SetVideoMode(w, h, bpp, flags);
#endif
if (sdlScreen == 0)
return false;
// Video mode could be set
screen = new Screen(sdlScreen);
initialized = true;
// Hack to hide the cursor after switching between
// window/fullscreen mode.
SDL_ShowCursor(SDL_ENABLE);
SDL_ShowCursor(SDL_DISABLE);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2, SDL_DEFAULT_REPEAT_INTERVAL / 2);
return true;
}
bool Video_SDL::is_fullscreen() const {
if (sdlScreen)
return (sdlScreen->flags & SDL_FULLSCREEN) != 0;
return false;
}
void Video_SDL::set_fullscreen(bool on_off) {
if (on_off != is_fullscreen())
toggle_fullscreen();
}
void Video_SDL::toggle_fullscreen() {
SDL_WM_ToggleFullScreen(sdlScreen);
}
} // namespace
/* -------------------- MouseCursor -------------------- */
namespace {
class MouseCursor {
public:
MouseCursor();
~MouseCursor();
void set_image(ecl::Surface *s, int hotx_, int hoty_);
void move(int newx, int newy);
void redraw(); // Redraw if position/image changed
void draw(); // Draw cursor if visible
void show();
void hide();
Rect get_rect() const;
Rect get_oldrect() const;
bool has_changed() { return changedp; }
int get_x() const { return x; }
int get_y() const { return y; }
private:
// Private methods
void grab_bg();
void init_bg();
void restore_bg();
// Variables
Surface *background; // Copy of screen contents behind cursor
Surface *cursor; // Pixmap of the cursor
int x, y;
int oldx, oldy;
int hotx, hoty; // Coordinates of hotspot inside cursor image
int visible;
bool changedp;
};
MouseCursor::MouseCursor() : background(0), cursor(0) {
oldx = oldy = 0;
hotx = hoty = 0;
visible = 0;
changedp = true;
}
MouseCursor::~MouseCursor() {
delete background;
delete cursor;
}
void MouseCursor::set_image(ecl::Surface *s, int hx, int hy) {
delete cursor;
cursor = s;
hotx = hx;
hoty = hy;
if (visible > 0)
init_bg();
}
void MouseCursor::draw() {
if (visible > 0) {
grab_bg();
GC gc(SCREEN->get_surface());
blit(gc, x - hotx, y - hoty, cursor);
SCREEN->update_rect(get_rect());
changedp = false;
}
}
void MouseCursor::redraw() {
if (visible > 0 && changedp) {
restore_bg();
draw();
}
}
void MouseCursor::move(int newx, int newy) {
x = newx;
y = newy;
changedp = true;
}
void MouseCursor::show() {
if (++visible == 1) {
init_bg();
changedp = true;
}
}
void MouseCursor::hide() {
if (--visible == 0) {
changedp = true;
restore_bg();
delete background;
background = 0;
}
}
Rect MouseCursor::get_rect() const {
int scrx = x - hotx;
int scry = y - hoty;
return Rect(scrx, scry, cursor->width(), cursor->height());
}
Rect MouseCursor::get_oldrect() const {
int scrx = oldx - hotx;
int scry = oldy - hoty;
return Rect(scrx, scry, cursor->width(), cursor->height());
}
void MouseCursor::init_bg() {
assert(visible > 0);
assert(cursor != 0);
delete background;
background = ecl::MakeSurfaceLike(cursor->width(), cursor->height(), SCREEN->get_surface());
grab_bg();
}
void MouseCursor::grab_bg() {
assert(background != 0);
GC gc(background);
blit(gc, 0, 0, SCREEN->get_surface(), get_rect());
oldx = x;
oldy = y;
}
void MouseCursor::restore_bg() {
if (background) {
GC gc(SCREEN->get_surface());
blit(gc, oldx - hotx, oldy - hoty, background);
SCREEN->update_rect(get_oldrect());
}
}
} // namespace
/* -------------------- Local Variables -------------------- */
namespace {
Video_SDL *video_engine = 0;
MouseCursor *cursor = 0;
Surface *back_buffer = 0;
/*! List of available video modes. */
video::VMInfo video_modes[] = {{
VM_640x480, 640, 480, // id, w, h
32, VTS_32, // tilesize, tiletype
"640x480", "VGA", "4:3", // name, fsname, fs only
"models-32.lua", "gfx32/", // initscript, dir
Rect(0, 0, 640, 480), // display area
0, 0, // menu background image offsets
120, 78, 4, "-120x78", // thumbnail size/extension
Rect(0, 0, 640, 416), // game area
Rect(0, 416, 640, 64), // statusbar area
Rect(10, 428, 117, 43), // time area
Rect(100, 422, 28, 43), // modes area
Rect(100, 425, 30, 43), // moves area
Rect(188, 433, 490, 52), // inventory area
Rect(180, 436, 444, 35), // text area
0, // statusbar coffsety
true, true, // available window, fullscreen
"-0-", "-0-" // fallback modes window, fullscreen
},
{
VM_640x512, 640, 512, // id, w, h
32, VTS_32, // tilesize, tiletype
"640x512", "640x512", "5:4", // name, fsname, fs only
"models-32.lua", "gfx32/", // initscript, dir
Rect(0, 0, 640, 480), // display area
0, 0, // menu background image offsets
120, 78, 4, "-120x78", // thumbnail size/extension
Rect(0, 0, 640, 416), // game area
Rect(0, 416, 640, 64), // statusbar area
Rect(15, 420, 110, 40), // time area
Rect(100, 420, 31, 40), // modes area
Rect(100, 420, 30, 40), // moves area
Rect(200, 433, 490, 52), // inventory area
Rect(150, 434, 475, 35), // text area
0, // statusbar coffsety
false, false, // 640x512 is deprecated!
"-0-", "-0-" // fallback modes window, fullscreen
},
{
VM_800x600, 800, 600, // id, w, h
40, VTS_40, // tilesize, tiletype
"800x600", "SVGA", "4:3", // name, fsname, fs only
"models-40.lua", "gfx40/", // initscript, dir
Rect(0, 0, 800, 600), // display area
0, 0, // menu background image offsets
120, 78, 4, "-120x78", // thumbnail size/extension
Rect(0, 0, 800, 520), // game area
Rect(0, 520, 800, 80), // statusbar area
Rect(16, 540, 140, 40), // time area
Rect(140, 530, 31, 40), // modes area
Rect(140, 540, 30, 40), // moves area
Rect(235, 539, 610, 46), // inventory area
Rect(225, 547, 555, 39), // text area
0, // statusbar coffsety
true, true, // available window, fullscreen
"-2-0-", "-2-0-" // fallback modes window, fullscreen
},
{
VM_1024x768, 1024, 768, // id, w, h
48, VTS_48, // tilesize, tiletype
"1024x768", "XGA", "4:3", // name, fsname, fs only
"models-48.lua", "gfx48/", // initscript, dir
Rect(32, 0, 960, 720), // display area
-128, -96, // menu background image offsets
120, 78, 4, "-120x78", // thumbnail size/extension
Rect(32, 0, 960, 624), // game area
Rect(32, 624, 960, 96), // statusbar area
Rect(50, 640, 170, 60), // time area
Rect(198, 634, 34, 60), // modes area
Rect(185, 640, 30, 60), // moves area
Rect(314, 650, 710, 46), // inventory area
Rect(302, 655, 666, 40), // text area
0, // statusbar coffsety
true, true, // available window, fullscreen
"-3-2-0-", "-3-2-0-" // fallback modes window, fullscreen
},
{
VM_960x720, 960, 720, // id, w, h
48, VTS_48, // tilesize, tiletype
"960x720", "960x720", "4:3", // name, fsname, fs only
"models-48.lua", "gfx48/", // initscript, dir
Rect(0, 0, 960, 720), // display area
-192, -144, // menu background image offsets
120, 78, 4, "-120x78", // thumbnail size/extension
Rect(0, 0, 960, 624), // game area
Rect(0, 624, 960, 96), // statusbar area
Rect(18, 640, 170, 60), // time area
Rect(166, 636, 34, 60), // modes area
Rect(153, 640, 30, 60), // moves area
Rect(282, 650, 710, 46), // inventory area
Rect(270, 656, 666, 40), // text area
0, // statusbar coffsety
true, true, // available window, fullscreen
"-4-2-0-", "-4-2-0-" // fallback modes window, fullscreen
},
{
VM_1280x720, 1280, 720, // id, w, h
48, VTS_48, // tilesize, tiletype
"1280x720", "HD720", "16:10", // name, fsname, fs only
"models-48.lua", "gfx48/", // initscript, dir
Rect(160, 0, 960, 720), // display area
-192, -96, // menu background image offsets
120, 78, 4, "-120x78", // thumbnail size/extension
Rect(160, 0, 960, 624), // game area
Rect(160, 624, 960, 96), // statusbar area
Rect(178, 640, 170, 60), // time area
Rect(313, 640, 30, 60), // modes area
Rect(313, 640, 30, 60), // moves area
Rect(400, 650, 710, 46), // inventory area
Rect(388, 654, 710, 40), // text area
0, // statusbar coffsety
false, true, // available window, fullscreen
"-0-", "-5-0-" // fallback modes window, fullscreen
},
{
VM_1280x960, 1280, 960, // id, w, h
64, VTS_64, // tilesize, tiletype
"1280x960", "1280x960", "4:3", // name, fsname, fs only
"models-64.lua", "gfx64/", // initscript, dir
Rect(0, 0, 1280, 960), // display area
-400, -90, // menu background image offsets
160, 104, 5, "-160x104", // thumbnail size/extension
Rect(0, 0, 1280, 832), // game area
Rect(0, 832, 1280, 128), // statusbar area
Rect(24, 857, 227, 80), // time area //TODO
Rect(204, 853, 40, 80), // modes area //TODO
Rect(204, 853, 40, 80), // moves area //TODO
Rect(376, 867, 947, 61), // inventory area //TODO
Rect(360, 884, 888, 53), // text area //TODO
1, // statusbar coffsety
true, true, // available window, fullscreen
"-6-4-2-0-", "-6-4-2-0-" // fallback modes window, fullscreen
},
{
VM_1440x960, 1440, 960, // id, w, h
64, VTS_64, // tilesize, tiletype
"1440x960", "1440x960", "3:2", // name, fsname, fs only
"models-64.lua", "gfx64/", // initscript, dir
Rect(80, 0, 1280, 960), // display area
-400, -90, // menu background image offsets
160, 104, 5, "-160x104", // thumbnail size/extension
Rect(80, 0, 1280, 832), // game area
Rect(80, 832, 1280, 128), // statusbar area
Rect(104, 853, 227, 80), // time area //TODO
Rect(284, 853, 40, 80), // modes area //TODO
Rect(284, 853, 40, 80), // moves area //TODO
Rect(480, 867, 947, 61), // inventory area //TODO
Rect(384, 873, 947, 53), // text area //TODO
0, // statusbar coffsety
false, true, // available window, fullscreen
"-0-", "-7-0-" // fallback modes window, fullscreen
},
{
VM_1280x1024, 1280, 1024, // id, w, h
64, VTS_64, // tilesize, tiletype
"1280x1024", "SXGA", "5:4", // name, fsname, fs only
"models-64.lua", "gfx64/", // initscript, dir
Rect(0, 0, 1280, 960), // display area
-400, -26, // menu background image offsets
160, 104, 5, "-160x104", // thumbnail size/extension
Rect(0, 0, 1280, 832), // game area
Rect(0, 832, 1280, 128), // statusbar area
Rect(18, 640, 170, 60), // time area //TODO
Rect(153, 640, 30, 60), // modes area //TODO
Rect(153, 640, 30, 60), // moves area //TODO
Rect(324, 650, 710, 46), // inventory area //TODO
Rect(360, 655, 888, 40), // text area //TODO
0, // statusbar coffsety
false, true, // available window, fullscreen
"-0-", "-8-0-" // fallback modes window, fullscreen
},
{
VM_1680x1050, 1680, 1050, // id, w, h
64, VTS_64, // tilesize, tiletype
"1680x1050", "WSXGA+", "16:10", // name, fsname, fs only
"models-64.lua", "gfx64/", // initscript, dir
Rect(200, 0, 1280, 960), // display area
-0, -0, // menu background image offsets
160, 104, 5, "-160x104", // thumbnail size/extension
Rect(200, 0, 1280, 832), // game area
Rect(200, 832, 1280, 128), // statusbar area
Rect(224, 853, 227, 80), // time area //TODO
Rect(404, 853, 40, 80), // modes area //TODO
Rect(404, 853, 40, 80), // moves area //TODO
Rect(600, 867, 947, 61), // inventory area //TODO
Rect(504, 873, 947, 53), // text area //TODO
0, // statusbar coffsety
false, true, // available window, fullscreen
"-0-", "-9-0-" // fallback modes window, fullscreen
},
{
VM_320x240, 320, 240, // id, w, h
16, VTS_16, // tilesize, tiletype
"320x240", "CGA", "4:3", // name, fsname, fs only
"models-16.lua", "gfx16/", // initscript, dir
Rect(0, 0, 320, 240), // display area
0, 0, // menu background image offsets
60, 39, 2, "-60x39", // thumbnail size/extension
Rect(0, 0, 320, 208), // game area
Rect(0, 208, 320, 32), // statusbar area
Rect(3, 212, 65, 21), // time area
Rect(52, 210, 14, 21), // modes area
Rect(50, 220, 15, 21), // moves area
Rect(94, 216, 245, 26), // inventory area
Rect(90, 217, 222, 17), // text area
0, // statusbar coffsety
true, true, // available window, fullscreen
"-10-0-", "-10-0-" // fallback modes window, fullscreen
}};
VideoModes current_video_mode = VM_None;
} // namespace
/* -------------------- Auxiliary functions -------------------- */
namespace {
bool vm_available(int w, int h, int &bpp, bool fullscreen) {
Uint32 flags = SDL_HWSURFACE;
if (fullscreen)
flags |= SDL_FULLSCREEN;
int newbpp = SDL_VideoModeOK(w, h, bpp, flags);
if (newbpp != 0) {
bpp = newbpp;
return true;
}
return false;
}
// This function is installed as an event filter by video::Init. It intercepts
// mouse motions, which are used to update the position of the mouse cursor
// (but passed on to the event queue).
int event_filter(const SDL_Event *e) {
if (e->type == SDL_MOUSEMOTION) {
cursor->move(e->motion.x, e->motion.y);
cursor->redraw();
}
return 1;
}
} // namespace
/* -------------------- Functions -------------------- */
void video::SetMouseCursor(ecl::Surface *s, int hotx, int hoty) {
cursor->set_image(s, hotx, hoty);
cursor->redraw();
}
void video::HideMouse() {
cursor->hide();
cursor->redraw();
}
void video::ShowMouse() {
cursor->show();
cursor->redraw();
}
int video::Mousex() {
return cursor->get_x();
}
int video::Mousey() {
return cursor->get_y();
}
/* -------------------- Input grabbing -------------------- */
TempInputGrab::TempInputGrab(bool onoff) {
old_onoff = SetInputGrab(onoff);
}
TempInputGrab::~TempInputGrab() {
SetInputGrab(old_onoff);
}
bool video::GetInputGrab() {
return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON;
}
bool video::SetInputGrab(bool onoff) {
bool old_onoff = GetInputGrab();
if (onoff) {
Screen *screen = GetScreen();
SDL_WarpMouse(screen->width() / 2, screen->height() / 2);
SDL_Event e;
while (SDL_PollEvent(&e)) {
; // swallow mouse motion event
}
SDL_WM_GrabInput(SDL_GRAB_ON);
} else
SDL_WM_GrabInput(SDL_GRAB_OFF);
return old_onoff;
}
Surface *video::BackBuffer() {
if (back_buffer == 0) {
back_buffer = Duplicate(SCREEN->get_surface());
}
return back_buffer;
}
const video::VMInfo *video::GetInfo(VideoModes vm) {
return &video_modes[vm];
}
const video::VMInfo *video::GetInfo() {
return GetInfo(current_video_mode);
}
void video::SetThumbInfo(int width, int height, std::string extension) {
video_modes[current_video_mode].thumbw = width;
video_modes[current_video_mode].thumbh = height;
video_modes[current_video_mode].thumbsext = extension;
}
int video::GetNumAvailableModes(bool isFullscreen) {
int avail = 0;
for (int i = VM_None + 1; i < VM_COUNT; i++)
if (isFullscreen ? video_modes[i].f_available : video_modes[i].w_available)
avail++;
return avail;
}
VideoModes video::GetVideoMode(int number, bool isFullscreen) {
int avail = 0;
for (int i = VM_None + 1; i < VM_COUNT; i++)
if (isFullscreen ? video_modes[i].f_available : video_modes[i].w_available) {
if (avail == number)
return static_cast<video::VideoModes>(i);
else
avail++;
}
return VM_None;
}
VideoModes video::GetBestUserMode(bool isFullscreen, int seq) {
std::string modes =
app.prefs->getString(isFullscreen ? "VideoModesFullscreen" : "VideoModesWindow");
if (modes.length() > 1) {
std::istringstream ms(modes);
int m = VM_None;
ms.ignore(); // leading '-'
while (ms && seq > 0) {
ms >> std::dec >> m;
ms.ignore();
if (m <= VM_COUNT && ((!isFullscreen && video_modes[m].w_available) ||
(isFullscreen && video_modes[m].f_available))) {
if (seq == 1) {
return static_cast<video::VideoModes>(m);
}
--seq;
}
}
}
return VM_None;
}
int video::GetModeNumber(VideoModes mode, bool isFullscreen) {
int avail = 0;
for (int i = VM_None + 1; i < mode; i++)
if (isFullscreen ? video_modes[i].f_available : video_modes[i].w_available)
avail++;
return avail;
}
bool video::ModeAvailable(VideoModes vm) {
const VMInfo *vminfo = GetInfo(vm);
string fname;
return (vminfo->w_available && app.systemFS->findFile(vminfo->initscript, fname));
}
void video::Init() {
static bool isInit = false;
static int bpp_default = 16;
int bpp = bpp_default;
if (!isInit) {
assert(NUMENTRIES(video_modes) == VM_COUNT);
isInit = true;
for (int i = VM_None + 1; i < VM_COUNT; i++) {
bpp = bpp_default;
if (video_modes[i].w_available &&
!vm_available(video_modes[i].width, video_modes[i].height, bpp, false)) {
video_modes[i].w_available = false;
Log << "Video mode " << video_modes[i].width << " x " << video_modes[i].height
<< " window not available\n";
}
if (bpp != bpp_default)
Log << "Video mode " << video_modes[i].width << " x " << video_modes[i].height
<< " window available with bpp " << bpp << "\n";
bpp = bpp_default;
if (video_modes[i].f_available &&
!vm_available(video_modes[i].width, video_modes[i].height, bpp, true)) {
video_modes[i].f_available = false;
Log << "Video mode " << video_modes[i].width << " x " << video_modes[i].height
<< " fullscreen not available\n";
}
if (bpp != bpp_default)
Log << "Video mode " << video_modes[i].width << " x " << video_modes[i].height
<< " fullscreen available with bpp " << bpp << "\n";
}
}
bool isFullScreen = app.prefs->getBool("FullScreen");
int vidmode = -1;
if (app.prefs->getString("VideoModesFullscreen").empty()) {
// initialize from 1.0 mode if never set before
vidmode = app.prefs->getInt("VideoMode");
app.prefs->setProperty("VideoModesFullscreen", video_modes[vidmode].fallback_fullscreen);
app.prefs->setProperty("VideoModesWindow", video_modes[vidmode].fallback_window);
}
vidmode = video::GetBestUserMode(isFullScreen);
video_engine = new Video_SDL();
assert(bpp == 16 || bpp == 32);
int fallback_sequence = 1;
while (true) {
VMInfo *vminfo = &video_modes[vidmode];
int w = vminfo->width;
int h = vminfo->height;
if (ModeAvailable(static_cast<VideoModes>(vidmode)) &&
vm_available(w, h, bpp, isFullScreen) && video_engine->init(w, h, bpp, isFullScreen)) {
// Success!
break;
}
// Video mode not available? Try the next fallback video mode
vidmode = video::GetBestUserMode(isFullScreen, ++fallback_sequence);
if (vidmode == VM_None) {
// Give up :-(
fprintf(stderr, "Couldn't open screen: %s\n", SDL_GetError());
exit(1);
}
}
current_video_mode = static_cast<VideoModes>(vidmode);
app.selectedVideoMode = current_video_mode;
// Set the caption icon -- this position after SDL_SetVideoMode() contradicts SDL doc!!
// But it proves to be the only working position for Linux.
// It works for Windows besides XP with selected "WindowsXP Design", too.
// Mac icon is set via Makefile
#ifndef MACOSX
Surface *icn = enigma::GetImage("enigma_marble");
if (icn)
SDL_WM_SetIcon(icn->get_surface(), NULL);
#endif
cursor = new MouseCursor;
int x, y;
SDL_GetMouseState(&x, &y);
cursor->move(x, y);
SDL_SetEventFilter(event_filter);
UpdateGamma();
}
void video::Shutdown() {
SDL_SetEventFilter(0);
delete video_engine;
delete cursor;
delete back_buffer;
video_engine = 0;
cursor = 0;
back_buffer = 0;
}
void video::ChangeVideoMode() {
MouseCursor *oldcursor = cursor;
cursor = 0;
Shutdown();
Init();
delete cursor;
cursor = oldcursor;
}
ecl::Screen *video::GetScreen() {
return SCREEN;
}
VideoModes video::GetVideoMode() {
return current_video_mode;
}
bool video::IsFullScreen() {
return video_engine->is_fullscreen();
}
int video::GetColorDepth() {
return SCREEN->get_surface()->bipp();
}
bool video::SetFullscreen(bool on) {
if ((on && video_modes[current_video_mode].f_available) ||
(!on && video_modes[current_video_mode].w_available)) {
video_engine->set_fullscreen(on);
bool is_fullscreen = video_engine->is_fullscreen();
if (on == is_fullscreen) {
app.prefs->setProperty("FullScreen", is_fullscreen);
}
}
return video::IsFullScreen();
}
bool video::ToggleFullscreen() {
return SetFullscreen(!video_engine->is_fullscreen());
}
void video::SetCaption(const char *str) {
video_engine->set_caption(str);
}
const string &video::GetCaption() {
return video_engine->get_caption();
}
void video::UpdateGamma() {
float gamma = static_cast<float>(app.prefs->getDouble("Gamma"));
if (gamma < 0.25)
gamma = 0.25; // Windows does not set gamma for values < 0.2271
SDL_SetGamma(gamma, gamma, gamma);
}
void video::Screenshot(const std::string &fname) {
// auto-create the directory if necessary
string directory;
if (ecl::split_path(fname, &directory, 0) && !ecl::FolderExists(directory)) {
ecl::FolderCreate(directory);
}
ecl::SavePNG(ecl::Grab(SCREEN->get_surface(), video_modes[current_video_mode].area), fname);
enigma::Log << "Wrote screenshot to '" << fname << "'\n";
}
/* -------------------- Special Effects -------------------- */
void video::FX_Fade(FadeMode mode) {
ecl::Screen *screen = ecl::Screen::get_instance();
Surface *d = screen->get_surface();
const double fadesec = 0.6;
double v = 255 / fadesec;
ecl::Surface *buffer = Duplicate(d);
double dt;
double a = mode == FADEIN ? 0 : 255;
GC gc(d);
while (true) {
Uint32 otime = SDL_GetTicks();
box(gc, d->size());
buffer->set_alpha(int(a));
blit(gc, 0, 0, buffer);
screen->update_all();
screen->flush_updates();
dt = (SDL_GetTicks() - otime) / 1000.0;
if ((mode == FADEIN && (a += v * dt) > 255) || (mode == FADEOUT && (a -= v * dt) < 0))
break;
}
if (mode == FADEIN) {
buffer->set_alpha(255);
blit(gc, 0, 0, buffer);
} else {
box(gc, d->size());
}
screen->update_all();
screen->flush_updates();
delete buffer;
}
void video::FX_Fly(Surface *newscr, int originx, int originy) {
double rest_time = 0.5;
double velx = -originx / rest_time;
double vely = -originy / rest_time;
double origx = originx;
double origy = originy;
Screen *scr = SCREEN;
GC scrgc(scr->get_surface());
while (rest_time > 0) {
Uint32 otime = SDL_GetTicks();
Rect r(static_cast<int>(origx), static_cast<int>(origy), scr->width(), scr->height());
blit(scrgc, r.x, r.y, newscr);
scr->update_rect(r);
scr->flush_updates();
double dt = (SDL_GetTicks() - otime) / 1000.0;
if (dt > rest_time)
dt = rest_time;
rest_time -= dt;
origx += velx * dt;
origy += vely * dt;
}
}
namespace {
class Effect_Push : public TransitionEffect {
public:
Effect_Push(ecl::Surface *newscr, int originx, int originy);
void tick(double dtime);
bool finished() const;
private:
double rest_time;
ecl::Surface *newscr;
std::unique_ptr<ecl::Surface> oldscr;
int originx, originy;
double velx, vely;
double accx, accy;
double x, y;
double t;
};
Effect_Push::Effect_Push(ecl::Surface *newscr_, int originx_, int originy_)
: rest_time(0.7),
newscr(newscr_),
oldscr(Duplicate(SCREEN->get_surface())),
originx(originx_),
originy(originy_),
velx(-2 * originx / rest_time),
vely(-2 * originy / rest_time),
accx(-0.5 * velx / rest_time),
accy(-0.5 * vely / rest_time),
x(originx),
y(originy),
t(0) {
}
void Effect_Push::tick(double dtime) {
Screen *scr = SCREEN;
GC scrgc(scr->get_surface());
if (rest_time > 0) {
if (dtime > rest_time)
dtime = rest_time;
rest_time -= dtime;
t += dtime;
x = (accx * t + velx) * t + originx;
y = (accy * t + vely) * t + originy;
blit(scrgc, (int)x - originx, (int)y, oldscr.get());
blit(scrgc, (int)x, (int)y - originy, oldscr.get());
blit(scrgc, (int)x - originx, (int)y - originy, oldscr.get());
blit(scrgc, (int)x, (int)y, newscr);