Global Variables Unit

The global variables unit of program TesterGameEngine

unit uGlobalVariables;
{
    Copyright (c) 2013 Steven Binns

    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/
}
interface

uses
  Classes, SysUtils, SDL, GL, GLU, GLUT, cmem;

procedure FILLEDRECTCOLOUR(x, y, w, h, colour: integer);
procedure RECTCOLOUR(x, y, w, h, colour: integer);
procedure Start2D;
procedure End2D;
procedure TextBox(Text: string);
procedure DrawText(X, Y: integer; Text: string);
procedure SetupEnemies();
procedure GenerateWalls();
function MessageBoxA(HWnd: integer; Text, Caption: PChar; Flags: integer): integer;
  stdcall; external 'user32.dll';

type
  tWall = class(TObject)
  public
    x1, y1, z1, x2, y2, z2, orientation: integer;
    constructor Create(newx1, newy1, newz1, newx2, newy2, newz2, neworient: integer);
  end;

type
  tMyRect = class(TObject)
  public
    x, y, Width, Height: integer;
    constructor Create(newx, newy, newwidth, newheight: integer);
    function getSDLRect(): pSDL_RECT;
  end;

type
  Enemy = class(TObject)
  public
    ID: string;
    atk, def, speed, hp: integer;
    constructor Create(newID: string; newatk, newdef, newspeed, newhp: integer);
  end;

type
  TFont = class(TObject)
  public
    TTexture: record
      Width, Height: integer;
      ID: GluInt;
    end;
  end;

type
  TQuestion = record
    QNumber: string[3];
    QSubject: string[32];
    QQuestion: string[64];
    QAnswer0, QAnswer1, QAnswer2, QAnswer3, QAnswer4, QAnswer5: string[32];
  end;

var
  QBase: file of TQuestion;
  Questions: array [0 .. 12] of TQuestion;
  screen: pSDL_SURFACE;
  event: pSDL_EVENT;
  textures: array [0 .. 1] of GLuint;
  fullscreen: boolean;
  mousex, mousey, WndH, WndW: integer;
  left, right, forw, back, up, down: boolean;
  pixels: pointer;
  Count: integer; //generic counter variable
  enemies: array [0 .. 99] of enemy;
  walls: array [0 .. 99] of tWall;
  pAtk, pDef, pSpeed, pHP, pMHP, pMP, pMMP: integer; //Stats

implementation

constructor tMyRect.Create(newx, newy, newwidth, newheight: integer);
  //constructs a rectangle object
begin
  x := newx;
  y := newy;
  Width := newwidth;
  Height := newheight;
end;

constructor Enemy.Create(newID: string; newatk, newdef, newspeed, newhp: integer);
begin //constructs an Enemy object
  ID := newID;
  atk := newatk;
  def := newdef;
  speed := newspeed;
  hp := newhp;
end;

function tMyRect.getSDLRect(): pSDL_RECT;
  //generates an SDL_RECT type variable from a tMyRect object
var
  temp: pSDL_RECT;
begin
  new(temp);
  temp^.x := x;
  temp^.y := y;
  temp^.w := Width;
  temp^.h := Height;
  Result := temp;
end;

procedure FILLEDRECTCOLOUR(x, y, w, h, colour: integer);
begin //draws a filled rectangle using SDL
  SDL_FILLRECT(screen, tMyRect.Create(x, y, w, h).getSDLRECT(), colour);
end;

procedure RECTCOLOUR(x, y, w, h, colour: integer);
begin //draws an empty rect using SDL
  SDL_FILLRECT(screen, tMyRect.Create(x, y, w, h).getSDLRECT(), colour);
  SDL_FILLRECT(screen, tMyRect.Create(x + 1, y + 1, w - 2, h - 2).getSDLRECT(), $00000000);
end;

procedure Start2D();
begin //Begins orthographic (2D) drawing for the scene
  glMatrixMode(GL_PROJECTION);
  glPushMatrix(); //saves the projection matrix to the stack
  glLoadIdentity(); //empties the projection matrix
  gluOrtho2D(0.0, WndW, 0.0, WndH); //switches to orthographic mode
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix(); //saves the modelview matrix to the stack
  glLoadIdentity(); //empty the modelview matrix
  glClear(GL_DEPTH_BUFFER_BIT);
  glDisable(GL_DEPTH_TEST);
end;

procedure End2D();
begin //Switches back to 3D drawing
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  //pops the projection matrix we saved earlier back into the actual projection matrix
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
  //pops the modelview matrix we saved earlier back into the actual projection matrix
  glEnable(GL_DEPTH_TEST);
end;

procedure TextBox(Text: string); //Brings up a text box
var
  test_enter: pSDL_EVENT;
  done: boolean;
