Browse Source

Added main loop, started event handling

Thomas Buck 10 years ago
parent
commit
a72a1e7f5b
9 changed files with 119 additions and 11 deletions
  1. 2
    0
      include/Config.h.in
  2. 6
    0
      include/OpenRaider.h
  3. 8
    0
      include/Window.h
  4. 6
    0
      include/WindowSDL.h
  5. 3
    1
      include/utils/time.h
  6. 29
    1
      src/OpenRaider.cpp
  7. 57
    5
      src/WindowSDL.cpp
  8. 7
    1
      src/main.cpp
  9. 1
    3
      src/utils/time.cpp

+ 2
- 0
include/Config.h.in View File

@@ -5,3 +5,5 @@
5 5
 #define DEFAULT_CONFIG_FILE "OpenRaider.init"
6 6
 #define DEFAULT_WIDTH 640
7 7
 #define DEFAULT_HEIGHT 480
8
+
9
+#define MAXIMUM_FPS 100

+ 6
- 0
include/OpenRaider.h View File

@@ -34,7 +34,13 @@ public:
34 34
 
35 35
     int initialize();
36 36
 
37
+    void run();
38
+
39
+    void cleanup();
40
+
37 41
 private:
42
+    bool mInit;
43
+    bool mRunning;
38 44
     Window *mWindow;
39 45
 };
40 46
 

+ 8
- 0
include/Window.h View File

@@ -8,6 +8,8 @@
8 8
 #ifndef _WINDOW_H_
9 9
 #define _WINDOW_H_
10 10
 
11
+#include <ctime>
12
+
11 13
 typedef struct {
12 14
     char *text;
13 15
     unsigned int x;
@@ -35,11 +37,17 @@ public:
35 37
 
36 38
     virtual int initialize() = 0;
37 39
 
40
+    virtual void eventHandling() = 0;
41
+
38 42
     virtual void writeString(WindowString *s) = 0;
39 43
 
44
+    virtual void delay(clock_t ms) = 0;
45
+
40 46
     virtual void swapBuffersGL() = 0;
41 47
 
42 48
     virtual void resizeGL(unsigned int w, unsigned int h);
49
+
50
+    virtual void cleanup() = 0;
43 51
 };
44 52
 
45 53
 #endif

+ 6
- 0
include/WindowSDL.h View File

@@ -38,10 +38,16 @@ public:
38 38
 
39 39
     virtual int initialize();
40 40
 
41
+    virtual void eventHandling();
42
+
41 43
     virtual void writeString(WindowString *s);
42 44
 
45
+    virtual void delay(clock_t ms);
46
+
43 47
     virtual void swapBuffersGL();
44 48
 
49
+    virtual void cleanup();
50
+
45 51
 private:
46 52
     bool mInit;
47 53
     char *mDriver;

+ 3
- 1
include/utils/time.h View File

@@ -8,11 +8,13 @@
8 8
 #ifndef _UTILS_TIME_H_
9 9
 #define _UTILS_TIME_H_
10 10
 
11
+#include <ctime>
12
+
11 13
 /*!
12 14
  * \brief Read the system timer
13 15
  * \returns number of ticks
14 16
  */
15
-unsigned int systemTimerGet();
17
+clock_t systemTimerGet();
16 18
 
