package main

import "core:fmt"
import "core:image/png"
import "core:os"
import sg "third-party:sokol/gfx"

Texture_Asset :: struct {
	img:  sg.Image,
	view: sg.View,
	w, h: i32,
}

load_texture :: proc(path: string) -> (Texture_Asset, bool) {
	// 1. Загружаем файл в память
	data, ok := os.read_entire_file(path, context.temp_allocator)
	if !ok {
		fmt.println("Failed to read file:", path)
		return {}, false
	}

	// 2. Декодируем PNG
	img, err := png.load_from_bytes(data, {.alpha_add_if_missing}, context.temp_allocator)
	if err != nil {
		fmt.println("Failed to decode PNG:", path, err)
		return {}, false
	}

	// 3. Создаем Sokol Image
	sg_img := sg.make_image(
		{
			width = i32(img.width),
			height = i32(img.height),
			pixel_format = .RGBA8,
			data = {
				mip_levels = {0 = {ptr = raw_data(img.pixels.buf), size = len(img.pixels.buf)}},
			},
		},
	)

	// 4. Создаем Sokol View (для шейдера)
	sg_view := sg.make_view({texture = {image = sg_img}})

	return Texture_Asset{img = sg_img, view = sg_view, w = i32(img.width), h = i32(img.height)},
		true
}
