Program TesterGameEngine

The code of program TesterGameEngine

program TesterGameEngine;
{
    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/
}
  {$mode delphi}
  {$apptype gui}

uses
  Classes, Math, CRT, SDL, SDL_GFX, GL, GLU,  GLUT, SysUtils,
  uGlobalVariables, uCamera, uGeometry, uBattle;
var
  fin: boolean = False; //whether or not the program should terminate
  DBase: TSearchRec;

begin
  randomize;
  //allocate space for the screen and event variables
  new(event);
  new(screen);
  WndW := 640;  //Set the window width for use in camera calculations
  WndH := 480;  //Set the window height for use in camera calculations
  Count := 0;   //Set the generic counter to 0
  SetupEnemies;
  GenerateWalls;
  //set up player stats
  pMHP := 100;
  pHP := 100;
  pMMP := 50;
  pMP := 50;
  pAtk := 3;
  pDef := 3;
  pSpeed := 3;

  SDL_INIT(SDL_INIT_VIDEO); //initialise SDL video mode

  SDL_GL_SETATTRIBUTE(SDL_GL_RED_SIZE, 5);
  SDL_GL_SETATTRIBUTE(SDL_GL_GREEN_SIZE, 5);
  SDL_GL_SETATTRIBUTE(SDL_GL_BLUE_SIZE, 5);
  SDL_GL_SETATTRIBUTE(SDL_GL_DEPTH_SIZE, 16);
  SDL_GL_SETATTRIBUTE(SDL_GL_DOUBLEBUFFER, 1);

  //Set up the game window.
  case MessageBoxA(0, 'Run in fullscreen mode?', 'Fullscreen', ($00000004) or ($00000020)) of
    // $00000004 makes a message box of yes/no type and $00000020 makes the ? symbol appear in
    // the text portion of the box.
    7: screen := SDL_SETVIDEOMODE(640, 480, 32, (SDL_OPENGL) or (SDL_SRCALPHA));
    //a No button will always return 7
    6: screen := SDL_SETVIDEOMODE(640, 480, 32, (SDL_OPENGL) or
                                  (SDL_FULLSCREEN) or (SDL_SRCALPHA)); //a Yes button will always return 6
  end;

  //Make the player choose a question database.
  if FindFirst('*.dat', faAnyFile and faReadOnly, DBase) = 0 then
  begin
    AssignFile(QBase, DBase.Name);
    Count := 0;
    reset(QBase);
    while not EOF(QBase) do
    begin
      Read(QBase, Questions[Count]); //Reads the questions from the database into an array
      Count += 1;
    end;
    Close(QBase);
  end;

  FindClose(DBase);

  SDL_ShowCursor(0); //hide the cursor

  //Initialise OpenGL
  glCLEARCOLOR(0.0, 0.90, 0.95, 0.0);
  glVIEWPORT(0, 0, WndW, WndH);
  glMATRIXMODE(GL_PROJECTION);
  glLOADIDENTITY;
  gluPERSPECTIVE(65.0, WndW / WndH, 1.0, 100.0); //Use perspective OpenGl drawing type
  glMATRIXMODE(GL_MODELVIEW);
  glLOADIDENTITY;
  glCLEAR(GL_COLOR_BUFFER_BIT); //Make sure the screen's display buffer is empty
  glENABLE(GL_CULL_FACE);
  glClearDepth(1.0);                              // Depth Buffer Setup
  glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
  glDepthFunc(GL_LEQUAL);                         // The Type Of Depth Test To Do
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  //depth perspective correction (the nicest openGL allows)
  //Set up main camera
  MainCamera := Camera.Create(0, 2, -20);
  ActiveCamera := MainCamera;
  ActiveCamera.SetUp(Vector.Create(0.0, 1.0, 0.0));
  SDL_WM_GrabInput(SDL_GRAB_ON); //Allows mouse to remain within the game window

  //Begin the main game loop.
  while fin = False do
  begin
    if SDL_POLLEVENT(event) = 1 then
    begin
      case event^.type_ of
        SDL_KEYDOWN:
        begin
          case event^.key.keysym.sym of
            119: forw := True;  //move forwards (w)
            97:  left := True;  //move left (a)
            115: back := True;  //move backwards (s)
            100: right := True; //move right (d)
            113: ActiveCamera.SetPosn(0, 2, 0); //return to (0,0,0) (1)
            27:  fin := True; //end the game (esc)
            122:
            begin  //Force an encounter
              forw := False;
              left := False;
              back := False;
              right := False;
              up := False;
              down := False;
              Battle(1, 1, 4, 5);
            end;
          end;
        end;
        SDL_KEYUP:
        begin
          case event^.key.keysym.sym of
            119: forw := False;  //disable foward moving
            97:  left := False;  //disable left moving
            115: back := False;  //disable backward moving
            100: right := False; //disable right moving
            113: up := False;
            101: down := False;
          end;
        end;
        SDL_MOUSEMOTION:
        begin
          mousex := event^.motion.xrel;  //Get the mouse's x-motion as a value
          mousey := -event^.motion.yrel; //Get the mouse's y-motion as a value
        end;
      end;
    end;

    if (forw xor back) or (left xor right) then
    begin
      if Random(3000) < 1 then
      begin //Whilst moving, there is a 1 in 3000 chance for an encounter (which is actually surprisingly often).
        Battle(1, 1, 4, 5);
        forw  := False;
        left  := False;
        back  := False;
        right := False;
        up    := False;
        down  := False;
      end;
    end
    else
    begin
      if Random(10000) < 1 then
      begin  //Whilst not moving there is a 1 in 10000 chance of an encounter (which isn't very often at all).
        forw  := False;
        left  := False;
        back  := False;
        right := False;
        up    := False;
        down  := False;
        Battle(1, 1, 4, 5);
      end;
    end;

    glMATRIXMODE(GL_MODELVIEW);
    glLOADIDENTITY();
    //This section clears the drawing buffers so that we can refill them and redraw the scene.
    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

    //Deal with moving
    if (forw = True) and (back = False) then
      ActiveCamera.MoveForward(0.0025);
    if (back = True) and (forw = False) then
      ActiveCamera.MoveForward(-0.0025);
    if (right = True) and (left = False) then
      ActiveCamera.MoveRight(0.0025);
    if (left = True) and (right = False) then
      ActiveCamera.MoveRight(-0.0025);

    if ActiveCamera.posn.y < 2 then
      ActiveCamera.posn.y := 2;
    //Activate the camera. (Refresh the position and perpective.)
    ActiveCamera.Activate(WndW, WndH, True);

    GLRect(ActiveCamera.posn.x - 100, 0, ActiveCamera.posn.z + 100,
           ActiveCamera.posn.x + 100, 0, ActiveCamera.posn.z - 100, 0.5, 0.5, 0.0); //The floor

    DrawWalls; //Takes the data from the walls array and turns it into physical walls in game

    SDL_GL_SWAPBUFFERS; //Tells SDL to swap the OpenGL buffers, which amounts to the
                        //scene being redrawn using the contents of the depth and colour buffer bits
  end;

  SDL_QUIT;  //un-initialise all SDL subsystems
end.
Programming - a skill for life!

by Steven Binns: Y12 Age ~17