#include <SDL.h>
#include <SDL_ttf.h>
#include <assert.h>
const char* fontFilename = "DejaVuSans.ttf";
const int fontSize = 20;
void DrawTextOld( SDL_Surface* screen, char *text, int x, int y) {
TTF_Font *font;
//font=TTF_OpenFont("gfx/font.ttf",12);
font = TTF_OpenFont( fontFilename, fontSize );
SDL_Color color = { 255, 255, 255 };
SDL_Rect coordinates;
coordinates.x = (int)x;
coordinates.y = (int)y;
SDL_Surface *message = NULL;
message=TTF_RenderText_Solid(font,text,color);
SDL_BlitSurface(message,0,screen,&coordinates);
TTF_CloseFont(font);
SDL_FreeSurface(message);
}
void DrawTextNew( SDL_Surface* screen, TTF_Font* font, char *text, int x, int y) {
//TTF_Font *font;
//font=TTF_OpenFont("gfx/font.ttf",12);
SDL_Color color = { 255, 255, 255 };
SDL_Rect coordinates;
coordinates.x = (int)x;
coordinates.y = (int)y;
SDL_Surface *message = NULL;
message=TTF_RenderText_Solid(font,text,color);
SDL_BlitSurface(message,0,screen,&coordinates);
//TTF_CloseFont(font);
SDL_FreeSurface(message);
}
void DrawText( SDL_Surface* screen, TTF_Font* font, char *text, int x, int y, int old ) {
if( old )
DrawTextOld( screen, text, x, y );
else
DrawTextNew( screen, font, text, x, y );
}
int main( int argc, char** argv )
{
assert( !SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) );
assert( SDL_JoystickOpen( 0 ) );
SDL_Surface* screen = SDL_SetVideoMode( 320, 240, 0, SDL_SWSURFACE );
assert( screen );
assert( !TTF_Init() );
TTF_Font* font = TTF_OpenFont( fontFilename, fontSize );
assert( font );
int old = 1;
Uint32 ticks0 = SDL_GetTicks();
Uint32 ticks = 0;
Uint32 frames = 0;
Uint32 fps = 0;
for( ;; ) {
int run = 1;
SDL_Event e;
while( SDL_PollEvent( &e ) ) {
if( e.type == SDL_QUIT ) {
run = 0;
}
else if( e.type == SDL_JOYBUTTONDOWN ) {
if( e.jbutton.button == 8 ) {
run = 0;
}
else if( e.jbutton.button == 9 ) {
old = old ? 0 : 1;
}
}
}
if( !run )
break;
SDL_FillRect( screen, 0, 0 );
const int x = 16;
int y = 16;
DrawText( screen, font, "Press menu to quit", x, y, old );
y += 30;
DrawText( screen, font, "Press select to change routine", x, y, old );
y += 30;
DrawText( screen, font, old ? "Old routine" : "New routine", x, y, old );
y += 30;
char buffer[ 100 ];
snprintf( buffer, sizeof( buffer ), "FPS: %d", fps );
DrawText( screen, font, buffer, x, y, old );
SDL_Flip( screen );
frames++;
Uint32 ticks1 = SDL_GetTicks();
ticks += ticks1 - ticks0;
ticks0 = ticks1;
if( ticks >= 1000 ) {
fps = ( 1000 * frames ) / ticks;
ticks = 0;
frames = 0;
}
}
TTF_Quit();
SDL_Quit();
return 0;
}