SDL Programming error(segmentation fault)

I want to compile an SDL programm on my Windows partition.
The compiler itself (finally) works as it should.
Under linux the Programm runs but under windows the window just pops up and dissappears nearly instantly.

Programm code:
main.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#include "character.h"
using namespace std;

SDL_Surface *optimage(string filename);
void setparts(SDL_Rect* clip);

int main(int argc, char** argv)
{
	SDL_Surface* screen;
	screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
	SDL_Event event;
	Uint32 start;
	bool running=true;
	bool arr[4]={0,0,0,0};
	character player(optimage("16sprite.png"),20,20,128,192,16,0);
	setparts(player.part);
	while(running)
	{
		start=SDL_GetTicks();
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
				{
					case SDL_QUIT:
						running=false;
						break;

					case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							arr[0]=1;
							break;

						case SDLK_LEFT:
							arr[1]=1;
							break;
						case SDLK_DOWN:
							arr[2]=1;
							break;
						case SDLK_RIGHT:
							arr[3]=1;
							break;
					}
					break;

					case SDL_KEYUP:
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							arr[0]=0;
							break;
						case SDLK_LEFT:
							arr[1]=0;
							break;
						case SDLK_DOWN:
							arr[2]=0;
							break;
						case SDLK_RIGHT:
							arr[3]=0;
							break;
					}
					break;
				}
		}
		if(arr[0])
			player.moveUp();
		if(arr[1])
			player.moveLeft();
		if(arr[2])
			player.moveDown();
		if(arr[3])
			player.moveRight();

		SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));
		player.show();
		SDL_Flip(screen);

		if(1000/30>SDL_GetTicks()-start)
			SDL_Delay(1000/30-(SDL_GetTicks()-start));
	}
	SDL_Quit();
}

SDL_Surface *optimage(string filename)
{
	SDL_Surface* loadedImage=NULL;
	SDL_Surface* optimizedImage;
	optimizedImage=NULL;
	loadedImage=IMG_Load(filename.c_str());

	if(loadedImage!=NULL )
	{
		optimizedImage=SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
	}
	return optimizedImage;
}

void setparts(SDL_Rect* clip)
{
	clip[0].x=0;
	clip[0].y=0;
	clip[0].w=32;
	clip[0].h=48;

	clip[1].x=33;
	clip[1].y=0;
	clip[1].w=32;
	clip[1].h=48;

	clip[2].x=65;
	clip[2].y=0;
	clip[2].w=32;
	clip[2].h=48;

	clip[3].x=97;
	clip[3].y=0;
	clip[3].w=32;
	clip[3].h=48;

	clip[4].x=0;
	clip[4].y=49;
	clip[4].w=32;
	clip[4].h=48;

	clip[5].x=33;
	clip[5].y=49;
	clip[5].w=32;
	clip[5].h=48;

	clip[6].x=65;
	clip[6].y=49;
	clip[6].w=32;
	clip[6].h=48;

	clip[7].x=97;
	clip[7].y=49;
	clip[7].w=32;
	clip[7].h=48;

	clip[8].x=0;
	clip[8].y=97;
	clip[8].w=32;
	clip[8].h=48;

	clip[9].x=33;
	clip[9].y=97;
	clip[9].w=32;
	clip[9].h=48;

	clip[10].x=65;
	clip[10].y=97;
	clip[10].w=32;
	clip[10].h=48;

	clip[11].x=97;
	clip[11].y=97;
	clip[11].w=32;
	clip[11].h=48;

	clip[12].x=0;
	clip[12].y=144;
	clip[12].w=32;
	clip[12].h=48;

	clip[13].x=33;
	clip[13].y=144;
	clip[13].w=32;
	clip[13].h=48;

	clip[14].x=65;
	clip[14].y=144;
	clip[14].w=32;
	clip[14].h=48;

	clip[15].x=97;
	clip[15].y=144;
	clip[15].w=32;
	clip[15].h=48;
}


character.cpp
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
#include "character.h"
#include <SDL/SDL.h>

character::character(SDL_Surface* img,int x,int y,int w, int h,int partsize,int fra)
{
	sprite=img;
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	part[partsize];
	frame=fra;
}

character::~character()
{
        SDL_FreeSurface(sprite);
}

void character::show()
{
	SDL_BlitSurface(sprite,&part[frame/6], SDL_GetVideoSurface(),&box);
}

void character::moveUp()
{
		box.y--;
		frame++;
		if(frame<72 or frame>=96)
			frame=72;
}

void character::moveDown()
{
	box.y++;
	frame++;
	if(frame>=24)
	{
		frame=0;
	}
}

void character::moveLeft()
{
	box.x--;
	frame++;
	if(frame<24 or frame>=48)
		frame=24;
}

void character::moveRight()
{
	box.x++;
	frame++;
	if(frame<48 or frame>=72)
		frame=48;
}

bool character::collision(SDL_Rect* col1,SDL_Rect* col2)
{
	if(col1->y >= col2->y + col2->h)
		return 0;
	if(col1->x >= col2->x + col2->w)
		return 0;
	if(col1->y + col1->h <= col2->y)
		return 0;
	if(col1->x + col1->w <= col2->x)
		return 0;
	return 1;
}


character.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <SDL/SDL.h>
#ifndef CHARACTER_H
#define CHARACTER_H

class character
{
	int x,y,xvel,yvel,frame;
	SDL_Surface* sprite;
	SDL_Rect box;
	bool collision(SDL_Rect* col1,SDL_Rect* col2);

	public:
	SDL_Rect part[];
	character(SDL_Surface* img,int x,int y,int w, int h,int partsize,int fra);
	~character();
	void show();
	void moveUp();
	void moveDown();
	void moveLeft();
	void moveRight();
};

#endif 


build/run log:

Checking for existence: C:\Users\theblackdog\Documents\test\bin\Debug\test.exe
Executing: "C:\Users\theblackdog\Documents\test\bin\Debug\test.exe"  (in C:\Users\theblackdog\Documents\test\.)
Process terminated with status -1073741819 (0 minutes, 17 seconds)
Last edited on
I think it may be a segmentation fault (which shows up when I close down the program may be the problem for my windows partition).
Sadly I have no experience with gdb..
This is the output of gdb when i run it:

(gdb) run
Starting program: /home/theblackdog/test/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00000000004013b3 in main (argc=1, argv=0x7fffffffe058) at main.cpp:86
86	}

Line 86 is the end of main... does that mean i have something in the code forgrotten (something in a destructor or so maybe?)
It would be really nice if someone could help me out with that problem or give me a good tutorial to gdb.

Update after some hours of searching I found out that it is because of something with part[] or partsize, after moving it to main it works now as it should.
*marking thread as solved*
Last edited on
Topic archived. No new replies allowed.