Bläddra i källkod

Added Game class.

Moved typedefs into global header.
Thomas Buck 10 år sedan
förälder
incheckning
6a2026b656
10 ändrade filer med 188 tillägg och 91 borttagningar
  1. 33
    0
      include/Game.h
  2. 3
    15
      include/OpenRaider.h
  3. 1
    41
      include/Window.h
  4. 68
    0
      include/global.h
  5. 1
    0
      src/CMakeLists.txt
  6. 1
    1
      src/Console.cpp
  7. 38
    0
      src/Game.cpp
  8. 1
    1
      src/Menu.cpp
  9. 41
    32
      src/OpenRaider.cpp
  10. 1
    1
      src/main.cpp

+ 33
- 0
include/Game.h Visa fil

@@ -0,0 +1,33 @@
1
+/*!
2
+ * \file include/Game.h
3
+ * \brief Game abstraction
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _GAME_H_
9
+#define _GAME_H_
10
+
11
+#include "global.h"
12
+
13
+/*!
14
+ * \brief Game abstraction
15
+ */
16
+class Game {
17
+public:
18
+
19
+    Game(const char *level);
20
+
21
+    ~Game();
22
+
23
+    void handleAction(ActionEvents action, bool isFinished);
24
+
25
+    void handleMouseMotion(int xrel, int yrel);
26
+
27
+    void display();
28
+
29
+private:
30
+
31
+};
32
+
33
+#endif

+ 3
- 15
include/OpenRaider.h Visa fil

@@ -10,7 +10,9 @@
10 10
 
11 11
 #include <vector>
12 12
 
13
+#include "global.h"
13 14
 #include "Console.h"
15
+#include "Game.h"
14 16
 #include "Menu.h"
15 17
 #include "Sound.h"
16 18
 #include "Window.h"
