Browse Source

Added Game class.

Moved typedefs into global header.
Thomas Buck 10 years ago
parent
commit
6a2026b656
10 changed files with 188 additions and 91 deletions
  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 View File

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 View File

10
 
10
 
11
 #include <vector>
11
 #include <vector>
12
 
12
 
13
+#include "global.h"
13
 #include "Console.h"
14
 #include "Console.h"
15
+#include "Game.h"
14
 #include "Menu.h"
16
 #include "Menu.h"
15
 #include "Sound.h"
17
 #include "Sound.h"
16
 #include "Window.h"
18
 #include "Window.h"
21
 class OpenRaider {
23
 class OpenRaider {
22
 public:
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
      * \brief Constructs an object of OpenRaider
27
      * \brief Constructs an object of OpenRaider
41
      */
28
      */
72
     Sound *mSound;
59
     Sound *mSound;
73
     Menu *mMenu;
60
     Menu *mMenu;
74
     Console *mConsole;
61
     Console *mConsole;
62
+    Game *mGame;
75
 
63
 
76
     bool mMapListFilled;
64
     bool mMapListFilled;
77
     std::vector<char *> mMapList;
65
     std::vector<char *> mMapList;

+ 1
- 41
include/Window.h View File

10
 
10
 
11
 #include <ctime>
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
  * \brief Windowing interface
16
  * \brief Windowing interface

+ 68
- 0
include/global.h View File

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 View File

32
 
32
 
33
 # Set Source files
33
 # Set Source files
34
 set (SRCS ${SRCS} "Console.cpp")
34
 set (SRCS ${SRCS} "Console.cpp")
35
+set (SRCS ${SRCS} "Game.cpp")
35
 set (SRCS ${SRCS} "main.cpp")
36
 set (SRCS ${SRCS} "main.cpp")
36
 set (SRCS ${SRCS} "Menu.cpp")
37
 set (SRCS ${SRCS} "Menu.cpp")
37
 set (SRCS ${SRCS} "OpenRaider.cpp")
38
 set (SRCS ${SRCS} "OpenRaider.cpp")

+ 1
- 1
src/Console.cpp View File

98
         LINE_GEOMETRY(window);
98
         LINE_GEOMETRY(window);
99
 
99
 
100
         // Draw half-transparent *overlay*
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
         glDisable(GL_TEXTURE_2D);
102
         glDisable(GL_TEXTURE_2D);
103
         glRecti(0, 0, window->mWidth, window->mHeight / 2);
103
         glRecti(0, 0, window->mWidth, window->mHeight / 2);
104
         glEnable(GL_TEXTURE_2D);
104
         glEnable(GL_TEXTURE_2D);

+ 38
- 0
src/Game.cpp View File

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 View File

91
 
91
 
92
     if (mVisible) {
92
     if (mVisible) {
93
         // Draw half-transparent *overlay*
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
         glDisable(GL_TEXTURE_2D);
95
         glDisable(GL_TEXTURE_2D);
96
         glRecti(0, 0, window->mWidth, window->mHeight);
96
         glRecti(0, 0, window->mWidth, window->mHeight);
97
         glEnable(GL_TEXTURE_2D);
97
         glEnable(GL_TEXTURE_2D);

+ 41
- 32
src/OpenRaider.cpp View File

13
 #include "WindowSDL.h"
13
 #include "WindowSDL.h"
14
 
14
 
15
 #include "config.h"
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
 #include "utils/strings.h"
21
 #include "utils/strings.h"
17
 #include "utils/time.h"
22
 #include "utils/time.h"
18
 #include "OpenRaider.h"
23
 #include "OpenRaider.h"
32
     mConsole = new Console();
37
     mConsole = new Console();
33
     mSound = new Sound();
38
     mSound = new Sound();
34
     mWindow = new WindowSDL();
39
     mWindow = new WindowSDL();
40
+    mGame = NULL;
35
 
41
 
36
     for (int i = 0; i < ActionEventCount; i++)
42
     for (int i = 0; i < ActionEventCount; i++)
37
         keyBindings[i] = unknown;
43
         keyBindings[i] = unknown;
38
 }
44
 }
39
 
45
 
40
 OpenRaider::~OpenRaider() {
46
 OpenRaider::~OpenRaider() {
47
+    if (mGame)
48
+        delete mGame;
49
+
41
     if (mMenu)
50
     if (mMenu)
42
         delete mMenu;
51
         delete mMenu;
43
 
52
 
338
         tmp++;
347
         tmp++;
339
 
348
 
340
     if (strcmp(tmp, "menu") == 0) {
349
     if (strcmp(tmp, "menu") == 0) {
341
-        return bind(menu, key);
350
+        return bind(menuAction, key);
342
     } else if (strcmp(tmp, "console") == 0) {
351
     } else if (strcmp(tmp, "console") == 0) {
343
-        return bind(console, key);
352
+        return bind(consoleAction, key);
344
     } else if (strcmp(tmp, "forward") == 0) {
353
     } else if (strcmp(tmp, "forward") == 0) {
345
-        return bind(forward, key);
354
+        return bind(forwardAction, key);
346
     } else if (strcmp(tmp, "backward") == 0) {
355
     } else if (strcmp(tmp, "backward") == 0) {
347
-        return bind(backward, key);
356
+        return bind(backwardAction, key);
348
     } else if (strcmp(tmp, "left") == 0) {
357
     } else if (strcmp(tmp, "left") == 0) {
349
-        return bind(left, key);
358
+        return bind(leftAction, key);
350
     } else if (strcmp(tmp, "right") == 0) {
359
     } else if (strcmp(tmp, "right") == 0) {
351
-        return bind(right, key);
360
+        return bind(rightAction, key);
352
     } else if (strcmp(tmp, "jump") == 0) {
361
     } else if (strcmp(tmp, "jump") == 0) {
353
-        return bind(jump, key);
362
+        return bind(jumpAction, key);
354
     } else if (strcmp(tmp, "crouch") == 0) {
363
     } else if (strcmp(tmp, "crouch") == 0) {
355
-        return bind(crouch, key);
364
+        return bind(crouchAction, key);
356
     } else if (strcmp(tmp, "use") == 0) {
365
     } else if (strcmp(tmp, "use") == 0) {
357
-        return bind(use, key);
366
+        return bind(useAction, key);
358
     } else if (strcmp(tmp, "holster") == 0) {
367
     } else if (strcmp(tmp, "holster") == 0) {
359
-        return bind(holster, key);
368
+        return bind(holsterAction, key);
360
     } else {
369
     } else {
361
         mConsole->print("bind-Error: Unknown action (%s --> %s)", key, action);
370
         mConsole->print("bind-Error: Unknown action (%s --> %s)", key, action);
362
         return -1;
371
         return -1;
590
     while (mRunning) {
599
     while (mRunning) {
591
         clock_t startTime = systemTimerGet();
600
         clock_t startTime = systemTimerGet();
592
 
601
 
602
+        // Get keyboard and mouse input
593
         mWindow->eventHandling();
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
         glClear(GL_COLOR_BUFFER_BIT);
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
         mWindow->glEnter2D();
614
         mWindow->glEnter2D();
600
         mConsole->display();
615
         mConsole->display();
601
         mMenu->display();
616
         mMenu->display();
602
         mWindow->glExit2D();
617
         mWindow->glExit2D();
603
 
618
 
604
-        // Put on screen
619
+        // Put new frame on screen
605
         mWindow->swapBuffersGL();
620
         mWindow->swapBuffersGL();
606
 
621
 
607
         // Fill map list after first render pass,
622
         // Fill map list after first render pass,
609
         if (!mMapListFilled)
624
         if (!mMapListFilled)
610
             fillMapList();
625
             fillMapList();
611
 
626
 
627
+        // Check time it took to compute the last frame
628
+        // and delay for an appropriate amount of time
612
         clock_t stopTime = systemTimerGet();
629
         clock_t stopTime = systemTimerGet();
613
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
630
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
614
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
631
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
616
 }
633
 }
617
 
634
 
618
 void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
635
 void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
619
-    if ((keyBindings[menu] == key) && pressed) {
636
+    if ((keyBindings[menuAction] == key) && pressed) {
620
         mMenu->setVisible(!mMenu->isVisible());
637
         mMenu->setVisible(!mMenu->isVisible());
621
     } else if (!mMenu->isVisible()) {
638
     } else if (!mMenu->isVisible()) {
622
-        if ((keyBindings[console] == key) && pressed) {
639
+        if ((keyBindings[consoleAction] == key) && pressed) {
623
             mConsole->setVisible(!mConsole->isVisible());
640
             mConsole->setVisible(!mConsole->isVisible());
624
         } else if (!mConsole->isVisible()) {
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
         } else {
648
         } else {
643
             mConsole->handleKeyboard(key, pressed);
649
             mConsole->handleKeyboard(key, pressed);
660
 }
666
 }
661
 
667
 
662
 void OpenRaider::handleMouseMotion(int xrel, int yrel) {
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
 void OpenRaider::handleMouseScroll(int xrel, int yrel) {
675
 void OpenRaider::handleMouseScroll(int xrel, int yrel) {

+ 1
- 1
src/main.cpp View File

63
 
63
 
64
     // Create globals
64
     // Create globals
65
     atexit(cleanupHandler);
65
     atexit(cleanupHandler);
66
-    printf("Initializing %s", VERSION);
66
+    printf("Initializing %s\n", VERSION);
67
     gOpenRaider = new OpenRaider();
67
     gOpenRaider = new OpenRaider();
68
 
68
 
69
     // Try to load a configuration
69
     // Try to load a configuration

Loading…
Cancel
Save