PDA

View Full Version : Can't get SDL_PollEvent to work.



Christian Knudsen
07-04-2008, 05:11 PM
I'm working on migrating a textmode game of mine from BP7.0 to FreePascal. In my BP7.0 code I used a unit for detecting whether or not a key is currently down. I originally thought that I could use the FreePascal Keyboard unit to do the same, but it doesn't seem able to put simultaneous keypresses on the event queue. So... I've decided to try out JEDI-SDL instead. For some reason I can't get the event structure to work:

IF SDL_INIT(SDL_INIT_AUDIO) < 0 THEN BEGIN
{ quit}
END
ELSE BEGIN
A := False;
B := False;

SDL_EnableUNICODE(1);
REPEAT
ClearScreenBuffer;
WHILE SDL_PollEvent(@Event) > 0 DO BEGIN
CASE Event.Type_ OF
SDL_KeyDown : BEGIN
CASE Event.Key.KeySym.Sym OF
SDLK_a : A := True;
SDLK_q : Stop := True;
END;
END;
SDL_KeyUp : BEGIN
CASE Event.Key.KeySym.Sym OF
SDLK_a : A := False;
END;
END;
END;
END;
IF A THEN WriteFullLineToScreenBuffer(0,0,3,'A DOWN ')
ELSE WriteFullLineToScreenBuffer(0,0,3,'A NOT DOWN');
UpdateTheScreen;
z.delay(25000);
UNTIL Stop;
END;


I want the above code excerpt to have the two booleans A and B, which both tell me whether or not the A and B keys are currently pressed. However, I always get the 'A NOT DOWN' message and I can't press 'q' to stop the loop from running. What am I doing wrong?

Also, I couldn't find any documentation for GetKeyStates in the JEDI docs only in the SDL for C docs. Can't this function be used in FreePascal?

waran
07-04-2008, 07:19 PM
And you had A pressed ... for 25 seconds?

First I'd try to disable Unicode.

Secondly I would write "SDL_PollEvent(@Event) <> 0" instead of ">0"
since it resembles Cs perverted bool=int=whatever behaviour better.

Christian Knudsen
07-04-2008, 08:02 PM
I don't need to have A pressed for 25 seconds. z.delay is a microsecond timer, so I would only have to press A for 25 milliseconds. I've tried disabling Unicode, but that didn't do anything. Neither did using <> instead of >. :(

Christian Knudsen
10-04-2008, 07:37 PM
Anybody else have any idea why this isn't working? I've had to put my plans to switch from BP7.0 to FreePascal completely on hold because of this... :(

waran
10-04-2008, 07:51 PM
At least your SDL_Loop looks perfectly correct to me (except for the "<>" thingy).
However your screenbuffer-stuff looks unfamiliar ... maybe try a simpler method (writeln).

masonwheeler
10-04-2008, 08:05 PM
I've had some trouble with this too. It looks like it's not catching any keypresses at all. This might be because SDL events are linked to SDL_Video, which you haven't SDL_Init-ed.

I'm not completely certain, but I think SDL can only catch events if a screen (window) created by SDL currently has input focus.

waran
10-04-2008, 09:06 PM
Sounds good to me. Try this:
http://www.pascalgamedevelopment.com/viewtopic.php?p=42877&sid=67b154878bc9b9e639df98d7fbb3922a#42877

The handle to your own application can be read from
system.MainInstance

This way you don't need to init SDL_Video or create a window with SDL - only in case it works, of course.

Christian Knudsen
11-04-2008, 04:42 AM
I'll have a go at this tonight. Thanks for the suggestions!

Christian Knudsen
12-04-2008, 03:29 PM
I took a look at the link and googled "sdl_setenv", but I have no clue what to do. :( How do I set focus to my console application so SDL can capture events without having to create a window with SDL or initialize SDL_Video?