Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

1

06.02.2008, 17:17

TTF problem

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//......SDL init.........

if (TTF_Init () == -1)   
{
         printf ("Can't initialize TTF_FONT: %s", TTF_GetError ());
} 
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
        Uint32 rmask = 0xff000000;
        Uint32 gmask = 0x00ff0000;
        Uint32 bmask = 0x0000ff00;
        Uint32 amask = 0x000000ff;
    #else
        Uint32 rmask = 0x000000ff;
        Uint32 gmask = 0x0000ff00;
        Uint32 bmask = 0x00ff0000;
        Uint32 amask = 0xff000000;
    #endif

SDL_Surafe *bitmap;
bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, 160,32, 32,rmask, gmask, bmask, amask);

#ifdef __unix__
       #define F_PFAD "/usr/share/fonts/"
       #define F_NAME "truetype/freefont"
#elif linux
       #define F_PFAD "/usr/share/fonts/"
       #define F_NAME "truetype/freefont"
#elif __MSDOS__ || __WIN32__ || _MSC_VER
        #define F_PFAD "C:\\WINDOWS\\Fonts\\"
        #define F_NAME "ARIAL.TTF"
#endif

std::string tmp = F_PFAD+F_NAME;
TTF_Font* font;
font = TTF_OpenFont(tmp.c_str(), 22);
if(!font)
   printf("Font create:cant open font, error: %s\n", TTF_GetError());


void draw_text(Sint16 x,Sint16 y,Uint16 width,Uint16 height,std::string str,Uint8 align = 0){
    if ( SDL_LockSurface(bitmap) == -1 ){
        printf("draw_text:cant lock bitmap,error: %s\n",SDL_GetError());
        return;
    } 
    if (align == 2)
        x += width/2;  //zentriert (hoffe rs ist richtig ^^)

    else if (align == 1)
        x += width-str.size();   //rechtsbündig (das auch)


    SDL_Surface *message;
    int w = width;
    int h = height;
        SDL_Color color = {0,0,0}
    if (TTF_SizeText(font,str.c_str(), &w, &h) == -1)
          printf("draw text:cant set width and height for text,error: %s\n", TTF_GetError());
    if ( !(message = TTF_RenderText_Solid( font, str.c_str(), color)) )
        printf("draw text:cant create text,error: %s\n", TTF_GetError());
    SDL_Rect rect = {x,y,width,height};
    if (SDL_BlitSurface(message, NULL, bitmap, &rect) == -1)
        printf("draw_text: cant draw text on bitmap ,error: %s\n",SDL_GetError());
    SDL_UnlockSurface(bitmap);
}
int main{
draw_text(0,0,160,32,"Hello Word");
SDL_Rect rect = {160,160,bimtap->w,bitmap->h};
SDL_BlitSurface(bitmap, NULL, screen, &rect)
if (SDL_Flip(screen) == -1)
        printf ("cant update the Screen,error : %s\n",SDL_GetError ());
SDL_Delay(5000);
TTF_Quit(); 
SDL_Quit(); 
return 0;
}


SDLget_error:
draw text:cant set width and height for text,error:
draw text:cant create text,error: Text has zero width
draw_text: cant draw text on bitmap ,error: SDL_UpperBlit: passed a NULL surface

denjo

Treue Seele

Beiträge: 163

Wohnort: BLB

  • Private Nachricht senden

2

06.02.2008, 17:58

Hi,

so wie ich das jetzt sehe, hast du weder die TTF initialisiert, noch die TTF wieder geschlossen ...

Vllt baust du dir ne kurze Init- und Quit-Funktion für die TTF:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool InitTTF ()
{
    if (TTF_Init () == -1)
    {
         printf ("Can't initialize TTF_FONT: %s", TTF_GetError ());

         return false;
    }
    
    return true;
}

void QuitTTF ()
{
    // SDL_ttf beenden

    TTF_Quit(); 
}


und die baust du dann am Anfang (TTF_Init) und am Ende (TTF_Quit) in der main () ein, obwohl man das eigentlich auch beides direkt in die main () einbauen kann... ;)
"Irren ist menschlich, Vergeben göttlich."
- Alexander Pope -

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

3

06.02.2008, 20:15

die init hab ich ja (nur nich hier)
aber das ist nicht das problem ,diese fehler melungen beim printf()

Beneroth

Alter Hase

Beiträge: 969

Wohnort: Schweiz

Beruf: Software Entwickler

  • Private Nachricht senden

4

06.02.2008, 23:10

mit

C-/C++-Quelltext

1
TTF_SizeText(font,str.c_str(), &w, &h)


fragst du die zu erwartende Breite (w) und Höhe (h) des Surfaces mit dem gerenderten Text bei diesem String und diesem Font ab. Not set, get :!:

Werbeanzeige