Monday, May 22, 2017

CRUD oriented game content management : PART 2.a : Creation



PART 2.a

Game content loading and creation :

Z-GDK specifically designed for making games. It relies heavily on image file for its content, but not just an image file, there are different types of image content primarily used in games. A Single image file is the simplest form of an image content to display a static sprite or mesh texture material to be rendered on screen. Some are using multiple image files to create an animated 2D sprite or 3D billboard and particles texture material. Others are using  a single image sprite sheet file for their animated sprite characters or a single image resource file consist of tiny images are commonly used for sprite, say for tile map content or an image collection of inventory items stored in just a single image file.

The idea is to create a TextureContent  that has a definition of image animation key set, if an animated image collection, in preparation for SpriteEntity and or 3D animated texture usage, a single instance of TextureContent can be applied to multiple SpriteEntity or 3D animated texture material as a source image texture content.

Z-GDK has different kinds of direct-texture-content-loading and creation scheme as listed below without using content pipeline processors and not relying on XNB files.

A. Creating  TextureContent  from a single image source file.
B. Creating single animated  TextureContent  from multiple image source files.
C. Creating single animated TextureContent from a single sprite sheet image source file.
D. Creating multiple static or animated TextureContent from texture atlas image source file.
E. Creating TextureContent from color. 



Let's start from the basic texture content loading and creation from a single image source file :

A. Creating  TextureContent  from a single image source file:
------------------------------------------------------------------------------------------------------------------

The simplest way of creating TextureContent is by loading from a single image source file, as shown
below:

Sample of  single image file: named ZGDKLogo.jpg:














//--> Create texture content from a single image source file.
//
TextureContent m_ZgdkLogoText = _Device.Content.TextureMngr.Create("Contents/Textures/ZGDKLogo.jpg");




// From here the created texture content can be assigned to 2D-Sprite entity
// or to 3D-Model entity as texture material.


//--> CREATE 2D-SPRITE ENTITY
LayerEntity m_Layer01 = _Device.Entity2D.LayerMngr.Create("TopLayer"); SpriteEntity m_LogoSpr = _Device.Entity2D.SpriteMngr.Create( m_ZgdkLogoText, m_Layer01 ); // m_LogoSpr.Position( 0,0 ); //--> CREATE 3D-MODEL ENTITY ModelEntity m_BoxModel = _Device.Entity3D.ModelMngr.Create( MeshPrimitveType.Cube,false,false,false); // m_BoxModel.Scale( new Vector3(10,10,10) ); m_BoxModel.Position( new Vector3(0,0,0) ); // //--> Box model materials setting. // m_BoxModel.ModelGroups(0).Materials(0).textureContent0 = m_ZgdkLogoText; m_BoxModel.ModelGroups(0).Materials(0).usePerPixelLighting = true;
The output using single instance of TextContent for 2D-Sprite and 3D-Model:
Next post : B. Creating a single animated  TextureContent  from multiple image source files.

Tuesday, May 9, 2017

Added some asteriods and dust clouds on my game prototype








Managing game content can also be CRUD oriented!



PART 1

what is CRUD?

It was first reference in the article entitle “from semantic to object-oriented data modeling” by Haim Kilov in Conference: Systems Integration, 1990.  But I’m pretty sure CRUD operation was used earlier than 1990. In application development CRUD is an acronym of the four most common operation in database development where the application have the functionality to Create, Read, Update and Delete a record.

CRUD can be used not only in database application, it can also be used in game development where you can Create/Load,  Read/GetData,  Update/Modify and Delete/Remove a content or  game entity at any time anywhere in the code.

Since the time of XNA I’m already not a fond of its content pipeline’s loading and unloading of game assets, not the technology involving in it, the content pipeline processors are good, so don’t get me wrong XNA is a nice piece of game framework after Managed DirectX or MDX. But the framework’s design on how to load and unload of game content are somehow limited or restricted, unless you do some workaround. For a much more flexibility and manageability I believed it should have the ability to Create/Load, Update/Modify, Read and Delete/Remove selected or all content at any giving time, wherever you wish in the code.

