Page 1 of 1

SDL_ttf problems (PC not DC)

Posted: Tue Mar 22, 2005 11:54 am
by Falco Girgis
I started writing the PC level editor for Null the other day when I ran into problems with SDL_ttf.

My program compiles and runs but immediately crashes and in the standard error .txt file that SDL creates I have this:
stderr.txt wrote:Fatal signal: Segmentation Fault (SDL Parachute Deployed)
Here is my code:
null.h

Code: Select all

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

void UpdateMouse();
void KeyboardInput();
void KeyLogic();
void Text();

SDL_Surface *g_pIcoSurface = IMG_Load("null_ico.PNG");
SDL_Surface *g_pMainSurface = NULL;
SDL_Surface *g_pTextSurface = NULL;
SDL_Event g_Event;
SDL_Rect g_Rect;

TTF_Font *g_Font = TTF_OpenFont("arial.ttf", 14);

Uint8 *key_array; 

Uint8 g_red, g_blue, g_green;
Uint32 g_color;

class Mouse {
public:
	int x;
	int y;

    Mouse(char* name) {
		g_pCursorSurface = IMG_Load(name);
		SDL_SetColorKey(g_pCursorSurface, SDL_SRCCOLORKEY, 0);
	}

	SDL_Surface *g_pCursorSurface;
};

Mouse mouse("cursor_ico.gif");

class Key {
public:

	Key() {
		q = false;
	}

	bool q;
};
Key key;

class System {
public:

    System() {
		fullscreen = false;
	}

	bool fullscreen;
};
System sys;
null.cpp

Code: Select all

/* Null Level Editor: PC Version
   Written in C/++ with SDL
   by Falco Jaenisch aka "GyroVorbis"

   Copyright Team Chaos
   http://thechaosrift.com
*/

#include "sdl.h"
#include "SDL_image.h"
#include "sdl_ttf.h"
#include <stdio.h>
#include <stdlib.h>

#include "null.h"

int main(int argc, char *argv[]) {

	if(SDL_Init(SDL_INIT_VIDEO) == -1) {
		fprintf(stderr, "Could not initialize SDL!\n");
        exit(1);
    }
    else
        fprintf(stdout, "SDL initialized properly!\n");
        atexit(SDL_Quit);
    }
	if(!TTF_Init()) atexit(TTF_Quit);
	else if(TTF_Init() == -1) fprintf(stderr, "SDL_TTF unable to initialize!\n");


	SDL_WM_SetCaption("Null Level Editor: PC Version", "Null Level Editor: PC Version");
	SDL_WM_SetIcon(g_pIcoSurface, NULL);
	SDL_ShowCursor(SDL_DISABLE);

    g_pMainSurface = SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);
   
    if(!g_pMainSurface) {
        fprintf(stderr,"Could not create main surface!\n");
        exit(1);
    }

    while(1) {
		if(SDL_PollEvent(&g_Event)) {
            if(g_Event.type == SDL_QUIT)
                break;
		}
		
		UpdateMouse();
		KeyboardInput();
		KeyLogic();
		Text();

		SDL_UpdateRect(g_pMainSurface, 0, 0, 0, 0);

		g_color = SDL_MapRGB(g_pMainSurface->format, 0, 0, 0);
		SDL_FillRect(g_pMainSurface, NULL, g_color);
	}

	TTF_CloseFont(g_Font);
	fprintf(stdout,"Terminating program normally.\n");
	return(0);
} 

void UpdateMouse() {
	SDL_PumpEvents();
	SDL_GetMouseState(&mouse.x, &mouse.y);
	//if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1))

	g_Rect.x = mouse.x;
	g_Rect.y = mouse.y;
	SDL_BlitSurface(mouse.g_pCursorSurface, NULL, g_pMainSurface, &g_Rect);
}

