GDI+ Gaming API (GdiGaming): CTP 1

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="text-decoration:underline GdiGaming API <br/>
Community Technology Preview 1
CTP 1 Download
https://skydrive.live.com/?cid=8357f4a651e4838b&sc=documents&uc=1&id=8357F4A651E4838B%21951# GdiGaming API (by downloading this software, you agree to the terms of use below)
Introduction
The GdiGaming API is a small, simplified, yet complete game-engine framework for Windows Forms applications which uses GDI+ for rendering graphics, Windows Media for playing sound, and Control mouse and keyboard handlers for input. The API provides
a GameEngine component and RenderCanvas control which can be placed on a Form at design-time to setup a game-engine environment. Individual projects then define classes for players, enemies, and other interactive game elements which each inherit from
the API provided GdiGaming.GameObject base class. Each custom class needs only to override appropriate methods on GameObject such as OnInput, OnUpdate, OnCollision, etc. in order to implement its game-logic. Each GameObject instance is assigned
to a GameScene instance, and the GameEngine automatically runs its currently assigned GameScene. Basic asset-managment is also provided to automatically handle loading and reusing image and sound assets.
Features

Simplistic Object-Oriented Design Resource/Asset Management Automatic Rendering of Sprites & Backgrounds Full Collision Animation Mouse and Keyboard Input Multiple Simultaneos Sounds Support for an Event-Driven Model
History
The idea of being able to create your own video game is probably what got me started in programming as a child. And it would be fair to say that I have spent many, many hours experimenting with all kinds of game design ideas. I have attempted
to build a number of game-engine frameworks over the years using VB.Net, with Xarcade and Xarcade Vengence being example games that grew with the development of those engines.
Just prior to the announcement of XNA, I had been working on a 2D game-engine framework based on DirectX. I was to the point of needing to design what I would later learn is called a "content pipeline" when I learned that the XNA Framework as just
around the corner. And when I looked into it, I found that it was nearly identical in concept to what I was building, only it was a complete DirectX implementation, whereas mine was a very narrow implementation and was only supporting 2D rendering
of textures.
So I abandoned that engine and began to learn XNA. It turned out to be an amazing framework, if a bit overwhelming when first starting out. And as of this writing, when one is ready to create a real, modern video-game, something XBox-worthy,
then XNA is still one of the premier development frameworks to choose from.
More recently though a development environment and game-engine named Unity3D has arrived. And this is true game development power made simple. One can think of Unity3D as a prebuilt "XNA" game engine which is cross-platform compatible.
I dont believe it is actually XNA, but it has a similar design concept and is completely scriptable in C#.
But the point is that Unity provides not only the game development framework like XNA, but also a completely rigged game-engine which is ready to be configured via the Unity IDE. So it is something akin to having Visual Studios drag-and-drop/property
grid-based form designer, only for a 3D world space with GameObjects that automatically support rendering, collision, input, updating, and all of the things a game engine needs to provide.
I became a big fan quickly, and created a beta of https://skydrive.live.com/?cid=8357f4a651e4838b&sc=documents&move=8357F4A651E4838B!936&sid=8357F4A651E4838B!522&iscopy=1&id=8357F4A651E4838B%21951#
Xarcade Invasion which is really only lacking an artistic team for completion. This beta demonstrates most of the major components of a complete game, and plays pretty well with a wired XBox controller plugged into your PCs USB port (keyboard works
too, but is harder to use).
Heres the thing though: even as well designed and easy to use as Unity is, it is even more overwhelming than XNA when you first get started. And there are quite a few foundational concepts about 3D graphics which one may need to learn before some
portions of Unitys options make sense.
So I thought: "what if we took the same pre-designed game-engine concept as Unity, but simplified it down to only handle a classic 2-D style video game?" The answer I came up with is the GdiGaming API.
Overview
The primary component of the GdiGaming API is the GameEngine. The GameEngine componet handles the execution of the primary game loop and performs the following operations on each game loop iteration:
<ol>
Updates Input<br/>
This includes updating the GameInput object, calling the OnInput method for each GameObject, and testing for/calling the mouse input methods for each object.
Updates Each Background Updates Each Game Object and Checks Collision Draws Each Background and GameObject Updates the Playing Audio Raises the GameLoop Event for the Containing Form Waits any Remaining Time in this Frame Updates the GameTime and Frame Rate Raises the FrameComplete Event for the Containing Form Restarts the Game Loop </ol>
The GameEngine contains a collection of GameScene objects, and has a CurrentSceneName property. When the containing form loads, it creates instances of the required GameScene objects and adds them to the GameEngine. It then sets the CurrentSceneName
on the GameEngine to the games initial scene.
The GameScene is an abstract class which the developer inherits from in order to create specific scenes for the game. Each scene is simply a named container for a group of backgrounds and game objects. Individual scenes can represent individual
levels in a game, or one scene might contain logic to handle multiple levels. Typically things like the games startup screen, main game screen, and menu screen would each be individual GameScene classes. The scene will override the Name property
to return a unique name, and then will typically override the OnLoad method to setup all necessary scene backgrounds, gameobjects, and data.
The GameObject is an abstract class which the developer inherits from in order to create the interactive elements of a game such as players, enemies, items, and logic-components. The GameObject provides the following overridable methods which allow
the developer to implement game-logic:

