Creating an OpenGL App - neslib/DelphiLearnOpenGL GitHub Wiki
The first thing we need to do to create stunning graphics is to create an OpenGL context and an application window to draw in. However, those operations are specific per operating system and OpenGL purposefully tries to abstract from these operations. This means we have to create a window, define a context and handle user input all by ourselves.
Application Framework
To facilitate this, we create a simple application framework in Delphi. This framework is very small and lightweight, and nothing as complex as FireMonkey or VCL at all. It is a good exercise in creating your own framework though, and you can easily build on top of it.
All framework classes are located in the directory \Tutorials\Common
. It's main class is TApplication
defined in the unit Sample.App
. This is an abstract base class which each tutorial derives from:
type
TApplication = class abstract
public
constructor Create(const AWidth, AHeight: Integer; const ATitle: String); virtual;
procedure Initialize; virtual; abstract;
procedure Update(const ADeltaTimeSec, ATotalTimeSec: Double); virtual; abstract;
procedure Shutdown; virtual; abstract;
procedure MouseDown(const AButton: TMouseButton; const AShift: TShiftState;
const AX, AY: Single); virtual;
procedure MouseMove(const AShift: TShiftState; const AX, AY: Single); virtual;
procedure MouseUp(const AButton: TMouseButton; const AShift: TShiftState;
const AX, AY: Single); virtual;
...
end;
You must override the methods Initialize
, Update
and Shutdown
:
Initialize
is where you create and load you OpenGL resources like buffers, textures, shaders etc.Update
is called for every frame (about 60 times per second) to update the application state and render a new frame.Shutdown
is where you clean up the app and release any resources that you created in theInitialize
method.
You can optionally override event methods like MouseDown
, KeyUp
etc., which are called whenever such an event is generated.
The [next tutorial](1.1 Hello Window) shows how to create the simplest OpenGL application using this class.
TPlatform
Class
We encapsulate all operating system specific operations, in an abstract static base class TPlatformBase
in the unit Sample.Platform
. This is a static class because it only defined class methods and class properties. It defines a single virtual method DoRun
that each OS-specific subclass overrides:
type
{ Static base class for platform-specific functionality. For every platform
(Windows, MacOS, iOS and Android), there is a derived class. }
TPlatformBase = class abstract // static
protected
{ Must be overridden to run the application. }
class procedure DoRun; virtual; abstract;
public
{ Runs the application. }
class procedure Run(const AApp: TApplication); static;
...
end;
TPlaformClass = class of TPlatformBase;
var
{ Actual platform class. For example, on Windows this will be TPlatformWindows }
TPlatform: TPlaformClass = nil;
You actually use the TPlatform
variable, which is set to the actual class type used, depending on operating system. So, to run an application, you can write something like TPlatform.Run(MyApplication)
. More on this in the next tutorial.
For each operating system, there is an additional unit (for example Sample.Platform.Windows
) that defines the actual platform class (like TPlatformWindows
).
If you are interested in how the platform-specific details of each operating system are handled, then take a look at these units. They are fairly well documented and should give you an idea of what is involved in creating a render view and OpenGL context, and how to respond to mouse/keyboard/touch input.
Now, lets create our first app in the [next tutorial](1.1 Hello Window).
:arrow_left: OpenGL (ES) | Contents | [1.1 Hello Window](1.1 Hello Window) :arrow_right: |
---|