@@ -21,21 +23,6 @@
21 23
 class OpenRaider {
22 24
 public:
23 25
 
24
-    typedef enum {
25
-        menu = 0,
26
-        console,
27
-        forward,
28
-        backward,
29
-        left,
30
-        right,
31
-        jump,
32
-        crouch,
33
-        use,
34
-        holster,
35
-
36
-        ActionEventCount // Should always be at the end
37
-    } ActionEvents;
38
-
39 26
     /*!
40 27
      * \brief Constructs an object of OpenRaider
41 28
      */
@@ -72,6 +59,7 @@ public:
72 59
     Sound *mSound;
73 60
     Menu *mMenu;
74 61
     Console *mConsole;
62
+    Game *mGame;
75 63
 
76 64
     bool mMapListFilled;
77 65
     std::vector<char *> mMapList;

+ 1
- 41
include/Window.h Visa fil

@@ -10,47 +10,7 @@
10 10
 
11 11
 #include <ctime>
12 12
 
13
-typedef enum {
14
-    zero = '0', one = '1',
15
-    two = '2', three = '3',
16
-    four = '4', five = '5',
17
-    six = '6', seven = '7',
18
-    eight = '8', nine = '9',
19
-    a = 'a', b = 'b',
20
-    c = 'c', d = 'd',
21
-    e = 'e', f = 'f',
22
-    g = 'g', h = 'h',
23
-    i = 'i', j = 'j',
24
-    k = 'k', l = 'l',
25
-    m = 'm', n = 'n',
26
-    o = 'o', p = 'p',
27
-    q = 'q', r = 'r',
28
-    s = 's', t = 't',
29
-    u = 'u', v = 'v',
30
-    w = 'w', x = 'x',
31
-    y = 'y', z = 'z',
32
-    quote, backslash, backspace, capslock,
33
-    comma, del, up, down, left, right,
34
-    end, equals, escape, f1, f2, f3, f4, f5,
35
-    f6, f7, f8, f9, f10, f11, f12, backquote,
36
-    home, insert, leftalt, leftctrl, leftbracket,
37
-    leftgui, leftshift, minus, numlock, pagedown,
38
-    pageup, pause, dot, rightalt, rightctrl, enter,
39
-    rightgui, rightbracket, rightshift, scrolllock,
40
-    semicolon, slash, space, tab,
41
-    leftmouse, middlemouse, rightmouse,
42
-    unknown
43
-} KeyboardButton;
44
-
45
-typedef struct {
46
-    char *text;
47
-    unsigned int x;
48
-    unsigned int y;
49
-    int w;
50
-    int h;
51
-    float scale;
52
-    unsigned char color[4];
53
-} WindowString;
13
+#include "global.h"
54 14
 
55 15
 /*!
56 16
  * \brief Windowing interface

+ 68
- 0
include/global.h Visa fil

@@ -0,0 +1,68 @@
1
+/*!
2
+ * \file include/global.h
3
+ * \brief Global typedefs
4
+ *
5
+ * \author xythobuz
6
+ */
7
+#ifndef _GLOBAL_H_
8
+#define _GLOBAL_H_
9
+
10
+typedef enum {
11
+    menuAction = 0,
12
+    consoleAction, // menu and console should always be the first two items
13
+    forwardAction,
14
+    backwardAction,
15
+    leftAction,
16
+    rightAction,
17
+    jumpAction,
18
+    crouchAction,
19
+    useAction,
20
+    holsterAction,
21
+
22
+    ActionEventCount // Should always be at the end
23
+} ActionEvents;
24
+
25
+typedef enum {
26
+    zero = '0', one = '1',
27
+    two = '2', three = '3',
28
+    four = '4', five = '5',
29
+    six = '6', seven = '7',
30
+    eight = '8', nine = '9',
31
+    a = 'a', b = 'b',
32
+    c = 'c', d = 'd',
33
+    e = 'e', f = 'f',
34
+    g = 'g', h = 'h',
35
+    i = 'i', j = 'j',
36
+    k = 'k', l = 'l',
37
+    m = 'm', n = 'n',
38
+    o = 'o', p = 'p',
39
+    q = 'q', r = 'r',
40
+    s = 's', t = 't',
41
+    u = 'u', v = 'v',
42
+    w = 'w', x = 'x',
43
+    y = 'y', z = 'z',
44
+    quote, backslash, backspace, capslock,
45
+    comma, del, up, down, left, right,
46
+    end, equals, escape, f1, f2, f3, f4, f5,
47
+    f6, f7, f8, f9, f10, f11, f12, backquote,
48
+    home, insert, leftalt, leftctrl, leftbracket,
49
+    leftgui, leftshift, minus, numlock, pagedown,
50
+    pageup, pause, dot, rightalt, rightctrl, enter,
51
+    rightgui, rightbracket, rightshift, scrolllock,
52
+    semicolon, slash, space, tab,
53
+    leftmouse, middlemouse, rightmouse,
54
+    unknown
55
+} KeyboardButton;
56
+
57
+typedef struct {
58
+    char *text;
59
+    unsigned int x;
60
+    unsigned int y;
61
+    int w;
62
+    int h;
63
+    float scale;
64
+    unsigned char color[4];
65
+} WindowString;
66
+
67
+#endif
68
+

+ 1
- 0
src/CMakeLists.txt Visa fil

@@ -32,6 +32,7 @@ set (LIBS ${LIBS} ${ZLIB_LIBRARIES})
32 32
 
33 33
 # Set Source files
34 34
 set (SRCS ${SRCS} "Console.cpp")
35
+set (SRCS ${SRCS} "Game.cpp")
35 36
 set (SRCS ${SRCS} "main.cpp")
36 37
 set (SRCS ${SRCS} "Menu.cpp")
37 38
 set (SRCS ${SRCS} "OpenRaider.cpp")

+ 1
- 1
src/Console.cpp Visa fil

@@ -98,7 +98,7 @@ void Console::display() {
98 98
         LINE_GEOMETRY(window);
99 99
 
100 100
         // Draw half-transparent *overlay*
101
-        glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
101
+        glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
102 102
         glDisable(GL_TEXTURE_2D);
103 103
         glRecti(0, 0, window->mWidth, window->mHeight / 2);
104 104
         glEnable(GL_TEXTURE_2D);

+ 38
- 0
src/Game.cpp Visa fil

@@ -0,0 +1,38 @@
1
+/*!
2
+ * \file src/Game.cpp
3
+ * \brief Game abstraction
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifdef __APPLE__
9
+#include <OpenGL/gl.h>
10
+#include <OpenGL/glu.h>
11
+#else
12
+#include <GL/gl.h>
13
+#include <GL/glu.h>
14
+#endif
15
+
16
+#include "main.h"
17
+#include "Window.h"
18
+#include "Game.h"
19
+
20
+Game::Game(const char *level) {
21
+}
22
+
23
+Game::~Game() {
24
+}
25
+
26
+void Game::handleAction(ActionEvents action, bool isFinished) {
27
+
28
+}
29
+
30
+void Game::handleMouseMotion(int xrel, int yrel) {
31
+
32
+}
33
+
34
+void Game::display() {
35
+    unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
36
+    gOpenRaider->mWindow->drawText(10, 10, 1.50f, color, "Game");
37
+}
38
+

+ 1
- 1
src/Menu.cpp Visa fil

@@ -91,7 +91,7 @@ void Menu::display() {
91 91
 
92 92
     if (mVisible) {
93 93
         // Draw half-transparent *overlay*
94
-        glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
94
+        glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
95 95
         glDisable(GL_TEXTURE_2D);
96 96
         glRecti(0, 0, window->mWidth, window->mHeight);
97 97
         glEnable(GL_TEXTURE_2D);

+ 41
- 32
src/OpenRaider.cpp Visa fil

@@ -13,6 +13,11 @@
13 13
 #include "WindowSDL.h"
14 14
 
15 15
 #include "config.h"
16
+#include "Console.h"
17
+#include "Game.h"
18
+#include "Menu.h"
19
+#include "Sound.h"
20
+#include "Window.h"
16 21
 #include "utils/strings.h"
17 22
 #include "utils/time.h"
18 23
 #include "OpenRaider.h"
@@ -32,12 +37,16 @@ OpenRaider::OpenRaider() {
32 37
     mConsole = new Console();
33 38
     mSound = new Sound();
34 39
     mWindow = new WindowSDL();
40
+    mGame = NULL;
35 41
 
36 42
     for (int i = 0; i < ActionEventCount; i++)
37 43
         keyBindings[i] = unknown;
38 44
 }
39 45
 
40 46
 OpenRaider::~OpenRaider() {
47
+    if (mGame)
48
+        delete mGame;
49
+
41 50
     if (mMenu)
42 51
         delete mMenu;
43 52
 
@@ -338,25 +347,25 @@ int OpenRaider::bind(const char *action, const char *key) {
338 347
         tmp++;
339 348
 
340 349
     if (strcmp(tmp, "menu") == 0) {
341
-        return bind(menu, key);
350
+        return bind(menuAction, key);
342 351
     } else if (strcmp(tmp, "console") == 0) {
343
-        return bind(console, key);
352
+        return bind(consoleAction, key);
344 353
     } else if (strcmp(tmp, "forward") == 0) {
345
-        return bind(forward, key);
354
+        return bind(forwardAction, key);
346 355
     } else if (strcmp(tmp, "backward") == 0) {
347
-        return bind(backward, key);
356
+        return bind(backwardAction, key);
348 357
     } else if (strcmp(tmp, "left") == 0) {
349
-        return bind(left, key);
358
+        return bind(leftAction, key);
350 359
     } else if (strcmp(tmp, "right") == 0) {
351
-        return bind(right, key);
360
+        return bind(rightAction, key);
352 361
     } else if (strcmp(tmp, "jump") == 0) {
353
-        return bind(jump, key);
362
+        return bind(jumpAction, key);
354 363
     } else if (strcmp(tmp, "crouch") == 0) {
355
-        return bind(crouch, key);
364
+        return bind(crouchAction, key);
356 365
     } else if (strcmp(tmp, "use") == 0) {
357
-        return bind(use, key);
366
+        return bind(useAction, key);
358 367
     } else if (strcmp(tmp, "holster") == 0) {
359
-        return bind(holster, key);
368
+        return bind(holsterAction, key);
360 369
     } else {
361 370
         mConsole->print("bind-Error: Unknown action (%s --> %s)", key, action);
362 371
         return -1;
@@ -590,18 +599,24 @@ void OpenRaider::run() {
590 599
     while (mRunning) {
591 600
         clock_t startTime = systemTimerGet();
592 601
 
602
+        // Get keyboard and mouse input
593 603
         mWindow->eventHandling();
594 604
 
595
-        // Temp Debug
596
-        glClearColor(0.25f, 0.75f, 0.25f, 1.0f);
605
+        // Clear screen
606
+        glClearColor(0.00f, 0.00f, 0.00f, 1.0f);
597 607
         glClear(GL_COLOR_BUFFER_BIT);
598 608
 
609
+        // Draw game scene
610
+        if (mGame)
611
+            mGame->display();
612
+
613
+        // Draw 2D overlays (console and menu)
599 614
         mWindow->glEnter2D();
600 615
         mConsole->display();
601 616
         mMenu->display();
602 617
         mWindow->glExit2D();
603 618
 
604
-        // Put on screen
619
+        // Put new frame on screen
605 620
         mWindow->swapBuffersGL();
606 621
 
607 622
         // Fill map list after first render pass,
@@ -609,6 +624,8 @@ void OpenRaider::run() {
609 624
         if (!mMapListFilled)
610 625
             fillMapList();
611 626
 
627
+        // Check time it took to compute the last frame
628
+        // and delay for an appropriate amount of time
612 629
         clock_t stopTime = systemTimerGet();
613 630
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
614 631
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
@@ -616,28 +633,17 @@ void OpenRaider::run() {
616 633
 }
617 634
 
618 635
 void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
619
-    if ((keyBindings[menu] == key) && pressed) {
636
+    if ((keyBindings[menuAction] == key) && pressed) {
620 637
         mMenu->setVisible(!mMenu->isVisible());
621 638
     } else if (!mMenu->isVisible()) {
622
-        if ((keyBindings[console] == key) && pressed) {
639
+        if ((keyBindings[consoleAction] == key) && pressed) {
623 640
             mConsole->setVisible(!mConsole->isVisible());
624 641
         } else if (!mConsole->isVisible()) {
625
-            if (keyBindings[forward] == key) {
626
-
627
-            } else if (keyBindings[backward] == key) {
628
-
629
-            } else if (keyBindings[left] == key) {
630
-
631
-            } else if (keyBindings[right] == key) {
632
-
633
-            } else if (keyBindings[jump] == key) {
634
-
635
-            } else if (keyBindings[crouch] == key) {
636
-
637
-            } else if (keyBindings[use] == key) {
638
-
639
-            } else if (keyBindings[holster] == key) {
640
-
642
+            for (int i = forwardAction; i < ActionEventCount; i++) {
643
+                if (keyBindings[i] == key) {
644
+                    if (mGame)
645
+                        mGame->handleAction((ActionEvents)i, pressed);
646
+                }
641 647
             }
642 648
         } else {
643 649
             mConsole->handleKeyboard(key, pressed);
@@ -660,7 +666,10 @@ void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton
660 666
 }
661 667
 
662 668
 void OpenRaider::handleMouseMotion(int xrel, int yrel) {
663
-
669
+    if ((!mConsole->isVisible()) && (!mMenu->isVisible())) {
670
+        if (mGame)
671
+            mGame->handleMouseMotion(xrel, yrel);
672
+    }
664 673
 }
665 674
 
666 675
 void OpenRaider::handleMouseScroll(int xrel, int yrel) {

+ 1
- 1
src/main.cpp Visa fil

@@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
63 63
 
64 64
     // Create globals
65 65
     atexit(cleanupHandler);
66
-    printf("Initializing %s", VERSION);
66
+    printf("Initializing %s\n", VERSION);
67 67
     gOpenRaider = new OpenRaider();
68 68
 
69 69
     // Try to load a configuration

Laddar…
Avbryt
Spara