17 19
 /*!
18 20
  * \brief Reset the system timer

+ 29
- 1
src/OpenRaider.cpp View File

@@ -10,10 +10,14 @@
10 10
 
11 11
 #include "WindowSDL.h"
12 12
 
13
+#include "config.h"
13 14
 #include "utils/strings.h"
15
+#include "utils/time.h"
14 16
 #include "OpenRaider.h"
15 17
 
16 18
 OpenRaider::OpenRaider() {
19
+    mInit = false;
20
+    mRunning = false;
17 21
     mWindow = NULL;
18 22
 }
19 23
 
@@ -33,10 +37,34 @@ int OpenRaider::loadConfig(const char *config) {
33 37
 
34 38
 int OpenRaider::initialize() {
35 39
     assert(mWindow == NULL);
40
+    assert(mInit == false);
41
+    assert(mRunning == false);
36 42
 
37 43
     mWindow = new WindowSDL();
38
-    mWindow->initialize();
44
+    if (mWindow->initialize() != 0)
45
+        return -1;
46
+
47
+    mInit = true;
39 48
 
40 49
     return 0;
41 50
 }
42 51
 
52
+void OpenRaider::run() {
53
+    assert(mInit == true);
54
+    assert(mRunning == false);
55
+
56
+    mRunning = true;
57
+    while (mRunning) {
58
+        clock_t startTime = systemTimerGet();
59
+
60
+        mWindow->eventHandling();
61
+
62
+        clock_t stopTime = systemTimerGet();
63
+        mWindow->delay((1000 / MAXIMUM_FPS) - (stopTime - startTime));
64
+    }
65
+}
66
+
67
+void OpenRaider::cleanup() {
68
+    mWindow->cleanup();
69
+}
70
+

+ 57
- 5
src/WindowSDL.cpp View File

@@ -31,6 +31,7 @@ WindowSDL::~WindowSDL() {
31 31
 void WindowSDL::setDriver(const char *driver) {
32 32
     assert(driver != NULL);
33 33
     assert(driver[0] != '\0');
34
+    assert(mInit == false);
34 35
 
35 36
     mDriver = bufferString("%s", driver);
36 37
 }
@@ -73,21 +74,24 @@ void WindowSDL::setMousegrab(bool grab) {
73 74
 }
74 75
 
75 76
 int WindowSDL::initialize() {
77
+    assert(mInit == false);
78
+
76 79
     if (SDL_Init(SDL_INIT_VIDEO) != 0) {
77
-        printf("SDL Error: %s\n", SDL_GetError());
80
+        printf("SDL_Init Error: %s\n", SDL_GetError());
78 81
         return -1;
79 82
     }
80 83
 
81 84
 #ifndef __APPLE__
82 85
     assert(mDriver != NULL);
83 86
     assert(mDriver[0] != '\0');
87
+
84 88
     if (SDL_GL_LoadLibrary(mDriver) < 0) {
85 89
         SDL_ClearError();
86 90
         if (SDL_GL_LoadLibrary("libGL.so") < 0) {
87 91
             SDL_ClearError();
88 92
             if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
89 93
                 printf("Could not load OpenGL driver!\n");
90
-                printf("SDL Error: %s\n", SDL_GetError());
94
+                printf("SDL_GL_LoadLibrary Error: %s\n", SDL_GetError());
91 95
                 return -2;
92 96
             }
93 97
         }
@@ -106,7 +110,7 @@ int WindowSDL::initialize() {
106 110
         || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
107 111
         || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
108 112
         || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
109
-        printf("SDL Error: %s\n", SDL_GetError());
113
+        printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
110 114
         mInit = false;
111 115
         return -3;
112 116
     }
@@ -114,14 +118,14 @@ int WindowSDL::initialize() {
114 118
     mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
115 119
                 mWidth, mHeight, flags);
116 120
     if (mWindow == NULL) {
117
-        printf("SDL Error: %s\n", SDL_GetError());
121
+        printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
118 122
         mInit = false;
119 123
         return -4;
120 124
     }
121 125
 
122 126
     mGLContext = SDL_GL_CreateContext(mWindow);
123 127
     if (mGLContext == NULL) {
124
-        printf("SDL Error: %s\n", SDL_GetError());
128
+        printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
125 129
         mInit = false;
126 130
         return -5;
127 131
     }
@@ -131,6 +135,39 @@ int WindowSDL::initialize() {
131 135
     return 0;
132 136
 }
133 137
 
138
+void WindowSDL::eventHandling() {
139
+    SDL_Event event;
140
+
141
+    assert(mInit == true);
142
+
143
+    while(SDL_PollEvent(&event)) {
144
+        switch (event.type) {
145
+            case SDL_QUIT:
146
+                exit(0);
147
+                break;
148
+
149
+            case SDL_MOUSEMOTION:
150
+
151
+                break;
152
+
153
+            case SDL_MOUSEBUTTONDOWN:
154
+            case SDL_MOUSEBUTTONUP:
155
+
156
+                break;
157
+
158
+            case SDL_KEYDOWN:
159
+            case SDL_KEYUP:
160
+
161
+                break;
162
+
163
+            case SDL_WINDOWEVENT:
164
+                if (event.window.event == SDL_WINDOWEVENT_RESIZED)
165
+                    setSize(event.window.data1, event.window.data2);
166
+                break;
167
+        }
168
+    }
169
+}
170
+
134 171
 void WindowSDL::writeString(WindowString *s) {
135 172
     assert(s != NULL);
136 173
     assert(s->text != NULL);
@@ -139,7 +176,22 @@ void WindowSDL::writeString(WindowString *s) {
139 176
 
140 177
 }
141 178
 
179
+void WindowSDL::delay(clock_t ms) {
180
+    assert(mInit == true);
181
+
182
+    SDL_Delay(ms);
183
+}
184
+
142 185
 void WindowSDL::swapBuffersGL() {
186
+    assert(mInit == true);
187
+
143 188
     SDL_GL_SwapWindow(mWindow);
144 189
 }
145 190
 
191
+void WindowSDL::cleanup() {
192
+    if (mInit) {
193
+        //SDL_QuitSubSystem(SDL_OPENGL);
194
+        SDL_Quit();
195
+    }
196
+}
197
+

+ 7
- 1
src/main.cpp View File

@@ -16,8 +16,10 @@
16 16
 OpenRaider *gOpenRaider = NULL;
17 17
 
18 18
 void cleanupHandler() {
19
-    if (gOpenRaider)
19
+    if (gOpenRaider) {
20
+        gOpenRaider->cleanup();
20 21
         delete gOpenRaider;
22
+    }
21 23
 }
22 24
 
23 25
 int main(int argc, char *argv[]) {
@@ -71,6 +73,10 @@ int main(int argc, char *argv[]) {
71 73
     // Initialize the "subsystems"
72 74
     gOpenRaider->initialize();
73 75
 
76
+    // Enter Main loop
77
+    printf("Starting " VERSION "\n");
78
+    gOpenRaider->run();
79
+
74 80
     return 0;
75 81
 }
76 82
 

+ 1
- 3
src/utils/time.cpp View File

@@ -5,8 +5,6 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
-#include <ctime>
9
-
10 8
 #include "utils/time.h"
11 9
 
12 10
 #define CLOCKS_PER_MS (CLOCKS_PER_SEC / 1000)
@@ -14,7 +12,7 @@
14 12
 clock_t system_timer_start;
15 13
 clock_t system_timer_stop;
16 14
 
17
-unsigned int systemTimerGet() {
15
+clock_t systemTimerGet() {
18 16
     system_timer_stop = clock();
19 17
     return (system_timer_stop - system_timer_start) / CLOCKS_PER_MS;
20 18
 }

Loading…
Cancel
Save