<commit_before>// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "engine.hpp"

#include "lib/imgui.h"
#include "lib/imgui_impl_sdl_gl3.h"

#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/matrix_interpolation.hpp>
#include <cmath>

#include "entity/box.hpp"
#include "entity/earth.hpp"
#include "entity/duck.hpp"
#include "entity/plane.hpp"
#include "entity/triangle.hpp"

Engine::~Engine() {
	IMG_Quit();

	ImGui_ImplSdlGL3_Shutdown();

	SDL_GL_DeleteContext(_context);
	SDL_DestroyWindow(_window);
	SDL_Quit();
}

int Engine::run() {
	_init();
	_quit = false;
	int fps = 0;
	uint32_t lastTime = SDL_GetTicks();

	bool showSettings = false;

	_resolutionChanged();
	_updateMovement(0, false);
	bool updateCamera = false;
	while (!_quit) {
		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			ImGui_ImplSdlGL3_ProcessEvent(&event);
			switch (event.type) {
			case SDL_QUIT:
				_quit = true;
				break;
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
					_quit = true;
					break;
				case SDLK_LSHIFT:
				case SDLK_RSHIFT:
					_speed = 10.0f;
					break;
				default:
					break;
				}

				break;

			case SDL_KEYUP:
				switch (event.key.keysym.sym) {
				case SDLK_LSHIFT:
				case SDLK_RSHIFT:
					_speed = 5.0f;
					break;
				default:
					break;
				}

			case SDL_MOUSEBUTTONDOWN:
				if (event.button.button == SDL_BUTTON_RIGHT) {
					updateCamera = true;
					SDL_ShowCursor(0);
					SDL_WarpMouseInWindow(_window, _width / 2, _height / 2);
				}
				break;

			case SDL_MOUSEBUTTONUP:
				if (event.button.button == SDL_BUTTON_RIGHT) {
					updateCamera = false;
					SDL_ShowCursor(1);
				}
				break;
			case SDL_WINDOWEVENT:
				if (event.window.event == SDL_WINDOWEVENT_RESIZED || event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
					_width = event.window.data1;
					_height = event.window.data2;
					_resolutionChanged();
				}
				break;
			default:
				break;
			}
		}
		ImGui_ImplSdlGL3_NewFrame(_window);

		{
			ImGui::SetNextWindowPos(ImVec2(8, 8), ImGuiSetCond_Always);
			ImGui::SetNextWindowSize(ImVec2(100, 100), ImGuiSetCond_FirstUseEver);
			ImGui::Begin("Info panel", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize);
			ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
			if (!showSettings && ImGui::Button("Show settings"))
				showSettings = !showSettings;
			ImGui::End();
		}

		if (showSettings) {
			ImGui::SetNextWindowPos(ImVec2(8, 48), ImGuiSetCond_Once);
			ImGui::SetNextWindowSize(ImVec2(40, 80), ImGuiSetCond_FirstUseEver);
			ImGui::Begin("Settings Window", &showSettings);
			if (ImGui::CollapsingHeader("OpenGL")) {
				if (ImGui::Checkbox("Backface culling", &_setting_ogl_doBackFaceCulling)) {
					if (_setting_ogl_doBackFaceCulling)
						glEnable(GL_CULL_FACE);
					else
						glDisable(GL_CULL_FACE);
				}
				ImGui::Checkbox("Render lights", &_setting_ogl_renderLights);
			}

			if (ImGui::CollapsingHeader("Base Program")) {
				_baseProgram->bind();
				if (ImGui::Checkbox("Geometry Backface Culling", &_setting_base_doBackFaceCulling))
					_baseProgram->setUniform("setting_doBackFaceCulling", _setting_base_doBackFaceCulling);

				ImGui::Separator();

				if (ImGui::DragFloat("Default Specular", &_setting_base_defaultSpecular, 0.01, 0, 1))
					_baseProgram->setUniform("setting_defaultSpecular", _setting_base_defaultSpecular);
			}

			if (ImGui::CollapsingHeader("Deferred Program")) {
				_deferredProgram->bind();
				if (ImGui::Checkbox("Ambient", &_setting_deferred_enableAmbient))
					_deferredProgram->setUniform("setting_enableAmbient", _setting_deferred_enableAmbient);
				if (ImGui::Checkbox("Shadow", &_setting_deferred_enableShadow))
					_deferredProgram->setUniform("setting_enableShadow", _setting_deferred_enableShadow);
				if (ImGui::Checkbox("Diffuse", &_setting_deferred_enableDiffuse))
					_deferredProgram->setUniform("setting_enableDiffuse", _setting_deferred_enableDiffuse);
				if (ImGui::Checkbox("Specular", &_setting_deferred_enableSpecular))
					_deferredProgram->setUniform("setting_enableSpecular", _setting_deferred_enableSpecular);

				ImGui::Separator();

				if (ImGui::DragFloat("Shininess", &_setting_deferred_shininess, 1, 0, 256))
					_deferredProgram->setUniform("setting_shininess", _setting_deferred_shininess);
			}

			ImGui::Separator();

			if (ImGui::CollapsingHeader("Lights")) {
				ImGui::Columns(2);
				for (int i = 0; i < LIGHT_COUNT; i++) {
					const ImVec4 color = ImColor(_lights[i].color.x, _lights[i].color.y, _lights[i].color.z, 1.0);

					ImGui::Text((std::string("Light #") + std::to_string(i)).c_str());
					ImGui::NextColumn();

					ImGui::PushStyleColor(ImGuiCol_Button, color);
					if (ImGui::ColorButton(color))
						ImGui::OpenPopup((std::string("color") + std::to_string(i)).c_str());
					ImGui::PopStyleColor();

					if (ImGui::BeginPopup((std::string("color") + std::to_string(i)).c_str())) {
						ImGui::Text("Edit lights color");
						if (ImGui::ColorEdit3("##edit", (float*)&_lights[i].color))
							_lightsBuffer->setDataRaw(&_lights[0], sizeof(Light) * LIGHT_COUNT);
<commit_msg>Adding more gui settings<commit_after>// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "engine.hpp"

#include "lib/imgui.h"
#include "lib/imgui_impl_sdl_gl3.h"

#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/matrix_interpolation.hpp>
#include <cmath>

#include "entity/box.hpp"
#include "entity/earth.hpp"
#include "entity/duck.hpp"
#include "entity/plane.hpp"
#include "entity/triangle.hpp"

Engine::~Engine() {
	IMG_Quit();

	ImGui_ImplSdlGL3_Shutdown();

	SDL_GL_DeleteContext(_context);
	SDL_DestroyWindow(_window);
	SDL_Quit();
}

int Engine::run() {
	_init();
	_quit = false;
	int fps = 0;
	uint32_t lastTime = SDL_GetTicks();

	_resolutionChanged();
	_updateMovement(0, false);
	bool updateCamera = false;
	while (!_quit) {
		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			ImGui_ImplSdlGL3_ProcessEvent(&event);
			switch (event.type) {
			case SDL_QUIT:
				_quit = true;
				break;
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
					_quit = true;
					break;
				case SDLK_LSHIFT:
				case SDLK_RSHIFT:
					_speed = 10.0f;
					break;
				default:
					break;
				}

				break;

			case SDL_KEYUP:
				switch (event.key.keysym.sym) {
				case SDLK_LSHIFT:
				case SDLK_RSHIFT:
					_speed = 5.0f;
					break;
				default:
					break;
				}

			case SDL_MOUSEBUTTONDOWN:
				if (event.button.button == SDL_BUTTON_RIGHT) {
					updateCamera = true;
					SDL_ShowCursor(0);
					SDL_WarpMouseInWindow(_window, _width / 2, _height / 2);
				}
				break;

			case SDL_MOUSEBUTTONUP:
				if (event.button.button == SDL_BUTTON_RIGHT) {
					updateCamera = false;
					SDL_ShowCursor(1);
				}
				break;
			case SDL_WINDOWEVENT:
				if (event.window.event == SDL_WINDOWEVENT_RESIZED || event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
					_width = event.window.data1;
					_height = event.window.data2;
					_resolutionChanged();
				}
				break;
			default:
				break;
			}
		}
		ImGui_ImplSdlGL3_NewFrame(_window);

		{
			ImGui::SetNextWindowPos(ImVec2(8, 8), ImGuiSetCond_Always);
			ImGui::SetNextWindowSize(ImVec2(384, 32), ImGuiSetCond_Once);
			ImGui::Begin("Info panel", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize);
			ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
			ImGui::End();
		}

		{
			ImGui::SetNextWindowPos(ImVec2(8, 48), ImGuiSetCond_Once);
			ImGui::SetNextWindowSize(ImVec2(384, 512), ImGuiSetCond_Once);
			ImGui::SetNextWindowCollapsed(true, ImGuiSetCond_Once);
			ImGui::Begin("Settings Window");
			if (ImGui::CollapsingHeader("OpenGL")) {
				if (ImGui::Checkbox("Backface culling", &_setting_ogl_doBackFaceCulling)) {
					if (_setting_ogl_doBackFaceCulling)
						glEnable(GL_CULL_FACE);
					else
						glDisable(GL_CULL_FACE);
				}
				ImGui::Checkbox("Render lights", &_setting_ogl_renderLights);
			}

			if (ImGui::CollapsingHeader("Base Program")) {
				_baseProgram->bind();
				if (ImGui::Checkbox("Geometry Backface Culling", &_setting_base_doBackFaceCulling))
					_baseProgram->setUniform("setting_doBackFaceCulling", _setting_base_doBackFaceCulling);

				ImGui::Separator();

				if (ImGui::DragFloat("Default Specular", &_setting_base_defaultSpecular, 0.005, 0, 1))
					_baseProgram->setUniform("setting_defaultSpecular", _setting_base_defaultSpecular);
			}

			if (ImGui::CollapsingHeader("Deferred Program")) {
				_deferredProgram->bind();
				if (ImGui::Checkbox("Ambient", &_setting_deferred_enableAmbient))
					_deferredProgram->setUniform("setting_enableAmbient", _setting_deferred_enableAmbient);
				if (ImGui::Checkbox("Shadow", &_setting_deferred_enableShadow))
					_deferredProgram->setUniform("setting_enableShadow", _setting_deferred_enableShadow);
				if (ImGui::Checkbox("Diffuse", &_setting_deferred_enableDiffuse))
					_deferredProgram->setUniform("setting_enableDiffuse", _setting_deferred_enableDiffuse);
				if (ImGui::Checkbox("Specular", &_setting_deferred_enableSpecular))
					_deferredProgram->setUniform("setting_enableSpecular", _setting_deferred_enableSpecular);

				ImGui::Separator();

				if (ImGui::DragFloat("Shininess", &_setting_deferred_shininess, 0.1, 0.1, 256))
					_deferredProgram->setUniform("setting_shininess", _setting_deferred_shininess);
			}

			ImGui::Separator();

			if (ImGui::CollapsingHeader("Lights")) {
				ImGui::Columns(2);
				for (int i = 0; i < LIGHT_COUNT; i++) {
					const ImVec4 color = ImColor(_lights[i].color.x, _lights[i].color.y, _lights[i].color.z, 1.0);

					ImGui::Text((std::string("Light #") + std::to_string(i)).c_str());
					ImGui::NextColumn();

					ImGui::PushStyleColor(ImGuiCol_Button, color);
					if (ImGui::ColorButton(color))
						ImGui::OpenPopup((std::string("color") + std::to_string(i)).c_str());
					ImGui::PopStyleColor();

					if (ImGui::BeginPopup((std::string("color") + std::to_string(i)).c_str())) {
						ImGui::Text((std::string("Editing Light #") + std::to_string(i)).c_str());
						if (ImGui::ColorEdit3("##color", glm::value_ptr(_lights[i].color))) {
							_lightsBuffer->setDataRaw(&_lights[0], sizeof(Light) * LIGHT_COUNT);
						}

						if (ImGui::DragFloat3("##pos", glm::value_ptr(_lights[i].pos), 0.1)) {
							_lightsBuffer->setDataRaw(&_lights[0], sizeof(Light) * LIGHT_COUNT);
							for (int i = 0; i < LIGHT_COUNT; i++)
								_lightsMatrix[i] = glm::scale(glm::translate(_lights[i].pos), glm::vec3(0.5f));
						}

{'fraction_non_alphanumeric': 0.14396567463940113, 'fraction_numerical': 0.012780719371918934, 'mean_word_length': 4.569395017793594, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4842893', 'n_tokens_mistral': 4793, 'n_tokens_neox': 3898, 'n_words': 617}