MonoGame on the other adopted the loading and unloading scheme found in XNA since  I’m using MonoGame framework on my Game Development Kit I decided to create my own content manager and not using MonoGame Content Pipeline, I’m not saying it’s a bad move or one’s should not use it. But I want more control over the content I will be loading and unloading within the whole life of the game.
What I have implement in Z-GDK is the ability to create, read and remove selected or all content at any given time anywhere in the code as shown in content managers below like texture and mesh content manager :


Content Managers :


Let's take a look at Texture Content Manager :


Let's take a look at Mesh Content Manager :



In Z-GDK Content or Entity managers have a simple properties and methods layout, but have different kinds of overloads; as for example the Remove methods has an overloads to remove item by index,  by auto assigned UID(Unique ID ), by Name or by user defined Scene ID as I will be tackle on the later parts. CRUD operations was also applied in game entity managers as shown below the model entity manager:





Z-GDK sample code on how easy and simple it is to load game content and to create game entity without using content pipeline :




See you on PART 2 : CRUD : Loading and Content Creation


Tuesday, April 25, 2017

Prototype game using Z-GDK(MG) Windows/Android test

Playing with Z-GDK(MG) and at the current state  I can already achieved this kind of game, running both on Windows and Android ^_ ^ y



Saturday, April 22, 2017

Z-GDK(MG) Architecture


Game Application Model Overview

I have always been a fond of a good framework which are easy to use and easy to understand by just taking a glance of it's framework overview, where you can already take a good grasp of how the game engine logic flow goes, hopefully I can also bring that to Z-GDK(MG). now to kick things off, here's an early overview wherein I have layout the game application model overview using the Z-GDK(MG).


Creating a new game project

The simplest way to create a new game application using Z-GDK(MG) is to open up the sample empty projects which contains a code templates that derives from Z-GDK(MG) GameFlow, it has an  application main entry point and game application class templates that is ready to run and test a game logic routine,

Now let's take a look at the main program file and see what it look's like:


The Program.cs source file used by Empty Project Template:

==============================================================


//!________________________________________________________________________
//!
//! PROGRAM FILE ID
//!________________________________________________________________________
//!
//! About    : Z-GDK(MG) 2017
//! File     : Program.cs
//! Remarks  : Empty Z-GDK(MG) project template
//!
//!________________________________________________________________________





//!________________________________________________________________________

//!

//! USING DIRECTIVES
//!________________________________________________________________________


#region USNG DIRECTIVES
//!!!
//!!
//!

using System;
//
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//
using ZGDK;
using ZD  = ZGDK.Device;
using Z3D = ZGDK.D3D;
//!
//!!
//!!!
#endregion USNG DIRECTIVES




//!________________________________________________________________________

//!

//! NAMESPACE CONTEXT

//!________________________________________________________________________
namespace ZGDKGAME
{
   
    /// <summary>
    /// Main program class.
    /// </summary>
    public static class Program
    {

          [STAThread]
        /// <summary>
        /// Program application main entry point.
        /// </summary>       
        static void Main()
        {

            #region DEVICE SETTING
            //!!!
            //!!
            //!

            var m_DeviceSetting = new ZD.DeviceSetting( ZD.DevicePlatform.WindowsDeskTop ); 
            //
            m_DeviceSetting.preferredScreenHeight = 768;
            m_DeviceSetting.preferredScreenWidth  = 1024;
            //

            m_DeviceSetting.preferredMaxFPS       = 64;

            //
            m_DeviceSetting.isFullScreen          = false;
            m_DeviceSetting.isFixedTimeStep       = false;
            m_DeviceSetting.isVerticalSynch       = false;
            //

            m_DeviceSetting.clearScreenColor      = Color.CornflowerBlue;
            m_DeviceSetting.cameraType            = Z3D.Camera.CameraType.FreeSpace;
           
            // Let's leave other device settings to it's default value for now.

           
//!
            //!!
            //!!!
            #endregion DEVICE SETTING


            #region GAME OBJECT CREATION
            //!!!
            //!!
            //!
           
            using ( var m_GameDevice = new ZD.GameDevice( new MyGame(), m_DeviceSetting ) )
            {
                m_GameDevice.Run(); // Let's run the game!
            }


            //!

            //!!
            //!!!  
            #endregion GAME OBJECT CREATION


        }//End Main


    }//End Program

   
}//End Namespace


