//Font
TTF_Font *fntarial=TTF_OpenFont("font/font1.ttf", 12);
SDL_Color fg = {0,0,255}; // Blue ("Fg" is foreground)
SDL_Surface *sText = TTF_RenderText_Solid( fntarial, "SFFKT!!!", fg );
draw(50,50,sText,Menu);
App locks while using SDL_tff...?
Hi, For some reason when I run this code it freezes the app. I checked the documentation and it says the code is right. Here have a look for yourself. I dont understand it. If it checks with the documentation, why wont it work?
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Code seams fine to me too. Had you used this font before? Maybe try a different font size too.
I will post my full code when I get home. Maby its where I impliment the code.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
#include <iostream>#include <stdlib.h>#include <winsock2.h>#include <SDL/SDL.h> #include <sdl_ttf.h>//Globalsint map [20][25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//Functionsvoid drawMap();void drawScene();void Jump(void);void FallDown(void);void HandleJump(void);void HandleFall(void);void PushUp(void);void PullDown(void);void drawTitle();void mainMenu();void game();//draws bitmaps, x = o is top, y = 0 is left, sprite is the imagevoid draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen);//SurfacesSDL_Surface *Menu;SDL_Surface *tilemap;//SpriteSDL_Surface *sprite1 = SDL_LoadBMP("gfx/sprites/hentai1.bmp");SDL_Surface *sprite2 = SDL_LoadBMP("gfx/sprites/eye1.bmp");SDL_Surface *cloud = SDL_LoadBMP("gfx/tiles/cloud.bmp");SDL_Surface *sun = SDL_LoadBMP("gfx/tiles/sun.bmp");//TilesSDL_Surface *tile1 = SDL_LoadBMP("gfx/tiles/tile1.bmp");SDL_Surface *tile2 = SDL_LoadBMP("gfx/tiles/tile2.bmp");SDL_Surface *grass1 = SDL_LoadBMP("gfx/tiles/grass1.bmp");//GUISDL_Surface *main1 = SDL_LoadBMP("gfx/gui/main1.bmp");SDL_Surface *main2 = SDL_LoadBMP("gfx/gui/main2.bmp");SDL_Surface *main3 = SDL_LoadBMP("gfx/gui/main3.bmp");//FontTTF_Font *fntarial=TTF_OpenFont("font/font1.ttf", 12);SDL_Color fg = {0,0,255}; // Blue ("Fg" is foreground)SDL_Surface *sText = TTF_RenderText_Solid( fntarial, "SFFKT!!!", fg );//struct Playertypedef struct Player {int x_pos;int y_pos;int x_vel;int y_vel;bool jump;bool inair;}Player;//for GUItypedef struct button {int x_pos;int y_pos;}button;Player p1;Player p2;//for the first playerint spriteh = 53;int spritew = 74;button JoinButton;button HostButton;//------------------------------------------------------------------------------using namespace std;int main(int argc, char *argv[]){ mainMenu(); return 0;}//------------------------------------------------------------------------------------//Draws stuff to the screenvoid draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen){ SDL_Rect source; SDL_Rect dest; source.x = 0; source.y = 0; source.w = sprite->w; source.h = sprite->h; dest.x = x; dest.y = y; dest.w = source.w; dest.h = source.h; SDL_BlitSurface(sprite, &source, screen, &dest); return;}//DRAWS MAP!!!void drawMap(){ int map [20][25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};for(int y = 0;y < 20; y++){ for(int x = 0;x < 25; x++) { if(map[y][x] == 1) { draw(x*32,y*32,tile1,tilemap); } else if (map[y][x] == 0) { draw(x*32,y*32,tile2,tilemap); } else if (map[y][x] == 2) { draw(x*32,y*32,grass1,tilemap); } }}}//draws backgroundvoid drawScene(){ drawMap(); draw(40,40,cloud,tilemap); draw(330,60,cloud,tilemap); draw(670,40,sun,tilemap); draw(40,40,cloud,tilemap); draw(p1.x_pos, p1.y_pos, sprite1, tilemap); draw(p2.x_pos, p2.y_pos, sprite2, tilemap); SDL_Flip(tilemap);}//jumpsvoid Jump(void){ if (p1.jump == true) { return; } p1.jump = true; p1.y_vel = 15;}void HandleJump(void){p1.y_pos -= p1.y_vel;p1.y_vel--;if (map[(p1.y_pos + spriteh)/32][p1.x_pos/32] == 0 && map[(p1.y_pos+spriteh)/32][(p1.x_pos + spritew)/32] == 0) { p1.jump = false; p1.y_vel =-1; }}void PushUp(void){ while(map[(p1.y_pos + spriteh)/32][p1.x_pos/32] == 0 && map[(p1.y_pos + spriteh)/32][(p1.x_pos + spritew)/32] == 0 ) p1.y_pos--;} void PullDown(void){ while(map[((p1.y_pos) + spriteh)/32][p1.x_pos/32] != 0 && map[((p1.y_pos) + spriteh)/32][(p1.x_pos + spritew)/32] != 0) { p1.y_pos++; }}void FallDown(void){ if (p1.inair == true) { return; } p1.inair = true; p1.y_vel = -5;}void HandleFall(void){FallDown();p1.y_pos -= p1.y_vel;p1.y_vel--;if (map[((p1.y_pos) + spriteh)/32][p1.x_pos/32] != 0 && map[((p1.y_pos) + spriteh)/32][(p1.x_pos + spritew)/32] != 0) { p1.inair = false; p1.y_pos +=2; }}void drawTitle(){ HostButton.x_pos = 75; HostButton.y_pos = 540; JoinButton.x_pos = 521; JoinButton.y_pos = 540; draw(0,0,main1,Menu); draw(50,50,sText,Menu); draw(HostButton.x_pos,HostButton.y_pos,main2,Menu); draw(JoinButton.x_pos,JoinButton.y_pos,main3,Menu); SDL_Flip(Menu);}//--------------------------------------------------void game(){ p1.x_pos = 32; p1.y_pos = 568; p2.x_pos = 717; p2.y_pos = 559; Uint8* keys; //Sets transparency SDL_SetColorKey(sprite1, SDL_SRCCOLORKEY, SDL_MapRGB(sprite1->format, 255, 100, 255)); SDL_SetColorKey(sprite2, SDL_SRCCOLORKEY, SDL_MapRGB(sprite2->format, 255, 100, 255)); SDL_SetColorKey(cloud, SDL_SRCCOLORKEY, SDL_MapRGB(cloud->format, 255, 100, 255)); SDL_SetColorKey(sun, SDL_SRCCOLORKEY, SDL_MapRGB(sun->format, 255, 100, 255)); //boolean used to determine if the game is running bool isRunning; //A SDL event, used to keep gamerunning. SDL_Event event; //unload function atexit(SDL_Quit); //sets the video mode that SDL uses // Peremiters are window width, window height,color depth, then flags tilemap = SDL_SetVideoMode(800, 640, 0, SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_WM_SetCaption( "Super Fun Fun Kill Time!!!", 0 ); //gives the boolean a value isRunning = true; drawMap(); //Main game loop while(isRunning == true) { if(p1.jump) { HandleJump(); } else if(!p1.jump) { PushUp(); } if (!p1.jump && map[((p1.y_pos+1) + spriteh)/32][p1.x_pos/32] != 0 && map[((p1.y_pos+1) + spriteh)/32][(p1.x_pos + spritew)/32] != 0 ) { HandleFall(); } if (p1.inair) { PullDown(); } else if(!p1.inair) { PushUp(); } //Poll event SDL_PollEvent(&event); //vent that ends SDL and main game loop if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) isRunning = false; } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { Jump(); } if ( keys[SDLK_LEFT] ) { if (map[(p1.y_pos/32)][(((p1.x_pos-2))/32)] !=0 && map[((p1.y_pos+spriteh)/32)][(((p1.x_pos-2))/32)] != 0 && map[((p1.y_pos+(spriteh/2))/32)][(((p1.x_pos-2))/32)] != 0) { p1.x_pos-=2; } } if ( keys[SDLK_RIGHT] ) { if (map[(p1.y_pos/32)][(((p1.x_pos+2))/32)] != 0 && map[((p1.y_pos+spriteh)/32)][(((p1.x_pos+2)+spritew)/32)] != 0 && map[((p1.y_pos+(spriteh/2))/32)][(((p1.x_pos+2)+spritew)/32)] != 0) { p1.x_pos+=2; } } if (event.type == SDL_QUIT) { SDL_FreeSurface(tile1); SDL_FreeSurface(tile2); SDL_FreeSurface(grass1); SDL_FreeSurface(sprite1); SDL_FreeSurface(cloud); SDL_FreeSurface(sun); SDL_FreeSurface(tilemap); isRunning = false; } drawScene(); } cout << "Testing a tile map..." <<endl; cout << "Lets hope this works." << endl; cout << "Geebers, it did (Knew that was going to happen)"<<endl; cout << "Now for colision." << endl; cout << "Haha I actuially did it, woohoo!" << endl; mainMenu(); return;}void mainMenu(){ //Used for keys Uint8* keys; //boolean used to determine if the game is running bool isRunning; //A SDL event, used to keep gamerunning. SDL_Event event; //initalizes font TTF_Init(); atexit(TTF_Quit); //unload function atexit(SDL_Quit); Menu = SDL_SetVideoMode(800, 640, 0, SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_WM_SetCaption( "Super Fun Fun Kill Time!!!", 0 ); //Draws menu drawTitle(); isRunning = true; //Main game loop while(isRunning == true) { //Poll event SDL_PollEvent(&event); //vent that ends SDL and main game loop if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) { isRunning = false; } } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { game(); } if ( keys[SDLK_LEFT] ) { isRunning = false; } if ( keys[SDLK_RIGHT] ) { isRunning = false; } if ( keys[SDLK_ESCAPE] ) { return; } drawTitle(); } if (event.type == SDL_QUIT) { SDL_FreeSurface(main1); SDL_FreeSurface(main2); SDL_FreeSurface(main3); SDL_FreeSurface(Menu); SDL_FreeSurface(sText); TTF_CloseFont( fntarial ); isRunning = false; } return ;}
Im guessing its the way that I initalize the font here
void drawTitle(){ HostButton.x_pos = 75; HostButton.y_pos = 540; JoinButton.x_pos = 521; JoinButton.y_pos = 540; draw(0,0,main1,Menu); draw(50,50,sText,Menu); draw(HostButton.x_pos,HostButton.y_pos,main2,Menu); draw(JoinButton.x_pos,JoinButton.y_pos,main3,Menu); SDL_Flip(Menu);}void mainMenu(){ //Used for keys Uint8* keys; //boolean used to determine if the game is running bool isRunning; //A SDL event, used to keep gamerunning. SDL_Event event; //initalizes font TTF_Init(); atexit(TTF_Quit); //unload function atexit(SDL_Quit); Menu = SDL_SetVideoMode(800, 640, 0, SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_WM_SetCaption( "Super Fun Fun Kill Time!!!", 0 ); //Draws menu drawTitle(); isRunning = true; //Main game loop while(isRunning == true) { //Poll event SDL_PollEvent(&event); //vent that ends SDL and main game loop if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) { isRunning = false; } } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { game(); } if ( keys[SDLK_LEFT] ) { isRunning = false; } if ( keys[SDLK_RIGHT] ) { isRunning = false; } if ( keys[SDLK_ESCAPE] ) { return; } drawTitle(); } if (event.type == SDL_QUIT) { SDL_FreeSurface(main1); SDL_FreeSurface(main2); SDL_FreeSurface(main3); SDL_FreeSurface(Menu); SDL_FreeSurface(sText); TTF_CloseFont( fntarial ); isRunning = false; } return ;}
For some reason when I compile it locks up with a black screen. Its annoying and I wish it would just work already.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
TTF_OpenFont is basicly broken. If opening the file fails for whatever reason, it dereferences a null pointer multiple times. I suggest rewriting your loading code like this:
(I submitted a patch to fix this bug months ago, but it still hasn't been updated)
EDIT: Whoops, looked closer. You're loading BMPs and fonts outside of a function. That's not good, as the order of initialization is not defined (but it will be BEFORE main(), which is bad. main() does special magic in SDL)
So you're loading fonts and BMPs before SDL or SDL_TTF is started. That's probably the problem.
TTF_Font *fntarial;SDL_RWops *rw=SDL_RWFromFile("font/font1.ttf");if(!rw){ printf("Error opening font: %s\n",SDL_GetError()); return 0;}else{ fntarial=TTF_OpenFontRW(rw, 1,12); if(!fntarial){ printf("Error loading font: %s\n",SDL_GetError()); return 0; }}
(I submitted a patch to fix this bug months ago, but it still hasn't been updated)
EDIT: Whoops, looked closer. You're loading BMPs and fonts outside of a function. That's not good, as the order of initialization is not defined (but it will be BEFORE main(), which is bad. main() does special magic in SDL)
So you're loading fonts and BMPs before SDL or SDL_TTF is started. That's probably the problem.
I believe your problem is not with SDL_ttf, but with the way the app works. Now correct me if I'm wrong, but you never make a call to SDL_Init()
Try adding this in:
So it looks like this:
This goes with what exactly TravisWells said at the end of his post:
Try adding this in:
SDL_Init(SDL_INIT_EVERYTHING);
So it looks like this:
SDL_Init(SDL_INIT_EVERYTHING); //initalizes font TTF_Init(); atexit(TTF_Quit); //unload function atexit(SDL_Quit);
This goes with what exactly TravisWells said at the end of his post:
Quote:
(I submitted a patch to fix this bug months ago, but it still hasn't been updated)
EDIT: Whoops, looked closer. You're loading BMPs and fonts outside of a function. That's not good, as the order of initialization is not defined (but it will be BEFORE main(), which is bad. main() does special magic in SDL)
So you're loading fonts and BMPs before SDL or SDL_TTF is started. That's probably the problem.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement