Before you begin
Before you begin with this tutorial it is important that you these things covered:
- Have your development environment set up correctly
- Know how to set up an empty project and compile/link against Frontend
The Getting Started section has tutorials for how to do this on the various platforms.
The Simple Framework
Classes tagged in with Simple wraps up more complex Frontend-functionality so it’s easier to get started. They are not meant for use in final game engines, but are nice for quick prototyping and demonstration. When you feel ready for it, you can replace these Simple-classes with your own, more specialized ones. Looking inside Simple-classes to see how they are implemented is a nice way to learn Frontend.
SimpleSetup sets up a lot of other Frontend-classes for you so you get a 3D graphics window straight up in no time. It sets up one window with a Graphics::Device covering the whole window. Graphics::Device interfaces are used to controll the 3D graphics hardware.
While following this tutorial, its a good idea to loop up classes and functions in the documentation.
Creating a SimpleSetup-object
SimpleSetup takes two arguments to its constructor:
- A WindowManager interface. This represents the window manager in which the window should be created. Here, we should pass in the OpenFrontend native window manager for now, that is GUI::CreateNativeWindowManager().
- A Graphics::Device::Factory interface. This should be a factory that can produce Graphics::Device objects of the desired type. Here you can choose wether you want to use the OpenFrontend implementation based on OpenGL or Direct3D9:
- OpenGL: Graphics::OpenFrontendGL2CG()
- Direct3D9: Graphics::OpenFrontendDX9HLSL() (Windows only)
To summarize, this is how your program should look so far:
#include <Frontend2Utils.h>
using namespace Frontend;
using namespace Frontend::Utils;
using namespace Frontend::Utils::Simple;
int main(int argc, char** argv)
{
SimpleSetup setup(GUI::CreateNativeWindowManager(), Graphics::OpenFrontendGL2CG());
… // The rest of the code in this tutorial goes here
}
Configuring SimpleSetup
Now that the SimpleSetup-object is created, we can set various settings on it describing how we want our application window to appear. See the SimpleSetup documentation for a complete list of all options. For now, we’ll just set the resolution to 800×600 and set the window title to “Hello Frontend”. When we are done setting options, we call the method Start() to tell SimpleSetup that we are good to go.
setup.SetTitle("Hello Frontend");
setup.Start();
Creating a Mainloop
After calling Start() we should load other things we need for our application, like textures and 3D scenes. As we don’t have anything like that for now, we skip this part, but remember this for later.
When we are done loading its time to enter an infinite loop called the mainloop. This loop will iterate “forever”, until the game/application is terminated. Each iteration of this loop corresponds to one frame, that is, we should draw a new image to the screen each iteration, and then call Update() on the setup object to present the image on the screen. Update() also processes messages sent to our window by the operating system, so the window feels responsive and behaves as expected. It is therefore important that we call that function often.
{
… // Pr-frame code goes here
}
Note that we will terminate the loop when Update() returns false. Update() returns false whenever the user or the operating system has sent a message to our window and instructed us to close. Quitting the loop when this happens will make Alt+F4 and clicking the X in the window corner work as expected.
Clearing the screeen
Now our mainloop is running, but we are not doing anything inside it yet. If you try to run your program now, you’ll probably get a black screen or some garbled image. If you get a garbled image, this is because the memory holding your window’s image is uninitialized and holds whatever that memory was used for earlier, which is not necessarily graphics at all. In order to clear the screen, we’ll have to use the Graphics::Device interface. SimpleSetup has already made this for us, and we can get it using GetGraphicsDevice(). Once we have it, we call the Clear() function on it.
This should clear the screen to red color. See the documentation for Clear() to see what all the parameters do.
This concludes Part 1 :)
Source code
Complete source code and VC++ project for this tutorial is found in the SVN repository at http://svn2.assembla.com/svn/fwg/trunk/tutorials/simple/part1/.