Problem mit .ttf laden

Simple Directmedia Layer: Portable Spieleprogrammierung
Antworten
canlot
Beiträge: 393
Registriert: Di Mär 08, 2011 11:01 pm
Wohnort: NRW

Problem mit .ttf laden

Beitrag von canlot » Do Apr 21, 2011 11:43 am

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.
Unwissenheit ist ein Segen

Benutzeravatar
Bebu
Beiträge: 562
Registriert: Mi Okt 21, 2009 6:19 pm
Wohnort: In der Nähe von Salzburg - Bin aber kein Österreicher!

Re: Problem mit .ttf laden

Beitrag von Bebu » Do Apr 21, 2011 12:55 pm

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?
Wer immer nach dem Unerreichbaren jagt, der wird irgendwann auf die Schnauze fallen!

canlot
Beiträge: 393
Registriert: Di Mär 08, 2011 11:01 pm
Wohnort: NRW

Re: Problem mit .ttf laden

Beitrag von canlot » Do Apr 21, 2011 1:30 pm

Das der Text nicht angezeigt wird.
Unwissenheit ist ein Segen

Benutzeravatar
Kerli
Beiträge: 1456
Registriert: So Jul 06, 2008 10:17 am
Wohnort: Österreich
Kontaktdaten:

Re: Problem mit .ttf laden

Beitrag von Kerli » Do Apr 21, 2011 1:43 pm

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>
"Make it idiot-proof and someone will invent an even better idiot." (programmers wisdom)

OpenGL Tutorials und vieles mehr rund ums Programmieren: http://www.tomprogs.at

canlot
Beiträge: 393
Registriert: Di Mär 08, 2011 11:01 pm
Wohnort: NRW

Re: Problem mit .ttf laden

Beitrag von canlot » Do Apr 21, 2011 1:57 pm

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.
Unwissenheit ist ein Segen

Benutzeravatar
Kerli
Beiträge: 1456
Registriert: So Jul 06, 2008 10:17 am
Wohnort: Österreich
Kontaktdaten:

Re: Problem mit .ttf laden

Beitrag von Kerli » Do Apr 21, 2011 3:09 pm

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*).
"Make it idiot-proof and someone will invent an even better idiot." (programmers wisdom)

OpenGL Tutorials und vieles mehr rund ums Programmieren: http://www.tomprogs.at

nufan
Wiki-Moderator
Beiträge: 2557
Registriert: Sa Jul 05, 2008 3:21 pm

Re: Problem mit .ttf laden

Beitrag von nufan » Do Apr 21, 2011 4:48 pm


canlot
Beiträge: 393
Registriert: Di Mär 08, 2011 11:01 pm
Wohnort: NRW

Re: Problem mit .ttf laden

Beitrag von canlot » Do Apr 21, 2011 10:07 pm

danke dani93 :D
hat funktioniert
Ich hab natürlich vergessen zu initialisieren
Unwissenheit ist ein Segen

Antworten