Unit gameScreen

The code of gameScreen

unit gameScreen;
{
    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, gl, glu;

type ObjGameScreen = class(TObject)
{This class is for having an OpenGL windows that can be drawn to}
  private
    screenSurface : PSDL_Surface;
  public
    procedure Init(screenWidth, screenHeight : integer);
    procedure SetName(WindowName : PChar);
    function Screen : PSDL_Surface;
end;

procedure FlipScreen;

implementation

procedure ObjGameScreen.Init(screenWidth, screenHeight : integer);
begin
  //Setting up all the OpenGL stuff necessary
  SDL_INIT(SDL_INIT_VIDEO);

  SDL_GL_SETATTRIBUTE(SDL_GL_RED_SIZE, 8);
  SDL_GL_SETATTRIBUTE(SDL_GL_GREEN_SIZE, 8);
  SDL_GL_SETATTRIBUTE(SDL_GL_BLUE_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  SDL_GL_SETATTRIBUTE(SDL_GL_DEPTH_SIZE, 64);
  SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
  SDL_GL_SETATTRIBUTE(SDL_GL_DOUBLEBUFFER, 1);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

  //Create a new surface for OpenGL that can be drawn to
  screenSurface := SDL_SetVideoMode(screenWidth, screenHeight, 32, SDL_OPENGL or SDL_HWSURFACE );
  if (screenSurface = nil) then
    begin
      SDL_Quit;
      writeln('Error Setting Video Mode');
      readln;
      Halt;
    end;

  glClearColor(1, 0, 0, 0); //Set the refresh colour to black
  glViewport(0, 0, screenWidth, screenHeight); //Set the area to render
  glMatrixMode(GL_PROJECTION); //Imagine a screen with images being projected onto it
  glLoadIdentity(); //Load the matrix
  gluOrtho2d(0, screenWidth, ScreenHeight, 0); //Set the size of the projector
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glEnable(GL_TEXTURE_2D);
  glDisable(GL_DEPTH_TEST); //This is 2D - we don't need depth testing

end;

procedure ObjGameScreen.SetName(WindowName : PChar);
begin
  SDL_WM_SetCaption(WindowName, nil);
end;

function ObjGameScreen.Screen : PSDL_Surface;
begin
  result := screenSurface;
end;

procedure FlipScreen;
begin
  SDL_GL_SWAPBUFFERS;
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