SDL whats wrong with display format?

Im following lazy foo's SDL tutorial to set the color key on an image.
I did many of his tutorials and now I have a problem that didn't exist before.

I load img1 but the line SDL_DisplayFormat(img1) DOES NOTHING ( found this out during debug) and so the program exits.What am I doing wrong?

Here is the snipper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SDL_Surface* loadimg(std::string filename)
{
	SDL_Surface* img1 = NULL;
	SDL_Surface* img2 =NULL;
	img1 = IMG_Load(filename.c_str());

	if(img1 !=NULL)
	{
		img2 = SDL_DisplayFormat(img1);
		SDL_FreeSurface(img1); 
		if(img2 != NULL)
		SDL_SetColorKey(img2,SDL_SRCCOLORKEY,SDL_MapRGB(img2->format,0xFF,0xFF,0xFF));
	}
	return img2;
}


Thanks in advance.
Last edited on
mozly wrote:
I load img1 but the line SDL_DisplayFormat(img1) DOES NOTHING ( found this out during debug) and so the program exits.What am I doing wrong?


Not sure what you are doing with the return value, but look at the following:

1
2
3
4
5
6
7
8
	if(img1 !=NULL)
	{
		img2 = SDL_DisplayFormat(img1);
		SDL_FreeSurface(img1); 
		if(img2 != NULL)
		SDL_SetColorKey(img2,SDL_SRCCOLORKEY,SDL_MapRGB(img2->format,0xFF,0xFF,0xFF));
	}
	return img1;


You are successfully copying img1 to img2, of course assuming img1 loaded successfully in the first place, but then you are freeing img1 here: SDL_FreeSurface(img1); which is fine. But you are returning img1 which you just destroyed. You should be returning the optimized image, img2. Make sense?
Last edited on
Actually i did return img2; return img1 is a typo. it is display format that doesent work.
Ok so you need to confirm if img1 was loaded successfully then. Run some tests. img1 must be NULL.
Last edited on
img1 is not NULL, I checked during runtime debug.
Alright, and have you confirmed that img2 is not NULL after img2 = SDL_DisplayFormat(img1);? Just trying to determine if that's really the problem because I don't see anything wrong with your code. If it's not null then there is a problem with your blitting function.
Last edited on
There is most definately a problem with the SDL_DisplayFormat()

here is the complete code if it helps
scroll to the bottom to see the function i'm having problems with


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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <SDL.h>
#include <SDL_image.h>
#include <string>
SDL_Surface* loadimg(std::string filename);

SDL_Surface* screen=NULL;
SDL_Surface* background = NULL;

SDL_Event event;



class Window
{
private:
	bool windowed;
	bool windowOK;
public:
	Window();
	void handle_event();
	void toggle_fullscreen();
	bool error();
};






void applysurface(int x, int y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clop = NULL)
{
	SDL_Rect offset;
	offset.x=x;
	offset.y=y;

	SDL_BlitSurface(src,clop,dst,&offset);
}

bool init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) ==-1)
	return false;

	background = loadimg("window.png");

	if(screen ==NULL || background ==NULL )
		return false;

	return true;
}

void bye()
{
	SDL_FreeSurface(background);
	SDL_Quit();
}


class Timer
{
public:
	Timer()
	{
		startTicks = 0;
		pausedTicks = 0;
		paused = false;
		started = false;
	}
	void start()
	{
		started = true;
		paused = false;
		startTicks = SDL_GetTicks();
	}
	void stop(){
	started = false;
	paused = false;
	}

	void pause(){
		if(started && !paused)
		{
			paused = true;
			pausedTicks = SDL_GetTicks() - startTicks;
		}
	}
	void unpause()
	{
		if(paused)
		{
			paused = false;
			startTicks = SDL_GetTicks() - pausedTicks;
			pausedTicks = 0;
		}
	}
	int get_ticks(){
		if(started)
		{
			if(paused)
				return pausedTicks;
			else
				return SDL_GetTicks() - startTicks;
		}
		return 0;
	}
	bool is_started(){
	return started;
	}
	bool is_paused()
	{
		return paused;
	}

protected:
	int startTicks;
	int pausedTicks;
	bool paused;
	bool started;
};

