Browse Source

Removed duplicated buffering code from GLString.

Also more asserts.
Thomas Buck 10 years ago
parent
commit
58c9004361
5 changed files with 85 additions and 122 deletions
  1. 9
    1
      include/utils/strings.h
  2. 34
    73
      src/GLString.cpp
  3. 18
    31
      src/Sound.cpp
  4. 16
    12
      src/System.cpp
  5. 8
    5
      src/utils/strings.cpp

+ 9
- 1
include/utils/strings.h View File

@@ -19,7 +19,15 @@ bool stringEndsWith(const char *str, const char *suffix);
19 19
 
20 20
 /*!
21 21
  * \brief Generates a buffered string for the printf call
22
- * \param string Format string like for printf
22
+ * \param string format string like for printf
23
+ * \param args arguments matching format string
24
+ * \returns string in a buffer
25
+ */
26
+char *bufferString(const char *string, va_list args) __attribute__((format(printf, 1, 0)));
27
+
28
+/*!
29
+ * \brief Generates a buffered string for the printf call
30
+ * \param string format string like for printf
23 31
  * \returns string in a buffer
24 32
  */
25 33
 char *bufferString(const char *string, ...) __attribute__((format(printf, 1, 2)));

+ 34
- 73
src/GLString.cpp View File

@@ -10,6 +10,7 @@
10 10
 #include <stdio.h>
11 11
 #include <stdlib.h>
12 12
 #include <stdarg.h>
13
+#include <assert.h>
13 14
 
14 15
 #ifdef __APPLE__
15 16
 #include <OpenGL/gl.h>
@@ -18,6 +19,7 @@
18 19
 #endif
19 20
 
20 21
 #include "Texture.h"
22
+#include "utils/strings.h"
21 23
 #include "GLString.h"
22 24
 