/* --End of file-- */

==============================================================


The main program.cs file simply creates an application program and defined the preferred device settings that will be needed when creating  the game device before running it as showed from the above codes.

Device setting:

Before creating the game device, it needs to know all the preferred setting of the game like height and width of the screen the preferred maximum frames per second and the like, some setting will be tackle as I continue posting the development of Z-GDK(MG) other features.





The application logic flow

The application logic flow is very simple after the main program entry point is created and the game device begins running.  the game application will starts  OnInitialize and then OnBeginRun followed by the Updates and the Draw calls  methods updates and draw calls methods will be called by the device automagically, until the application EndRun  is called to terminate the application.

Application methods:

BEGINRUN
   OnInitialize()
   OnBeginRun()
UPDATE

   OnUpdate()
   OnPostUpdate()
RENDER 3D

   OnPreDraw3D()
   OnDraw3D()
RENDER 2D
   OnPeDraw2D()
   OnDraw2D()
ENDRUN

    OnEndRun()

From here let's take  look at Application Game Source file and see what it looks like:


The Game.cs source file used by Empty Project Template:

==========================================================


//!________________________________________________________________________
//!
//! PROGRAM FILE ID
//!________________________________________________________________________
//!
//! About    : Z-GDK(MG) 2017
//! File     : Game.cs
//! Remarks  : Empty Z-GDK(MG) project template
//!
//!________________________________________________________________________




//!________________________________________________________________________

//!

//! USING DIRECTIVES
//!________________________________________________________________________



#region USNG DIRECTIVES
//!!!
//!!
//!

using System;
//
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//
using ZGDK;

using ZD  = ZGDK.Device;
using ZC  = ZGDK.Device.Content;
using Z2D = ZGDK.D2D;using Z3D = ZGDK.D3D;
//!
//!!
//!!!
#endregion USNG DIRECTIVES


//!________________________________________________________________________

//!

//! NAMESPACE CONTEXT

//!________________________________________________________________________

namespace ZGDKGAME
{


    /// <summary>
    ///
    /// </summary>
    public class MyGame : ZD.GameFlow
    {


        #region F I E L D S
        //!!
        //!
            
        ZD.GameDevice       _Device  = null; // Handle to game device
        ZC.FontContent      _Font    = null// Handle to font content
        Z2D.Text.TextEntity _TextFps = null; // Handle to Text entity
        //!
        //!!
        #endregion F I E L D S



        #region  B E G I N  R U N 
        //!!
        //!

        /// <summary>
        /// Occurs right after the game device has been initialized.
        /// </summary>     
        public void OnInitialize(  ZD.GameDevice gameDevice )
        {
               _Device = gameDevice;

        }       

        /// <summary>
        /// Occurs right after the game device begin running.
        /// </summary>
        public void OnBeginRun()
        {
                 // Load some font
          _Font = _Device.Content.FontContentMngr.Create"("TrueTypeFont/ArialTruTypeFont" );
          // Create text entity to display our frame per second on screen
          _TextFps = _Device.D2D.TextEntityMngr.Create(10,0,0,_Font, Color.White);
       }

        //!
        //!!
        #endregion B E G I N  R U N 



        #region U P D A T E
        //!!
        //!

