Przeglądaj źródła

Nicer style for GLString

Thomas Buck 11 lat temu
rodzic
commit
ba6f732c64
4 zmienionych plików z 45 dodań i 139 usunięć
  1. 1
    1
      Doxyfile
  2. 6
    6
      Makefile
  3. 22
    93
      src/GLString.cpp
  4. 16
    39
      test/GLString.cpp

+ 1
- 1
Doxyfile Wyświetl plik

@@ -38,7 +38,7 @@ PROJECT_NAME           = OpenRaider
38 38
 # could be handy for archiving the generated documentation or if some version
39 39
 # control system is used.
40 40
 
41
-PROJECT_NUMBER         = 0.1.2
41
+PROJECT_NUMBER         = 0.1.2-dev
42 42
 
43 43
 # Using the PROJECT_BRIEF tag one can provide an optional one line description
44 44
 # for a project that appears at the top of each page and should give viewer a

+ 6
- 6
Makefile Wyświetl plik

@@ -202,7 +202,7 @@ OBJS = \
202 202
 	$(BUILDDIR)/Sound.o \
203 203
 	$(BUILDDIR)/System.o \
204 204
 	$(BUILDDIR)/Texture.o \
205
-	$(BUILDDIR)/TGA.o \
205
+	$(BUILDDIR)/tga.o \
206 206
 	$(BUILDDIR)/TombRaider.o \
207 207
 	$(BUILDDIR)/Vector3d.o \
208 208
 	$(BUILDDIR)/ViewVolume.o \
@@ -293,7 +293,7 @@ TEST_MAP_TR3=~/.OpenRaider/paks/tr3/scotland.tr2
293 293
 TEST_MAP_TR2=~/.OpenRaider/paks/tr2/unwater.tr2
294 294
 TEST_MAP_TR1=~/.OpenRaider/paks/tr1/level1.phd
295 295
 
296
-test.build: Matrix.test Math.test Memory.test Network.test Sound.test TGA.test GLString.test TombRaider.test
296
+test.build: Matrix.test Math.test Memory.test Network.test Sound.test tga.test GLString.test TombRaider.test
297 297
 
298 298
 test: test.build
299 299
 	@-echo "================================================="
@@ -310,7 +310,7 @@ test: test.build
310 310
 	$(BUILD_TEST_DIR)/Sound.test
311 311
 	@-echo "================================================="
312 312
 	@-echo "Running TGA unit test"
313
-	$(BUILD_TEST_DIR)/TGA.test
313
+	$(BUILD_TEST_DIR)/tga.test
314 314
 	@-echo "================================================="
315 315
 	@-echo "Running GLString unit test"
316 316
 	$(BUILD_TEST_DIR)/GLString.test
@@ -332,7 +332,7 @@ TombRaider.test:
332 332
 	@-mkdir -p $(BUILD_TEST_DIR)
333 333
 	$(CC) $(FLAGS_ALL) $(WARNINGS) -Iinclude $(TR_FLAGS) -o $(BUILD_TEST_DIR)/TombRaiderTest.o -c test/TombRaider.cpp
334 334
 	$(MAKE) targets NAME=TombRaider.test BUILDDIR=$(BUILD_TEST_DIR) \
335
-	OBJS="$(BUILD_TEST_DIR)/TombRaiderTest.o $(BUILD_TEST_DIR)/TombRaider.o $(BUILD_TEST_DIR)/TGA.o $(BUILD_TEST_DIR)/memory_test.o" \
335
+	OBJS="$(BUILD_TEST_DIR)/TombRaiderTest.o $(BUILD_TEST_DIR)/TombRaider.o $(BUILD_TEST_DIR)/tga.o $(BUILD_TEST_DIR)/memory_test.o" \
336 336
 	CFLAGS="$(BASE_CFLAGS) -g $(TR_FLAGS)" \
337 337
 	LD_FLAGS="-lz -lstdc++"
338 338
 
@@ -386,10 +386,10 @@ Sound.test:
386 386
 
387 387
 #################################################################
388 388
 
389
-TGA.test:
389
+tga.test:
390 390
 	mkdir -p $(BUILD_TEST_DIR)
391 391
 	$(CC) $(TEST_FLAGS) $(WARNINGS) \
