/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "game/renderer/gl/gl_context.hpp"

#include <config.hpp>

#include "game/window/window.hpp"
#if defined(WINDOW_WIN32)
#include "game/renderer/gl/win32_gl_context.hpp"
#endif
#if defined(WINDOW_X11)
#include "game/renderer/gl/x11_gl_context.hpp"
#endif
#include "game/renderer/gl/opengl.hpp"
#include "common/logger.hpp"

using namespace polygun::renderer;

#if defined(RENDERER_GL)
bool GLContext::is_extension_supported(const std::string& name) {
	return m_supported_extensions.find(name)!=m_supported_extensions.end();
}

void GLContext::query_extensions() {
	PFNGLGETSTRINGIPROC glGetStringi = (PFNGLGETSTRINGIPROC)get_proc_address("glGetStringi");
	if(!glGetStringi)
		return;
	int extension_count;
	glGetIntegerv(GL_NUM_EXTENSIONS, &extension_count);
	for(int i = 0; i<extension_count; i++)
		m_supported_extensions.insert(reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i)));
}
#endif

GLContext* GLContext::create_context(window::Window* window) {
	switch(window->get_window_type()) {
	case window::WindowType::WINDOW_TYPE_WIN32:
#if defined(WINDOW_WIN32)
		return new Win32GLContext(window);
#else
		LOG_FATAL("Failed to create GLContext for Win32 window: not compiled in");
		break;
#endif
	case window::WindowType::WINDOW_TYPE_X11:
#if defined(WINDOW_X11)
		return new X11GLContext(window);
#else
		LOG_FATAL("Failed to create GLContext for X11 window: not compiled in");
		break;
#endif
	case window::WindowType::WINDOW_TYPE_WAYLAND:
		return nullptr;
	case window::WindowType::WINDOW_TYPE_SERENITYOS:
		return nullptr;
	}
	return nullptr;
}
