Archived

This topic is now archived and is closed to further replies.

RuneLancer

Problems with SDL

Recommended Posts

RuneLancer    253
I''ve just started using SDL (today, in fact; about an hour ago) and good god, is it ever EASY! Bloody nice little API. I''ve decided, as an exercise, to convert one of my old projects from DirectDraw to SDL. Well, it''s done, but I ran into a snag. Mainly, how to handle windows. With DirectDraw, you''d create a window and, in brief, attach your primary surface to that window so that whatever you draw is rendered to it. Simple enough; DirectX takes care of pretty much everything. You just need to create that window and, in your main loop, have a message pump (naturally). With SDL, the window is automatically created for you when you call SDL_SetVideoMode. My question is, how the bloody heck am I supposed to handle windows messages if I don''t have a window handle to work off of? Did I miss something in the docs? IS there a way to get the handle to the SDL window? I''d also like to be able to render text to it without writing a font engine (I used to use TextOut after getting the DC; slow but for the scope of the game, it wasn''t necessary to have anything faster). So, did I miss anything in the docs or is there some other means of managing a message pump for SDL? o.O;

Share this post


Link to post
Share on other sites
hplus0603    11347
SDL works on many platforms, thus it doesn''t expose WM_PAINT which is Windows-only :-)

SDL wraps the messages in its own equivalent callbacks, structures, and enums, where the SDL equivalents are the same across platforms, so the code is portable. Thus, you use the SDL functions to get and dispatch events in your main loop, not Windows functions.

Share this post


Link to post
Share on other sites
JTippetts    12950
SDL wraps the message pump up for access through the SDL event functions. Events are polled using functions such as SDL_PollEvents(), and are encapsulated as SDL_Event structures for processing by your message handler. The documentation for SDL event handling can be found here. As far as I know, there is no way to directly retrieve the window handle for an SDL window, since SDL is designed to be cross platform. There is no such thing as a window handle on Linux or other platforms. But through the event functions, you can handle input events for keyboard, mouse, joystick, etc...


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites
RuneLancer    253
Huh. Wow. I''m rather stunned. SDL pretty much takes care of everything and leaves you little more than the game to worry about.

...

Wow.

Well, sadly, I suppose this means I''ll have to write my own font engine if I want to display stuff. Not that it''d take very long anyways. Well, I feel a bit weird dumping my message pump and the likes. It''s like going back to VB, but keeping the power of C/C++. I like it.

I believe I have seen the light.

Share this post


Link to post
Share on other sites
RuneLancer    253
Ah yes, one more question.

How do I load stuff from a .RES with SDL? Mainly, a bitmap resource? I could just load my stuff from external bitmaps but I want to know if there''s a way to do this. The SDL docs don''t seem to say so, and given the fact it''s cross-platform I can see how it would cause problems.

Is there a work-around?

Share this post


Link to post
Share on other sites
markr    1692
As far as handling windows messages is concerned - you don''t have to. SDL provides its own input handling which will inform your application of things that are deemed useful (in practice it''s totally sufficient).

There isn''t an SDL equivalent of every message, but it doesn''t matter because you don''t need to handle those types of messages (or SDL does it for you).

As far as drawing text is concerned - no, you can''t use the GDI. However, I strongly suspect that in the general case, you can''t use the GDI functions on directx surfaces anyway (I could easily be wrong here). Most games will want to do their own text output anyway:

- Fonts / sizes that are not available as standard
- Multicoloured fonts
- Translucent text

All things that the GDI can''t do.

There are probably ways you can use the GDI functions anyway - such as using an intermediate memory bitmap - but these won''t be terribly efficient (although of course, you probably don''t care)

Mark

Share this post


Link to post
Share on other sites
RuneLancer    253
Efficiency isn''t my primary concern; this is an old project, and I just want to get the hang of SDL by converting it. A "sequel" of sorts to this project uses its own font engine anyhow; pretty much all of my projects do for the reasons you''ve listed. So, basically, it''s a "so long as it works..." situation.

As for using the GDI with DirectDraw, you can by locking a surface and getting a DC from it. Not terribly efficient, yes, but it works...

My "message pump" works just fine now. I''m rather amazed at how SDL makes everything so easy. You just have to write the actual game code, and SDL handles everything else for you. Very cool.

Share this post


