#include "CLoadingWindow.hpp"

CLoadingWindow::CLoadingWindow(std::string title, std::string iconPath, std::string splashPath, unsigned int width, unsigned int height) :
	CWindow(title, iconPath, width, height, false, false, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALWAYS_ON_TOP),
	m_splashPath(splashPath),
	m_psurface(nullptr) {
}

CLoadingWindow::~CLoadingWindow(void) {
}

void CLoadingWindow::preinitialization(void) {
}

void CLoadingWindow::postinitialization(void) {
	m_psurface = SDL_GetWindowSurface(getWindow());

	// Set the icon.
	SDL_Surface* splash;
	splash = IMG_Load(m_splashPath.c_str());
	if (splash != nullptr) {
		SDL_Renderer* renderer = SDL_CreateRenderer(getWindow(), -1, SDL_RENDERER_ACCELERATED);
		if (renderer == NULL) {
			throw CLibException(std::string("Unable to create renderer: ") + SDL_GetError());
		}

		SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, splash);
		if (tex == NULL) {
			throw CLibException(std::string("Unable to create texture: ") + SDL_GetError());
		}

		SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);

		SDL_FreeSurface(splash);

		SDL_RenderClear(renderer);
		SDL_RenderCopy(renderer, tex, NULL, NULL);
		SDL_RenderPresent(renderer);
	}
}