Browse Source

Wrote TTF string rendering

Thomas Buck 10 years ago
parent
commit
9ab9b81ba7
4 changed files with 63 additions and 20 deletions
  1. 1
    0
      include/Window.h
  2. 5
    3
      include/WindowSDL.h
  3. 12
    0
      src/OpenRaider.cpp
  4. 45
    17
      src/WindowSDL.cpp

+ 1
- 0
include/Window.h View File

15
     unsigned int x;
15
     unsigned int x;
16
     unsigned int y;
16
     unsigned int y;
17
     float scale;
17
     float scale;
18
+    unsigned char color[3];
18
 } WindowString;
19
 } WindowString;
19
 
20
 
20
 /*!
21
 /*!

+ 5
- 3
include/WindowSDL.h View File

9
 #define _WINDOW_SDL_H_
9
 #define _WINDOW_SDL_H_
10
 
10
 
11
 #include "SDL.h"
11
 #include "SDL.h"
12
+#include "SDL_ttf.h"
12
 
13
 
13
 #include "Window.h"
14
 #include "Window.h"
14
 
15
 
57
     unsigned int mHeight;
58
     unsigned int mHeight;
58
     bool mFullscreen;
59
     bool mFullscreen;
59
     bool mMousegrab;
60
     bool mMousegrab;
60
-    char *mFont;
61
-    bool mFontInit;
62
-
63
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
61
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
64
     SDL_GLContext mGLContext; //!< The OpenGL Context
62
     SDL_GLContext mGLContext; //!< The OpenGL Context
63
+
64
+    bool mFontInit;
65
+    char *mFontName;
66
+    TTF_Font *mFont;
65
 };
67
 };
66
 
68
 
67
 #endif
69
 #endif

+ 12
- 0
src/OpenRaider.cpp View File

47
     if (mWindow->initialize() != 0)
47
     if (mWindow->initialize() != 0)
48
         return -1;
48
         return -1;
49
 
49
 
50
+    mWindow->setFont("~/.OpenRaider/data/test.ttf");
51
+
50
     // Initialize windows font
52
     // Initialize windows font
51
     if (mWindow->initializeFont() != 0)
53
     if (mWindow->initializeFont() != 0)
52
         return -2;
54
         return -2;
66
 
68
 
67
         mWindow->eventHandling();
69
         mWindow->eventHandling();
68
 
70
 
71
+        /*WindowString s;
72
+        s.text = bufferString("Hello World");
73
+        s.x = 100;
74
+        s.y = 100;
75
+        s.scale = 1.0;
76
+        s.color[0] = s.color[1] = s.color[2] = 0xFF;
77
+        mWindow->writeString(&s);*/
78
+
79
+        mWindow->swapBuffersGL();
80
+
69
         clock_t stopTime = systemTimerGet();
81
         clock_t stopTime = systemTimerGet();
70
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
82
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
71
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
83
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));

+ 45
- 17
src/WindowSDL.cpp View File

8
 #include <cstdio>
8
 #include <cstdio>
9
 #include <assert.h>
9
 #include <assert.h>
10
 
10
 
11
-#include "SDL_ttf.h"
12
-
13
-#ifdef __APPLE__
14
-#include <OpenGL/gl.h>
15
-#include <OpenGL/glu.h>
16
-#else
17
-#include <GL/gl.h>
18
-#include <GL/glu.h>
19
-#endif
20
-
21
 #include "config.h"
11
 #include "config.h"
22
 #include "utils/strings.h"
12
 #include "utils/strings.h"
23
 #include "WindowSDL.h"
13
 #include "WindowSDL.h"
31
     mHeight = DEFAULT_HEIGHT;
21
     mHeight = DEFAULT_HEIGHT;
32
     mFullscreen = false;
22
     mFullscreen = false;
33
     mMousegrab = false;
23
     mMousegrab = false;
