Page 1 of 2

SDL help

Posted: Wed Apr 01, 2009 6:56 pm
by herby490
I just started SDL yesterday and I already came in to my first problem. I can not seem to be able get anything on the screen. I have successfully gotten a window but nothing other than that. For example in Focus on SDL there is a program that creates random rectangles all over the screen. Instead of getting that I just get a black window. I am typing exactly what it says to type and I have even double checked the code and tried lazyfoo's tutorial as well, same result. I setup SDL the exact way it said to do it and I have include SDL.dll into my project. Is it something with my graphics card because earlier I ran a test and it said I have 0 kilobytes of video memory.

Re: SDL help

Posted: Wed Apr 01, 2009 7:39 pm
by Ginto8
Did you link it correctly?

on Windows, you need: -lmingw32 -lSDLmain -lSDL
on Linux, you need: -lSDL

Unless you're using other libraries like SDL_image or SDL_ttf. Then you have to add links.

Re: SDL help

Posted: Wed Apr 01, 2009 7:43 pm
by herby490
I have Visual C++ so I need to link with SDL.lib SDLmain.lib

Re: SDL help

Posted: Wed Apr 01, 2009 10:35 pm
by thejahooli
Post the code. Then we can check wether it's to do with the code or not.

Re: SDL help

Posted: Thu Apr 02, 2009 5:42 pm
by herby490
Ok here is the code:

Code: Select all

#include "sdl.h"
#include <cstdlib>
SDL_Surface *surf = NULL;
SDL_Event gvent ;
SDL_Rect bob;
Uint8 red, green, blue;
Uint32 color;
const int screen_width = 640;
const int screen_height = 480;
int main(int argc,char *args[])
{

	SDL_Init(SDL_INIT_VIDEO);
	atexit(SDL_Quit);
	surf = SDL_SetVideoMode(screen_width,screen_height,32,SDL_ANYFORMAT);

		while(1)
		{
			if(SDL_PollEvent(&gvent) == 0)
			{
				red = rand() % 256;
				green = rand() % 256;
				blue = rand() % 256;
				color = SDL_MapRGB(surf->format,red,green,blue);
				bob.y = rand()% screen_height;
				bob.x = rand() % screen_width;
				bob.h = rand() % screen_height - bob.y;
				bob.x = rand() % screen_width - bob.x;
				SDL_FillRect(surf,&bob,color);
				SDL_UpdateRect(surf,0,0,0,0);
			}
			else 
			{
				if(gvent.type == SDL_QUIT)
				{
					break;
				}
			}
		}

			return 0;
}


This code is straight out of Focus on Sdl with the exception of a few name changes.

Everything compiles and runs ok but I get a window as output instead of random rectangles. I have also tried LazyFoo's second tutorial with the same result.

Re: SDL help

Posted: Thu Apr 02, 2009 6:13 pm
by Maevik
in Visual C++, did you go to Tools > Options > Projects and Solutions > VC++ Directories
and add in your SDL directories?

Re: SDL help

Posted: Thu Apr 02, 2009 6:16 pm
by herby490
Yes everything compiles fine and I had no problem with SDL until I got to this application, and even when I am here there is still a window it is just blank.

Re: SDL help

Posted: Thu Apr 02, 2009 6:24 pm
by Ewan
Herby, I had exactly the same symptoms earlier today. Eventually I realised it was something to do with the image formats. I was loading .bmps that still thought they were .pngs because I'd just renamed them with the .bmp extension instead of properly opening them and re-saving. Could your problem be something similar?

EDIT: Sorry, just looked at your code and you're not loading images :oops:

Guess I fail, sorry I couldn't help!

Re: SDL help

Posted: Thu Apr 02, 2009 6:57 pm
by herby490
I think it is something with my graphics card. Is there any way to find out if that is the problem.

Re: SDL help

Posted: Fri Apr 03, 2009 2:15 am
by MarauderIIC
Check the return value of SDL_Init, perhaps.

Re: SDL help

Posted: Fri Apr 03, 2009 4:16 am
by fantastico
You got a small typo with big impact in the 4th line of your bob rectangle calculation: You are assigning to bob.x again instead of bob.w so width is probably 0.

Re: SDL help

Posted: Fri Apr 03, 2009 4:15 pm
by herby490
Thank you so much that works. I guess when I tried lazyfoo's tutorial I just put the bmps not in the correct folder.

Re: SDL help

Posted: Fri Apr 03, 2009 6:11 pm
by herby490
Ok where do you out the bmps. I am running a new application that loads a bmp and copies it to random areas on the screen but its not working. I get this error when I run it
Unhandled exception at 0x00ba150a in test.exe: 0xC0000005: Access violation reading location 0x00000008.
I have put the bmp in the same folder as my application. Do I have to put it somewhere else, and do I have to do that try, catch, and throw thing that I skimmed over because I could not find any use of it (Please tell me if there is a use).

Re: SDL help

Posted: Fri Apr 03, 2009 7:51 pm
by herby490
After doing some debugging I found out that the problem is with loading the image. Here is the image loading part of the program:

Code: Select all

screen= SDL_LoadBMP("ball.bmp");
	if(screen == NULL)
	{
		return ;
	}
The program returns 1 when I run it. I also have ball.bmp in the file with the exe as well as the project file. Do I need to post it somewhere else?

More info: When I ran SDL_GetError it returned the string "Couldn't open ball.bmp"

Re: SDL help

Posted: Sat Apr 04, 2009 4:27 am
by fantastico
Are you using some IDE? If so, at least on linux, if you are using relative filenames like your "ball.bmp" it will look for them relative to the current "working directory". IDEs usually set the working directory to your top level project directory and not to the directory in which the executable is created (like Debug/ or Release/). Try putting the file into the top level project directory or run the executable directly instead of using some "run program" button of the IDE. Last resort would be using absolute paths but that is annoying and too much typing :)