void KeyboardInput() {
	SDL_PumpEvents();
	key_array = SDL_GetKeyState(NULL);

	if(key.q) {
		if(key_array[SDLK_q])
			key.q = false;
		if(!key_array[SDLK_q])
			key.q = false;
	}
	else
		if(key_array[SDLK_q])
			key.q = true;

	if(key_array[SDLK_ESCAPE]) exit(1);
}

void KeyLogic() {
	if(key.q && sys.fullscreen) {
		g_pMainSurface = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);
		sys.fullscreen = false;
	}
	else if (key.q && !sys.fullscreen) {
		g_pMainSurface = SDL_SetVideoMode(640,480,0,SDL_FULLSCREEN);
		sys.fullscreen = true;
	}
}

void Text() {
	//set up color
	SDL_Color color;
	color.r = 0;
	color.g = 255;
	color.b = 100;

	g_pTextSurface = TTF_RenderText_Blended(g_Font, "Hello, world!", color);

	//rectangles
	SDL_Rect rcSrc;
	SDL_Rect rcDst;

	//source rectangle
	rcSrc.x = 0;
	rcSrc.y = 0;
	rcSrc.w = g_pTextSurface->w;
	rcSrc.h = g_pTextSurface->h;

	//destination rectangle
	rcDst = rcSrc;
	rcDst.x = (SCREEN_WIDTH-rcDst.w)/2;
	rcDst.y = (SCREEN_HEIGHT-rcDst.h)/2;

	//blit the surface
	SDL_BlitSurface(g_pTextSurface, &rcSrc, g_pMainSurface, &rcDst);
}
I've been writing the progress of the program to a text file and I've figured out that it crashes when it reaches this line:

Code: Select all

g_pTextSurface = TTF_RenderText_Blended(g_Font, "Hello, world!", color);
I'm pretty sure that you'd only need to take a look at Text(), but I posted more just in case.

Also, I'm pretty new to SDL (got this handy little book that shows you how to use things). If I'm doing something wrong or if you see another/better way that I could be doing something at this point, I'd be grateful if you corrected me. :D

Thanks for any help in advance.

Posted: Wed Mar 23, 2005 7:54 am
by Falco Girgis
Forget it, I got help elsewhere.

You guys suck.

Posted: Fri Mar 25, 2005 5:49 pm
by JS Lemming
My only suggestion is to wisely choose what fonts you use. Not that arial is uncommon, but I saw you used some crazzy font in you screen shot of the null level editor (top - center of pic). If someone doesn't have that font.... the computer is liable to explode... or WORSE.... burn like an apple tring to open a text file.

Posted: Fri Mar 25, 2005 6:38 pm
by Falco Girgis
Nah, I'm distributing the font with the editor. It's like the DLLs and crap. You aren't going to have one without the other. I'll make sure everybody gets the "I Suck at Golf" font (seriously what it is called).

Posted: Fri Mar 25, 2005 6:43 pm
by JS Lemming
GyroVorbis wrote:I'll make sure everybody gets the "I Suck at Golf" font (seriously what it is called).
Thats friggen awsome. Yeah, I forgot you can just tote fonts all about.

Posted: Fri Mar 25, 2005 9:30 pm
by DJ Yoshi
Segfaulting is the gays.

Posted: Fri Mar 25, 2005 9:31 pm
by Falco Girgis
Segfaulting?

Posted: Fri Mar 25, 2005 10:37 pm
by DJ Yoshi
Segmentation Fault. It's a term you see more in linux, than anywhere else, but it can be done just about anywhere I guess. It's...not a pretty sight in linux. My OGL drivers segfaulted in linux, had to reinstall to do anything non-desktop.

Posted: Fri Mar 25, 2005 11:56 pm
by Falco Girgis
Dude, you have no clue how much I see that.

Code: Select all

Segmentation Fault (Deploying SDL Parachute)
It happens to me quite frequently when I do something wrong and SDL freaks out and dies on me. Yeah, I know exactly what you're saying now.