ソースを参照

Nicer style for GLString

Thomas Buck 11年前
コミット
ba6f732c64
4個のファイルの変更45行の追加139行の削除
  1. 1
    1
      Doxyfile
  2. 6
    6
      Makefile
  3. 22
    93
      src/GLString.cpp
  4. 16
    39
      test/GLString.cpp

+ 1
- 1
Doxyfile ファイルの表示

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

+ 6
- 6
Makefile ファイルの表示

202
 	$(BUILDDIR)/Sound.o \
202
 	$(BUILDDIR)/Sound.o \
203
 	$(BUILDDIR)/System.o \
203
 	$(BUILDDIR)/System.o \
204
 	$(BUILDDIR)/Texture.o \
204
 	$(BUILDDIR)/Texture.o \
205
-	$(BUILDDIR)/TGA.o \
205
+	$(BUILDDIR)/tga.o \
206
 	$(BUILDDIR)/TombRaider.o \
206
 	$(BUILDDIR)/TombRaider.o \
207
 	$(BUILDDIR)/Vector3d.o \
207
 	$(BUILDDIR)/Vector3d.o \
208
 	$(BUILDDIR)/ViewVolume.o \
208
 	$(BUILDDIR)/ViewVolume.o \
293
 TEST_MAP_TR2=~/.OpenRaider/paks/tr2/unwater.tr2
293
 TEST_MAP_TR2=~/.OpenRaider/paks/tr2/unwater.tr2
294
 TEST_MAP_TR1=~/.OpenRaider/paks/tr1/level1.phd
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
 test: test.build
298
 test: test.build
299
 	@-echo "================================================="
299
 	@-echo "================================================="
310
 	$(BUILD_TEST_DIR)/Sound.test
310
 	$(BUILD_TEST_DIR)/Sound.test
311
 	@-echo "================================================="
311
 	@-echo "================================================="
312
 	@-echo "Running TGA unit test"
312
 	@-echo "Running TGA unit test"
313
-	$(BUILD_TEST_DIR)/TGA.test
313
+	$(BUILD_TEST_DIR)/tga.test
314
 	@-echo "================================================="
314
 	@-echo "================================================="
315
 	@-echo "Running GLString unit test"
315
 	@-echo "Running GLString unit test"
316
 	$(BUILD_TEST_DIR)/GLString.test
316
 	$(BUILD_TEST_DIR)/GLString.test
332
 	@-mkdir -p $(BUILD_TEST_DIR)
332
 	@-mkdir -p $(BUILD_TEST_DIR)
333
 	$(CC) $(FLAGS_ALL) $(WARNINGS) -Iinclude $(TR_FLAGS) -o $(BUILD_TEST_DIR)/TombRaiderTest.o -c test/TombRaider.cpp
333
 	$(CC) $(FLAGS_ALL) $(WARNINGS) -Iinclude $(TR_FLAGS) -o $(BUILD_TEST_DIR)/TombRaiderTest.o -c test/TombRaider.cpp
334
 	$(MAKE) targets NAME=TombRaider.test BUILDDIR=$(BUILD_TEST_DIR) \
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
 	CFLAGS="$(BASE_CFLAGS) -g $(TR_FLAGS)" \
336
 	CFLAGS="$(BASE_CFLAGS) -g $(TR_FLAGS)" \
337
 	LD_FLAGS="-lz -lstdc++"
337
 	LD_FLAGS="-lz -lstdc++"
338
 
338
 
386
 
386
 
387
 #################################################################
387
 #################################################################
388
 
388
 
389
-TGA.test:
389
+tga.test:
390
 	mkdir -p $(BUILD_TEST_DIR)
390
 	mkdir -p $(BUILD_TEST_DIR)
391
 	$(CC) $(TEST_FLAGS) $(WARNINGS) \
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 ファイルの表示

3
  * \brief Open GL rendering font/string class
3
  * \brief Open GL rendering font/string class
4
  *
4
  *
5
  * \author Mongoose
5
  * \author Mongoose
6
+ * \author xythobuz
6
  */
7
  */
7
 
8
 
8
 #include <string.h>
9
 #include <string.h>
23
 #endif
24
 #endif
24
 
25
 
25
 
26
 
26
-GLString::GLString()
27
-{
27
+GLString::GLString() {
28
     _num_string_max = 0;
28
     _num_string_max = 0;
29
     _num_string = 0;
29
     _num_string = 0;
30
     _scale = 1.0;
30
     _scale = 1.0;
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
             if (_string[i].text)
38
             if (_string[i].text)
45
-            {
46
                 delete [] _string[i].text;
39
                 delete [] _string[i].text;
47
-            }
48
         }
40
         }
49
-
50
         delete [] _string;
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
     if (!max_strings)
