I think my logic for this
StateMachineHandler() function is Flawed. Going back to an old thread where @PartTimeCoder had made a suggestion to me about using state modes along with game states, (the original thread can be found
here but is locked which is the only reason I am making a new thread). Anyway, it seems to be that the code leaves the function and then is recalled as part of the top level loop, on recalling, the state has been updated and it drops into that next state! I am wondering if it doesn't do anything about checking for new states (i.e. it's not interested on if that variable was updated within the switch, it just knows it found the one it wanted, and will now go past anything remaining).
Any advice, help, pointers, examples would be great.
Also I am assuming it doesn't matter I took it out of
main.agc and placed it into its own code file, I gather it doesn't need some additional calls to
Sync() ?? for clarity I have also included
main.agc code below (and before we point out there no includes for
game-state-handler.agc all includes are in a separate file called
pch.agc)
Kind Regards
GunnerJnr
****EDIT****
I have also attached a small gif of the debugging stepping through the code for a bit better clarity of what is happening inside the Select/Case checking..
PS - Thanks to @Baxslash for the Loading bar code, I have it commented out for now, I will most likely modify it to suit my needs, but for now, it is not needed. I see there is a great community sharing knowledge, @PTC also has some great links in his signature which I aim to have a look at in the near future.
GameStateHandler.agc
//----------------------------- 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
//-------------------------- GAME STATES MODES -----------------------------//
#CONSTANT gMODE_BEGIN 1
#CONSTANT gMODE_PROCESS 2
#CONSTANT gMODE_END 3
global g_statemode
//-------------------------- GAME STATES FUNCTIONS -----------------------------//
// This function handles the different states of the game/app
Function GameStateHandler()
// Here we set conditions for all our different game states
Select (g_gamestate)
// The game is preparing to run
Case gGAME_LOAD
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
// Set game state to menu after loading assets
g_gamestate = gGAME_MENU
EndCase
EndSelect
/** 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
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
// Call Function to set up time objects
CreateCurrentTime()
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
// Prints current frame rate to the screen
PrintDebugInfo()
// Displays and updates the current time on screen
DisplayCurrentTime()
if GetPointerPressed() = 1
//
g_statemode = gMODE_END
endif
EndCase
Case gMODE_END
// unload and cleanup the menu
// CleanUp()
//
g_gamestate = gLEVEL_SELECT
EndCase
EndSelect
/** NOTE: We could force GAME_RUN here if we wanted to **/
EndCase
// The game is on the level selection screen
Case gLEVEL_SELECT
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
if GetPointerPressed() = 1
// Go To gGAME_RUNNING state
g_gamestate = gGAME_RUNNING
endif
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
EndCase
EndSelect
EndCase
// The game is running
Case gGAME_RUNNING
// This section will contain the entire game logic loop
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
EndCase
EndSelect
/** 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
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
EndCase
EndSelect
EndCase
// The game is resumed
Case gGAME_RESUMED
//TODO
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
EndCase
EndSelect
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
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
EndCase
EndSelect
EndCase
// The game has been exited/quit
Case gGAME_END
// If the game is in this state then we want to shut everything down
//
g_statemode = gMODE_BEGIN
Select (g_statemode)
Case gMODE_BEGIN
// load and setup the menu
//
g_statemode = gMODE_PROCESS
EndCase
Case gMODE_PROCESS
// process menu events
//
g_statemode = gMODE_END
EndCase
Case gMODE_END
// unload and cleanup the menu
// CleanUp
CleanUp()
EndCase
EndSelect
/** 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
EndFunction
//----------------------------------- GAME STATE FUNCTIONS -------------------------------//
//----------------------------------------------------------------------------------------//
//-------------------- GAME LOAD --------------------//
// TODO
//-------------------- GAME MENU --------------------//
// TODO
//-------------------- LEVEL SELECT -----------------//
// TODO
//-------------------- GAME RUNNING -----------------//
// TODO
//-------------------- GAME PAUSED ------------------//
// TODO
//-------------------- GAME RESUMED -----------------//
// TODO
//-------------------- GAME RESTART -----------------//
// TODO
//-------------------- GAME END ---------------------//
// TODO
Main.agc
// Project: App Prototype
// Created: 07/01/2018
// Company: GUNNERStudios
// Programmer: David Gunner Jnr
// Email: GunnerJnr@live.co.uk
// Website: https://gunnerjnr.uk/
// Include Header Files
#include "pch.agc"
// Call on our setup function to set up the window, etc..
Setup()
Do
// Handle the game/app states
GameStateHandler()
// If the ESCAPE key is pressed, then exit the program
If GetRawKeyState( KEY_ESCAPE ) Then Exit
// Updates the screen (buffer)
Sync()
Loop
Here is the gif showing the debugging stepping through..