Basic Game Loop

Once your extension is loaded you can now advance you adding your first game loop. Here is a quick example, opening the window, and drawing "Hello World" onto it.

// Exit application if the RayLib extension is missing.
if (!extension_loaded('raylib')) { echo 'no raylib'; exit; }

// Initialization
//--------------------------------------------------------------------------------------
$screenWidth  = 800;
$screenHeight = 450;
$lightGray    = new Color(245, 245, 245, 255);
$gray         = new Color(200, 200, 200, 255);

\raylib\Window::init($screenWidth, $screenHeight, "raylib [core] example - basic window");

\raylib\Timming::setTargetFps(60);
//--------------------------------------------------------------------------------------

// Main game loop
while (!\raylib\Window::shouldClose())    // Detect window close button or ESC key
{
    // Update
    //----------------------------------------------------------------------------------
    // TODO: Update your variables here
    //----------------------------------------------------------------------------------

    // Draw
    //----------------------------------------------------------------------------------
    \raylib\Draw::begin();

    \raylib\Draw::clearBackground($lightGray);

    \raylib\Text::draw("Hello World!", 190, 200, 20, $gray);

    \raylib\Draw::end();
    //----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
\raylib\Window::close();        // Close window and OpenGL context
//--------------------------------------------------------------------------------------

Important Note

  • Window::init does a lot under the hood, but more importantly opens a context for loading textures. So if you want to load textures. they MUST be loaded after Window::init.
    • Window::shouldClose() interacts with Timming::setTargetFps. This function will wait based on the target frames per second. This way your CPU isn't running at 100%. So if you have your frames per second set at 30, and your running your update logic in that same loop, then your update logic will also run at 30 FPS.
  • Your target FPS should be based on how fast you want updates to happen. Fast paced games should have a faster FPS this way things like user input are captured as soon as possible. There are ways to work around this, but for now keep this in mind.