/*******************************************************************************************
*
*   raylib [textures] example - Load textures from raw data
*
*   NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
*   This example has been created using raylib 1.3 (www.raylib.com)
*   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
*   Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

using static Raylib_cs.Raylib;

namespace Examples.Textures;

public class RawData
{
    public unsafe static int Main()
    {
        // Initialization
        //--------------------------------------------------------------------------------------
        const int screenWidth = 800;
        const int screenHeight = 450;

        InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data");

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

        // Load RAW image data (512x512, 32bit RGBA, no file header)
        Image fudesumiRaw = LoadImageRaw(
            "resources/fudesumi.raw",
            384,
            512,
            PixelFormat.UncompressedR8G8B8A8,
            0
        );
        Texture2D fudesumi = LoadTextureFromImage(fudesumiRaw);
        UnloadImage(fudesumiRaw);

        // Generate a checked texture by code
        int width = 960;
        int height = 480;

        // Store pixel data
        Color* pixels = (Color*)Raylib.MemAlloc((uint)(width * height * sizeof(Color)));
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                if (((x / 32 + y / 32) / 1) % 2 == 0)
                {
                    pixels[y * width + x] = Color.Orange;
                }
                else
                {
                    pixels[y * width + x] = Color.Gold;
                }
            }
        }

        // Load pixels data into an image structure and create texture
        Image checkedIm = new Image
        {
            Data = pixels,
            Width = width,
            Height = height,
            Format = PixelFormat.UncompressedR8G8B8A8,
            Mipmaps = 1,
        };
        Texture2D checkedTex = LoadTextureFromImage(checkedIm);
        Raylib.MemFree(pixels);
        //---------------------------------------------------------------------------------------

        // Main game loop
        while (!WindowShouldClose())
        {
            // Update
            //----------------------------------------------------------------------------------
            // TODO: Update your variables here
            //----------------------------------------------------------------------------------

            // Draw
            //----------------------------------------------------------------------------------
            BeginDrawing();
            ClearBackground(Color.RayWhite);

            int x = screenWidth / 2 - checkedTex.Width / 2;
            int y = screenHeight / 2 - checkedTex.Height / 2;
            DrawTexture(checkedTex, x, y, ColorAlpha(Color.White, 0.5f));
            DrawTexture(fudesumi, 430, -30, Color.White);

            DrawText("CHECKED TEXTURE ", 84, 85, 30, Color.Brown);
            DrawText("GENERATED by CODE", 72, 148, 30, Color.Brown);
            DrawText("and RAW IMAGE LOADING", 46, 210, 30, Color.Brown);

            DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, Color.Brown);

            EndDrawing();
            //----------------------------------------------------------------------------------
        }

        // De-Initialization
        //--------------------------------------------------------------------------------------
        UnloadTexture(fudesumi);
        UnloadTexture(checkedTex);

        CloseWindow();
        //--------------------------------------------------------------------------------------

        return 0;
    }
}
