Who knows SDL?

I am using SDL on a Linux machine and I keep seg faulting :(

the program is supposed to blit a image to the screen and check the x & y coordinates when a user clicks.

sorry it is so long but I thought I want to Include all my source code and also I am following lazy foo's tutorial.

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

#include <SDL/SDL.h>
#include <string>
#include <iostream>

using namespace std;

//screen attributes
const int SCREEN_WIDTH = 100;
const int SCREEN_HEIGHT = 100;
const int SCREEN_BPP = 32;//bits per pixel

//new attributes ytbc
bool overTest;
int x;
int y;

//surfaces
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

//event that will be used
SDL_Event event;

SDL_Surface *load_image( string filename ){
	//temp starage for the image that's loaded
	SDL_Surface* loadedImage = NULL;

	//the optimized image that will be used
	SDL_Surface* optimizedImage = NULL;
	
	//Load the image
	loadedImage = SDL_LoadBMP( filename.c_str() );
	
	if( loadedImage != NULL ){
		//create the optimized image
		optimizedImage = SDL_DisplayFormat( loadedImage );
		
		//free the surface
		SDL_FreeSurface( loadedImage );
	}
	return optimizedImage;

}


bool load_files(){
	//load image
	image = load_image( "x.bmp" );

	//if error
	if( image == NULL ){
		return false;
	}

	//if all is well
	return true;
}

void clean_up(){
	//free teh image
	SDL_FreeSurface( image );

	//quit
	SDL_Quit();
}




void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ){
	//Make a temp rectangle to hold the offsets
	SDL_Rect offset;

	//give the offsets to the rectangle
	offset.x = x;
	offset.y = y;

	//blit surface
	SDL_BlitSurface( source, NULL, destination, &offset );
}


bool init(){
	//initialize all SDL subsystems
	if( SDL_Init(SDL_INIT_EVERYTHING ) == -1 ){
		return false;
	}

	//setup the screen
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
	
	//if error
	if( screen = NULL ){	
		return false;
	}

	SDL_WM_SetCaption( "Event test", NULL );
	
	//if all is well in boolean init
	return true;//T-T de-bugged but not forgotten
}



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



	// makes sure the program waits for a quit	
	bool quit = false;
	
	//init.
	if( init() == false ){		
		return 1;
	}
		
	//load files 
	if ( load_files() == true ){
		return 1;
	}



	//surfaces to be used
	//SDL_Surface* test = NULL;

	//load the images
	image = load_image( "image.bmp" );

	//load box
	SDL_Rect box;

	//load attributes
	int box_x = 100; //x axis
	int box_y = 100; //y axis
	int box_w = 100; //width
	int box_h = 100; //height

	//image test

	//apply_surface( box_x, box_y, screen, image);
	if(image != NULL){//this check if it blits
		SDL_BlitSurface( screen, NULL, image, NULL );//blit to screen
	} else {
		cout << "image returned NULL\n";
	}
	

	//update screen
	if(screen != NULL){//I added this to make sure it copied the image 
		SDL_Flip(screen); //this should update the screen
	} else {
		cout << "screen returned NULL\n";
	}

	//while the user hasn't quit
	while( quit == false ){
		
		//while there's an event to poll
		while( SDL_PollEvent( &event ) ){
			
			//if user has X'd out the screen
			if( event.type == SDL_QUIT ){
				//quit
				quit = true;
			}
			if( event.type == SDL_MOUSEBUTTONDOWN ){					
				x = event.button.x;
				y = event.button.y;
				cout << "x: " << x  << " y: " << y << endl;
			}		
		}
		
	}
	//free surface and quit
	clean_up();
	
	return 0;
}
Line 94: Assignment in conditional.
Line 151: Superfluous check.
Topic archived. No new replies allowed.