Window::Window()
{
	screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE | SDL_RESIZABLE);

	if(screen == NULL)
	{
		windowOK = false;
		return;
	}
	else
	{
		windowOK = true;
	}

	SDL_WM_SetCaption("window event test", NULL);

	windowed = true;
}

void Window::toggle_fullscreen()
{
	if(windowed == true)
	{
		screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN);

		if(screen == NULL)
		{
			windowOK = false;
			return;
		}

		windowed = false;
	}

	else if(windowed == false)
	{
		screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_RESIZABLE);

		if(screen == NULL)
		{
			windowOK = false;
			return;
		}
		
		windowed = true;
	}
}

void Window::handle_event()
{
	if(windowOK == false)
	{
		return;
	}

	if(event.type == SDL_VIDEORESIZE)
	{
		screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_SWSURFACE | SDL_RESIZABLE);

		if(screen == NULL)
		{
			windowOK = false;
			return;
		}
	}

	else if( (event.type == SDL_KEYDOWN) && (event.key.keysym.sym == SDLK_RETURN))
	{
		toggle_fullscreen();
	}

	else if(event.type == SDL_ACTIVEEVENT)
	{
		if(event.active.state & SDL_APPACTIVE)
		{
			if(event.active.gain == 0)
			{
				SDL_WM_SetCaption("Windwo Event Test:iconified",NULL);

			}
			else
			{
				SDL_WM_SetCaption("Window Event Test",NULL);
			}
		}
		else if(event.active.state & SDL_APPINPUTFOCUS)
		{
			if(event.active.gain == 0)
			{
				SDL_WM_SetCaption("Window Event Test: Keyboard focus lost",NULL);

			}
			else
			{
				SDL_WM_SetCaption("Window Event Test", NULL);
			}

		}

		else if(event.active.state & SDL_APPMOUSEFOCUS)
		{
			if(event.active.gain ==0 )
			{
				SDL_WM_SetCaption("Window event Test: Mouse Focus Lost", NULL);
			}
			else
			{
				SDL_WM_SetCaption("Window Event test", NULL);
			}
		}
	}
	else if(event.type == SDL_VIDEOEXPOSE)
	{
		if (SDL_Flip(screen) == -1)
		{
			windowOK = false;
			return;
		}
	}

}

bool Window::error()
{
	return !windowOK;
}

int main( int argc, char* args[] )
{
	bool quit = false;

	if(!init())
		return 1;

	Window myWindow;

	if(myWindow.error() == true)
	{
		return 1;
	}

	while(!quit)
	{
		while(SDL_PollEvent(&event))
		{
			myWindow.handle_event();
			if((event.type == SDL_KEYDOWN) && (event.key.keysym.sym == SDLK_ESCAPE))
			{
				quit = true;
			}

			if(event.type == SDL_QUIT)
				quit = true;

		}

		if(myWindow.error())
			return 1;

		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF,0xFF,0xFF));
		applysurface((screen->w - background->w)/2, (screen->h - background->h)/2, background, screen);

		if(SDL_Flip(screen) == -1)
			return 1;
	}



	return 0;
}


SDL_Surface* loadimg(std::string filename)
{
  SDL_Surface* img1 = IMG_Load(filename.c_str());

  if(img1 ==NULL)
  {
    return NULL;
  }

  SDL_Surface* img2 = SDL_DisplayFormat(img1);
  SDL_FreeSurface(img1); 
  if(img2 == NULL)
  {
    return NULL;
  }

  SDL_SetColorKey(img2,SDL_SRCCOLORKEY,SDL_MapRGB(img2->format,0xFF,0xFF,0xFF));

  return img2;
}
Last edited on
Good Lord, this was a nightmare to debug... I believe i've found your problem. You need to set the Video mode first before attempting to load or copy images. You currently don't do this until after you try loading/copying the surfaces. Your setting it during your Window object's contstructor call. The below code needs to be used before calling your imgload function.

screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE | SDL_RESIZABLE);

Because SDL_DisplayFormat copies pixel info, etc... if you haven't set the video mode before you try loading the images, I believe it's all going to be NULL data because it doesn't know dimensions, etc... Give that a shot and let me know if it works.
Last edited on
Topic archived. No new replies allowed.