Unit gameVariables

The code of gameVariables

unit gameVariables;
{
    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, sdl_gfx;

const
  engineVersion = '0011';
  maxSpeedsXY = 1000; //For setting upper limits on object speeds
  gameName = 'Lost In Space';
  gameVersion = 2;
  constFrameRate = 60;
  constFrameDelay = 1000 div constFrameRate;
  RadToDeg = 180 / pi;
  DegToRad = pi / 180;

  //SDL Init flags
  InitVideo = 1;
  InitAudio = 2;

  //Object flags
  SpriteUpdate = 1;

  //Game constants
  maxSounds = 256;
  maxSprites = 65535;//4096;  //This value is only so large as hashing collision isn't properly implemented yet
  maxBullets = 256;
  maxRocks = 32;

type
  S8 = ShortInt;
  U8 = byte;
  chr8 = ANSIChar;  // or char
  S16 = SmallInt;
  U16 = word;
  S32 = LongInt;
  U32 = Cardinal;
  S64 = Int64;
  U64 = UInt64;
  F32 = single;
  F64 = Double;

type TColour = record
  r, g, b : U8;
end;

PColour = ^TColour;

var
  paused : boolean = false;
  frameRate : pFPSManager;
  stage : U8;
  musicVolume : U8;
  frames : U32;

function bitCheck(flags, value : byte) : boolean;
procedure toggleKeyCheck(keyNum : U16; var boolKey : boolean; var toggleValue : boolean);
function HashString(nameX : string; modNum : U16) : U16;

implementation

uses gameInput;

function bitCheck(flags, value : byte) : boolean;
begin
  result := (flags AND value) = value; //Check for a specific value in a byte,
end;                                   //e.g. 8 in 15 = true

procedure toggleKeyCheck(keyNum : U16; var boolKey : boolean; var toggleValue : boolean);
{Check if a key is pressed down, then invert it
}
begin
  if ((checkKeyState(keyNum) and (boolKey = false))) then
    boolKey := true;

  if ((checkKeyState(keyNum) = false) and (boolKey = true)) then
    begin
      toggleValue := not(toggleValue);
      boolKey := false;
    end;
end;

function HashString(nameX : string; modNum : U16) : U16;
{Take a string and the key value, and then perform an algorithm to turn it into
a number. The same input will always give the same output.
}
var
  hashedAddress : U16;
  inputName : string;
  count : U8;
begin
  hashedAddress := 1;
  inputName := uppercase(NameX);//Always uppercase the values, otherwise confusion can occur
  for count := 1 to length(NameX) do
    begin
      hashedAddress := (hashedAddress * ord(inputName[count])) - ord(inputName[count]);
    end;//(Current value * ASCII value of current letter) - ASCII value of current letter

  result := abs((hashedAddress * hashedAddress) mod modNum);
  //Result must be positive and larger than 0
  if (result < 1) then
    result := 1;
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