Hi all (A question about SDL and events)

Hi!

I’m not new to this list (I was suscribed 1-2 years ago when
I was trying to decide between using SDL and Allegro as my
library of choice to code small games and programs), but I’ll
introduce myself another time :slight_smile:

I’m Santiago Romero, from Spain. I work as a System Administrator
and on spare time I work on my Spectrum emulator (written in
Allegro at this time) and some other graphical projects.

1 year ago I was trying to decide between using Allegro or SDL
for my graphical programs. I selected allegro for different
reasons:

1.- SDL didn’t work under MSDOS, and allegro did.
(this reason is not valuable today).

2.- SDL works oriented to events/messages (like Windows, and
like pure XLib) with PollEvent/GetEvent loops. I do prefer
the classic style (not message oriented) like:

void main()
{
initall();

while(!done)
{
get_user_input();
move_all();
wait_some_time(),
draw_frame();
}
}

instead of:

getevent(&event);
switch(event.type)
{
case key_down: blahhh
case …
}

Is there any change SDL to work as Allegro in a linear-code-fashion?
I just would like to know how to read keypresses, check for mouse
X,Y and status, and so on, in any part of my program, and don’t
bother with events… This is quite important to me, in order to
port my Allegro emulator to SDL

3.- I also noticed that Allegro provided lots of functions to the
programmer, while SDL was more “surface oriented” and the programmer
needs to recode (or find) libraries to draw sprites, fonts, and so
on. Is there any nice wrapper (with sprites, fonts…) for SDL?

4.- Having to check the pixel depth each time (as the SDL drawpixel
code does) for all the routines (sprites, etc) is not inefficient?

Sorry all for the big message and thanks a lot to all SDL developers.

PS: If someone wants to know why I’m looking for SDL now instead of
allegro, it’s due to SDL being bundled in almost all the Linux
distributions by default. Allegro has to be installed by the user.
Lots of apps/games use SDL, so the user probably already has SDL
installed on the system.–
Santiago Romero
Departamento de Sistemas
sromero at servicom2000.com

Av. Primado Reig 189, entlo
46020 Valencia - Spain
Telf. (+34) 96 332 12 00
Fax. (+34) 96 332 12 01
http://www.servicom2000.com

/me kicks into SDL relations mode

Is there any change SDL to work as Allegro in a linear-code-fashion?
I just would like to know how to read keypresses, check for mouse
X,Y and status, and so on, in any part of my program, and don’t
bother with events… This is quite important to me, in order to
port my Allegro emulator to SDL

SDL does’nt force you exclusively use events for input, functions
like SDL_GetKeyState and SDL_GetMouseState have been in the API for a
while to just get the keyboard/mouse state once through the loop with
functions.

3.- I also noticed that Allegro provided lots of functions to the
programmer, while SDL was more “surface oriented” and the programmer
needs to recode (or find) libraries to draw sprites, fonts, and so
on. Is there any nice wrapper (with sprites, fonts…) for SDL?

