Consider the following C++ program (sorry, not minimal, but still
simple). It runs without assertions being triggered or error messages
being shown but I don’t see any text output at all (the rest works as
expected):
#include <SDL.h>
#include <SDL_ttf.h>
#include <GL/gl.h>
#include
#include
#include
using namespace std;
static int shade_models[] = { GL_FLAT, GL_SMOOTH };
float square_vertices[][2] =
{
{-0.5f, -0.5f},
{-0.5f, 0.5f},
{0.5f, 0.5f},
{0.5f, -0.5f}
};
static SDL_Surface *
obtain_text_surface(const string& fontname, const string& str)
{
int retval = TTF_Init();
assert(retval != -1);
// TODO: Fix font leakage
TTF_Font *font = TTF_OpenFont(fontname.c_str(), 12);
assert(font);
SDL_Color color={0, 0, 0xFF, 0};
return TTF_RenderText_Blended(font, str.c_str(), color);
}
static void
display(SDL_Surface * screen, SDL_Surface *text_surface)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3ub(0xFF, 0x38, 0x59);
glVertex2fv(square_vertices[0]);
glColor3ub(0xa0, 0x0E, 0x10);
glVertex2fv(square_vertices[1]);
glColor3ub(0x90, 0x90, 0x90);
glVertex2fv(square_vertices[2]);
glColor3ub(0x01, 0x01, 0x20);
glVertex2fv(square_vertices[3]);
glEnd();
SDL_Rect dest = {0,0,0,0};
if (SDL_BlitSurface(text_surface, NULL, screen, &dest) == -1)
{
cout << "SDL_BlitSurface() failed. SDL_GetError() says: "
<< SDL_GetError() << endl;
}
SDL_GL_SwapBuffers();
}
int
main(int argc, char *argv[])
{
(void)argc;
(void)argv;
int retval = SDL_Init(SDL_INIT_VIDEO);
assert(retval == 0);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
assert(screen != NULL);
SDL_WM_SetCaption("Simple 1-3 (using SDL)", NULL);
glShadeModel(GL_FLAT);
SDL_Surface *text_surface =
obtain_text_surface("arial.ttf", "hello world");
assert(text_surface);
SDL_Event event;
bool run_mainloop = true;
while (run_mainloop == true)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
run_mainloop = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case 'q': /* Catches 'Q' too. */
run_mainloop = false;
break;
case 't': /* Catches 'T' too, toggles shade mode. */
static int i = 0;
glShadeModel(shade_models[++i % 2]);
break;
default:
;
}
break;
}
}
display(screen, text_surface);
SDL_Delay(50);
}
SDL_FreeSurface(text_surface);
SDL_Quit();
}
If I replace the call to TTF_RenderText_Blended() with one to _Solid()
it returns NULL…
Any ideas?
/ E