        /// <summary>
        /// Game logic update routine.
        /// </summary>  
        public void OnUpdate( XF.GameTime GT, float elapseTimeMs )
        {
         
 }

        /// <summary>
        /// Occurs right after entities has been updated.
        /// </summary>       
        public void OnPostUpdate( XF.GameTime GT, float elapseTimeMs )
        {

 }

        //!
        //!!
        #endregion U P D A T E




        #region R E N D E R  3 D
        //!!
        //!

        /// <summary>
        /// Occurs just before rendering all 3D entities begin.
        /// </summary>
        public void OnPreDraw3D( float elapseTimeMs )
        {

        }

        /// <summary>
        /// Occurs right after all 3D entities has been rendered.
        /// </summary>
        public void OnDraw3D( float elapseTimeMs )
        {

 }


        //!
        //!!
        #endregion R E N D E R  3 D



        #region R E N D E R  2 D
        //!!
        //!

        /// <summary>
        /// Occurs just before rendering all 2D entities begin.
        /// </summary>      
        public void OnPreDraw2D( float elapseTimeMs )
        {

                    // Save device captured frames per second to text. 
            _TextFpsUps.textContent = "FPS : " + this._Device.FPS ";
        }

        /// <summary>
        /// Occurs right after all 2D entities has been rendered.
        /// </summary>     
        public void OnDraw2D( float elapseTimeMs )
        {

        }



        //!

        //!!
        #endregion R E N D E R  2 D




        #region  E N D  R U N 

        //!!

        //!


        /// <summary>
        /// On game end running
        /// </summary>
        public void OnEndRun()
        {

        }


        //!

        //!!
        #endregion E N D  R U N 



    }//EndClass



 }//EndNameSpace



/* --End of file-- */
===========================================================




Initialization and Begin Running the Game

When the game device has been created from the main entry point and run it, the OnInitialize method immediate occurs and pass the created GameDevice that needs to  be save it's handle from within the game application followed by OnBeginRun where some content that will be use by the game entire lifetime can be loaded from here. Content loading and Unloading can be done anywhere anytime per content on my implementation, since I don't used the MonoGame default content manager, I implemented my own  content manager which is more flexible and manageable. which I will tackle on my next post. The OnBeginRun methods is also a good place to initialize your game entities members.


The Game Update Loop

The application OnUpdate method is responsible for handling the update of game logic, normally this is called by the device more frequently than the Draw methods base on the specified maximum update frames per second on the device setting, the OnPostUpdate method occurs right after the device updated all the entities.


Drawing on Screen

The application draw methods is responsible for informing the game application that a screen refresh has been occurred or repainted. The OnPreDraw3D method occurs just before all 3D entities begins rendering, a the good place to put codes related to rendering not affected by update prior to rendering, the OnDraw3D  on the other hand occurs right after all 3D entities has been drawn followed by  OnPreDraw2D like OnPreDraw3D this occurs just before all 2D entities begins rendering on screen, the OnDraw2D occurs right after all 2D entities has been drawn this is a good place to get how many frames per second has been drawn. The *DEVICE.FPS returns the actual frames per second of the game performed and *DEVICE.UPS displays the number of updates per second. 

The rendering device depends on many factors which includes the total quantity of graphic content needed to be processed on each frame and of course the capabilities of the hardware, but still, the maximum target frames per second  can be specified on the device setting.

m_DeviceSetting.preferredMaxFPS  = 64;

Ending or Quitting the Game

I’ll  be a good citizen by cleaning up mess I put into someone’s computer,  the  OnEndRun occurs when the application shuts down or terminated all device-defined tasks associated with freeing, releasing and resetting framework's related resources. But on some other cases, like for example; if the user closes the application without ending the game or exiting the application normally; it’s a good practice to make sure that the game and device will also shutdown gracefully when this case occurs, so this is a good place to put your clean up codes to destroy all of your content from OnEndRun methods.


Final output





Well, I'm running out of English and grammar so there it is : - D