47
     if (!max_strings)
58
-    {
59
         return;
48
         return;
60
-    }
61
 
49
 
62
     _num_string_max = max_strings;
50
     _num_string_max = max_strings;
63
     _string = new gl_string_t[max_strings];
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
     gl_string_t *str = GetString(string);
56
     gl_string_t *str = GetString(string);
70
-
71
-
72
     if (str && pos < str->len)
57
     if (str && pos < str->len)
73
-    {
74
         str->text[pos] = c;
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
     gl_string_t *str = GetString(string);
63
     gl_string_t *str = GetString(string);
82
-
83
-
84
     if (str)
64
     if (str)
85
-    {
86
         return str->len;
65
         return str->len;
87
-    }
88
-
89
     return 0;
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
     gl_string_t *str = GetString(string);
71
     gl_string_t *str = GetString(string);
96
-
97
-
98
     if (str)
72
     if (str)
99
-    {
100
         return str->text;
73
         return str->text;
101
-    }
102
-
103
     return 0;
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
     if (str)
80
     if (str)
115
-    {
116
         str->active = active;
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
     va_list args;
86
     va_list args;
124
     gl_string_t *str;
87
     gl_string_t *str;
125
     unsigned int len;
88
     unsigned int len;
126
 
89
 
127
-
128
     str = GetString(string);
90
     str = GetString(string);
129
 
91
 
130
-    if (s && s[0] && str)
131
-    {
92
+    if (s && s[0] && str) {
132
         str->active = true;
93
         str->active = true;
133
-
134
         len = strlen(s);
94
         len = strlen(s);
135
-
136
         if (len > str->len)
95
         if (len > str->len)
137
-        {
138
             len = str->len - 1;
96
             len = str->len - 1;
139
-        }
140
 
97
 
141
         va_start(args, s);
98
         va_start(args, s);
142
         vsnprintf(str->text, str->len-2, s, args);
99
         vsnprintf(str->text, str->len-2, s, args);
146
 }
103
 }
147
 
104
 
148
 
105
 
149
-void GLString::Scale(float scale)
150
-{
106
+void GLString::Scale(float scale) {
151
     _scale = scale;
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
     int sz = 60;
112
     int sz = 60;
158
     int n;
113
     int n;
159
     va_list args;
114
     va_list args;
162
     // Mongoose 2002.01.01, Only allow valid strings
117
     // Mongoose 2002.01.01, Only allow valid strings
163
     //   we must assume it's NULL terminated also if it passes...
118
     //   we must assume it's NULL terminated also if it passes...
164
     if (!string || !string[0])
119
     if (!string || !string[0])
165
-    {
166
         return -1;
120
         return -1;
167
-    }
168
 
121
 
169
     if (_num_string > _num_string_max)
122
     if (_num_string > _num_string_max)
170
-    {
171
         return -2;
123
         return -2;
172
-    }
173
 
124
 
174
     // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
125
     // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
175
     //   on first try
126
     //   on first try
188
     n = vsnprintf(_string[_num_string].text, sz, string, args);
139
     n = vsnprintf(_string[_num_string].text, sz, string, args);
189
 
140
 
190
     // Mongoose 2002.01.01, Realloc correct amount if truncated
141
     // Mongoose 2002.01.01, Realloc correct amount if truncated
191
-    while (1)
192
-    {
142
+    while (1) {
193
         if (n > -1 && n < sz)
143
         if (n > -1 && n < sz)
194
-        {
195
             break;
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
             sz = n + 1;
147
             sz = n + 1;
202
             delete [] _string[_num_string].text;
148
             delete [] _string[_num_string].text;
203
             _string[_num_string].text = new char[sz];
149
             _string[_num_string].text = new char[sz];
204
             n = vsnprintf(_string[_num_string].text, sz, string, args);
150
             n = vsnprintf(_string[_num_string].text, sz, string, args);
205
-
206
             break;
151
             break;
207
-        }
208
-        else // glibc 2.0
209
-        {
152
+        } else { // glibc 2.0
210
             sz *= 2;
153
             sz *= 2;
211
             delete [] _string[_num_string].text;
154
             delete [] _string[_num_string].text;
212
             _string[_num_string].text = new char[sz];
155
             _string[_num_string].text = new char[sz];
213
             n = vsnprintf(_string[_num_string].text, sz, string, args);
156
             n = vsnprintf(_string[_num_string].text, sz, string, args);
214
         }
157
         }
215
     }
158
     }
216
-
217
     va_end(args);
159
     va_end(args);
218
 
160
 
219
-
220
     // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
161
     // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
221
     _string[_num_string].len = sz;
162
     _string[_num_string].len = sz;
222
 
163
 
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
         if (_string[i].active)
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
     if (id < _num_string)
181
     if (id < _num_string)
250
-    {
251
         return _string + id;
182
         return _string + id;
252
-    }
253
-
254
     return NULL;
183
     return NULL;
255
 }
184
 }
256
 
185
 

+ 16
- 39
test/GLString.cpp ファイルの表示

22
 
22
 
23
 void swap_buffers();
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
     glMatrixMode(GL_PROJECTION);
26
     glMatrixMode(GL_PROJECTION);
31
-    aspect = (GLfloat)width/(GLfloat)height;
27
+    GLfloat aspect = (GLfloat)width/(GLfloat)height;
32
 
28
 
33
     // Mongoose 2002.01.01, Setup view volume, with a nice FOV
29
     // Mongoose 2002.01.01, Setup view volume, with a nice FOV
30
+    // xythobuz:
34
     // gluPerspective is deprecated!
31
     // gluPerspective is deprecated!
35
     // gluPerspective(40.0, aspect, 1, 2000);
32
     // gluPerspective(40.0, aspect, 1, 2000);
36
     // fix: http://stackoverflow.com/a/2417756
33
     // fix: http://stackoverflow.com/a/2417756
41
     glMatrixMode(GL_MODELVIEW);
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
     static float x = 0.0, y = 0.0, z = -150.0, r = 0.0;
42
     static float x = 0.0, y = 0.0, z = -150.0, r = 0.0;
48
 
43
 
49
-
50
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
44
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
51
     glLoadIdentity();
45
     glLoadIdentity();
52
 
46
 
81
     swap_buffers();
75
     swap_buffers();
82
 }