23 25
 GLString::GLString() {
@@ -40,8 +42,7 @@ GLString::~GLString() {
40 42
 
41 43
 
42 44
 void GLString::Init(unsigned int max_strings) {
43
-    if (!max_strings)
44
-        return;
45
+    assert(max_strings > 0);
45 46
 
46 47
     _num_string_max = max_strings;
47 48
     _string = new gl_string_t[max_strings];
@@ -50,121 +51,82 @@ void GLString::Init(unsigned int max_strings) {
50 51
 
51 52
 void GLString::SetChar(unsigned int string, unsigned int pos, char c) {
52 53
     gl_string_t *str = GetString(string);
53
-    if (str && pos < str->len)
54
+    assert(str != NULL);
55
+    if (pos < str->len)
54 56
         str->text[pos] = c;
55 57
 }
56 58
 
57 59
 
58 60
 unsigned int GLString::GetStringLen(unsigned int string) {
59 61
     gl_string_t *str = GetString(string);
60
-    if (str)
61
-        return str->len;
62
-    return 0;
62
+    assert(str != NULL);
63
+    return str->len;
63 64
 }
64 65
 
65 66
 
66 67
 char *GLString::GetBuffer(unsigned int string) {
67 68
     gl_string_t *str = GetString(string);
68
-    if (str)
69
-        return str->text;
70
-    return 0;
69
+    assert(str != NULL);
70
+    return str->text;
71 71
 }
72 72
 
73 73
 
74 74
 void GLString::setActive(unsigned int string, bool active) {
75 75
     gl_string_t *str = GetString(string);
76
-    if (str)
77
-        str->active = active;
76
+    assert(str != NULL);
77
+    str->active = active;
78 78
 }
79 79
 
80 80
 
81 81
 void GLString::SetString(unsigned int string, const char *s, ...) {
82 82
     va_list args;
83
-    gl_string_t *str;
83
+    gl_string_t *str = GetString(string);
84 84
 
85
-    str = GetString(string);
85
+    assert(s != NULL);
86
+    assert(s[0] != '\0');
87
+    assert(str != NULL);
86 88
 
87
-    if (s && s[0] && str) {
88
-        str->active = true;
89
+    delete [] str->text;
89 90
 
90
-        va_start(args, s);
91
-        vsnprintf(str->text, str->len-2, s, args);
92
-        str->text[str->len-1] = 0;
93
-        va_end(args);
94
-    }
95
-}
91
+    va_start(args, s);
92
+    str->text = bufferString(s, args);
93
+    va_end(args);
96 94
 
95
+    str->active = true;
96
+    str->len = (unsigned short)strlen(str->text);
97
+}
97 98
 
98 99
 void GLString::Scale(float scale) {
99 100
     _scale = scale;
100 101
 }
101 102
 
102
-
103 103
 int GLString::glPrintf(int x, int y, const char *string, ...) {
104
-    int sz = 60;
105
-    int n;
106 104
     va_list args;
107 105
 
108
-
109
-    // Mongoose 2002.01.01, Only allow valid strings
110
-    //   we must assume it's NULL terminated also if it passes...
111
-    if (!string || !string[0])
112
-        return -1;
106
+    assert(string != NULL);
107
+    assert(string[0] != '\0');
113 108
 
114 109
     if (_num_string > _num_string_max)
115 110
         return -2;
116 111
 
117
-    // xythobuz 2014.02.23, Activate, so we don't have to call SetString
118
-    // directly after glPrintf, with the same text...
119
-    _string[_num_string].active = true;
120
-
121
-    // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
122
-    //   on first try
123
-    _string[_num_string].text = new char[sz];
112
+    va_start(args, string);
124 113
 
125
-    // Mongoose 2002.01.03, Setup scale factor
114
+    _string[_num_string].active = true;
126 115
     _string[_num_string].scale = _scale;
127
-
128
-    //  Mongoose 2002.01.01, Setup position
129 116
     _string[_num_string].x = x;
130 117
     _string[_num_string].y = y;
118
+    _string[_num_string].text = bufferString(string, args);
131 119
 
132
-    va_start(args, string);
133
-
134
-    // Mongoose 2002.01.01, Get exact size needed if the first try fails
135
-    n = vsnprintf(_string[_num_string].text, sz, string, args);
136
-
137
-    // Mongoose 2002.01.01, Realloc correct amount if truncated
138
-    while (1) {
139
-        if (n > -1 && n < sz)
140
-            break;
141
-
142
-        if (n > -1) { // glibc 2.1
143
-            sz = n + 1;
144
-            delete [] _string[_num_string].text;
145
-            _string[_num_string].text = new char[sz];
146
-            n = vsnprintf(_string[_num_string].text, sz, string, args);
147
-            break;
148
-        } else { // glibc 2.0
149
-            sz *= 2;
150
-            delete [] _string[_num_string].text;
151
-            _string[_num_string].text = new char[sz];
152
-            n = vsnprintf(_string[_num_string].text, sz, string, args);
153
-        }
154
-    }
155
-    va_end(args);
156
-
157
-    // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
158
-    _string[_num_string].len = (unsigned short)sz;
120
+    //! \fixme All this is really unnecessary complex!
121
+    _string[_num_string].len = (unsigned short)strlen(_string[_num_string].text);
159 122
 
160
-    // Mongoose 2002.01.01, Incement string counter, since we just
161
-    //   allocated a string
162 123
     ++_num_string;
163 124
 
125
+    va_end(args);
126
+
164 127
     return 0;
165 128
 }
166 129
 
167
-
168 130
 void GLString::Render() {
169 131
     for (unsigned int i = 0; i < _num_string; ++i) {
170 132
         if (_string[i].active)
@@ -172,10 +134,9 @@ void GLString::Render() {
172 134
     }
173 135
 }
174 136
 
175
-
176 137
 gl_string_t *GLString::GetString(unsigned int id) {
177
-    if (id < _num_string)
178
-        return _string + id;
179
-    return NULL;
138
+    assert(id < _num_string);
139
+
140
+    return _string + id;
180 141
 }
181 142
 

+ 18
- 31
src/Sound.cpp View File

@@ -22,6 +22,7 @@
22 22
 #include <sys/stat.h>
23 23
 #include <fcntl.h>
24 24
 #include <unistd.h>
25
+#include <assert.h>
25 26
 
26 27
 #include "Sound.h"
27 28
 
@@ -78,8 +79,9 @@ int Sound::init()
78 79
 
79 80
 void Sound::listenAt(float pos[3], float angle[3])
80 81
 {
81
-    if (!mInit)
82
-        return;
82
+    assert(mInit == true);
83
+    assert(pos != NULL);
84
+    assert(angle != NULL);
83 85
 
84 86
     alListenerfv(AL_POSITION, pos);
85 87
     alListenerfv(AL_ORIENTATION, angle);
@@ -88,8 +90,9 @@ void Sound::listenAt(float pos[3], float angle[3])
88 90
 
89 91
 void Sound::sourceAt(int source, float pos[3])
90 92
 {
91
-    if (!mInit || source < 0)
92
-        return;
93
+    assert(mInit == true);
94
+    assert(source >= 0);
95
+    assert(pos != NULL);
93 96
 
94 97
     alSourcefv(mSource[source-1], AL_POSITION, pos);
95 98
 }
@@ -103,12 +106,10 @@ int Sound::addFile(const char *filename, int *source, unsigned int flags)
103 106
     ALenum format;
104 107
     ALvoid *data;
105 108
 
106
-
107
-    if (!mInit || !filename || !source)
108
-    {
109
-        printf("Sound::AddFile> ERROR pre condition assertion failed\n");
110
-        return -1000;
111
-    }
109
+    assert(mInit == true);
110
+    assert(filename != NULL);
111
+    assert(filename[0] != '\0');
112
+    assert(source != NULL);
112 113
 
113 114
     *source = -1;
114 115
 
@@ -167,11 +168,9 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
167 168
     ALvoid *data;
168 169
     int error = 0;
169 170
 
170
-    if (!mInit || !wav || !source)
171
-    {
172
-        printf("Sound::AddWave> ERROR pre condition assertion failed\n");
173
-        return -1000;
174
-    }
171
+    assert(mInit == true);
172
+    assert(wav != NULL);
173
+    assert(source != NULL);
175 174
 
176 175
     *source = -1;
177 176
 
@@ -233,17 +232,8 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
233 232
 
234 233
 void Sound::play(int source)
235 234
 {
236
-    if (!mInit)
237
-    {
238
-        printf("Sound::Play> ERROR: Sound not initialized\n");
239
-        return;
240
-    }
241
-
242
-    if (source < 0)
243
-    {
244
-        printf("Sound::Play> ERROR: Source Id invalid\n");
245
-        return;
246
-    }
235
+    assert(mInit == true);
236
+    assert(source >= 0);
247 237
 
248 238
     alSourcePlay(mSource[source-1]);
249 239
 }
@@ -251,11 +241,8 @@ void Sound::play(int source)
251 241
 
252 242
 void Sound::stop(int source)
253 243
 {
254
-    if (!mInit || source < 0)
255
-    {
256
-        printf("Sound::Stop> ERROR pre condition assertion failed\n");
257
-        return;
258
-    }
244
+    assert(mInit == true);
245
+    assert(source >= 0);
259 246
 
260 247
     alSourceStop(mSource[source-1]);
261 248
 }

+ 16
- 12
src/System.cpp View File

@@ -14,6 +14,7 @@
14 14
 #include <string.h>
15 15
 #include <stdarg.h>
16 16
 #include <cmath>
17
+#include <assert.h>
17 18
 
18 19
 #ifdef __APPLE__
19 20
 #include <OpenGL/gl.h>
@@ -58,16 +59,18 @@ System::~System() {
58 59
 }
59 60
 
60 61
 unsigned int System::addCommandMode(const char *command) {
61
-    if (command && command[0] == '[') {
62
-        mCmdModes.pushBack(command);
63
-        return (mCmdModes.size() - 1);
64
-    } else {
65
-        return 0;
66
-    }
62
+    assert(command != NULL);
63
+    assert(command[0] == '[');
64
+
65
+    mCmdModes.pushBack(command);
66
+    return (mCmdModes.size() - 1);
67 67
 }
68 68
 
69 69
 //! \fixme Modifer support later
70 70
 void System::bindKeyCommand(const char *cmd, unsigned int key, int event) {
71
+    assert(cmd != NULL);
72
+    assert(cmd[0] != '\0');
73
+
71 74
     printf("Bound command '%s' -> event %i (0x%x key)\n", cmd, event, key);
72 75
     mKeyEvents[key] = event;
73 76
 }
@@ -76,8 +79,8 @@ void System::command(const char *cmd) {
76 79
     bool modeFound = false;
77 80
     char *cmdbuf;
78 81
 
79
-    if (!cmd || !cmd[0]) // Null command string
80
-        return;
82
+    assert(cmd != NULL);
83
+    assert(cmd[0] != '\0');
81 84
 
82 85
     if (cmd[0] == '[') { // Set a mode, eg "[Engine.OpenGL.Driver]"
83 86
         for (mCmdModes.start(); mCmdModes.forward(); mCmdModes.next()) {
@@ -105,6 +108,9 @@ int System::loadResourceFile(const char *filename) {
105 108
     char c;
106 109
     int i, j;
107 110
 
111
+    assert(filename != NULL);
112
+    assert(filename[0] != '\0');
113
+
108 114
     f = fopen(filename, "r");
109 115
 
110 116
     if (!f) {
@@ -191,10 +197,8 @@ void System::setDriverGL(const char *driver) {
191 197
 }
192 198
 
193 199
 void System::resizeGL(unsigned int w, unsigned int h) {
194
-    if (!w || !h) {
195
-        printf("resizeGL> ERROR assertions 'w > 0', 'h > 0' failed\n");
196
-        return;
197
-    }
200
+    assert(w > 0);
201
+    assert(h > 0);
198 202
 
199 203
     glViewport(0, 0, w, h);
200 204
     glMatrixMode(GL_PROJECTION);

+ 8
- 5
src/utils/strings.cpp View File

@@ -31,19 +31,16 @@ bool stringEndsWith(const char *str, const char *suffix) {
31 31
     return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
32 32
 }
33 33
 
34
-char *bufferString(const char *string, ...) {
34
+char *bufferString(const char *string, va_list args) {
35 35
     int sz = 60;
36 36
     int n;
37 37
     char *text;
38
-    va_list args;
39 38
 
40 39
     assert(string != NULL);
41 40
     assert(string[0] != '\0');
42 41
 
43 42
     text = new char[sz];
44 43
 
45
-    va_start(args, string);
46
-
47 44
     n = vsnprintf(text, sz, string, args);
48 45
 
49 46
     if (n < 0) {
@@ -56,8 +53,14 @@ char *bufferString(const char *string, ...) {
56 53
         n = vsnprintf(text, sz, string, args);
57 54
     }
58 55
 
59
-    va_end(args);
56
+    return text;
57
+}
60 58
 
59
+char *bufferString(const char *string, ...) {
60
+    va_list args;
61
+    va_start(args, string);
62
+    char *text = bufferString(string, args);
63
+    va_end(args);
61 64
     return text;
62 65
 }
63 66
 

Loading…
Cancel
Save