AppInput Code

The code of the AppInput unit

unit AppInput;
{
   Copyright (c) 2011 Max Foster

    Licensed under the Apache License, Version 2.0 (the'License'); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, SDL;

//Initialises AppInput
procedure InitAppInput;

//Quits AppInput
procedure QuitAppInput;

//Refreshes events to their current state for use
procedure RefreshEvents;

//Returns true if the key specified by the keycode is currently being pressed
function AppKeyPressed(code:integer):boolean;

//Returns the X value of the cursor relative to the AppDisplay window
function AppMouseX:integer;

//Returns the Y value of the cursor relative to the AppDisplay window
function AppMouseY:integer;

//If the left mouse button is down then true is returned
function AppMouseLeft:boolean;

//If the right mouse button is down then true is returned
function AppMouseRight:boolean;

//If the middle mouse button is down then true is returned
function AppMouseMiddle:boolean;

//If a mouse button other than the left, right or middle button is down then true is returned
function AppMouseOther:boolean;

//If the mouse wheel is being scrolled up, then true is returned
function AppMouseWheelUp:boolean;

//If the mouse wheel is being scrolled down, then true is returned
function AppMouseWheelDown:boolean;

implementation

var
  event: pSDL_EVENT;
  keys: array[1 .. 255] of boolean;
  mousex, mousey: integer;
  mouseleft, mouseright, mousemiddle, mouseother, mousewheelup, mousewheeldown: boolean;

procedure InitAppInput;
var
  i : integer;
begin
  for i := 0 to Length(keys) do
    keys[i] := false;
  NEW(event);
end;

procedure QuitAppInput;
begin
  DISPOSE(event);
end;

procedure RefreshEvents;
begin
  if SDL_POLLEVENT(event) = 1 then
    begin
      case event^.type_ of
        SDL_KEYDOWN : keys[event^.key.keysym.sym] := true;
        SDL_KEYUP : keys[event^.key.keysym.sym] := false;
        SDL_MOUSEMOTION : begin
                            mousex := event^.motion.x;
                            mousey := event^.motion.y;
                          end;
        SDL_MOUSEBUTTONDOWN : begin
                                case event^.button.button of
                                  SDL_BUTTON_LEFT : mouseleft := true;
                                  SDL_BUTTON_RIGHT : mouseright := true;
                                  SDL_BUTTON_MIDDLE : mousemiddle := true;
                                  SDL_BUTTON_WHEELUP : mousewheelup := true;
                                  SDL_BUTTON_WHEELDOWN : mousewheeldown := true;
                                otherwise
                                  mouseother := true;
                                end;
                             end;
        SDL_MOUSEBUTTONUP : begin
                              case event^.button.button of
                                SDL_BUTTON_LEFT : mouseleft := false;
                                SDL_BUTTON_RIGHT : mouseright := false;
                                SDL_BUTTON_MIDDLE : mousemiddle := false;
                                SDL_BUTTON_WHEELUP : mousewheelup := false;
                                SDL_BUTTON_WHEELDOWN : mousewheeldown := false;
                              otherwise
                                mouseother := false;
                              end;
                            end;
      end;
    end;
end;

function AppKeyPressed(code : integer) : boolean;
begin
  RefreshEvents;
  Result := keys[code];
end;

function AppMouseX : integer;
begin
  RefreshEvents;
  Result := mousex;
end;

function AppMouseY : integer;
begin
  RefreshEvents;
  Result := mousey;
end;

function AppMouseLeft : boolean;
begin
  RefreshEvents;
  Result := mouseleft;
end;

function AppMouseRight : boolean;
begin
  RefreshEvents;
  Result := mouseright;
end;

function AppMouseMiddle : boolean;
begin
  RefreshEvents;
  Result := mousemiddle;
end;

function AppMouseOther : boolean;
begin
  RefreshEvents;
  Result := mouseother;
end;

function AppMouseWheelUp : boolean;
begin
  RefreshEvents;
  Result := mousewheelup;
end;

function AppMouseWheelDown : boolean;
begin
  RefreshEvents;
  Result := mousewheeldown;
end;

end.
Programming - a skill for life!

by Max Foster: L6 Age ~16