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,6 +15,7 @@ typedef struct {
15 15
     unsigned int x;
16 16
     unsigned int y;
17 17
     float scale;
18
+    unsigned char color[3];
18 19
 } WindowString;
19 20
 
20 21
 /*!

+ 5
- 3
include/WindowSDL.h View File

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

+ 12
- 0
src/OpenRaider.cpp View File

@@ -47,6 +47,8 @@ int OpenRaider::initialize() {
47 47
     if (mWindow->initialize() != 0)
48 48
         return -1;
49 49
 
50
+    mWindow->setFont("~/.OpenRaider/data/test.ttf");
51
+
50 52
     // Initialize windows font
51 53
     if (mWindow->initializeFont() != 0)
52 54
         return -2;
@@ -66,6 +68,16 @@ void OpenRaider::run() {
66 68
 
67 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 81
         clock_t stopTime = systemTimerGet();
70 82
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
71 83
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));

+ 45
- 17
src/WindowSDL.cpp View File

@@ -8,16 +8,6 @@
8 8
 #include <cstdio>
9 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 11
 #include "config.h"
22 12
 #include "utils/strings.h"
23 13
 #include "WindowSDL.h"
@@ -31,10 +21,11 @@ WindowSDL::WindowSDL() {
31 21
     mHeight = DEFAULT_HEIGHT;
32 22
     mFullscreen = false;
33 23
     mMousegrab = false;
34
-    mFont = NULL;
35
-    mFontInit = false;
36 24
     mWindow = NULL;
37 25
     mGLContext = NULL;
26
+    mFontInit = false;
27
+    mFontName = NULL;
28
+    mFont = NULL;
38 29
 
39 30
 #ifdef WIN32
40 31
     setDriver("libGL32.dll");
@@ -49,14 +40,17 @@ WindowSDL::~WindowSDL() {
49 40
         SDL_Quit();
50 41
     }
51 42
 
43
+    if (mFont)
44
+        TTF_CloseFont(mFont);
45
+
52 46
     if (mFontInit)
53 47
         TTF_Quit();
54 48
 
55 49
     if (mDriver)
56 50
         delete [] mDriver;
57 51
 
58
-    if (mFont)
59
-        delete [] mFont;
52
+    if (mFontName)
53
+        delete [] mFontName;
60 54
 }
61 55
 
62 56
 void WindowSDL::setDriver(const char *driver) {
@@ -215,19 +209,25 @@ void WindowSDL::setFont(const char *font) {
215 209
     assert(font[0] != '\0');
216 210
     assert(mFontInit == false);
217 211
 
218
-    mFont = fullPath(font, 0);
212
+    mFontName = fullPath(font, 0);
219 213
 }
220 214
 
221 215
 int WindowSDL::initializeFont() {
222 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 220
     if (TTF_Init() != 0) {
227 221
         printf("Could not initialize SDL-TTF!\n");
228 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 231
     mFontInit = true;
232 232
     return 0;
233 233
 }
@@ -237,6 +237,34 @@ void WindowSDL::writeString(WindowString *s) {
237 237
     assert(s->text != NULL);
238 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