/******************************************************************************
*	File		-	editor_helpers.cpp
*	Author		-	Joey Pollack
*	Date		-	2022/03/02 (y/m/d)
*	Mod Date	-	2022/03/02 (y/m/d)
*	Description	-	Helper methods for the editor
******************************************************************************/

#include "editor_helpers.h"

#include <core/core.h>
#include <graphics/graphics.h>
#include <assets/types/image.h>
#include <utils/stb/stb_image.h>

namespace lunarium { namespace editor
{
    lunarium::Image* FileLoaders::LoadImage(std::filesystem::path file)
    {
        int w, h, n;
        stbi_set_flip_vertically_on_load(1);
        unsigned char* buffer = stbi_load(file.string().c_str(), &w, &h, &n, 0);

        if (!buffer)
        {
            return nullptr;
        }

        Image* i = new Image();
        i->SetData(buffer);
        i->SetFormat(ImageFormat::RGBA);

        if (n == 3)
        {
            i->SetFormat(ImageFormat::RGB);
        }

        i->SetWidth(w);
        i->SetHeight(h);

        Core::Graphics().RegisterImage(*i);
        return i;                   
    }    
}}