Sign in to follow this  
doyleman77

Beginning SDL

Recommended Posts

doyleman77    367
I've tinkered with C++ in consoles for quite some time, and finally started to move towards my first API -> SDL. The lazyfoo guides are great in getting something up and running, but I always find myself asking the why and how to everything.

Example: how does one know how to use all the structures required to even setting up a simple window? or about rects. For some reason, the purpose of rects confuse me.

The documentation gives some insight about what the structures and methods built in SDL do, but it's too vague that I didn't really know what ones to call together to string up a simple window to display on screen.

So, how does one figure this all out, lets say, without the use of a tutorial like lazyfoo's?

Share this post


Link to post
Share on other sites
boogyman19946    1487
If you're using C++, maybe it would make more sense to use SFML? I mean, you could encapsulate SDL into C++ classes, but it would probably prove more convenient using a C++ built library in the first place.

EDIT: I tried assembling my own SDL program on linux, but the compiler seems not to be able to find SDL_CreateWindow, SDL_ShowWindow, and SDL_DestroyWindow even though I've included the header that they are supposed to be in.

EDIT 2: Looks like version 1.2 does not contain the above functions whatsoever, and I was reading the description for 1.3 therefore the functions clearly didn't exist. Looks like 1.3 has more direct ways to create Windows, maybe you should look into that. :D

Share this post


Link to post
Share on other sites
PrestoChung    334
Quote:
Original post by boogyman19946

EDIT: I tried assembling my own SDL program on linux, but the compiler seems not to be able to find SDL_CreateWindow, SDL_ShowWindow, and SDL_DestroyWindow even though I've included the header that they are supposed to be in.


I'm not sure about Linux, but did you link the SDL.lib files and include the SDL.dll file?

Share this post


Link to post
Share on other sites
doyleman77    367
@mixmaster - I'm using just the default 1.2. but this is more or less a generic question of how everyone learned how to properly set up the API itself.

There's a 'huge' difference going from console to SDL, and I was more or less curious how everyone managed to figure out what structures and methods to use to set up a SDL game/application.

@boogyman - I have looked at SFML, but I feel the SDL approach may help solidify my grasp in making OO programs. As per your linux problem - i've not yet used 1.3, is it stable enough for general use now? I've only ever tried 1.2.

Share this post


Link to post
Share on other sites
boogyman19946    1487
Quote:
Original post by PrestoChung
Quote:
Original post by boogyman19946

EDIT: I tried assembling my own SDL program on linux, but the compiler seems not to be able to find SDL_CreateWindow, SDL_ShowWindow, and SDL_DestroyWindow even though I've included the header that they are supposed to be in.


I'm not sure about Linux, but did you link the SDL.lib files and include the SDL.dll file?


I was a little bit vague saying the compiler can't find those functions. What I meant to say is that the compiler says they are completely undeclared in the headers. If I was missing SDL.lib, the linker would complain about undefined references. SDL.dll is dynamic, so I wouldn't hear about that until I ran the program. ^.^

Quote:
Original post by doyleman77
I have looked at SFML, but I feel the SDL approach may help solidify my grasp in making OO programs. As per your linux problem - i've not yet used 1.3, is it stable enough for general use now? I've only ever tried 1.2.


I see then ^.^ Looks like 1.3 is still unstable, which is mind boggling because the API documentation in the wiki only contains documentation about 1.3. Strange case indeed.

Share this post


Link to post
Share on other sites
mixmaster    373
PM Sent

SDL 1.2 was a life saver for me. Works great and there is nothing wrong with wrapping SDL in C++. My old engine runs very well on SDL. But I will say this, I have moved on and replaced SDL with my own lib now because I found I only used the openGL side of SDL. So it became bloatware for me.

Wrap SDL in C++ classes its fine, I did !

Share this post


Link to post
Share on other sites
ICUP    140
doyleman77, I started out with SDL as well after finishing up with console. It definitely was a big change.

You're right in that documentation on its own is vague, sometimes. Sometimes you do need examples of how certain structures can be used and such. I followed lazyfoo's tutorials and I also got the book "Focus on SDL" which was basically the documentation, but explained in more detail with simple examples. Now that I'm more familiar with SDL and its libarary of functions, I almost always now consult the documentation itself.

An explanation on rects:

SDL_Rect is basically a rectangle. It has dimensions height and width, and variables x and y to indicate where the rectangle can be placed and/or how much of an image to clip. Rectangles are often used to blit images or part of an image to the screen, and where it can be placed. Btw, lazyfoo's tutorial does cover rects in more detail, but in the later chapters.


SDL_Surface *picture = IMG_Load( "batman.png" );

SDL_Rect srcRect; // rectangle for picture
srcRect.x = 0;
srcRect.y = 10;
SDL_Rect dstRect; // rectangle for destination
dstRect.x = 50;
dstRect.y = 50;

SDL_BlitSurface( picture, &srcRect, screen, &dstRect ); // screen is an SDL_Surface set to SDL_SetVideoMode(...);
SDL_Flip( screen );






What the above code does is it loads an image (from batman.png). Then it sets rectangles, srcRect and dstRect. What will be drawn to the screen after SDL_Flip()?

The png file of Batman, with 10 pixels cut off from the top, and that image placed 50 pixels in the positive x direction and 50 pixels in the positive y direction.

The second parameter of blit surface takes a SDL_Rect. This rect tells you how much of the image should be blitted. The last parameter is another SDL_Rect. This rect determines where the image should be placed on to the screen. If I did this:

SDL_BlitSurface( picture, &srcRect, screen, &srcRect );

You would instead get the clipped picture placed 10 pixels down in the positive y direction.

Hope that clears things up on SDL_Rect.

Share this post


Link to post
Share on other sites
doyleman77    367
ICUP - thank you, that helped me understand them quite a bit now. :)
I'll look into the book you had mentioned, and read through that in conjunction with lazyfoo.

thanks everyone, for your input and suggestions

Share this post


Link to post
Share on other sites
toony    80
once you learn to walk on the grass you gain the fundamental idea of what walking is like and how it feels and should be executed etc... my point is you learn from someone just like a baby learns from his parents, and when you know enough and feel the need to improve you try to invent whats required and if not you look for answers elsewhere until you find what you need.

i never expect others to fix all my programming needs, i simply never stop trying. it's what makes you unbreakable, i'd like to think.

let me show you how i deal with sprites in sdl:

class Animation {
private:
SDL_Surface* sprite;
Uint8 frame, frame_max;
Uint8 frame_repeat;
Uint8 repeat_max;

public:
Sint16 w, h;
bool active;

Animation(Uint8 repeat)
: repeat_max(repeat), frame_repeat(repeat), frame(0) {}

void draw();
void animate();
void load_sprite(char* filename, Uint8 columns);
}

// provided that you aligned the sprite frames accordingly of course!
void load_sprite(char* filename, Uint8 frames)
{
if (!(sprite = SDL_LoadBMP("walking.bmp")))
exit(1); // exception would be better
w = sprite->w / frames;
h = sprite->h;
frame_max = frames;
}

void Animation::animate()
{
draw();

if(!--frame_repeat) {
frame_repeat = repeat_max;
if(++frame >= frame_max) {
active = false;
frame = 0;
}
}
}

void Animation::draw()
{ // if we are making a side scroller
SDL_rect source = {frame*w, 0, w, h}; // centre on player

static SDL_rect destination = {0, 0, 0, 0};

SDL_BlitSurface(sprite, &source, screen, &destination);
}



[Edited by - toony on November 15, 2010 11:11:17 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this