SDL Input framework
This commit is contained in:
parent
aec906fff3
commit
9d9a736a89
|
@ -39,34 +39,98 @@ Uses
|
||||||
SDL;
|
SDL;
|
||||||
|
|
||||||
Const
|
Const
|
||||||
AppWindowX = 640;
|
AppWindowX = 800;
|
||||||
AppWindowY = 480;
|
AppWindowY = 600;
|
||||||
|
AppInputSize = 128;
|
||||||
|
|
||||||
Type
|
Type
|
||||||
TSDLScreenMode = (mode_80x25, mode_80x50, mode_132x50);
|
TSDLScreenMode = (mode_80x25, mode_80x50, mode_132x50);
|
||||||
|
|
||||||
TSDLConsole = Class
|
TSDLConsole = Class
|
||||||
|
InputBuffer : Array[1..AppInputSize] of Char;
|
||||||
|
InputPos : Integer;
|
||||||
|
InputSize : Integer;
|
||||||
|
|
||||||
InputEvent : pSDL_EVENT;
|
InputEvent : pSDL_EVENT;
|
||||||
Screen : pSDL_SURFACE;
|
Screen : pSDL_SURFACE;
|
||||||
|
|
||||||
Constructor Create;
|
Constructor Create (InitMode: TSDLScreenMode);
|
||||||
Destructor Destroy;
|
Destructor Destroy;
|
||||||
|
|
||||||
|
Procedure PushInput (Ch: Char);
|
||||||
|
Procedure ProcessEvent;
|
||||||
|
Function KeyPressed : Boolean;
|
||||||
|
Procedure Delay (MS: LongInt);
|
||||||
End;
|
End;
|
||||||
|
|
||||||
Implementation
|
Implementation
|
||||||
|
|
||||||
Constructor TSDLConsole.Create;
|
Constructor TSDLConsole.Create (InitMode: TSDLScreenMode);
|
||||||
Begin
|
Begin
|
||||||
Inherited Create;
|
Inherited Create;
|
||||||
|
|
||||||
SDL_INIT(SDL_INIT_VIDEO OR SDL_INIT_EVENTTHREAD);
|
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;
|
End;
|
||||||
|
|
||||||
Destructor TSDLConsole.Destroy;
|
Destructor TSDLConsole.Destroy;
|
||||||
Begin
|
Begin
|
||||||
|
Dispose (InputEvent);
|
||||||
|
|
||||||
SDL_QUIT;
|
SDL_QUIT;
|
||||||
|
|
||||||
Inherited Destroy;
|
Inherited Destroy;
|
||||||
End;
|
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.
|
End.
|
||||||
|
|
Loading…
Reference in New Issue