Hi since I saw a few people on the facebook AppGameKit page asking about this I thought I just as well share my example here for others to use also. I am sure there are probably better and more efficient ways to achieve the same result and I am definitely not saying I am an expert by any stretch of the imagination.
Here is the code there will also be a zip/RAR file containing the working project. Its basically a simple state machine that demonstrates how to change states in your game/apps. Also it could easily be converted to Tier 2.
[Setup.agc]
// Force variables to be declared before using them
#Option_Explicit
[Constants.agc]
// Error Constants
#CONSTANT ERROR_IGNORE = 0 // MODE = 0 - IGNORE
#CONSTANT ERROR_REPORT = 1 // MODE = 1 - REPORT
#CONSTANT ERROR_STOP = 2 // MODE = 2 - STOP
// Window/Screen Constants
#CONSTANT SCREEN_WIDTH = 1334 // Screen Width
#CONSTANT SCREEN_HEIGHT = 750 // Screen Height
#CONSTANT APP_TITLE = "State Machine Example"
// FPS (Frame Rate)
#CONSTANT FPS_UNLIMITED = 0 // 0 for unlimited.
#CONSTANT FPS_30 = 30
#CONSTANT FPS_60 = 60
// FPS Mode (0 (default) to save CPU and battery, 1 to use a possibly more accurate but CPU intensive method)
#CONSTANT DEFAULT_MODE = 0
#CONSTANT CPU_MODE = 1
// FALSE = 0, TRUE = 1
#CONSTANT FALSE = 0
#CONSTANT TRUE = 1
// OFF = 0, ON = 1
#CONSTANT OFF = 0
#CONSTANT ON = 1
// 0 - NOT_VISIBLE, 1 - VISIBLE
#CONSTANT NOT_VISIBLE = 0
#CONSTANT VISIBLE = 1
// App Constants
#CONSTANT CURRENT_TIME_INDEX = 1 // Index Id for current time
// Text & Alignment Constants
#CONSTANT ALIGN_LEFT = 0 // Set the mode for the text alignment (0 = left)
#CONSTANT ALIGN_CENTER = 1 // Set the mode for the text alignment (1 = center)
#CONSTANT ALIGN_RIGHT = 2 // Set the mode for the text alignment (2 = right)
#CONSTANT CURRENT_TIME_TEXT_SIZE = 28 // Constant variable sets the size of the current time and date text
//----------------------------- GLOBALS ------------------------------//
// Assign the type to an Identifier and apply the data types
// Global Identifiers ( Defaults to Integer )
//-------------------------- GAME STATES -----------------------------//
#CONSTANT gGAME_LOAD = 0 // The game is preparing to run
#CONSTANT gGAME_MENU = 1 // The game is in the main menu mode
#CONSTANT gLEVEL_SELECT = 2 // The game is on the level selection screen
#CONSTANT gGAME_RUNNING = 3 // The game is running
#CONSTANT gGAME_PAUSED = 4 // The game is paused
#CONSTANT gGAME_RESUMED = 5 // The game is resumed
#CONSTANT gGAME_RESTART = 6 // The game is restarted
#CONSTANT gGAME_END = 7 // The game has been exited/quit
global g_gamestate // Create a new variable to store and set our current state
global g_previousstate // Create a new variable to store the previous game state
g_gamestate = gGAME_LOAD // Set ( our current ) the starting state
//-------------------------- MENU ITEMS ------------------------------//
#CONSTANT MENU_BACKGROUND_IMAGE = "menu/MenuBackground.png"
global gMenuBackground // Create a new variable to store the menu background image
//--------------------- LEVEL SELECT ITEMS ---------------------------//
#CONSTANT LEVEL_SELECT_BACKGROUND_IMAGE = "level_select/LevelSelect.png"
global gLevelSelectBackground // Create a new variable to store the menu background image
[Window Setup.agc]
Function WindowSetup()
// Show all errors
SetErrorMode(ERROR_STOP)
// Set window properties
SetWindowTitle( APP_TITLE )
SetWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT, FALSE )
// Set display properties
SetVirtualResolution( SCREEN_WIDTH, SCREEN_HEIGHT )
SetScissor( 0, 0, 0, 0 )
SetOrientationAllowed( FALSE, FALSE, TRUE, TRUE ) // ( Portrait, Portrait 2, Landscape, Landscape 2 )
SetDisplayAspect(-1)
SetSyncRate( FPS_30, 0 ) // 30fps instead of 60 to save battery
UseNewDefaultFonts( 1 ) // since version 2.0.20 we can use nicer default fonts
EndFunction
[Main.agc]
///--------------------------------- APP DETAILS & INFO -----------------------------------------------------///
//------------------------------------------------------------------------------------------------------------//
// Project: State Machine Example
// Created: 2017-19-07
// Company: GUNNERStudios
// Programmer: David Gunner Jnr
// Email: GunnerJnr@live.co.uk
// Website: http://gunnerjnr.uk
//
//------------------------------------------ Includes ------------------------------------------------//
//----------------------------------------------------------------------------------------------------//
#include "setup.agc"
#include "constants.agc"
#include "window_setup.agc"
//------------------------------------------ APP BEGIN CODE ------------------------------------------------///
//-----------------------------------------------------------------------------------------------------------//
//--------------------------------- Window Creation and Properties ------------------------------------//
//-----------------------------------------------------------------------------------------------------//
WindowSetup() // call our window setup function
///--------------------------------- Main Program Loop Begin -------------------------//
//------------------------------------------------------------------------------------//
Do
// Here we set conditions for all our different game states
Select (g_gamestate)
// The game is preparing to run
Case gGAME_LOAD
// Call on our funtion to load all the game assets, etc..
GameLoad()
// Set game state to menu after loading assets
g_gamestate = gGAME_MENU
/** NOTE: We could miss out this step and just go straight to the game running phase,
** However, we can use it to set things up before the game actually starts, we can do
** some house keeping and things..
**/
EndCase
// The game is in menu mode
Case gGAME_MENU
// Here we call our main menu function, and let it handle switching states
//TODO
MainMenu()
if GetPointerPressed() = 1
//
HideMenu()
//
g_gamestate = gLEVEL_SELECT
endif
/** NOTE: We could force GAME_RUN here if we wanted to **/
EndCase
// The game is on the level selection screen
Case gLEVEL_SELECT
// Here we navigate to the level selection screen
// Call our function to display the level select screen and images
LevelSelectScreen()
if GetPointerPressed() = 1
//
HideLevelSelectScreen()
// Go To gGAME_RUNNING state
g_gamestate = gGAME_RUNNING
endif
EndCase
// The game is running
Case gGAME_RUNNING
// This section will contain the entire game logic loop
//TODO
// Now we want to handle any game logic/AI
//HandleGame()
/** NOTE: The only we the state can be changed from here is through user interaction
** in the input selection (clicking buttons, etc..), or by losing the game of course.
**/
EndCase
// The game is paused
Case gGAME_PAUSED
//TODO
EndCase
// The game is resumed
Case gGAME_RESUMED
//TODO
EndCase
// The game is restarted
Case gGAME_RESTART
// This is essentially a cleanup state, we use it to tie up any loose ends before restrting the game.
//TODO
//RestartGame()
EndCase
// The game has been exited/quit
Case gGAME_END
// If the game is in this state then we want to shut everything down
ReleaseObjects()
/** NOTE: We don't need to change or switch any states here because we are already in this state,
** on the next loop iteration our code will fall out of the main while and return control back to the OS
**/
EndCase
EndSelect
//----------------------- DEBUGGING USE ONLY ------------------------------//
// Prints the debugging info to the screen
PrintDebugInfo()
//----------------------- UPDATE GAME -------------------------------------//
// Refresh the screen (Backbuffer)
Sync()
Loop
///------------------------------ Main Program loop end ------------------------------///
//-------------------------------------------------------------------------------------//
//------------------------------------------ FUNCTION DEFINITIONS ------------------------------------------//
//----------------------------------------------------------------------------------------------------------//
//------------------------------ MENU -------------------------------------//
// Create Menu Screen
Function CreateMenu()
// Creates the image object for the menu background image
gMenuBackground = CreateSprite( LoadImage( MENU_BACKGROUND_IMAGE ) )
// Sets the visibility of the menu background image
SetSpriteVisible( gMenuBackground, NOT_VISIBLE )
EndFunction
// Displays the menu on the screen
Function DisplayMenu()
// Sets the visibility of the menu sprite to be shown
SetSpriteVisible( gMenuBackground, VISIBLE )
EndFunction
// Hide Menu
Function HideMenu()
// Set the visibility of the menu sprite to hidden
SetSpriteVisible( gMenuBackground, NOT_VISIBLE)
EndFunction
//-------------------------- LEVEL SELECT --------------------------------//
// Create Level Select Screen
Function CreateLevelSelectScreen()
// Creates the image object for the level selection screen
gLevelSelectBackground = CreateSprite( LoadImage( LEVEL_SELECT_BACKGROUND_IMAGE ) )
// Sets the visibility of the menu background image
SetSpriteVisible( gLevelSelectBackground, NOT_VISIBLE )
EndFunction
// Displays the level selection screen
Function DisplayLevelSelectScreen()
// Sets the level select image to visible
SetSpriteVisible( gLevelSelectBackground, VISIBLE )
EndFunction
// Hide Menu
Function HideLevelSelectScreen()
// Set the visibility of the menu sprite to hidden
SetSpriteVisible( gLevelSelectBackground, NOT_VISIBLE)
EndFunction
//------------------------------ CLEANUP ------------------------------//
// The ReleaseObjects Method will be used to shut everything down
Function ReleaseObjects()
// Handle the releasing and deleting of all objects
DeleteAllSprites()
DeleteAllImages()
DeleteAllObjects()
DeleteAllText()
EndFunction
//----------------------------- DEBUGGING -----------------------------//
// Prints the dubug info to the device screen
Function PrintDebugInfo()
// Prints the current platform the game is being run on
Print ( "Device Platform = " + GetDeviceName() )
// Prints the width of the device in px
Print ( "Device Width = " + str( GetDeviceWidth() ) )
// Prints the height of the device in px
Print ( "Device Height = " + str( GetDeviceHeight() ) )
// Checks which orientation the device is currently in then prints it to the device screen
Select GetOrientation()
// Portrait
Case 1:
Print ( "Portrait Mode" )
EndCase
// Portrait 180
Case 2:
Print ( "Portrait Mode - 180 Degrees Rotation" )
EndCase
// Landscape 90 degrees counter clockwise
Case 3:
Print ( "Landscape Mode - 90 Degrees Counter Clockwise" )
EndCase
// Landscape 90 degrees clockwise
Case 4:
Print ( "Landscape Mode - 90 Degrees Clockwise" )
EndCase
// End Select
EndSelect
// Check which game state we are currently in and print it to the screen
RemStart
Note we could actually write the gamestates here instead of using a number while checking the states...
E.G - if (g_gamestate = gGAME_MENU)
RemEnd
if ( g_gamestate = 0 )
Print ("Current Game State: = " + "Game Loading" )
elseif ( g_gamestate = 1 )
Print ("Current Game State: = " + "Game Menu" )
elseif ( g_gamestate = 2 )
Print ("Current Game State: = " + "Game Level Select" )
elseif ( g_gamestate = 3 )
Print ("Current Game State: = " + "Game Running" )
elseif ( g_gamestate = 4 )
Print ("Current Game State: = " + "Game Paused" )
elseif ( g_gamestate = 5 )
Print ("Current Game State: = " + "Game Resumed" )
elseif ( g_gamestate = 6 )
Print ("Current Game State: = " + "Restarting Game" )
endif
// Set the size of the print font
SetPrintSize( 16 )
// Prints the cureen FPS (frame rate) to the screen (ours should be capped to 30fps to save battery)
Print ( "Frame rate = " + str( screenFPS() ) )
// Prints the number of sprites drawn to the screen
Print ( "Sprites drawn = " + str( GetManagedSpriteDrawnCount() ) )
// Prints the number of particales drawn to the screen
Print ( "Particles drawn = " + str( GetParticleDrawnQuadCount() ) )
// Prints the Drawing set up time
Print ( "Drawing set up time = " + str( GetDrawingSetupTime() ) )
// Prints the drawing time
Print ( "Drawing time = " + str( GetDrawingTime() ) )
EndFunction
//----------------------------------- GAME STATE FUNCTIONS -------------------------------//
//----------------------------------------------------------------------------------------//
//-------------------- GAME LOAD --------------------//
// Pre loads any assets while the game is loading
Function GameLoad()
// Create The Menu Objects
CreateMenu()
// Create the level select objects
CreateLevelSelectScreen()
EndFunction
//-------------------- GAME MENU --------------------//
// Calls the functions which will display the menu to the screen
Function MainMenu()
// Displays the Menu to the screen
DisplayMenu()
EndFunction
//-------------------- LEVEL SELECT -----------------//
// Calls the functions which will display and handle the level selection screen
Function LevelSelectScreen()
// Displays the level select screen images & objects
DisplayLevelSelectScreen()
EndFunction
//-------------------- GAME RUNNING -----------------//
// TODO
//-------------------- GAME PAUSED ------------------//
// TODO
//-------------------- GAME RESUMED -----------------//
// TODO
//-------------------- GAME RESTART -----------------//
// TODO
//-------------------- GAME END ---------------------//
// TODO
NB - Obviously this can be changed to suit anybody's needs also the functions could be moved into separate classes/files to save filling main up with too much code. But either way hopefully it will help some people.
Cheers and Enjoy!
GunnerJnr