Ubuntu g++ Error Programming/Ubuntu

Status
Not open for further replies.

theopfor

In Runtime
Messages
173
Location
Right behind you
I am using terminal to compile my SDL program. I have the TTF (TrueTypeFont) library, the and SDL image library installed alongside the SDL library. I compile the code like so:

Code:
g++ main.cpp -lSDL -lSDL_ttf -lSDL_image

I then get a message saying "Segmentation fault (core dumped)." This hasn't happened before I installed the TTF SDL library.

In case the code may be of any help:
Code:
#include <SDL/SDL.h> 		//Include SDL 
#include <SDL/SDL_image.h> 	//For most images that aren't BMP
#include <SDL/SDL_ttf.h>	//TrueTypeFont

int main(int argc, char** argv){
	
	SDL_Init(SDL_INIT_EVERYTHING); //Initialise SDL
	TTF_Init(); //Initialise TrueTypeFont
	
	bool running = true;
	Uint32 start; //FPS
	const int FPS = 30; //FPS 
	bool b[4]={0,0,0,0}; //Up, left, down, right
	
	
	SDL_Surface	*screen, *image, *text;
	SDL_Rect 		rect;
	TTF_Font  	*font;
	SDL_Color		color3 = {255,255,255};
	
	screen 		= SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); //Make the screen
	rect.x = 0; rect.y = 0; rect.w= 19; rect.h = 40; Uint32 color = //Make the rectangle
									SDL_MapRGB(screen->format, 0x00,0x00,0x00); //Color the rectangle
	Uint32 bgcolor	= SDL_MapRGB(screen->format, 0x00,0x00,0x00); //Screen color
	image 		= IMG_Load("image.png");  //Image
	font 			= TTF_OpenFont("Arial", 32);
	text			= TTF_RenderText_Solid(font, "Hello world!", color3);
	
	while(running){ 	/////////MAIN LOOP STARTS HERE/////////
		
		start=SDL_GetTicks(); //Start FPS regulator
		
		SDL_Event event;
		while(SDL_PollEvent(&event)){
			
			switch(event.type){
				case SDL_QUIT:
					running = false;
					break;
				case SDL_KEYDOWN: //////START CONTROLS////
					switch(event.key.keysym.sym){
						case SDLK_UP:
							b[0]=1;
							break;
						case SDLK_LEFT:
							b[1]=1;
							break;
						case SDLK_DOWN:
							b[2]=1;
							break;
						case SDLK_RIGHT:
							b[3]=1;
							break;
					}
				break;
				case SDL_KEYUP:
					switch(event.key.keysym.sym){
						case SDLK_UP:
							b[0]=0;
							break;
						case SDLK_LEFT:
							b[1]=0;
							break;
						case SDLK_DOWN:
							b[2]=0;
							break;
						case SDLK_RIGHT:
							b[3]=0;
							break;
					}		/////////////END CONTROLS/////////////
				break;
			}
			
		}
		
		//Logic
		
		if(b[0])
			rect.y--;
		if(b[1])
			rect.x--;
		if(b[2])
			rect.y++;
		if(b[3])
			rect.x++;
		
		//Render
		SDL_FillRect(screen, &screen->clip_rect, bgcolor); //Screen color
		SDL_FillRect(screen, &rect, color);
		SDL_BlitSurface(image, NULL, screen, &rect);
		SDL_BlitSurface(text, NULL, screen, NULL);
		SDL_Flip(screen); //Show everything
		
		if(1000/FPS > SDL_GetTicks() - start) //Check FPS
			SDL_Delay(1000/FPS - (SDL_GetTicks()-start)); //If we are ahead of the FPS, delay
		
	}				/////////MAIN LOOP ENDS HERE//////////
	
	SDL_FreeSurface(image); //Free the image
	SDL_FreeSurface(text); //Free the text
	TTF_CloseFont(font); //Close the fonts
	TTF_Quit(); //Quit TTF
	SDL_Quit();
	return 0;
}

Any help is appreciated.
 
Check the stacktrace from gdb; see where it's getting the segmentation fault (maybe some problem with one of the libraries you installed).
 
I don't know what the gdb is, but it hasn't happened before I installed the TTF library. If I comment out the TTF_Init() then it works fine.
 
How to Debug Using GDB

Part-way down it shows how to use it with a Core Dump.

If it's throwing the error on the TTF_Init(), then there's either a problem with the library, or how it's trying to initialize within your program. Could try removing the library, and re-downloading a clean copy. Make sure your G++ is up to date, and make sure the TTF library you're using is compatible with the version of G++ you're using.
 
Status
Not open for further replies.
Back
Top Bottom