Textures And Images

Images are different than textures. Images are processed by the CPU/RAM and ideal for altering the individual pixels, for example resizing, cropping, etc. Images cannot be rendered and must be converted to textures to be rendered. Also Images can be loaded before the window is initialized, while textures must be loaded after the window is initialized. Textures are handled by the GPU/VRAM.

Loading Images

Images can be loaded at any point in the lifecycle of the application.

<?php
$image = new \raylib\Image('path-to-image');

Unloading Images

In RayLib you can unload images to free up RAM, to do this PHP, you just unset the object.

<?php
$image = new \raylib\Image('path-to-image.png');
$image->colorInvert();
$image->export('save-path.png');
unset($image);

Converting Images to Textures

Loading textures can become a burden on your system so sometimes its better to load them as images, and then when needed convert to textures. You can do this via $texture = $image->toTexture();. Please note that while images can be loaded before the window is initialized, they cannot be converted to textures until the window is initialized.

<?php
$image = new \raylib\Image('path-to-image.png');
$image->colorInvert();
$image->export('save-path.png');
\raylib\Window::init(300, 300, "Loading Image To Texture");
$texture = $image->toTexture();
unset($image);

Loading Textures

You don't have to load an image in order to load a texture you can load files directly to textures.

<?php
$image = new \raylib\Texture('path-to-image');

Unloading Textures

In RayLib you can unload images to free up RAM, to do this PHP, you just unset the object.

<?php
$image = new \raylib\Image('path-to-image.png');
$image->colorInvert();
$image->export('save-path.png');
unset($image);

Drawing Textures

Only textures can be draw, images cannot. Using the loaded texture, there are a few draw methods, but you can use the simplest $texture->draw($x, $y, $tint).

<?php

use raylib\Color;
use raylib\Text;
use raylib\Draw;
use raylib\Image;
use raylib\Window;

// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
$raywhite = new Color(245, 245, 245, 255);
$white = new Color(255, 255, 255, 255);
$gray     = new Color(130, 130, 130, 255);

$image = new Image(__DIR__  . '/resources/raylib_logo.png');    // Loaded in CPU memory (RAM)

Window::init($screenWidth, $screenHeight, "raylib [textures] example - image loading");

// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)

$texture = $image->toTexture();                                 // Image converted to texture, GPU memory (VRAM)

unset($image);    // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM

$texture_x = $screenWidth / 2 - $texture->width / 2;
$texture_y = $screenHeight / 2 - $texture->height / 2;
//---------------------------------------------------------------------------------------

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


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

    Draw::clearBackground($raywhite);

    $texture->draw($texture_x, $texture_y, $white);

    Text::draw("this IS a texture loaded from an image!", 300, 370, 10, $gray);


    Draw::end();
    //----------------------------------------------------------------------------------

}

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