Link to post
Share on other sites
dude, dont bother writing your own font engine. there is a wonderfull library called SDL_ttf. just go to www.libsdl.org and you will can download it and check out the docs. also, theres another font engine for SDL that someone else made - i heard its really nice, too. just google for SDL font engine and im sure youll find it. so basically with SDL you have 2 font engines to choose from. i havent used either yet, so i couldnt recommened anything. but id like to know how your results go because im going to have to add text to my game very soon and im not sure which one i should use, but im prolly gunna wind up using SDL_ttf. oh and welcome to the wonderfull world of SDL, doesnt it kick ass?



[edited by - graveyard filla on March 28, 2004 5:48:39 PM]

Share this post


Link to post
Share on other sites
RuneLancer    253
quote:
Original post by graveyard filla
Dude, don''t bother writing your own font engine. There is a wonderfull library called SDL_ttf.
...
But I''d like to know how your results go because I''m going to have to add text to my game very soon and I''m not sure which one I should use, but I''m prolly gunna wind up using SDL_ttf.

Oooh, that makes it even better! Thanks! I''ll let you know how it all works out.

quote:
Oh and welcome to the wonderfull world of SDL, doesn''t it kick ass?

It most certainly does! To think I used to believe DirectDraw was better.

Share this post


Link to post
Share on other sites
bobstevens    204
Most stuff you won''t need to do with Windows calls, but if you really need it, you can use the (undocumented?) SDL_GetWMInfo call to get hwnd:

#include "SDL_syswm.h"

SDL_SysWMinfo i;
if (SDL_GetWMInfo (&i)) {
HWND hwnd = i.window;
}


This is platform specific, so in general you don''t want to do it. If SDL offers another way, you should use that instead.

Share this post


Link to post
Share on other sites
RuneLancer    253
AHA! So there IS a way to get a handle to an SDL window! Seeing as I'm writing Windows-only code, this will come quite in handy! Thank you very much, bobstevens!

I'm still going to use sdl_ttf, for the sake of seeing what it can do. I use my own font engine in all of my projects (well, more recent ones at least) and if this'll save me time, so much the better.

I'm done converting my first C game from DirectDraw to SDL. The code isn't 100% error-free and doesn't check for a lot of stuff (it assumes you WILL be able to provide 640x480x32 res, which isn't necessarily the case) but it works. The only thing SDL are the graphics; the rest is dumb Windows API usage (like GetAsyncKeyState; yuck). Here's a link to it. I'll post the fully-SDL-converted version if anyone wants it.

(Hit enter to start. Move with the arrows, shoot with space. You can only go up and down and yes, there's no sound. I didn't know how to use DirectSound and DirectMusic back then Probably choke-full of bugs but eh. If the timing's off, it's probably because I use GetTickCount as a timer... for now. Oh, and there's no text yet. Kinda bland because you can't see your score. Hit escape when you've had enough.)

Edit: screwed up the link.

[edited by - RuneLancer on March 28, 2004 6:30:28 PM]

Share this post


Link to post
Share on other sites
C-Junkie    1099
my only complaint is that it wasn''t implemented in C++ using namespaces to get rid of those SDL_, and using object for things like surfaces.

sdl::surface t;
t.LoadBMP(...);
t.blit(screen,sdl::rect(50,50,0,0));

mmmm, i wish ;-)

Share this post


Link to post
Share on other sites
bobstevens    204
quote:
Original post by C-Junkie
my only complaint is that it wasn''t implemented in C++ using namespaces to get rid of those SDL_, and using object for things like surfaces.


Well that''s an odd request for someone named C-Junkie, but maybe SDLmm is a little bit like what you''re looking for: http://sdlmm.sourceforge.net/

Share this post


Link to post
Share on other sites
just a little update. i just added text to my game, and i must strongly recommend using SDL_ttf for making text. i honestly thought it would be at least a little bit complicated, but it is an absolute breeze. like the rest of SDL, its only a couple function calls to start up the system and right any text you want to the screen. i googled and discovered a wonderfull library called stringstream which converts integers to strings, and my scoring system is complete. use SDL_ttf! it rocks!

Share this post


Link to post
Share on other sites
sark    104
i wrote an C++ sdl wrapper a while ago, i didn''t like the way SDLmm handled events. Mine uses boost''s signal-slots library. It''s not hard to understand once you open it, there''s not much to it. Give me a shout if anyone want to look at it.

Share this post


Link to post
Share on other sites
isnt a wrapper basically just a framework? like a set of classes that organize and use SDL functions? bah, id rather write my own. thats where all the learning happends besides SDL is sooooo simple, theres no reason to use someone else''s wrapper

Share this post


Link to post
Share on other sites