/
wzapp.cpp
1298 lines (1127 loc) · 36.8 KB
/
wzapp.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
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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2011 Warzone 2100 Project
Warzone 2100 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.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** @file wzapp.cpp
* Qt-related functions.
*/
#include <QtGui/QImageReader>
#include <QtGui/QBitmap>
#include <QtGui/QPainter>
#include <QtGui/QMouseEvent>
#include <QtGui/QDesktopWidget>
#include <QtGui/QMessageBox>
#include <QtGui/QIcon>
// Get platform defines before checking for them.
// Qt headers MUST come before platform specific stuff!
#include "wzapp.h"
#if defined(WZ_CC_MSVC)
#include "wzapp.moc.h" // this is generated on the pre-build event.
#endif
#include "lib/exceptionhandler/dumpinfo.h"
#include "file.h"
#include "configfile.h"
#include "lib/ivis_opengl/piestate.h"
#include "lib/ivis_opengl/pieclip.h"
#include "lib/ivis_opengl/screen.h"
#include "wzapp_c.h"
#include "src/main.h"
#include "src/configuration.h"
#include "lib/gamelib/gtime.h"
#include <deque>
#include "wzfs.h"
class PhysicsEngineHandler : public QAbstractFileEngineHandler
{
public:
QAbstractFileEngine *create(const QString &fileName) const;
};
inline QAbstractFileEngine *PhysicsEngineHandler::create(const QString &fileName) const
{
if (fileName.toLower().startsWith("wz::"))
{
QString newPath = fileName;
return new PhysicsFileSystem(newPath.remove(0, 4));
}
return NULL;
}
/* The possible states for keys */
typedef enum _key_state
{
KEY_UP,
KEY_PRESSED,
KEY_DOWN,
KEY_RELEASED,
KEY_PRESSRELEASE, ///< When a key goes up and down in a frame
KEY_DOUBLECLICK, ///< Only used by mouse keys
KEY_DRAG, ///< Only used by mouse keys
} KEY_STATE;
typedef struct _input_state
{
KEY_STATE state; ///< Last key/mouse state
UDWORD lastdown; ///< last key/mouse button down timestamp
Vector2i pressPos; ///< Location of last mouse press event.
Vector2i releasePos; ///< Location of last mouse release event.
} INPUT_STATE;
/// constant for the interval between 2 singleclicks for doubleclick event in ms
#define DOUBLE_CLICK_INTERVAL 250
/** The current state of the keyboard */
static INPUT_STATE aKeyState[KEY_MAXSCAN];
/** How far the mouse has to move to start a drag */
#define DRAG_THRESHOLD 5
/** Which button is being used for a drag */
static MOUSE_KEY_CODE dragKey;
/** The start of a possible drag by the mouse */
static SDWORD dragX, dragY;
/** The current mouse button state */
static INPUT_STATE aMouseState[6];
/** The size of the input buffer */
#define INPUT_MAXSTR 512
/** The input string buffer */
struct InputKey
{
InputKey(UDWORD key_ = 0, utf_32_char unicode_ = 0) : key(key_), unicode(unicode_) {}
UDWORD key;
utf_32_char unicode;
};
static std::deque<InputKey> inputBuffer;
static QColor fontColor;
static uint16_t mouseXPos = 0, mouseYPos = 0;
static CURSOR lastCursor = CURSOR_ARROW;
static bool crashing = false;
unsigned int screenWidth = 0;
unsigned int screenHeight = 0;
static void inputAddBuffer(UDWORD key, utf_32_char unicode);
static int WZkeyToQtKey(int code);
/*!
* The mainloop.
* Fetches events, executes appropriate code
*/
void WzMainWindow::tick()
{
updateGL(); // Calls paintGL(), which may not be called directly.
}
void WzMainWindow::loadCursor(CURSOR cursor, int x, int y, QImageReader &buffer)
{
buffer.device()->reset();
buffer.setClipRect(QRect(x, y, 32, 32));
cursors[cursor] = new QCursor(QPixmap::fromImage(buffer.read()));
}
WzMainWindow::WzMainWindow(const QGLFormat &format, QWidget *parent) : QGLWidget(format, parent)
{
myself = this;
notReadyToPaint = true;
timer = new QTimer(this);
tickCount.start();
connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
for (int i = 0; i < CURSOR_MAX; cursors[i++] = NULL) ;
timer->start(0);
setAutoFillBackground(false);
setAutoBufferSwap(false);
setMouseTracking(true);
setWindowIcon(QIcon(QPixmap::fromImage(QImage("wz::images/warzone2100.png", "PNG"))));
QImageReader buffer("wz::images/intfac5.png", "PNG");
if (!buffer.canRead())
{
debug(LOG_ERROR, "Failed to read cursor image: %s", buffer.errorString().toAscii().constData());
}
loadCursor(CURSOR_EMBARK, 0, 128, buffer);
loadCursor(CURSOR_DEST, 32, 128, buffer);
loadCursor(CURSOR_DEFAULT, 64, 128, buffer);
loadCursor(CURSOR_BUILD, 96, 128, buffer);
loadCursor(CURSOR_SCOUT, 128, 128, buffer);
loadCursor(CURSOR_DISEMBARK, 160, 128, buffer);
loadCursor(CURSOR_ATTACK, 192, 128, buffer);
loadCursor(CURSOR_GUARD, 224, 128, buffer);
loadCursor(CURSOR_FIX, 0, 160, buffer);
loadCursor(CURSOR_SELECT, 32, 160, buffer);
// loadCursor(CURSOR_REPAIR, 64, 160, buffer); // FIX ME: This IS in infac.img, but the define is MIA
loadCursor(CURSOR_SEEKREPAIR, 64, 160, buffer); // FIX ME: This is NOT in infac.img!
loadCursor(CURSOR_PICKUP, 96, 160, buffer);
loadCursor(CURSOR_NOTPOSSIBLE, 128, 160, buffer);
loadCursor(CURSOR_MOVE, 160, 160, buffer);
loadCursor(CURSOR_LOCKON, 192, 160, buffer);
// loadCursor(CURSOR_ECM, 224, 160, buffer); // FIX ME: Not defined yet!
loadCursor(CURSOR_JAM, 224, 160, buffer); // FIX ME: This is NOT in infac.img, and is using IMAGE CURSOR ECM ?
loadCursor(CURSOR_ATTACH, 0, 192, buffer);
loadCursor(CURSOR_BRIDGE, 32, 192, buffer);
loadCursor(CURSOR_BOMB, 64, 192, buffer);
// Reused (unused) cursors
cursors[CURSOR_ARROW] = new QCursor(Qt::ArrowCursor);
cursors[CURSOR_MENU] = new QCursor(Qt::ArrowCursor);
cursors[CURSOR_BOMB] = new QCursor(Qt::ArrowCursor);
cursors[CURSOR_EDGEOFMAP] = new QCursor(Qt::ArrowCursor);
cursors[CURSOR_SIGHT] = new QCursor(Qt::ArrowCursor);
cursors[CURSOR_TARGET] = new QCursor(Qt::ArrowCursor);
cursors[CURSOR_UARROW] = new QCursor(Qt::SizeVerCursor);
cursors[CURSOR_DARROW] = new QCursor(Qt::SizeVerCursor);
cursors[CURSOR_LARROW] = new QCursor(Qt::SizeHorCursor);
cursors[CURSOR_RARROW] = new QCursor(Qt::SizeHorCursor);
// Fonts
regularFont.setFamily("DejaVu Sans");
regularFont.setPixelSize(12);
boldFont.setFamily("DejaVu Sans");
boldFont.setPixelSize(21);
boldFont.setWeight(QFont::DemiBold);
smallFont.setFamily("DejaVu Sans");
smallFont.setPixelSize(9);
// Want focusOutEvent messages.
setFocusPolicy(Qt::StrongFocus);
// Want áéíóú inputMethodEvent messages.
setAttribute(Qt::WA_InputMethodEnabled);
}
WzMainWindow::~WzMainWindow()
{
for (int i = 0; i < CURSOR_MAX; delete cursors[i++]) ;
}
WzMainWindow *WzMainWindow::instance()
{
assert(myself != NULL);
return myself;
}
void WzMainWindow::initializeGL()
{
}
void WzMainWindow::drawPixmap(int XPos, int YPos, QPixmap *pix)
{
QPainter painter(context()->device());
painter.drawPixmap(XPos, YPos, *pix);
rendStatesRendModeHack(); // rendStates.rendMode = REND_ALPHA;
pie_SetRendMode(REND_OPAQUE); // beat state machinery into submission
}
void WzMainWindow::resizeGL(int width, int height)
{
screenWidth = width;
screenHeight = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, height, 0, 1, -1);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void WzMainWindow::paintGL()
{
if (notReadyToPaint)
{
return;
}
if (crashing)
{
glClear(GL_COLOR_BUFFER_BIT);
swapBuffers();
return;
}
mainLoop();
// Tell the input system about the start of another frame
inputNewFrame();
}
void WzMainWindow::setCursor(CURSOR index)
{
QWidget::setCursor(*cursors[index]);
}
void WzMainWindow::setCursor(QCursor cursor)
{
QWidget::setCursor(cursor);
}
WzMainWindow *WzMainWindow::myself = NULL;
void WzMainWindow::setFontType(enum iV_fonts fontID)
{
switch (fontID)
{
case font_regular:
setFont(regularFont);
break;
case font_large:
setFont(boldFont);
break;
case font_small:
setFont(smallFont);
break;
default:
break;
}
}
void WzMainWindow::setFontSize(float size)
{
QFont theFont = font();
theFont.setPixelSize(size);
setFont(theFont);
}
void WzMainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (event->x() < 0 || event->y() < 0 || event->x() >= width() || event->y() >= height())
{
mouseXPos = width()/2; // Don't scroll when mouse is outside window.
mouseYPos = height()/2;
}
else
{
mouseXPos = event->x();
mouseYPos = event->y();
}
if (!mouseDown(MOUSE_MMB))
{
/* now see if a drag has started */
if ((aMouseState[dragKey].state == KEY_PRESSED ||
aMouseState[dragKey].state == KEY_DOWN)
&& (ABSDIF(dragX, mouseXPos) > DRAG_THRESHOLD ||
ABSDIF(dragY, mouseYPos) > DRAG_THRESHOLD))
{
aMouseState[dragKey].state = KEY_DRAG;
}
}
}
MOUSE_KEY_CODE WzMainWindow::buttonToIdx(Qt::MouseButton button)
{
MOUSE_KEY_CODE idx;
switch (button)
{
case Qt::LeftButton : idx = MOUSE_LMB; break;
case Qt::RightButton : idx = MOUSE_RMB; break;
case Qt::MidButton : idx = MOUSE_MMB; break;
case Qt::XButton1 : idx = MOUSE_MMB; break;
case Qt::XButton2 : idx = MOUSE_MMB; break;
default:
case Qt::NoButton : idx = MOUSE_BAD; break; // strange case
}
return idx;
}
// TODO consider using QWidget::mouseDoubleClickEvent() for double-click
void WzMainWindow::mousePressEvent(QMouseEvent *event)
{
mouseXPos = event->x();
mouseYPos = event->y();
Qt::MouseButtons presses = event->buttons(); // full state info for all buttons
MOUSE_KEY_CODE idx = buttonToIdx(event->button()); // index of button that caused event
if (idx == MOUSE_BAD)
{
debug(LOG_ERROR, "bad mouse idx"); // FIXME remove
return; // not recognized mouse button
}
aMouseState[idx].pressPos.x = mouseXPos;
aMouseState[idx].pressPos.y = mouseYPos;
if (aMouseState[idx].state == KEY_UP
|| aMouseState[idx].state == KEY_RELEASED
|| aMouseState[idx].state == KEY_PRESSRELEASE)
{
if (!presses.testFlag(Qt::MidButton)) //skip doubleclick check for wheel
{
// whether double click or not
if (realTime - aMouseState[idx].lastdown < DOUBLE_CLICK_INTERVAL)
{
aMouseState[idx].state = KEY_DOUBLECLICK;
aMouseState[idx].lastdown = 0;
}
else
{
aMouseState[idx].state = KEY_PRESSED;
aMouseState[idx].lastdown = realTime;
}
}
else //mouse wheel up/down was used, so notify. FIXME.
{
aMouseState[idx].state = KEY_PRESSED;
aMouseState[idx].lastdown = 0;
}
if (idx == MOUSE_LMB || idx == MOUSE_RMB) // Not the mousewheel. I'm running around in the mouse wheel, but not getting anywhere. Squeak!
{
dragKey = idx;
dragX = mouseX();
dragY = mouseY();
}
}
}
void WzMainWindow::wheelEvent(QWheelEvent *event)
{
int direction = event->delta();
if (direction > 0)
{
aMouseState[MOUSE_WUP].state = KEY_PRESSED;
aMouseState[MOUSE_WUP].lastdown = realTime;
}
else
{
aMouseState[MOUSE_WDN].state = KEY_PRESSED;
aMouseState[MOUSE_WDN].lastdown = realTime;
}
}
void WzMainWindow::mouseReleaseEvent(QMouseEvent *event)
{
mouseXPos = event->x();
mouseYPos = event->y();
MOUSE_KEY_CODE idx = buttonToIdx(event->button());
if (idx == MOUSE_BAD)
{
return; // not recognized mouse button
}
aMouseState[idx].releasePos.x = mouseXPos;
aMouseState[idx].releasePos.y = mouseYPos;
if (aMouseState[idx].state == KEY_PRESSED)
{
aMouseState[idx].state = KEY_PRESSRELEASE;
}
else if (aMouseState[idx].state == KEY_DOWN
|| aMouseState[idx].state == KEY_DRAG
|| aMouseState[idx].state == KEY_DOUBLECLICK)
{
aMouseState[idx].state = KEY_RELEASED;
}
}
static unsigned int setKey(int code, bool pressed)
{
if (pressed)
{
if (aKeyState[code].state == KEY_UP ||
aKeyState[code].state == KEY_RELEASED ||
aKeyState[code].state == KEY_PRESSRELEASE)
{
// whether double key press or not
aKeyState[code].state = KEY_PRESSED;
aKeyState[code].lastdown = 0;
}
}
else
{
if (aKeyState[code].state == KEY_PRESSED)
{
aKeyState[code].state = KEY_PRESSRELEASE;
}
else if (aKeyState[code].state == KEY_DOWN)
{
aKeyState[code].state = KEY_RELEASED;
}
}
return code;
}
void WzMainWindow::realHandleKeyEvent(QKeyEvent *event, bool pressed)
{
Qt::KeyboardModifiers mods = event->modifiers();
unsigned int lastKey;
bool isKeypad = event->modifiers() & Qt::KeypadModifier;
switch (event->text().size())
{
case 0:
debug(LOG_INPUT, "Key%s 0x%04X, isKeypad=%d", pressed ? "Down" : "Up ", event->key(), isKeypad);
case 1:
debug(LOG_INPUT, "Key%s 0x%04X, isKeypad=%d, 0x%04X", pressed ? "Down" : "Up ", event->key(), isKeypad, event->text().unicode()[0].unicode());
break;
case 2:
debug(LOG_INPUT, "Key%s 0x%04X, isKeypad=%d, 0x%04X, 0x%04X", pressed ? "Down" : "Up ", event->key(), isKeypad, event->text().unicode()[0].unicode(), event->text().unicode()[1].unicode());
break;
case 3:
debug(LOG_INPUT, "Key%s 0x%04X, isKeypad=%d, 0x%04X, 0x%04X, ...", pressed ? "Down" : "Up ", event->key(), isKeypad, event->text().unicode()[0].unicode(), event->text().unicode()[1].unicode());
break;
}
if (!isKeypad)
{
switch (event->key())
{
case Qt::Key_Shift : lastKey = setKey(KEY_LSHIFT, pressed); break;
case Qt::Key_Control : lastKey = setKey(KEY_LCTRL, pressed); break;
case Qt::Key_Alt : lastKey = setKey(KEY_LALT, pressed); break;
case Qt::Key_AltGr : lastKey = setKey(KEY_RALT, pressed); break;
case Qt::Key_Meta : lastKey = setKey(KEY_LMETA, pressed); break;
case Qt::Key_Escape : lastKey = setKey(KEY_ESC, pressed); break;
case Qt::Key_Backspace : lastKey = setKey(KEY_BACKSPACE, pressed); break;
case Qt::Key_QuoteLeft : lastKey = setKey(KEY_BACKQUOTE, pressed); break;
case Qt::Key_1 : lastKey = setKey(KEY_1, pressed); break;
case Qt::Key_2 : lastKey = setKey(KEY_2, pressed); break;
case Qt::Key_3 : lastKey = setKey(KEY_3, pressed); break;
case Qt::Key_4 : lastKey = setKey(KEY_4, pressed); break;
case Qt::Key_5 : lastKey = setKey(KEY_5, pressed); break;
case Qt::Key_6 : lastKey = setKey(KEY_6, pressed); break;
case Qt::Key_7 : lastKey = setKey(KEY_7, pressed); break;
case Qt::Key_8 : lastKey = setKey(KEY_8, pressed); break;
case Qt::Key_9 : lastKey = setKey(KEY_9, pressed); break;
case Qt::Key_0 : lastKey = setKey(KEY_0, pressed); break;
case Qt::Key_Minus : lastKey = setKey(KEY_MINUS, pressed); break;
case Qt::Key_Equal : lastKey = setKey(KEY_EQUALS, pressed); break;
case Qt::Key_Backtab:
case Qt::Key_Tab : lastKey = setKey(KEY_TAB, pressed); break;
case Qt::Key_Q : lastKey = setKey(KEY_Q, pressed); break;
case Qt::Key_W : lastKey = setKey(KEY_W, pressed); break;
case Qt::Key_E : lastKey = setKey(KEY_E, pressed); break;
case Qt::Key_R : lastKey = setKey(KEY_R, pressed); break;
case Qt::Key_T : lastKey = setKey(KEY_T, pressed); break;
case Qt::Key_Y : lastKey = setKey(KEY_Y, pressed); break;
case Qt::Key_U : lastKey = setKey(KEY_U, pressed); break;
case Qt::Key_I : lastKey = setKey(KEY_I, pressed); break;
case Qt::Key_O : lastKey = setKey(KEY_O, pressed); break;
case Qt::Key_P : lastKey = setKey(KEY_P, pressed); break;
case Qt::Key_BracketLeft : lastKey = setKey(KEY_LBRACE, pressed); break;
case Qt::Key_BracketRight : lastKey = setKey(KEY_RBRACE, pressed); break;
case Qt::Key_Enter : lastKey = setKey(KEY_RETURN, pressed); break;
case Qt::Key_Return : lastKey = setKey(KEY_RETURN, pressed); break;
case Qt::Key_A : lastKey = setKey(KEY_A, pressed); break;
case Qt::Key_S : lastKey = setKey(KEY_S, pressed); break;
case Qt::Key_D : lastKey = setKey(KEY_D, pressed); break;
case Qt::Key_F : lastKey = setKey(KEY_F, pressed); break;
case Qt::Key_G : lastKey = setKey(KEY_G, pressed); break;
case Qt::Key_H : lastKey = setKey(KEY_H, pressed); break;
case Qt::Key_J : lastKey = setKey(KEY_J, pressed); break;
case Qt::Key_K : lastKey = setKey(KEY_K, pressed); break;
case Qt::Key_L : lastKey = setKey(KEY_L, pressed); break;
case Qt::Key_Semicolon : lastKey = setKey(KEY_SEMICOLON, pressed); break;
case Qt::Key_QuoteDbl : lastKey = setKey(KEY_QUOTE, pressed); break; // ?
case Qt::Key_Apostrophe : lastKey = setKey(KEY_BACKQUOTE, pressed); break; // ?
case Qt::Key_Backslash : lastKey = setKey(KEY_BACKSLASH, pressed); break;
case Qt::Key_Z : lastKey = setKey(KEY_Z, pressed); break;
case Qt::Key_X : lastKey = setKey(KEY_X, pressed); break;
case Qt::Key_C : lastKey = setKey(KEY_C, pressed); break;
case Qt::Key_V : lastKey = setKey(KEY_V, pressed); break;
case Qt::Key_B : lastKey = setKey(KEY_B, pressed); break;
case Qt::Key_N : lastKey = setKey(KEY_N, pressed); break;
case Qt::Key_M : lastKey = setKey(KEY_M, pressed); break;
case Qt::Key_Comma : lastKey = setKey(KEY_COMMA, pressed); break;
case Qt::Key_Period : lastKey = setKey(KEY_FULLSTOP, pressed); break;
case Qt::Key_Slash : lastKey = setKey(KEY_FORWARDSLASH, pressed); break;
case Qt::Key_Space : lastKey = setKey(KEY_SPACE, pressed); break;
case Qt::Key_CapsLock : lastKey = setKey(KEY_CAPSLOCK, pressed); break;
case Qt::Key_F1 : lastKey = setKey(KEY_F1, pressed); break;
case Qt::Key_F2 : lastKey = setKey(KEY_F2, pressed); break;
case Qt::Key_F3 : lastKey = setKey(KEY_F3, pressed); break;
case Qt::Key_F4 : lastKey = setKey(KEY_F4, pressed); break;
case Qt::Key_F5 : lastKey = setKey(KEY_F5, pressed); break;
case Qt::Key_F6 : lastKey = setKey(KEY_F6, pressed); break;
case Qt::Key_F7 : lastKey = setKey(KEY_F7, pressed); break;
case Qt::Key_F8 : lastKey = setKey(KEY_F8, pressed); break;
case Qt::Key_F9 : lastKey = setKey(KEY_F9, pressed); break;
case Qt::Key_F10 : lastKey = setKey(KEY_F10, pressed); break;
case Qt::Key_NumLock : lastKey = setKey(KEY_NUMLOCK, pressed); break;
case Qt::Key_ScrollLock : lastKey = setKey(KEY_SCROLLLOCK, pressed); break;
case Qt::Key_F11 : lastKey = setKey(KEY_F11, pressed); break;
case Qt::Key_F12 : lastKey = setKey(KEY_F12, pressed); break;
case Qt::Key_Home : lastKey = setKey(KEY_HOME, pressed); break;
case Qt::Key_Up : lastKey = setKey(KEY_UPARROW, pressed); break;
case Qt::Key_PageUp : lastKey = setKey(KEY_PAGEUP, pressed); break;
case Qt::Key_Left : lastKey = setKey(KEY_LEFTARROW, pressed); break;
case Qt::Key_Right : lastKey = setKey(KEY_RIGHTARROW, pressed); break;
case Qt::Key_End : lastKey = setKey(KEY_END, pressed); break;
case Qt::Key_Down : lastKey = setKey(KEY_DOWNARROW, pressed); break;
case Qt::Key_PageDown : lastKey = setKey(KEY_PAGEDOWN, pressed); break;
case Qt::Key_Insert : lastKey = setKey(KEY_INSERT, pressed); break;
case Qt::Key_Delete : lastKey = setKey(KEY_DELETE, pressed); break;
default:
debug(LOG_WARNING, "Ignoring normal key %d.\n", event->key());
lastKey = 0;
event->ignore();
break;
}
}
else // Is keypad.
{
switch (event->key())
{
case Qt::Key_Asterisk : lastKey = setKey(KEY_KP_STAR, pressed); break;
case Qt::Key_0 : lastKey = setKey(KEY_KP_0, pressed); break;
case Qt::Key_1 : lastKey = setKey(KEY_KP_1, pressed); break;
case Qt::Key_2 : lastKey = setKey(KEY_KP_2, pressed); break;
case Qt::Key_3 : lastKey = setKey(KEY_KP_3, pressed); break;
case Qt::Key_4 : lastKey = setKey(KEY_KP_4, pressed); break;
case Qt::Key_5 : lastKey = setKey(KEY_KP_5, pressed); break;
case Qt::Key_6 : lastKey = setKey(KEY_KP_6, pressed); break;
case Qt::Key_7 : lastKey = setKey(KEY_KP_7, pressed); break;
case Qt::Key_8 : lastKey = setKey(KEY_KP_8, pressed); break;
case Qt::Key_9 : lastKey = setKey(KEY_KP_9, pressed); break;
case Qt::Key_Home : lastKey = setKey(KEY_KP_7, pressed); break;
case Qt::Key_Up : lastKey = setKey(KEY_KP_8, pressed); break;
case Qt::Key_PageUp : lastKey = setKey(KEY_KP_9, pressed); break;
case Qt::Key_Left : lastKey = setKey(KEY_KP_4, pressed); break;
case Qt::Key_Clear : lastKey = setKey(KEY_KP_5, pressed); break;
case Qt::Key_Right : lastKey = setKey(KEY_KP_6, pressed); break;
case Qt::Key_End : lastKey = setKey(KEY_KP_1, pressed); break;
case Qt::Key_Down : lastKey = setKey(KEY_KP_2, pressed); break;
case Qt::Key_PageDown : lastKey = setKey(KEY_KP_3, pressed); break;
case Qt::Key_Insert : lastKey = setKey(KEY_KP_0, pressed); break;
case Qt::Key_Delete : lastKey = setKey(KEY_KP_FULLSTOP, pressed); break;
case Qt::Key_Plus : lastKey = setKey(KEY_KP_PLUS, pressed); break;
case Qt::Key_Minus : lastKey = setKey(KEY_KP_MINUS, pressed); break;
case Qt::Key_Period : lastKey = setKey(KEY_KP_FULLSTOP, pressed); break;
case Qt::Key_Slash : lastKey = setKey(KEY_KP_BACKSLASH, pressed); break; // HACK Shouldn't there be a KEY_KP_SLASH? Most keypads only have a forwardslash key.
case Qt::Key_Enter : lastKey = setKey(KEY_KPENTER, pressed); break;
case Qt::Key_Return : lastKey = setKey(KEY_KPENTER, pressed); break;
default:
debug(LOG_WARNING, "Ignoring keypad key %d.\n", event->key());
lastKey = 0;
event->ignore();
break;
}
}
if (pressed)
{
inputAddBuffer(lastKey, event->text().unicode()->unicode());
}
event->accept();
}
void WzMainWindow::keyReleaseEvent(QKeyEvent *event)
{
realHandleKeyEvent(event, false);
}
void WzMainWindow::keyPressEvent(QKeyEvent *event)
{
realHandleKeyEvent(event, true);
}
void WzMainWindow::inputMethodEvent(QInputMethodEvent *event)
{
// Foward all "committed" characters. Should be more advanced than that, but better than nothing.
for (int i = 0; i < event->commitString().size(); ++i)
{
inputAddBuffer(' ', event->commitString()[i].unicode());
}
QWidget::inputMethodEvent(event);
}
void WzMainWindow::focusOutEvent(QFocusEvent *event)
{
debug(LOG_INPUT, "Main window lost focus.");
inputLoseFocus();
}
void WzMainWindow::close()
{
qApp->quit();
}
/************************************/
/*** ***/
/*** C interface ***/
/*** ***/
/************************************/
int wzInit(int argc, char *argv[], int fsaa, bool vsync, int w, int h, bool fullscreen)
{
char buf[256];
QGL::setPreferredPaintEngine(QPaintEngine::OpenGL); // Workaround for incorrect text rendering on nany platforms.
QApplication app(argc, argv);
PhysicsEngineHandler engine; // register abstract physfs filesystem
// Setting up OpenGL
QGLFormat format;
format.setDoubleBuffer(true);
format.setAlpha(true);
if (vsync)
{
format.setSwapInterval(1);
}
if (fsaa)
{
format.setSampleBuffers(true);
format.setSamples(fsaa);
}
WzMainWindow mainwindow(format);
if (!mainwindow.context()->isValid())
{
QMessageBox::critical(NULL, "Oops!", "Warzone2100 failed to create an OpenGL context. This probably means that your graphics drivers are out of date. Try updating them!");
return EXIT_FAILURE;
}
if (fullscreen)
{
QDesktopWidget *desktop = qApp->desktop();
w = desktop->width();
h = desktop->height();
pie_SetVideoBufferWidth(w);
pie_SetVideoBufferHeight(h);
}
mainwindow.setMinimumSize(w, h);
mainwindow.setMaximumSize(w, h);
if (fullscreen)
{
WzMainWindow::instance()->showFullScreen();
}
screenWidth = w;
screenHeight = h;
mainwindow.show();
mainwindow.setReadyToPaint();
ssprintf(buf, "Video Mode %d x %d (%s)", w, h, fullscreen ? "fullscreen" : "window");
addDumpInfo(buf);
debug(LOG_MAIN, "Final initialization");
if (finalInitialization() != 0)
{
debug(LOG_ERROR, "Failed to carry out final initialization.");
return EXIT_FAILURE;
}
debug(LOG_MAIN, "Entering main loop");
app.exec();
saveConfig();
debug(LOG_MAIN, "Shutting down Warzone 2100");
return EXIT_SUCCESS;
}
void wzQuit()
{
WzMainWindow::instance()->close();
}
void wzScreenFlip()
{
WzMainWindow::instance()->swapBuffers();
}
int wzGetTicks()
{
return WzMainWindow::instance()->ticks();
}
/****************************************/
/*** Mouse and keyboard support ***/
/****************************************/
void pie_ShowMouse(bool visible)
{
if (!visible)
{
WzMainWindow::instance()->setCursor(QCursor(Qt::BlankCursor));
}
else
{
WzMainWindow::instance()->setCursor(lastCursor);
}
}
void wzSetCursor(CURSOR index)
{
WzMainWindow::instance()->setCursor(index);
lastCursor = index;
}
void wzCreateCursor(CURSOR index, uint8_t *data, uint8_t *mask, int w, int h, int hot_x, int hot_y)
{
// TODO REMOVE
}
void wzGrabMouse()
{
WzMainWindow::instance()->grabMouse();
}
void wzReleaseMouse()
{
WzMainWindow::instance()->releaseMouse();
}
bool wzActiveWindow()
{
return WzMainWindow::instance()->underMouse();
}
uint16_t mouseX()
{
return mouseXPos;
}
uint16_t mouseY()
{
return mouseYPos;
}
Vector2i mousePressPos(MOUSE_KEY_CODE code)
{
return aMouseState[code].pressPos;
}
Vector2i mouseReleasePos(MOUSE_KEY_CODE code)
{
return aMouseState[code].releasePos;
}
void SetMousePos(uint16_t x, uint16_t y)
{
static int mousewarp = -1;
if (mousewarp == -1)
{
int val;
mousewarp = 1;
if (getWarzoneKeyNumeric("nomousewarp", &val))
{
mousewarp = !val;
}
}
if (mousewarp)
{
WzMainWindow::instance()->cursor().setPos(x, y);
}
}
/* This returns true if the mouse key is currently depressed */
bool mouseDown(MOUSE_KEY_CODE code)
{
return (aMouseState[code].state != KEY_UP);
}
/* This returns true if the mouse key was double clicked */
bool mouseDClicked(MOUSE_KEY_CODE code)
{
return (aMouseState[code].state == KEY_DOUBLECLICK);
}
/* This returns true if the mouse key went from being up to being down this frame */
bool mousePressed(MOUSE_KEY_CODE code)
{
return ((aMouseState[code].state == KEY_PRESSED) ||
(aMouseState[code].state == KEY_DOUBLECLICK) ||
(aMouseState[code].state == KEY_PRESSRELEASE));
}
/* This returns true if the mouse key went from being down to being up this frame */
bool mouseReleased(MOUSE_KEY_CODE code)
{
return ((aMouseState[code].state == KEY_RELEASED) ||
(aMouseState[code].state == KEY_DOUBLECLICK) ||
(aMouseState[code].state == KEY_PRESSRELEASE));
}
/* Check for a mouse drag, return the drag start coords if dragging */
bool mouseDrag(MOUSE_KEY_CODE code, UDWORD *px, UDWORD *py)
{
if (aMouseState[code].state == KEY_DRAG)
{
*px = dragX;
*py = dragY;
return true;
}
return false;
}
void keyScanToString(KEY_CODE code, char *ascii, UDWORD maxStringSize)
{
QString ourString;
QByteArray ba;
const QKeySequence ks = WZkeyToQtKey(code); // convert key to something Qt understands
ourString = ks.toString(QKeySequence::NativeText); // and convert it to a QtString
ba = ourString.toLatin1(); // convert that to a byte array
snprintf(ascii, maxStringSize, "%s", ba.data()); // and use that data as the text for the key
#if defined(WZ_OS_MAC) // Yet another mac hack, since these are normally shown as symbols, NOT text.
if ( (code == KEY_LALT) || (code == KEY_RALT) )
snprintf(ascii, maxStringSize, "Alt +");
else if ( (code == KEY_LCTRL) || (code == KEY_RCTRL) ) // apparently, ctrl = cmd and cmd = ctrl on macs.
snprintf(ascii, maxStringSize, "Cmd +");
else if ( code == KEY_ESC )
snprintf(ascii, maxStringSize, "Esc");
#endif
}
/* Initialise the input module */
void inputInitialise(void)
{
unsigned int i;
for (i = 0; i < KEY_MAXSCAN; i++)
{
aKeyState[i].state = KEY_UP;
}
for (i = 0; i < 6; i++)
{
aMouseState[i].state = KEY_UP;
}
inputBuffer.clear();
dragX = screenWidth / 2;
dragY = screenHeight / 2;
dragKey = MOUSE_LMB;
}
/* add count copies of the characater code to the input buffer */
static void inputAddBuffer(UDWORD key, utf_32_char unicode)
{
inputBuffer.push_back(InputKey(key, unicode));
}
/* Clear the input buffer */
void inputClearBuffer(void)
{
inputBuffer.clear();
}
/* Return the next key press or 0 if no key in the buffer.
* The key returned will have been remaped to the correct ascii code for the
* windows key map.
* All key presses are buffered up (including windows auto repeat).
*/
UDWORD inputGetKey(utf_32_char *unicode)
{
if (inputBuffer.empty())
{
return 0;
}
UDWORD retVal = inputBuffer.front().key;
if (retVal == 0)
{
retVal = ' '; // Don't return 0 if we got a virtual key, since that's interpreted as no input.
}
if (unicode != NULL)
{
*unicode = inputBuffer.front().unicode;
}
inputBuffer.pop_front();
return retVal;
}
/*!
* This is called once a frame so that the system can tell
* whether a key was pressed this turn or held down from the last frame.
*/
void inputNewFrame(void)
{
unsigned int i;
/* Do the keyboard */
for (i = 0; i < KEY_MAXSCAN; i++)
{
if (aKeyState[i].state == KEY_PRESSED)
{
aKeyState[i].state = KEY_DOWN;
}
else if (aKeyState[i].state == KEY_RELEASED ||
aKeyState[i].state == KEY_PRESSRELEASE)
{
aKeyState[i].state = KEY_UP;
}
}
/* Do the mouse */
for (i = 0; i < 6; i++)
{
if (aMouseState[i].state == KEY_PRESSED)