Esempio n. 1
0
byte *Render_text_SFont(const char *str, int font_number, int *width, int *height)
{
  SFont_Font *font;
  SDL_Surface * TexteColore;
  SDL_Surface * Texte8Bit;
  SDL_Surface *Surface_fonte;
  byte * new_brush;

  // Chargement de la fonte
  Surface_fonte=IMG_Load(Font_name(font_number));
  if (!Surface_fonte)
  {
    DEBUG("Font loading failed",0);
    return NULL;
  }
  font=SFont_InitFont(Surface_fonte);
  if (!font)
  {
    DEBUG("Font init failed",1);
    return NULL;
  }
  
  // Calcul des dimensions
  *height=SFont_TextHeight(font);
  *width=SFont_TextWidth(font, str);
  // Allocation d'une surface SDL
  TexteColore=SDL_CreateRGBSurface(SDL_SWSURFACE, *width, *height, 24, 0, 0, 0, 0);
  // Rendu du texte
  SFont_Write(TexteColore, font, 0, 0, str);
  if (!TexteColore)
  {
    DEBUG("Rendering failed",2);
    SFont_FreeFont(font);
    return NULL;
  }
  
  Texte8Bit=SDL_DisplayFormat(TexteColore);
  SDL_FreeSurface(TexteColore);
    
  new_brush=Surface_to_bytefield(Texte8Bit, NULL);
  if (!new_brush)
  {
    DEBUG("Converting failed",3);
    SDL_FreeSurface(TexteColore);
    SDL_FreeSurface(Texte8Bit);
    SFont_FreeFont(font);
    return NULL;
  }
  SDL_FreeSurface(Texte8Bit);
  SFont_FreeFont(font);

  return new_brush;
}
Esempio n. 2
0
int main(int argc, char *argv[]) 
{
    SDL_Surface* screen =NULL;
    bool done = false; 
    SDL_Event evt; 
    int FPS = 0; 
    int pastFPS = 0; 
    int past = 0; 
    int currentTime = 0;
    static char buffer[20] = {0};
    
    SDL_Surface *font_bitmap_surface=NULL;
    SFont_Font* Font;
    
    
    
    /* Initialize SDL */
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        exit (1);
    }
    
    SDL_ShowCursor(SDL_DISABLE); /* There is no mouse */

    /* Initialize the screen / window */
    /*screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE|SDL_FULLSCREEN);  SDL_FULLSCREEN not needed on Dingoo, useful on desktop */
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
    if (screen == NULL)
    {
        SDL_Quit();
        exit (2);
    }
    
    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL); /* Not needed on Dingoo */

    font_bitmap_surface = get_default_data_font();
    Font = SFont_InitFont(font_bitmap_surface);
    if(!Font) {
        fprintf(stderr, "An error occured while setting up font.");
        exit(1);
    }
    
    while(!done) 
    {
        if( SDL_PollEvent(&evt) ) 
        { 
            switch (evt.type)
            {
                        case SDL_KEYDOWN:
                             /* quit on any key press */
                             done = true; 
                             break;
                        case SDL_QUIT:
                             done = true; 
                             break;
            }
        }

        currentTime = SDL_GetTicks();
#define LOCK_FPS_TO_60FPS
#undef LOCK_FPS_TO_60FPS
#ifdef LOCK_FPS_TO_60FPS
        /*
        ** Alternative is to sleep....
        ** however sleep may not have the expected granularity (e.g. under native)
        ** Check if 1/60th of a second has elapsed
        ** 1 (sec) / 60 == 1000 (millisec) / 60 == 16.66666666666666.... ~16
        */
        if ( currentTime - past >= 16 ) 
#endif /* LOCK_FPS_TO_60FPS */
        { 
            past = SDL_GetTicks(); /* this should be done before redrawing screen */

            #define ALWAYS_FLIP
            #ifdef ALWAYS_FLIP
            SFont_Write(screen, Font, 0, 20, "Hello World");
            SDL_UpdateRect(screen, 0, 0, 0, 0);
            #endif /* ALWAYS_FLIP */
            FPS++; 
        }

        /*
        ** Every 1 second re-calculate FPS display string
        */
        if ( currentTime - pastFPS >= 1000 )
        { 
            sprintf( buffer, "%d FPS", FPS );
            SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 ); /* rather brute force wipe out area first... */
            SFont_Write (screen, Font, 0,0, buffer);
            SDL_UpdateRect(screen, 0, 0, 0, 0); /* SDL_UpdateRect() is a way to only mark part of the screen as updated for performance */
            #ifndef ALWAYS_FLIP
            SDL_Flip( screen );
            #endif /* ALWAYS_FLIP */
        
            FPS = 0;
            pastFPS = currentTime;
        }
    }

    SFont_FreeFont(Font);
    SDL_Quit();

    return 0; 
}
Esempio n. 3
0
	void FontTool::close() {
		SFont_FreeFont(FontTool::sfont);
	}