Problem mit .ttf laden
Verfasst: Do Apr 21, 2011 11:43 am
Hallo Zusammen 
Ich hab ein Problem mit laden und anzeigen von TrueType Fonts.
Tut mir Leid das der Code so unübersichtlich ist.
Das Programm soll wenn es keine Fonts laden kann den Wert 1 zurückgeben, was es auch tut.

Ich hab ein Problem mit laden und anzeigen von TrueType Fonts.
Tut mir Leid das der Code so unübersichtlich ist.
Code: Alles auswählen
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
#include <windows.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* png = SDL_LoadBMP("cb.bmp");
SDL_Surface* bmp = SDL_LoadBMP("VLCSZ.bmp");
SDL_Surface* background = SDL_LoadBMP("metal.bmp");
SDL_Surface* cod = IMG_Load("cod4_3.jpg");
SDL_Surface *message = NULL;
TTF_Font *font = TTF_OpenFont("arial.ttf", 20 );
SDL_Color textColor = { 255, 255, 255 };
message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy dog", textColor );
SDL_Rect text;
text.x = 0;
text.y = 0;
if(font == 0)
return 1;
SDL_Rect COD;
COD.x = 0;
COD.y = 0;
if (!bmp || !png || !background)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = 50;
dstrect.y = 100;
SDL_Rect logo;
logo.x = 100;
logo.y = 200;
int move_x = 1;
int move_y = 1;
SDL_WM_SetCaption( "Hello World", NULL );
//Uint32 old_tick, new_tick = SDL_GetTicks();
//float frame_time = 1/50.f;
// program main loop
SDL_Surface* optimited = NULL;
optimited = SDL_DisplayFormat(png);
Uint32 colorkey = SDL_MapRGB( optimited->format, 0 && 0x01 && 0x02, 0 && 0x01 && 0x02, 0 && 0x01 && 0x02);
SDL_SetColorKey(optimited, SDL_SRCCOLORKEY, colorkey);
SDL_SetColorKey(bmp, SDL_SRCCOLORKEY, colorkey);
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
//SDL_FillRect(screen, NULL, 0);
dstrect.y += move_y;
logo.x += move_x;
if(logo.x == 480 || logo.x == 0)
{
move_x *= -1;
}
if(dstrect.y == 400 || dstrect.y == 0)
move_y *= -1;
//SDL_BlitSurface(cod, 0, screen, &COD);
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
SDL_BlitSurface(optimited, 0, screen, &logo);
SDL_BlitSurface(message, 0, screen, &text);
//old_tick = new_tick;
//new_tick = SDL_GetTicks();
//frame_time = (new_tick - old_tick) / 1000.f;
// DRAWING ENDS HERE
SDL_Delay(2);
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
SDL_FreeSurface(png);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}