Seite 1 von 1

Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 11:43 am
von canlot
Hallo Zusammen :D
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;
}

Das Programm soll wenn es keine Fonts laden kann den Wert 1 zurückgeben, was es auch tut.

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 12:55 pm
von Bebu
canlot hat geschrieben:Das Programm soll wenn es keine Fonts laden kann den Wert 1 zurückgeben, was es auch tut.
Und was ist jetzt genau das Problem?

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 1:30 pm
von canlot
Das der Text nicht angezeigt wird.

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 1:43 pm
von Kerli
Schau doch einmal was du mit TTF_GetError() bekommst. Vielleicht wird einfach nur die Datei nicht gefunden...

Gibt es übrigens einen Grund für diese grauenhaften Includes? Wozu die Unterscheidung zwischen C und C++, wozu windows.h und warum den speziellen Pfad für Apple? Warum nicht einfach so:

Code: Alles auswählen

#include <cstdlib>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 1:57 pm
von canlot
Kerli hat geschrieben:Schau doch einmal was du mit TTF_GetError() bekommst. Vielleicht wird einfach nur die Datei nicht gefunden...
Ich bekomme kein Error.
if(TTF_GetError() == 0)
return 1;
Das Programm wird ausgeführt.


Ich denke das die Fonts nicht geladen werden könnten, wieso allerdings weiß ich nicht.
Ja die Includes waren nicht von mir sondern von CodeBlocks schon da wie der Grundgerüst auch.

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 3:09 pm
von Kerli
Eigentlich habe ich es so gemeint:

Code: Alles auswählen

if(font == 0)
{
  printf("Unable to load font: %s\n", TTF_GetError());
  return 1;
}
TTF_GetError() gibt einen String zurück (char*).

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 4:48 pm
von nufan

Re: Problem mit .ttf laden

Verfasst: Do Apr 21, 2011 10:07 pm
von canlot
danke dani93 :D
hat funktioniert
Ich hab natürlich vergessen zu initialisieren