SDL Input framework
This commit is contained in:
parent
aec906fff3
commit
9d9a736a89
|
@ -39,34 +39,98 @@ Uses
|
|||
SDL;
|
||||
|
||||
Const
|
||||
AppWindowX = 640;
|
||||
AppWindowY = 480;
|
||||
AppWindowX = 800;
|
||||
AppWindowY = 600;
|
||||
AppInputSize = 128;
|
||||
|
||||
Type
|
||||
TSDLScreenMode = (mode_80x25, mode_80x50, mode_132x50);
|
||||
|
||||
TSDLConsole = Class
|
||||
InputBuffer : Array[1..AppInputSize] of Char;
|
||||
InputPos : Integer;
|
||||
InputSize : Integer;
|
||||
|
||||
InputEvent : pSDL_EVENT;
|
||||
Screen : pSDL_SURFACE;
|
||||
|
||||
Constructor Create;
|
||||
Constructor Create (InitMode: TSDLScreenMode);
|
||||
Destructor Destroy;
|
||||
|
||||
Procedure PushInput (Ch: Char);
|
||||
Procedure ProcessEvent;
|
||||
Function KeyPressed : Boolean;
|
||||
Procedure Delay (MS: LongInt);
|
||||
End;
|
||||
|
||||
Implementation
|
||||
|
||||
Constructor TSDLConsole.Create;
|
||||
Constructor TSDLConsole.Create (InitMode: TSDLScreenMode);
|
||||
Begin
|
||||
Inherited Create;
|
||||
|
||||
SDL_INIT(SDL_INIT_VIDEO OR SDL_INIT_EVENTTHREAD);
|
||||
|
||||
Screen := SDL_SetVideoMode(AppWindowX, AppWindowY, 32, SDL_SWSURFACE);
|
||||
|
||||
If Screen = NIL Then Halt;
|
||||
|
||||
New (InputEvent);
|
||||
|
||||
InputSize := 0;
|
||||
InputPos := 0;
|
||||
End;
|
||||
|
||||
Destructor TSDLConsole.Destroy;
|
||||
Begin
|
||||
Dispose (InputEvent);
|
||||
|
||||
SDL_QUIT;
|
||||
|
||||
Inherited Destroy;
|
||||
End;
|
||||
|
||||
Procedure TSDLConsole.PushInput (Ch: Char);
|
||||
Begin
|
||||
Inc (InputSize);
|
||||
|
||||
If InputSize > AppInputSize Then Begin
|
||||
InputSize := 1;
|
||||
InputPos := 1;
|
||||
End;
|
||||
|
||||
InputBuffer[InputSize] := Ch;
|
||||
End;
|
||||
|
||||
Procedure TSDLConsole.ProcessEvent;
|
||||
Begin
|
||||
Case InputEvent^.Type_ of
|
||||
SDL_KEYDOWN : Case InputEvent^.Key.KeySym.Sym of
|
||||
// remap SDL keys to pascal CRT
|
||||
27 : PushInput(#27);
|
||||
Else
|
||||
PushInput(Chr(InputEvent^.Key.KeySym.Sym));
|
||||
End;
|
||||
End;
|
||||
End;
|
||||
|
||||
Function TSDLConsole.KeyPressed : Boolean;
|
||||
Var
|
||||
Queued : LongInt;
|
||||
Begin
|
||||
Result := False;
|
||||
|
||||
Queued := SDL_PollEvent(InputEvent);
|
||||
|
||||
If Queued > 0 Then
|
||||
ProcessEvent;
|
||||
|
||||
Result := InputSize > 0;
|
||||
End;
|
||||
|
||||
Procedure TSDLConsole.Delay (MS: LongInt);
|
||||
Begin
|
||||
SDL_DELAY(MS);
|
||||
End;
|
||||
|
||||
End.
|
||||
|
|
Loading…
Reference in New Issue