OnCollision OnDraw OnLoad OnInput OnMouseDown OnMouseUp OnUpdate
Each method provides an eventargs reference which contains a reference to the game engine, game time, and possibly other appropriate data according to the method.
So every game will have one instance of a GameEngine component and one instance of a RenderCanvas control added to the main form, and then will define individual GameObject and GameScene classes in order to create all of the games content.
Asset management is handled by the GdiGaming.AssetManager object on the GameEngine. When you develop your game project, you add a folder to the project called "Assets". All of the games image and sound files are then added to this folder.
You only need to set each files "Copy to Output Directory" property to "Always", and then the AssetManager will allow your code to refer to any asset by its file name. The asset manager will take care of loading an asset from disk the first time it
is requested, and then reusing that asset for subsequent requests. Each GameScene will also cache the assets it uses, so all game-logic should go through the GameEngine.CurrentScene to request assets.
Performance Considerations
The GameEngine defaults to a framerate of 30FPS; it is suggested that you do not change this default value.
The performance it highly dictated by the number of sprites being drawn to the screen, the number of objects serving as colliders, and the number of objects handling collision (an object can cause collison with another object, but have no capacity to respond
to that collision itself).
The GameEngine can support thousands of GameObjects, but the more of them which require advanced rendering (such as rotation, scaling, multiple sprites, etc) or use full collision, the lower the total becomes. The GameEngine reveals a FrameRate property
and IsRunningSlow property which can be used to montior performance and limit the number of new objects added to the game at one time. IsRunningSlow will return true whenever the current framerate has dropped below 80% of the target framerate.
This allows the game logic to tune itself before the drop in performance becomes noticeable to the player.
In my testing on a powerful computer, the API can handle ~2400 objects, all of which are colliders and 10-20 of which handle their collision. A framerate of 27-29 FPS is maintained without exceeding 50% the processor core used. If the total number
of objects is reduced to ~1000, then over 100 objects with full collision can be supported at 28-29 FPS. So every game will have to balance its total number of visible game objects with collision, based on overall needs.
Also, note that each simultaneous sound requires a new instance of the MediaPlayer class, so care should be taken to not play too many sound effects too rapidly. The framework will limit simultaneous playing sounds to 20 by default.
Getting Started
After downloading and extracting the GdiGaming API zip file to a location on your hard drive, start a new Windows Forms Application project.
Make a few minor changes to the default Form1 in the designer:
<ol>
Set the FormBorderStyle to Fixed3D Set the MaximizeBox to False Set the Size to 810,630 </ol>
Next, right-click an item in the ToolBox and select Add Tab. Name the new tab "GdiGaming Components". Right-click this new tab and select "Choose Items...". When the dialog opens, click the Browse button, locate the GdiGaming.dll,
and click OK. This will add the GameEngine and RenderCanvas items to the .Net Framework Components tab and should have them checked. Click OK again.
Now the GameEngine and RenderCanvas should be in your ToolBox and you can drag one of each onto Form1.
Go to the properties for the RenderCanvas and set its Location to 0,0.
Go to the properties for the GameEngine, select its Canvas property and use the DropDown to select RenderCanvas1.
Now you can go to the Code View for Form1 and paste in the following template code:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Activated(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Activated
GameEngine1.StartGame()
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Deactivate(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Deactivate
<span style="color:Blue; If GameEngine1.EngineState = GdiGaming.GameEngineState.Started <span style="color:Blue; Then
GameEngine1.PauseGame()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub Form1_FormClosing(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.Windows.Forms.FormClosingEventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.FormClosing
<span style="color:Blue; If <span style="color:Blue; Not GameEngine1.EngineState = GdiGaming.GameEngineState.Stopped <span style="color:Blue; Then
GameEngine1.EndGame()
e.Cancel = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Green; Add the games initial scene to the engine; replace "ExampleScene" with your custom scene
GameEngine1.Scenes.Add(<span style="color:Blue; New ExampleScene)
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub GameEngine1_FrameComplete(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As GdiGaming.GameEngineEventArgs) <span style="color:Blue; Handles GameEngine1.FrameComplete
<span style="color:Green; Uncomment the following line to easily monitor the framerate during development:
<span style="color:Green; Me.Text = e.Engine.FrameRate.ToString
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub GameEngine1_GameStopped(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles GameEngine1.GameStopped
<span style="color:Blue; Me.Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class

[/code]
<br/>
This basic procedure can be used for each new game and you then only need to modify the Scene objects which are created during Form.Load.

Finally begin creating your custom GameObjects and Scenes!
Example
An example set of classes and assets will be provided in a subsequent post to this thread.
Summary
The GdiGaming API is designed as a beginners learning tool for creating video games in .Net. The API provides a simplistic, yet complete, game-engine and development framework. The API is suitable for small "classic-style" 2D games with up to
a couple of thousand game objects, a few hundred of which are serving as colliders, with about 10% of the colliders actually handing their unique collisions.
While initial testing has shown this framework to perform pretty well given the amount of functionality it provides, it still needs to be tested on slower computers. While there may still be bugs to discover, and there is certainly still functionality
to modify or add, the current features seem to be performing as intended and with the expected system resource utilization (which is relatively high for the type of game, due to GDI not being entirely suited to this type of interaction, but still well within
reason for a Windows Forms application).
Comments and documentation will be added in future releases; Id like to get some initial feedback and usage suggestions before writing too much documentation, in case any major issues are discovered.
Terms of Use
The GdiGaming API is authorized solely for non-commercial use in personal and/or educational projects.
Decompiling or otherwise deconstructing the compiled assembly is strictly prohibited.
You are hereby granted the right to redistribute this API in any non-commercial personal and/or education product, providing that these terms are included with your compiled assembly.
This API is provided "as-is" without warranty of any kind either expressed or implied, and is not guaranteed to be fit for any purpose.
<hr class="sig Reed Kimble - "When you do things right, people wont be sure youve done anything at all"

View the full article
 
Back
Top