Unit gameInput

The code of gameInput

unit gameInput;
{
    Copyright (c) 2011 Christopher Winward
    
    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, gameVariables, sdl;

procedure keyArraySetup;
procedure keyArrayUpdate;
procedure toggleKeyState(key : U16);
function checkKeyState(key : U16) : boolean;
procedure toggleMouseState(key : U8);
function checkMouseState(key : U8) : boolean;
function GetMouseX : S32;
function GetMouseY : S32;

implementation

var
  mouseX, mouseY : S32;  //These are all here so they aren't accidentally changed from outside
  keyInputArray : array[1 .. 321] of boolean;  //unless specfically told to.
  mouseInputArray : array[1 .. 5] of boolean;
  AppFocus : boolean;

procedure keyArraySetup;
var
  count : U16;
begin
  for count := 1 to 321 do
    begin
      keyInputArray[count] := false;
    end;
  for count := 1 to 5 do
    begin
      mouseInputArray[count] := false;
    end;
  if (count = 322) then
    writeln(AppFocus); //This is to stop a specific hint appearing
end;

procedure keyArrayUpdate;
var
  event : TSDL_Event;
  count : U16;
begin
  while (SDL_PollEvent(@event) > 0) do
    begin
      for count := 1 to 321 do
        begin
          if (event.key.keysym.sym = count) then
            begin
              if (event.type_ = SDL_KeyDown) then
                begin
                  keyInputArray[count] := true;
                end;
              if (event.type_ = SDL_KeyUp) then
                begin
                  keyInputArray[count] := false;
                end;
            end;
        end;

      for count := 1 to 5 do
        begin
          if (event.button.button = count) then
            begin
              if (event.type_ = SDL_MouseButtonDown) then
                begin
                  mouseInputArray[count] := true;
                end;
              if (event.type_ = SDL_MouseButtonUp) then
                begin
                  mouseInputArray[count] := false;
                end;
            end;
        end;

      if (event.type_ = SDL_ActiveEvent) then
        begin
          if (SDL_GetAppState = SDL_AppActive) then
            begin
              AppFocus := false;
            end
          else
            begin
              AppFocus := true
            end;
        end;
    end;

  SDL_GetMouseState(mouseX,mouseY);

end;

function checkKeyState(key : U16) : boolean;
begin
  result := keyInputArray[ord(key)];
end;

procedure toggleKeyState(key : U16);
begin
  keyInputArray[ord(key)] := not (keyInputArray[ord(key)]);
end;

function checkMouseState(key : U8) : boolean;
begin
  result := mouseInputArray[ord(key)];
end;

procedure toggleMouseState(key : U8);
begin
  mouseInputArray[ord(key)] := not (mouseInputArray[ord(key)]);
end;

function GetMouseX : S32;
begin
  result := MouseX;
end;

function GetMouseY : S32;
begin
  result := MouseY;
end;

end.

Remarks

Could you write a game that uses routines in these SpaceShooter units?

Programming - a skill for life!

by Christopher Winward: L6 Age ~16