Difference between revisions of "GSDP:GAM100/CProcessing/SetNextGameStateForced()"

From Inside
Jump to: navigation, search
>Zachary.logsdon
(New page: {{GSDP:GAM100API.css}} = SetNextGameStateForced() = == Description == This sets the next state for the program to enter. This will call the shutdown function of the current state if own e...)
 
>Zachary.logsdon
Line 14: Line 14:
 
== Example Usage ==
 
== Example Usage ==
 
<syntaxhighlight lang='c'>
 
<syntaxhighlight lang='c'>
void init(void)
+
// Horizontal position of the square
 +
float x_pos;
 +
void init()
 
{
 
{
   /* Set the size of the window */
+
   // Start the square at the left of the screen
   size(500, 500);
+
  x_pos = 0;
 +
 
 +
  // Set the square to draw yellow
 +
   fill(color(255, 255, 0, 255));
 
}
 
}
  
void update(void)
+
void update()
 
{
 
{
   /* Set the background color to black every frame */
+
   // Set background to black
 
   background(color(0, 0, 0, 255));
 
   background(color(0, 0, 0, 255));
  
   /* Draw a rectangle at the mouse position */
+
   // Draw the square
   rect(mouseX, mouseY, 50, 50);
+
   rect(x_pos, (float)canvasHeight / 2.0f, 100, 100);
 +
  x_pos += 2;
 +
 
 +
  // If space pressed, reset the state
 +
  if (keyPressed(KEY_SPACE))
 +
    SetNextGameStateForced(init, update, NULL);
 
}
 
}
  

Revision as of 12:11, 13 October 2019

SetNextGameStateForced()

Description

This sets the next state for the program to enter. This will call the shutdown function of the current state if own exists. Calling SetNextGameStateForced() allows you to reset your current game state, while SetNextGameState() with the current game state will not.

Parameters

SetNextGameState(FunctionPtr init, FunctionPtr update, FunctionPtr shutdown)

  • init - (FunctionPtr) The name of the function called once when the state first begins. This can be NULL if no initialization is needed for your state.
  • update - (FunctionPtr) The name of the function called every frame to update the state.
  • shutdown - (FunctionPtr) The name of the function called when you leave a state. This occurs when you call SetNextGameState() or SetNextGameStateForced() to change states. This can be NULL if no cleanup is needed for your state.

Example Usage

// Horizontal position of the square
float x_pos;
void init()
{
  // Start the square at the left of the screen
  x_pos = 0;

  // Set the square to draw yellow
  fill(color(255, 255, 0, 255));
}

void update()
{
  // Set background to black
  background(color(0, 0, 0, 255));

  // Draw the square
  rect(x_pos, (float)canvasHeight / 2.0f, 100, 100);
  x_pos += 2;

  // If space pressed, reset the state 
  if (keyPressed(KEY_SPACE))
    SetNextGameStateForced(init, update, NULL);
}

int main(void)
{
  // Set the initial game state
  SetNextGameState(init, update, NULL);

  // Run the program
  Run();    
}

See Also

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>