Skip to content

Commit f5a46ad

Browse files
committed
Merge pull request #24 from Databean/betterText
Better text
2 parents d2128ab + 6abbfcf commit f5a46ad

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/GameView.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void GameView::render() {
9393
it->render();
9494
}
9595

96+
glColor3d(1, 1, 1);
9697
renderText("resources/TypeWritersSubstitute-Black.ttf", 50, {.2, .9}, {.8, 1}, "Settlers of Catan");
9798

9899
glFlush();

src/Renderer.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ GLuint loadTextAsTexture(const std::string& fontPath, int fontSize, const std::s
3434
throw runtime_error("TTF_OpenFont: " + string(TTF_GetError()));
3535
}
3636

37-
SDL_Color foreground { 255, 0, 0, 0 };
38-
//SDL_Color background { 0, 0, 0, 0 };
37+
//Use glColor... if you don't want black text.
38+
SDL_Color color {0, 0, 0};
3939

40-
SDL_Surface* textSurface = TTF_RenderText_Blended(font, text.c_str(), foreground);
40+
SDL_Surface* textSurface = TTF_RenderText_Blended(font, text.c_str(), color);
4141
TTF_CloseFont(font);
4242

4343
if(!textSurface) {
@@ -49,6 +49,23 @@ GLuint loadTextAsTexture(const std::string& fontPath, int fontSize, const std::s
4949
SDL_FreeSurface(textSurface);
5050
SDL_FreeFormat(format);
5151

52+
// TTF_RenderText produces ARGB images. OpenGL doesn't take ARGB images, only RGBA images
53+
// so we have to move the color values around so that OpenGL renders it properly.
54+
55+
int bpp = imageSurface->format->BytesPerPixel;
56+
SDL_LockSurface(imageSurface);
57+
for(int x = 0; x < imageSurface->w; x++) {
58+
for(int y = 0; y < imageSurface->h; y++) {
59+
Uint8 *p = (Uint8 *)imageSurface->pixels + y * imageSurface->pitch + x * bpp;
60+
// Starts out as ARGB.
61+
std::swap(p[0], p[1]); //RAGB
62+
std::swap(p[1], p[2]); //RGAB
63+
std::swap(p[2], p[3]); //RGBA
64+
}
65+
}
66+
67+
SDL_UnlockSurface(imageSurface);
68+
5269
GLuint texture;
5370
glGenTextures(1, &texture);
5471
glBindTexture(GL_TEXTURE_2D, texture);
@@ -75,7 +92,8 @@ void renderText(const std::string& fontPath, int fontSize, const std::pair<float
7592

7693
GLuint texture = loadTextAsTexture(fontPath, fontSize, text);
7794

78-
glColor3f(1.0, 1.0, 1.0);
95+
glBindTexture(GL_TEXTURE_2D, texture);
96+
7997
glBegin(GL_QUADS);
8098
glTexCoord2f(0, 1);
8199
glVertex2f(bottomLeft.first, bottomLeft.second);

0 commit comments

Comments
 (0)