I’m sure Pygame does all that if you are willing to switch languages
:slight_smile: I’ve heard talk that some of the common libraries may be folded
into SDL in the future (at least so they are distributed together).
However you still don’t get quite as much built in as with Allegro
(though Allegro has it’s share of libraries too). However to cut down
on finding I always like to point people to the libsdl.org library
section (http://www.libsdl.org/libraries.html). For fonts I would
recommend SDL_TTF for flexibility and my own KBF_Lite
(http://kokido.sf.net/KBFLite.html) for simplicity.

4.- Having to check the pixel depth each time (as the SDL drawpixel
code does) for all the routines (sprites, etc) is not inefficient?

For sprites you should’nt have to check everytime, after you load
them just use the SDL_DisplayFormat, or SDL_DisplayFormatAlpha
function and they’ll be converted to the perfect for fast blits.

Sorry all for the big message and thanks a lot to all SDL developers.

No problem

1 year ago I was trying to decide between using Allegro or SDL
for my graphical programs. I selected allegro for different
reasons:

1.- SDL didn’t work under MSDOS, and allegro did.
(this reason is not valuable today).

IYAM, this reason was of little consiquence a year ago. :wink:

2.- SDL works oriented to events/messages (like Windows, and
like pure XLib) with PollEvent/GetEvent loops. I do prefer
the classic style (not message oriented) like:

void main()
{
initall();

while(!done)
{
get_user_input();
move_all();
wait_some_time(),
draw_frame();
}
}

instead of:

getevent(&event);
switch(event.type)
{
case key_down: blahhh
case …
}

Is there any change SDL to work as Allegro in a linear-code-fashion?
I just would like to know how to read keypresses, check for mouse
X,Y and status, and so on, in any part of my program, and don’t
bother with events… This is quite important to me, in order to
port my Allegro emulator to SDL

Yes and no. No there is nothing in SDL to work that way, but yes you can
write your code that way regardless of how SDL wants it. For example,
have a look at relnev’s Quake 2 changes. He’s ported that to SDL quite
nicely I understand, and I know that Quake 2 works as you describe.

3.- I also noticed that Allegro provided lots of functions to the
programmer, while SDL was more “surface oriented” and the programmer
needs to recode (or find) libraries to draw sprites, fonts, and so
on. Is there any nice wrapper (with sprites, fonts…) for SDL?

There are a few libraries for various things. SDL is a more minimal
approach than Allegro is… A sprite isn’t much more than a surface
probably with a keycolor transparency, so you probably just need a
structure holding the different sprite frames and the like. You’ll not
need a library for that I’m sure.

Fonts are most often done either with a fixed-width bitmap font (no lib
needed) or with either SFont or SDL_ttf. Both of these libs provide you
with variable width fonts. SFont is lightweight, using its own method of
extracting individual variable-width chars from bitmap fonts. SDL_ttf is
a bit heavier but supports TTF, PCF, etc.

Other things are easy to add, and most of the bigger ones are found along
side the SDL library in most installations. SDL_mpeg, SDL_mixer, and
SDL_image come immediately to mind. SDL_net is also pretty handy since it
takes away some of the issues related to portable network programming. I
haven’t used SDL_net, but memory tells me it handles TCP and UDP, which is
really all most people will ever need.

4.- Having to check the pixel depth each time (as the SDL drawpixel
code does) for all the routines (sprites, etc) is not inefficient?

This cannot be avoided and is indeed a pain for single pixel draws. The
solution is the same as it is in most other environments: PutPixel is not
very fast. blits are much faster. Just remember to SDL_DisplayFormat or
DisplayFormatAlpha your surfaces first to make sure that the blit gets to
use the trivial case rather than converting bit depths.On Mon, Mar 25, 2002 at 02:46:52PM +0100, Santiago Romero wrote:

Sorry all for the big message and thanks a lot to all SDL developers.

PS: If someone wants to know why I’m looking for SDL now instead of
allegro, it’s due to SDL being bundled in almost all the Linux
distributions by default. Allegro has to be installed by the user.
Lots of apps/games use SDL, so the user probably already has SDL
installed on the system.


Santiago Romero
Departamento de Sistemas
sromero at servicom2000.com

Av. Primado Reig 189, entlo
46020 Valencia - Spain
Telf. (+34) 96 332 12 00
Fax. (+34) 96 332 12 01
http://www.servicom2000.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Joseph Carter My opinions are always right

  • athener calls Amnesty International House of Pancakes

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020325/9b84c6b8/attachment.pgp

Is there any change SDL to work as Allegro in a linear-code-fashion?
I just would like to know how to read keypresses, check for mouse
X,Y and status, and so on, in any part of my program, and don’t
bother with events… This is quite important to me, in order to
port my Allegro emulator to SDL

You can already do this really. Read up on the docs for SDL to learn about
all the details. You’ll still have to take some action to keep SDL checking
and handling the events so it can pass that on to you.

Messages aren’t really all that bad, though. You had a get_user_input()
function. Well, what does that do exactly? Probably the exact same thing
that a message processing loop would, so put it all in there. I sometimes
like to create an abstraction layer between the messages and the data you
derive from it. Just make that data global, and you can check it from
anywhere. If you design it well once and isolate it all into one module,
you can reuse it in all your new games. Should make porting to other things
besides SDL far easier as well, just in case that should ever happen.

3.- I also noticed that Allegro provided lots of functions to the
programmer, while SDL was more “surface oriented” and the programmer
needs to recode (or find) libraries to draw sprites, fonts, and so
on. Is there any nice wrapper (with sprites, fonts…) for SDL?

Heh, ya, those are the many libraries available. It’s not packaged as
neatly into one bit library, but then again, it’s not as bloated either.
Not quite as simple for a beginner, but having to struggle a little more
teaches you more, so it’s probably not really so bad. Using multiple
libraries tends to be typical for many programs anyway, so getting used to
that isn’t so bad either. I think Allegro might have been sheltering you
somewhat in that respect. :slight_smile:

4.- Having to check the pixel depth each time (as the SDL drawpixel
code does) for all the routines (sprites, etc) is not inefficient?

No, not really, because it’s optimized that way, assuming you design it to
be anyway. You can usually group it all together for a sort of batch
process. For example, drawing a line, you’ll draw a bunch of pixels. You
only need to check the depth once for each line if you do it right. If you
know you are only going to support one color depth, you don’t even have to
check at all, which is optimized in itself. That’s one of the nice things
about doing things yourself instead of using libraries to do them; they can
be more efficient.

-Jason

----- Original Message -----
From: compiler@escomposlinux.org (Santiago Romero)
To:
Sent: Monday, March 25, 2002 8:46 AM
Subject: [SDL] Hi all (A question about SDL and events)

/me kicks into SDL relations mode

Is there any change SDL to work as Allegro in a linear-code-fashion?
I just would like to know how to read keypresses, check for mouse
X,Y and status, and so on, in any part of my program, and don’t
bother with events… This is quite important to me, in order to
port my Allegro emulator to SDL

that’s easy. write a function that gets called each frame polling all
the events and store mouse coordinates and mouse button states in
variables that can be accessed from everywhere in your program (i.e.
make them global)
same goes for key states, create an array
char keys[512] , inti with zeroes and set the corresponding key to 1
when the key is being pressed down and reset to zero when it’s released.

like this:

while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
// key pressed
keys[event.key.keysym.sym] = 1;
}
else if(event.type == SDL_KEYUP)
{
// key released
keys[event.key.keysym.sym] = 0;
}
}

to see if is being pressed, for example, use

if(keys[SDLK_ESCAPE]==1)

(note that the SDLK_xxxx defines are a bit wildly mapped, and different
from OS to OS, but i’ve never had problems with an array size of 512)

eik

(note that the SDLK_xxxx defines are a bit wildly mapped, and different
from OS to OS, but i’ve never had problems with an array size of 512)

myarray[SDLK_LAST] will give you an array that won’t overflow.

“if (SDL_GetKeyState(NULL)[SDLK_RETURN])” can also work.

–ryan.