#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL_rotozoom.h> // SDL_gfx Rotozoom
#include <SDL/SDL_gfxPrimitives.h>// SDL_gfx Primitives
#include <SDL/SDL_framerate.h>
#include <SDL/SDL_ttf.h>
Mix_Chunk *sound;
TTF_Font *font = NULL;
//The color of the font
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
if( TTF_Init() == -1 )
{
return false;
}
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096) < 0) exit(0);
Mix_AllocateChannels(16);
sound=Mix_LoadWAV("ballout.wav");
font = TTF_OpenFont( "arial.ttf", 28 );
SDL_Surface *imgTxt ; // Store image of the text for blit
SDL_Rect txtRect ; // Store (x,y) of text for blit
SDL_Color fColor ; // Font color (R,G,B)
//If there was a problem in loading the background
//If there was an error in loading the font
if( font == NULL )
{
return false;
}
// load an image
Mix_PlayChannel(-1,sound,0);
SDL_Color textColor = { 255, 255, 255 };
SDL_Surface* bmp =IMG_Load("cb.png");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
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));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
Uint32 g_Color;
g_Color=SDL_MapRGB(screen->format,255,255,255);
boxColor(screen, 100, 0, 300, 300,g_Color);
// finally, update the screen
txtRect.x = 100;
txtRect.y = 200;
fColor.r = fColor.g = fColor.b = 245; // Set font to white color
imgTxt = TTF_RenderText_Solid( font , "SDL_ttf Test : Load & Display Font" , fColor );
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
Mix_FreeChunk(sound);
// all is well ;)
printf("SDL_gfx: %s\n", rotozoomSurface ? "ok!" : "WTF?");
printf("Exited cleanly\n");
return 0;
}
[koodicpp]Tällä koodilla olen testaillut mutta ihmettelen miksei ttf fontit tulostu?
Et ole komentanut ohjelmaa piirtämään pintaa (jossa teksti olisi) mihinkään. Eli ennen kuin kuva flipataan, pitäisi sen saada tietää mihin moinen pinta tulee.
kiitos
Aihe on jo aika vanha, joten et voi enää vastata siihen.