SDL help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

SDL help

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL help

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post by herby490 »

I have Visual C++ so I need to link with SDL.lib SDLmain.lib
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: SDL help

Post by thejahooli »

Post the code. Then we can check wether it's to do with the code or not.
I'll make your software hardware.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post 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.
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: SDL help

Post by Maevik »

in Visual C++, did you go to Tools > Options > Projects and Solutions > VC++ Directories
and add in your SDL directories?
My love is like a Haddoken, it's downright fierce!
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post 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.
Ewan
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Mon Mar 23, 2009 11:46 am

Re: SDL help

Post 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!
They pull out the truncheon, that's when the trouble starts.

'Cause when you've got a badge, the laws don't apply.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post by herby490 »

I think it is something with my graphics card. Is there any way to find out if that is the problem.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL help

Post by MarauderIIC »

Check the return value of SDL_Init, perhaps.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: SDL help

Post 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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post 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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post 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).
Last edited by herby490 on Fri Apr 03, 2009 7:52 pm, edited 1 time in total.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: SDL help

Post 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"
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: SDL help

Post 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 :)
Post Reply