/*
	GameEngine and Editor
	by John Ryland
	Copyright (c) 2023
*/

////////////////////////////////////////////////////////////////////////////////////
//	Image

#include "Image.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

namespace GameEngine {

Image::Image()
    : data(nullptr)
    , ownData(false)
{
}

Image::~Image()
{
    if (ownData)
    {
        stbi_image_free(data);
    }
}

void Image::Load(const char *filename)
{
    data = stbi_load(filename, &width, &height, &components, 0);
    ownData = true;
    if (!data)
    {
        printf("Failed to load image: %s", filename);
    }
}

void Image::Assign(int a_width, int a_height, uint8_t* a_data, bool transferOwnership)
{
    data = a_data;
    ownData = transferOwnership;
    width = a_width;
    height = a_height;
}

} // GameEngine namespace
