ColorKeying in sdl

This program makes an error that says something about the memory location. The error itself does not say anything specific about the problem. I am trying to remove the background in an image...

code:
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
#include "SDL.h"
#include <iostream>
int main(int argc, char * args[])
{
	bool running = true;
	//Init SDL
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		running = false;
	}
	SDL_Surface *screen;

	screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);

	if(screen == NULL)
	{
		running = false;
	}


	SDL_Event occur;

	SDL_Surface *image;

	image = SDL_LoadBMP("Test.bmp");

	Uint32 colorkey = SDL_MapRGB( image->format, 0, 0xFF, 0xFF );
	SDL_SetColorKey( image, SDL_SRCCOLORKEY, colorkey );
	while(running == true)
	{
		SDL_PollEvent(&occur);

		if(occur.type == SDL_QUIT)
		{
			running = false;
		}
		SDL_FillRect(screen, NULL, 0);

		SDL_BlitSurface(image, NULL, screen, NULL);
		std::cout << "hello";

		SDL_Flip(screen);
	}
	//Quit SDL
	SDL_Quit();
	return 0;
}
Make sure it is seeing the image correctly?

1
2
3
4
if(!image)
{
	cout << "Problem loading image" << endl;
}
Were will cout write "Problem loading image"? I don't think it is possible to output right out to the window that shows the image. But I can confirm that the image loads correctly because when I remove the two lines with colorkey everything works like expected.

I compile using visual studio 2012
James2250, if there was a problem with opening the image the error would come after compiling was done...
I thought you were getting a memory violation error during runtime (which is similar to what would show up if it could not find the image correctly). I would also suggest posting exactly what the error message says.
I just suggested cout as you can have the console running along side the window if you wanted to.

Your code seems to be set up correctly for me, I have heard of SDL_SetColorKey having problems with 32-bit images so you might want to make sure yours is 24-bit and try it.
unhandled exception at (address) in test2.exe(the program) The operation completed successfully.

If i click continue it continues to show these kinds of errors...
What do you mean by 24bit and 32bit? Shouldn't the image work whatever format? and how do I change it? I can show it with the normal background but I want to remove it.
Wow, so much work still haven't found a solution. I wish there would just be an installer for installing sdl. The problem is probably with my way of installing it. Showing a picture works though.
SDL on windows sends all console output to file. The text you print with cout will end up in a file named stdout.txt.
SDL on windows sends all console output to file. The text you print with cout will end up in a file named stdout.txt.
To avoid this it is possible to #undef main but I'm not sure of the implications it has
Thanks for the help, it is some problem with loading image, but the image is 24bit.
Ok some new info, that might help solve the problem. If I remove the line the program will work, but not show the picture unless you run the program from the folder. (instead of when compiling) The problem may be that the compiler don't look for the picture there, and it may not even look for it before it is done compiling. Anyone familiar with visual studio 2012?
It will look for the file in the current working directory. If the current working directory is not the same directory where Test.png is located it will not find it. If you start your program by double clicking on the executable file the current working directory will be the directory where the executable file is located. If you start your program from the command line the current working directory will be the directory that you set using the cd command. If you start your program from an IDE the current working directory is often set to the project directory (not the source directory) but this can differ between IDEs.
Topic archived. No new replies allowed.