My program compiles and runs but immediately crashes and in the standard error .txt file that SDL creates I have this:
Here is my code:stderr.txt wrote:Fatal signal: Segmentation Fault (SDL Parachute Deployed)
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;
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);
}
Code: Select all
g_pTextSurface = TTF_RenderText_Blended(g_Font, "Hello, world!", color);
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.