begin
  glCLEARCOLOR(0.0, 0.0, 0.0, 0.0);
  new(test_enter);
  done := False;
  glEnable(GL_TEXTURE_2D); //Enables 2D texturing

  Start2D;

  glDisable(GL_TEXTURE_2D);

  glBegin(GL_QUADS); //draws the background of the textbox
  glColor3f(0, 0.0, 0.0);
  glVertex2f(5.0, WndH - 475);
  glVertex2f(WndW - 5, WndH - 475);
  glVertex2f(WndW - 5, WndH - 360);
  glVertex2f(5.0, WndH - 360);
  glEnd();
  glBegin(GL_LINE_LOOP); //draws the outline of the textbox
  glColor3f(1.0, 0.0, 0.0);
  glVertex2f(5, WndH - 475);
  glVertex2f(WndW - 5, WndH - 475);
  glVertex2f(WndW - 5, WndH - 360);
  glVertex2f(5, WndH - 360);
  glEnd();
  DrawText(10, 95, Text);
  End2D;
  SDL_GL_SWAPBUFFERS;
  repeat
    SDL_POLLEVENT(test_enter);
    if (test_enter^.type_ = SDL_KEYUP) and (test_enter^.key.keysym.sym = 13) then
      done := True; //hold game until enter is pressed
  until done = True;
  glCLEARCOLOR(0.0, 0.90, 0.95, 0.0);
end;

procedure DrawText(X, Y: integer; Text: string); //renders text onto the screen
var
  i, j: integer;
begin
  glColor4f(1, 1, 1, 1.0);
  j := X;
  glDisable(GL_TEXTURE);
  glDisable(GL_TEXTURE_2D);
  for i := 0 to Length(Text) do
  begin
    if j > 1 then
      j += glutBitmapWidth(GLUT_BITMAP_TIMES_ROMAN_24, Ord(Text[i - 1]));
    glRasterPos2i(j, Y);
    //move the raster (text drawing) position along to the next character position
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, Ord(Text[i]));
    //render each character in turn
  end;
  glEnable(GL_TEXTURE);
  glEnable(GL_TEXTURE_2D);

end;

procedure SetupEnemies();
begin //fills the 'enemies' array
  enemies[1] := Enemy.Create('Slime', 5, 1, 3, 10);
  enemies[2] := Enemy.Create('Warg', 10, 1, 3, 10);
  enemies[3] := Enemy.Create('Pixie', 5, 5, 5, 15);
  enemies[4] := Enemy.Create('Ogre', 10, 5, 2, 20);
  enemies[5] := Enemy.Create('Mothbat', 10, 1, 7, 10);
end;

constructor tWall.Create(newx1, newy1, newz1, newx2, newy2, newz2, neworient: integer);
begin //constructs a wall object
  x1 := newx1;
  x2 := newx2;
  y1 := newy1;
  y2 := newy2;
  z1 := newz1;
  z2 := newz2;
  orientation := neworient;
end;

procedure GenerateWalls; //fills the 'walls' array
var
  i: integer;
begin
  for i := 0 to 99 do
  begin
    walls[i] := tWall.Create(0, 0, 0, 0, 0, 0, 0);
  end;
  //orientation 0 varies in x
  //orientation 1 varies in z
  //orientation 2 is parallel to the floor
  walls[0] := tWall.Create(-20, 0, 10, 20, 15, 10, 0);
  walls[1] := tWall.Create(-20, 0, -10, -20, 15, 10, 1);
  walls[2] := tWall.Create(20, 0, -10, 20, 15, 10, 1);
  walls[3] := tWall.Create(-20, 0, -10, -5, 15, -10, 0);
  walls[4] := tWall.Create(5, 0, -10, 20, 15, -10, 0);
  walls[5] := tWall.Create(-30, 0, -20, -5, 15, -20, 0);
  walls[6] := tWall.Create(5, 0, -20, 30, 15, -20, 0);
  walls[7] := tWall.Create(-5, 0, -20, -5, 10, -10, 1);
  walls[8] := tWall.Create(5, 0, -20, 5, 10, -10, 1);
  walls[9] := tWall.Create(-30, 0, -20, -30, 15, 20, 1);
  walls[10] := tWall.Create(30, 0, -20, 30, 15, 20, 1);
  walls[11] := tWall.Create(-30, 0, 20, 30, 15, 20, 0);
  walls[12] := tWall.Create(5, 10, -10, -5, 10, -20, 2);
  walls[13] := tWall.Create(5, 10, -20, -5, 15, -20, 0);
  walls[14] := tWall.Create(5, 10, -10, -5, 15, -10, 0);
  walls[15] := tWall.Create(20, 15, 10, -20, 15, -10, 2);
end;

end.
Programming - a skill for life!

by Steven Binns: Y12 Age ~17