34
-    mFont = NULL;
35
-    mFontInit = false;
36
     mWindow = NULL;
24
     mWindow = NULL;
37
     mGLContext = NULL;
25
     mGLContext = NULL;
26
+    mFontInit = false;
27
+    mFontName = NULL;
28
+    mFont = NULL;
38
 
29
 
39
 #ifdef WIN32
30
 #ifdef WIN32
40
     setDriver("libGL32.dll");
31
     setDriver("libGL32.dll");
49
         SDL_Quit();
40
         SDL_Quit();
50
     }
41
     }
51
 
42
 
43
+    if (mFont)
44
+        TTF_CloseFont(mFont);
45
+
52
     if (mFontInit)
46
     if (mFontInit)
53
         TTF_Quit();
47
         TTF_Quit();
54
 
48
 
55
     if (mDriver)
49
     if (mDriver)
56
         delete [] mDriver;
50
         delete [] mDriver;
57
 
51
 
58
-    if (mFont)
59
-        delete [] mFont;
52
+    if (mFontName)
53
+        delete [] mFontName;
60
 }
54
 }
61
 
55
 
62
 void WindowSDL::setDriver(const char *driver) {
56
 void WindowSDL::setDriver(const char *driver) {
215
     assert(font[0] != '\0');
209
     assert(font[0] != '\0');
216
     assert(mFontInit == false);
210
     assert(mFontInit == false);
217
 
211
 
218
-    mFont = fullPath(font, 0);
212
+    mFontName = fullPath(font, 0);
219
 }
213
 }
220
 
214
 
221
 int WindowSDL::initializeFont() {
215
 int WindowSDL::initializeFont() {
222
     assert(mFontInit == false);
216
     assert(mFontInit == false);
223
-    assert(mFont != NULL);
224
-    assert(mFont[0] != '\0');
217
+    assert(mFontName != NULL);
218
+    assert(mFontName[0] != '\0');
225
 
219
 
226
     if (TTF_Init() != 0) {
220
     if (TTF_Init() != 0) {
227
         printf("Could not initialize SDL-TTF!\n");
221
         printf("Could not initialize SDL-TTF!\n");
228
         return -1;
222
         return -1;
229
     }
223
     }
230
 
224
 
225
+    mFont = TTF_OpenFont(mFontName, 24);
226
+    if (mFont == NULL) {
227
+        printf("TTF_OpenFont Error: %s\n", TTF_GetError());
228
+        return -2;
229
+    }
230
+
231
     mFontInit = true;
231
     mFontInit = true;
232
     return 0;
232
     return 0;
233
 }
233
 }
237
     assert(s->text != NULL);
237
     assert(s->text != NULL);
238
     assert(mInit == true);
238
     assert(mInit == true);
239
 
239
 
240
+    SDL_Color color;
241
+    color.r = s->color[0];
242
+    color.g = s->color[1];
243
+    color.b = s->color[2];
244
+
245
+    SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s->text, color);
246
+    if (surface == NULL) {
247
+        printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
248
+        return;
249
+    }
250
+
251
+    SDL_Surface *window = SDL_GetWindowSurface(mWindow);
252
+    if (window == NULL) {
253
+        printf("SDL_GetWindowSurface Error: %s\n", SDL_GetError());
254
+        return;
255
+    }
256
+
257
+    SDL_Rect destination;
258
+    destination.x = s->x;
259
+    destination.y = s->y;
260
+    destination.w = (int)((float)surface->w * s->scale);
261
+    destination.h = (int)((float)surface->h * s->scale);
262
+
263
+    if (SDL_BlitScaled(surface, NULL, window, &destination) != 0) {
264
+        printf("SDL_BlitScaled Error: %s\n", SDL_GetError());
265
+        return;
266
+    }
240
 
267
 
268
+    SDL_FreeSurface(surface);
241
 }
269
 }
242
 
270
 

Loading…
Cancel
Save