76
 }
83
 
77
 
84
-
85
-void swap_buffers()
86
-{
78
+void swap_buffers() {
87
     SDL_GL_SwapBuffers();
79
     SDL_GL_SwapBuffers();
88
 }
80
 }
89
 
81
 
90
 
82
 
91
-void shutdown_gl()
92
-{
83
+void shutdown_gl() {
93
     SDL_Quit();
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
     int i;
88
     int i;
99
     const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
89
     const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
100
 
90
 
139
     TEXT->setActive(3, true);
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
     SDL_Event event;
133
     SDL_Event event;
146
     unsigned int mkeys, mod, key;
134
     unsigned int mkeys, mod, key;
147
     int flags;
135
     int flags;
152
     char *driver = NULL;
140
     char *driver = NULL;
153
 #endif
141
 #endif
154
 
142
 
155
-
156
     // Setup clean up on exit
143
     // Setup clean up on exit
157
     atexit(shutdown_gl);
144
     atexit(shutdown_gl);
158
 
145
 
163
     SDL_Init(SDL_INIT_VIDEO);
150
     SDL_Init(SDL_INIT_VIDEO);
164
 
151
 
165
 #ifndef __APPLE__
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
         SDL_ClearError();
154
         SDL_ClearError();
169
 
155
 
170
         // Fallback 1
156
         // Fallback 1
171
-        if (SDL_GL_LoadLibrary("libGL.so") < 0)
172
-        {
157
+        if (SDL_GL_LoadLibrary("libGL.so") < 0) {
173
             SDL_ClearError();
158
             SDL_ClearError();
174
 
159
 
175
             // Fallback 2
160
             // Fallback 2
176
-            if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
177
-            {
161
+            if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
178
                 fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
162
                 fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
179
                 fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
163
                 fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
180
                 exit(1);
164
                 exit(1);
185
 
169
 
186
     flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
170
     flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
187
 
171
 
188
-    if (fullscreen)
189
-    {
172
+    if (fullscreen) {
190
         flags |= SDL_FULLSCREEN;
173
         flags |= SDL_FULLSCREEN;
191
         SDL_ShowCursor(SDL_DISABLE);
174
         SDL_ShowCursor(SDL_DISABLE);
192
     }
175
     }
203
     // Init rendering
186
     // Init rendering
204
     init_gl(width, height);
187
     init_gl(width, height);
205
 
188
 
206
-    for (;;)
207
-    {
189
+    for (;;) {
208
         // Pause for 10-20 ms
190
         // Pause for 10-20 ms
209
         SDL_Delay(10);
191
         SDL_Delay(10);
210
 
192
 
211
         event_display(width, height);
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
                 case SDL_QUIT:
197
                 case SDL_QUIT:
218
                     exit(0);
198
                     exit(0);
219
                     break;
199
                     break;
282
     return 0;
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
     TEXT = new GLString();
266
     TEXT = new GLString();
290
     main_gl(argc, argv);
267
     main_gl(argc, argv);
291
     return 0;
268
     return 0;

読み込み中…
キャンセル
保存