Opening a Window

 

This tutorial will show you how to open a window using Gadget 2D. First let’s have a look at the simplest project that can be made with Gadget 2D:

 

#include "Gadget/Engine.h" //This file includes all the other files

 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

     Core G; //Create an instance of the game engine

     G.Window.Open();  //This line of code creates a window.

 

     //Load stuff here

 

     while(!G.Window.IsOpen()) //Loop continuously as long as the engine is running

     {

          G.Renderer.BeginFrame(); //Clears the buffers

 

          //Render and move stuff here

             

          G.Renderer.EndFrame(); //Finished rendering, display the result

     }

     return 0;

}            

 

The above code creates an instance of the “Core” class and uses it to create a window using the default settings. Notice that I didn’t tell the engine how big to make the window. By default, the window size is 800 by 600 with a black background.

 

 

The window’s width and height have to be set before the window is opened. Changing these variables after the window has been opened will have no effect. The code below shows how to open a window that is 1024 pixels wide and 768 pixels high. If you plan to open a window in full screen mode, it will need to be one of the standard window sizes. By default, windows are created in windowed mode which means they can be dragged around, they can be any size or aspect ratio, and they share the desktop with other programs you may have running.

     Core G; //Create an instance of the game engine

 

     G.Window.SetSize(1024, 768); //Set the window size

 

     G.Window.Open();  //This line of code creates a window.

 

 

Notice that the windows we have opened so far have all been positioned near the top left corner of the desktop. Sure the user could click on the title bar and drag the window wherever they want it. But we might want the window to start at a different location. We can set the x and y position of the window.

 

     Core G; //Create an instance of the game engine

 

     G.Window.SetSize(1024, 768); //Set the window size

 

     G.Window.SetPosition(600, 50);

 

     G.Window.Open();  //This line of code creates a window.

 

Now let’s do something about that black background. The background color can be changed before or after the window has been created. In fact, you can change the background color as often as you like. Go ahead and turn your monitor into a strobe light if you want to. The code below changes the background color to green by changing the red, green, and blue channels individually. Each channel should be between 0 and 1, where 1 is the maximum brightness for the channel.

     Core G; //Create an instance of the game engine

 

     G.Window.SetClearColor(0.0f, 1.0f, 0.0f); //Set the background color to green

 

     G.Window.Open();  //This line of code creates a window.

 

You can also run your game in full screen mode, also referred to as excusive graphics mode. Make sure your window is set to one of the standard window sizes like 1024 by 768. If the resolution is supported by the video card, your game will fill the screen entirely so that not even the task bar is visible. Games typically run a little faster in this mode. The disadvantage is that the player would need to close the game in order to check their MSN messages. The full screen mode must be set before the window is opened.

     Core G; //Create an instance of the game engine

 

     G.Window.SetSize(1024, 768); //Set the window size

 

     G.Window.SetClearColor(0.0f, 1.0f, 0.0f); //Set the background color to green

 

     G.Window.SetFullScreen(true); //Set this to true for full screen mode

 

     G.Window.Open();  //This line of code creates a window.

 

Example1_5.jpg

Click on the link below to download the Main.cpp file. Replace the Main.cpp that came with the engine by copying over it and then recompile it to run this example.

Windowing.zip  1KB