Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Hi Ray

I have a question about texture loading and unloading correctness:

#include "raylib.h"

int main(void) {

    const int screenWidth = 400;

    const int screenHeight = 300;

    SetTargetFPS(60);

    InitWindow(screenWidth, screenHeight, "DrawTexture Example");

    int fontsize = 20;

    char *getfont = "fonts/JuliaMono-Light.ttf";

    Font font = LoadFontEx(getfont, fontsize, 0, 250);

    char *ptr = "Texture";

    int x = 50, y = 30;

    Image image = GenImageColor(screenWidth, screenHeight, WHITE);

    ImageDrawTextEx(&image, font, ptr, (Vector2){ x, y }, fontsize, 0, BLUE);

    Texture2D texture = LoadTextureFromImage(image);

    while (!WindowShouldClose()) {

        if (IsKeyDown(KEY_ENTER)) {

            ImageDrawRectangle(&image, x, y, 300, 30, WHITE);   // erase or text will become ugly

            ImageDrawTextEx(&image, font, ptr, (Vector2){ x, y }, fontsize, 0, RED);

            UnloadTexture(texture);                             // 1 saving memory or is it wrong?

            texture = LoadTextureFromImage(image);

        }

        else if (IsKeyDown(KEY_SPACE)) {

            ImageDrawRectangle(&image, x, y, 300, 30, WHITE);   // erase or text will become ugly

            ImageDrawTextEx(&image, font, ptr, (Vector2){ x, y }, fontsize, 0, PURPLE);

            UnloadTexture(texture);                             // 2 saving memory or is it wrong?

            texture = LoadTextureFromImage(image);

        }

        BeginDrawing();

        ClearBackground(WHITE);

        DrawTexture(texture, 0, 0, WHITE);

        EndDrawing();

    }

    UnloadImage(image);

    UnloadTexture(texture);

    CloseWindow();

    return 0;

}

/*

The console reports an incremental higher TEXTURE: [ID num] each time I use Enter or space. Is it only an intern Raylib count or?

Thanks in advance.

*/