392
-		src/TGA.cpp test/TGA.cpp -o $(BUILD_TEST_DIR)/TGA.test
392
+		src/tga.cpp test/tga.cpp -o $(BUILD_TEST_DIR)/tga.test
393 393
 
394 394
 #################################################################
395 395
 

+ 22
- 93
src/GLString.cpp Wyświetl plik

@@ -3,6 +3,7 @@
3 3
  * \brief Open GL rendering font/string class
4 4
  *
5 5
  * \author Mongoose
6
+ * \author xythobuz
6 7
  */
7 8
 
8 9
 #include <string.h>
@@ -23,8 +24,7 @@
23 24
 #endif
24 25
 
25 26
 
26
-GLString::GLString()
27
-{
27
+GLString::GLString() {
28 28
     _num_string_max = 0;
29 29
     _num_string = 0;
30 30
     _scale = 1.0;
@@ -32,111 +32,68 @@ GLString::GLString()
32 32
 }
33 33
 
34 34
 
35
-GLString::~GLString()
36
-{
37
-    unsigned int i;
38
-
39
-    if (_string)
40
-    {
41
-
42
-        for (i = 0; i < _num_string; ++i)
43
-        {
35
+GLString::~GLString() {
36
+    if (_string) {
37
+        for (unsigned int i = 0; i < _num_string; ++i) {
44 38
             if (_string[i].text)
45
-            {
46 39
                 delete [] _string[i].text;
47
-            }
48 40
         }
49
-
50 41
         delete [] _string;
51 42
     }
52 43
 }
53 44
 
54 45
 
55
-void GLString::Init(unsigned int max_strings)
56
-{
46
+void GLString::Init(unsigned int max_strings) {
57 47
     if (!max_strings)
58
-    {
59 48
         return;
60
-    }
61 49
 
62 50
     _num_string_max = max_strings;
63 51
     _string = new gl_string_t[max_strings];
64 52
 }
65 53
 
66 54
 
67
-void GLString::SetChar(unsigned int string, unsigned int pos, char c)
68
-{
55
+void GLString::SetChar(unsigned int string, unsigned int pos, char c) {
69 56
     gl_string_t *str = GetString(string);
70
-
71
-
72 57
     if (str && pos < str->len)
73
-    {
74 58
         str->text[pos] = c;
75
-    }
76 59
 }
77 60
 
78 61
 
79
-unsigned int GLString::GetStringLen(unsigned int string)
80
-{
62
+unsigned int GLString::GetStringLen(unsigned int string) {
81 63
     gl_string_t *str = GetString(string);
82
-
83
-
84 64
     if (str)
85
-    {
86 65
         return str->len;
87
-    }
88
-
89 66
     return 0;
90 67
 }
91 68
 
92 69
 
93
-char *GLString::GetBuffer(unsigned int string)
94
-{
70
+char *GLString::GetBuffer(unsigned int string) {
95 71
     gl_string_t *str = GetString(string);
96
-
97
-
98 72
     if (str)
99
-    {
100 73
         return str->text;
101
-    }
102
-
103 74
     return 0;
104 75
 }
105 76
 
106 77
 
107
-void GLString::setActive(unsigned int string, bool active)
108
-{
109
-    gl_string_t *str;
110
-
111
-
112
-    str = GetString(string);
113
-
78
+void GLString::setActive(unsigned int string, bool active) {
79
+    gl_string_t *str = GetString(string);
114 80
     if (str)
115
-    {
116 81
         str->active = active;
117
-    }
118 82
 }
119 83
 
120 84
 
121
-void GLString::SetString(unsigned int string, const char *s, ...)
122
-{
85
+void GLString::SetString(unsigned int string, const char *s, ...) {
123 86
     va_list args;
124 87
     gl_string_t *str;
125 88
     unsigned int len;
126 89
 
127
-
128 90
     str = GetString(string);
129 91
 
130
-    if (s && s[0] && str)
131
-    {
92
+    if (s && s[0] && str) {
132 93
         str->active = true;
133
-
134 94
         len = strlen(s);
135
-
136 95
         if (len > str->len)
137
-        {
138 96
             len = str->len - 1;
139
-        }
140 97
 
141 98
         va_start(args, s);
142 99
         vsnprintf(str->text, str->len-2, s, args);
@@ -146,14 +103,12 @@ void GLString::SetString(unsigned int string, const char *s, ...)
146 103
 }
147 104
 
148 105
 
149
-void GLString::Scale(float scale)
150
-{
106
+void GLString::Scale(float scale) {
151 107
     _scale = scale;
152 108
 }
153 109
 
154 110
 
155
-int GLString::glPrintf(int x, int y, const char *string, ...)
156
-{
111
+int GLString::glPrintf(int x, int y, const char *string, ...) {
157 112
     int sz = 60;
158 113
     int n;
159 114
     va_list args;
@@ -162,14 +117,10 @@ int GLString::glPrintf(int x, int y, const char *string, ...)
162 117
     // Mongoose 2002.01.01, Only allow valid strings
163 118
     //   we must assume it's NULL terminated also if it passes...
164 119
     if (!string || !string[0])
165
-    {
166 120
         return -1;
167
-    }
168 121
 
169 122
     if (_num_string > _num_string_max)
170
-    {
171 123
         return -2;
172
-    }
173 124
 
174 125
     // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
175 126
     //   on first try
@@ -188,35 +139,25 @@ int GLString::glPrintf(int x, int y, const char *string, ...)
188 139
     n = vsnprintf(_string[_num_string].text, sz, string, args);
189 140
 
190 141
     // Mongoose 2002.01.01, Realloc correct amount if truncated
191
-    while (1)
192
-    {
142
+    while (1) {
193 143
         if (n > -1 && n < sz)
194
-        {
195 144
             break;
196
-        }
197 145
 
198
-        // Mongoose 2002.01.01, For glibc 2.1
199
-        if (n > -1)
200
-        {
146
+        if (n > -1) { // glibc 2.1
201 147
             sz = n + 1;
202 148
             delete [] _string[_num_string].text;
203 149
             _string[_num_string].text = new char[sz];
204 150
             n = vsnprintf(_string[_num_string].text, sz, string, args);
205
-
206 151
             break;
207
-        }
208
-        else // glibc 2.0
209
-        {
152
+        } else { // glibc 2.0
210 153
             sz *= 2;
211 154
             delete [] _string[_num_string].text;
212 155
             _string[_num_string].text = new char[sz];
213 156
             n = vsnprintf(_string[_num_string].text, sz, string, args);
214 157
         }
215 158
     }
216
-
217 159
     va_end(args);
218 160
 
219
-
220 161
     // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
221 162
     _string[_num_string].len = sz;
222 163
 
@@ -228,29 +169,17 @@ int GLString::glPrintf(int x, int y, const char *string, ...)
228 169
 }
229 170
 
230 171
 
231
-void GLString::Render(int width, int height)
232
-{
233
-    unsigned int i;
234
-
235
-    for (i = 0; i < _num_string; ++i)
236
-    {
172
+void GLString::Render(int width, int height) {
173
+    for (unsigned int i = 0; i < _num_string; ++i) {
237 174
         if (_string[i].active)
238
-        {
239
-            glPrint2d(_string[i].x, _string[i].y,
240
-                    _string[i].scale,
241
-                    _string[i].text);
242
-        }
175
+            glPrint2d(_string[i].x, _string[i].y, _string[i].scale, _string[i].text);
243 176
     }
244 177
 }
245 178
 
246 179
 
247
-gl_string_t *GLString::GetString(unsigned int id)
248
-{
180
+gl_string_t *GLString::GetString(unsigned int id) {
249 181
     if (id < _num_string)
250
-    {
251 182
         return _string + id;
252
-    }
253
-
254 183
     return NULL;
255 184
 }
256 185
 

+ 16
- 39
test/GLString.cpp Wyświetl plik

@@ -22,15 +22,12 @@ Texture gTexture;
22 22
 
23 23
 void swap_buffers();
24 24
 
25
-
26
-void event_resize(int width, int height)
27
-{
28
-    GLfloat aspect;
29
-
25
+void event_resize(int width, int height) {
30 26
     glMatrixMode(GL_PROJECTION);
31
-    aspect = (GLfloat)width/(GLfloat)height;
27
+    GLfloat aspect = (GLfloat)width/(GLfloat)height;
32 28
 
33 29
     // Mongoose 2002.01.01, Setup view volume, with a nice FOV
30
+    // xythobuz:
34 31
     // gluPerspective is deprecated!
35 32
     // gluPerspective(40.0, aspect, 1, 2000);
36 33
     // fix: http://stackoverflow.com/a/2417756
@@ -41,12 +38,9 @@ void event_resize(int width, int height)
41 38
     glMatrixMode(GL_MODELVIEW);
42 39
 }
43 40
 
44
-
45
-void event_display(int width, int height)
46
-{
41
+void event_display(int width, int height) {
47 42
     static float x = 0.0, y = 0.0, z = -150.0, r = 0.0;
48 43
 
49
-
50 44
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
51 45
     glLoadIdentity();
52 46
 
@@ -81,20 +75,16 @@ void event_display(int width, int height)
81 75
     swap_buffers();
82 76
 }
83 77
 
84
-
85
-void swap_buffers()
86
-{
78
+void swap_buffers() {
87 79
     SDL_GL_SwapBuffers();
88 80
 }
89 81
 
90 82
 
91
-void shutdown_gl()
92
-{
83
+void shutdown_gl() {
93 84
     SDL_Quit();
94 85
 }
95 86
 
96
-void init_gl(unsigned int width, unsigned int height)
97
-{
87
+void init_gl(unsigned int width, unsigned int height) {
98 88
     int i;
99 89
     const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
100 90
 
@@ -139,9 +129,7 @@ void init_gl(unsigned int width, unsigned int height)
139 129
     TEXT->setActive(3, true);
140 130
 }
141 131
 
142
-
143
-int main_gl(int argc, char *argv[])
144
-{
132
+int main_gl(int argc, char *argv[]) {
145 133
     SDL_Event event;
146 134
     unsigned int mkeys, mod, key;
147 135
     int flags;
@@ -152,7 +140,6 @@ int main_gl(int argc, char *argv[])
152 140
     char *driver = NULL;
153 141
 #endif
154 142
 
155
-
156 143
     // Setup clean up on exit
157 144
     atexit(shutdown_gl);
158 145
 
@@ -163,18 +150,15 @@ int main_gl(int argc, char *argv[])
163 150
     SDL_Init(SDL_INIT_VIDEO);
164 151
 
165 152
 #ifndef __APPLE__
166
-    if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0)
167
-    {
153
+    if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0) {
168 154
         SDL_ClearError();
169 155
 
170 156
         // Fallback 1
171
-        if (SDL_GL_LoadLibrary("libGL.so") < 0)
172
-        {
157
+        if (SDL_GL_LoadLibrary("libGL.so") < 0) {
173 158
             SDL_ClearError();
174 159
 
175 160
             // Fallback 2
176
-            if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
177
-            {
161
+            if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
178 162
                 fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
179 163
                 fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
180 164
                 exit(1);
@@ -185,8 +169,7 @@ int main_gl(int argc, char *argv[])
185 169
 
186 170
     flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
187 171
 
188
-    if (fullscreen)
189
-    {
172
+    if (fullscreen) {
190 173
         flags |= SDL_FULLSCREEN;
191 174
         SDL_ShowCursor(SDL_DISABLE);
192 175
     }
@@ -203,17 +186,14 @@ int main_gl(int argc, char *argv[])
203 186
     // Init rendering
204 187
     init_gl(width, height);
205 188
 
206
-    for (;;)
207
-    {
189
+    for (;;) {
208 190
         // Pause for 10-20 ms
209 191
         SDL_Delay(10);
210 192
 
211 193
         event_display(width, height);
212 194
 
213
-        while (SDL_PollEvent(&event))
214
-        {
215
-            switch (event.type)
216
-            {
195
+        while (SDL_PollEvent(&event)) {
196
+            switch (event.type) {
217 197
                 case SDL_QUIT:
218 198
                     exit(0);
219 199
                     break;
@@ -282,10 +262,7 @@ int main_gl(int argc, char *argv[])
282 262
     return 0;
283 263
 }
284 264
 
285
-int main(int argc, char *argv[])
286
-{
287
-    printf("[GLString class test]\n");
288
-
265
+int main(int argc, char *argv[]) {
289 266
     TEXT = new GLString();
290 267
     main_gl(argc, argv);
291 268
     return 0;

Ładowanie…
Anuluj
Zapisz