<commit_before>#include "os/win32/win32.h"
#include <windowsx.h>
#include <assert.h>

#include "GL/glew.h"
#include "GL/wglew.h"

Sigma::event::KeyboardInputSystem IOpSys::KeyboardEventSystem; // Handles keyboard events
Sigma::event::MouseInputSystem IOpSys::MouseEventSystem; // Handles keyboard events
double IOpSys::curDelta;
int win32::keyUp[256];
int win32::mousePos[2];

win32::~win32() {
	wglMakeCurrent(this->hdc, 0); // Remove the rendering context from our device context
	wglDeleteContext(this->hrc); // Delete our rendering context

	ReleaseDC(this->hwnd, this->hdc); // Release the device context from our window
}

void win32::ToggleFullscreen() {
	if (this->fullscreen) {
		SetWindowLongPtr(this->hwnd, GWL_STYLE, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE);
		MoveWindow(this->hwnd, this->windowedSize.left, this->windowedSize.top, this->windowedSize.right - this->windowedSize.left, this->windowedSize.bottom - this->windowedSize.top, TRUE);
	}
	else {
		// Save the current windowed position
		GetWindowRect(this->hwnd, &this->windowedSize);

		SetWindowLongPtr(this->hwnd, GWL_STYLE, WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE);

		// Get the monitor info about the monitor the window is most over.
		HMONITOR hmon = MonitorFromWindow(this->hwnd, MONITOR_DEFAULTTONEAREST);
		MONITORINFO monInfo;
		monInfo.cbSize = sizeof(MONITORINFO);
		GetMonitorInfo(hmon, &monInfo);

		// Move the window and resize it to take up the whole monitor
		MoveWindow(this->hwnd, monInfo.rcMonitor.left, monInfo.rcMonitor.top, monInfo.rcMonitor.right - monInfo.rcMonitor.left, monInfo.rcMonitor.bottom - monInfo.rcMonitor.top, TRUE);
	}
	this->fullscreen = !this->fullscreen;
}

void* win32::CreateGraphicsWindow(const unsigned int width, const unsigned int height) {
	WNDCLASS windowClass;
	//HWND hwnd;
	DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

	hInstance = GetModuleHandle(NULL);

	windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	windowClass.lpfnWndProc = (WNDPROC) WndProc;
	windowClass.cbClsExtra = 0;
	windowClass.cbWndExtra = 0;
	windowClass.hInstance = hInstance;
	windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.hbrBackground = NULL;
	windowClass.lpszMenuName = NULL;
	windowClass.lpszClassName = "GL Test Window";

	this->windowedSize.left = 50;
	this->windowedSize.top = 50;
	this->windowedSize.right = width+50;
	this->windowedSize.bottom = height+50;

	AdjustWindowRectEx(&this->windowedSize, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, false, dwExStyle);

	if (!RegisterClass(&windowClass)) {
		return false;
	}
	this->hwnd = CreateWindowEx(dwExStyle, windowClass.lpszClassName, windowClass.lpszClassName, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, this->windowedSize.left, this->windowedSize.top, this->windowedSize.right - this->windowedSize.left, this->windowedSize.bottom - this->windowedSize.top, NULL, NULL, hInstance, NULL);

	assert(this->hwnd != 0);	

	// Now that we have created the window to the size we want, we must get the correct client size and store that.
	GetClientRect(this->hwnd, &this->windowedSize);

	ShowWindow(this->hwnd, SW_SHOW);
	UpdateWindow(this->hwnd);


	POINT midwindow = {512,384};
	ClientToScreen(this->hwnd, &midwindow);
	SetCursorPos(midwindow.x, midwindow.y);

	StartOpengGL();

	return this->hdc;
}

bool win32::mouselook = false;

LRESULT CALLBACK win32::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	bool mouseMoved = false;
	POINT midwindow = {512,384};
	switch (message) {
	case WM_KEYUP:
		KeyboardEventSystem.KeyUp(wParam);
		keyUp[wParam] = 1;
		return 0;
		break;
	case WM_KEYDOWN:
		KeyboardEventSystem.KeyDown(wParam);
		return 0;
		break;
	case WM_CHAR: 
		KeyboardEventSystem.CharDown(wParam);
		return 0;
		break;
	case WM_MOUSEMOVE:
		if (mouselook) {
			mouseMoved = true;
			if ((abs(GET_X_LPARAM(lParam) - midwindow.x) > 2) && (abs(GET_Y_LPARAM(lParam) - midwindow.y) > 2)) {
				MouseEventSystem.MouseMove((GET_X_LPARAM(lParam) - midwindow.x) / 1024.0f * 500.0f, (GET_Y_LPARAM(lParam) - midwindow.y) / 768.0f * 500.0f);
			}
			else if (abs(GET_X_LPARAM(lParam) - midwindow.x) > 2) {
				MouseEventSystem.MouseMove((GET_X_LPARAM(lParam) - midwindow.x) / 1024.0f * 500.0f, 0.0f);
			}
			else if (abs(GET_Y_LPARAM(lParam) - midwindow.y) > 2) {
				MouseEventSystem.MouseMove(0.0f, (GET_Y_LPARAM(lParam) - midwindow.y) / 768.0f * 500.0f);
			}
			else {
				mouseMoved = false;
			}
			ClientToScreen(hWnd, &midwindow);
			SetCursorPos(midwindow.x, midwindow.y);
		}
		break;
	case WM_LBUTTONDOWN:
		MouseEventSystem.MouseDown(Sigma::event::BUTTON::LEFT, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
		break;
	case WM_LBUTTONUP:
		MouseEventSystem.MouseUp(Sigma::event::BUTTON::LEFT, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
		break;
	case WM_RBUTTONDOWN:
		ShowCursor(false);
		mouselook = true;
		SetCursorPos(midwindow.x, midwindow.y);
		break;
	case WM_RBUTTONUP:
		ShowCursor(true);
		mouselook = false;
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	if (!mouseMoved) {
		MouseEventSystem.MouseMove(0.f, 0.f);
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

bool win32::MessageLoop() {
	MSG msg;
	
	ZeroMemory(keyUp, sizeof(keyUp));
	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // If we have a message to process, process it
		if (msg.message == WM_QUIT) {
			return false;
		}
		else {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	Sleep(1);
	return true;
}

bool win32::SetupTimer() {
	LARGE_INTEGER li;
	if(!QueryPerformanceFrequency(&li)) {
		return false;
	}

	this->frequency = static_cast<double>(li.QuadPart)/1000.0;

	QueryPerformanceCounter(&li);<commit_msg>Removed hard coding from win32 WndPRoc<commit_after>#include "os/win32/win32.h"
#include <windowsx.h>
#include <assert.h>

#include "GL/glew.h"
#include "GL/wglew.h"

Sigma::event::KeyboardInputSystem IOpSys::KeyboardEventSystem; // Handles keyboard events
Sigma::event::MouseInputSystem IOpSys::MouseEventSystem; // Handles keyboard events
double IOpSys::curDelta;
int win32::keyUp[256];
int win32::mousePos[2];

win32::~win32() {
	wglMakeCurrent(this->hdc, 0); // Remove the rendering context from our device context
	wglDeleteContext(this->hrc); // Delete our rendering context

	ReleaseDC(this->hwnd, this->hdc); // Release the device context from our window
}

void win32::ToggleFullscreen() {
	if (this->fullscreen) {
		SetWindowLongPtr(this->hwnd, GWL_STYLE, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE);
		MoveWindow(this->hwnd, this->windowedSize.left, this->windowedSize.top, this->windowedSize.right - this->windowedSize.left, this->windowedSize.bottom - this->windowedSize.top, TRUE);
	}
	else {
		// Save the current windowed position
		GetWindowRect(this->hwnd, &this->windowedSize);

		SetWindowLongPtr(this->hwnd, GWL_STYLE, WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE);

		// Get the monitor info about the monitor the window is most over.
		HMONITOR hmon = MonitorFromWindow(this->hwnd, MONITOR_DEFAULTTONEAREST);
		MONITORINFO monInfo;
		monInfo.cbSize = sizeof(MONITORINFO);
		GetMonitorInfo(hmon, &monInfo);

		// Move the window and resize it to take up the whole monitor
		MoveWindow(this->hwnd, monInfo.rcMonitor.left, monInfo.rcMonitor.top, monInfo.rcMonitor.right - monInfo.rcMonitor.left, monInfo.rcMonitor.bottom - monInfo.rcMonitor.top, TRUE);
	}
	this->fullscreen = !this->fullscreen;
}

void* win32::CreateGraphicsWindow(const unsigned int width, const unsigned int height) {
	WNDCLASS windowClass;
	//HWND hwnd;
	DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

	hInstance = GetModuleHandle(NULL);

	windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	windowClass.lpfnWndProc = (WNDPROC) WndProc;
	windowClass.cbClsExtra = 0;
	windowClass.cbWndExtra = 0;
	windowClass.hInstance = hInstance;
	windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.hbrBackground = NULL;
	windowClass.lpszMenuName = NULL;
	windowClass.lpszClassName = "GL Test Window";

	this->windowedSize.left = 50;
	this->windowedSize.top = 50;
	this->windowedSize.right = width+50;
	this->windowedSize.bottom = height+50;

	AdjustWindowRectEx(&this->windowedSize, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, false, dwExStyle);

	if (!RegisterClass(&windowClass)) {
		return false;
	}
	this->hwnd = CreateWindowEx(dwExStyle, windowClass.lpszClassName, windowClass.lpszClassName, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, this->windowedSize.left, this->windowedSize.top, this->windowedSize.right - this->windowedSize.left, this->windowedSize.bottom - this->windowedSize.top, NULL, NULL, hInstance, NULL);

	assert(this->hwnd != 0);	

	// Now that we have created the window to the size we want, we must get the correct client size and store that.
	GetClientRect(this->hwnd, &this->windowedSize);

	ShowWindow(this->hwnd, SW_SHOW);
	UpdateWindow(this->hwnd);

	StartOpengGL();

	return this->hdc;
}

bool win32::mouselook = false;

LRESULT CALLBACK win32::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	bool mouseMoved = false;

	RECT windowSize;
	GetClientRect(hwnd, &windowSize);
	unsigned int windowWidth = windowSize.right - windowSize.left;
	unsigned int windowHeight = windowSize.bottom - windowSize.top;

	POINT midwindow = {windowWidth / 2, windowHeight / 2};

	switch (message) {
	case WM_KEYUP:
		KeyboardEventSystem.KeyUp(wParam);
		keyUp[wParam] = 1;
		return 0;
		break;
	case WM_KEYDOWN:
		KeyboardEventSystem.KeyDown(wParam);
		return 0;
		break;
	case WM_CHAR: 
		KeyboardEventSystem.CharDown(wParam);
		return 0;
		break;
	case WM_MOUSEMOVE:
		if (mouselook) {
			mouseMoved = true;
			if ((abs(GET_X_LPARAM(lParam) - midwindow.x) > 2) && (abs(GET_Y_LPARAM(lParam) - midwindow.y) > 2)) {
				MouseEventSystem.MouseMove((GET_X_LPARAM(lParam) - midwindow.x) / static_cast<float>(windowHeight) * 500.0f, (GET_Y_LPARAM(lParam) - midwindow.y) / static_cast<float>(windowWidth) * 500.0f);
			}
			else if (abs(GET_X_LPARAM(lParam) - midwindow.x) > 2) {
				MouseEventSystem.MouseMove((GET_X_LPARAM(lParam) - midwindow.x) / static_cast<float>(windowHeight) * 500.0f, 0.0f);
			}
			else if (abs(GET_Y_LPARAM(lParam) - midwindow.y) > 2) {
				MouseEventSystem.MouseMove(0.0f, (GET_Y_LPARAM(lParam) - midwindow.y) / static_cast<float>(windowWidth) * 500.0f);
			}
			else {
				mouseMoved = false;
			}
			ClientToScreen(hwnd, &midwindow);
			SetCursorPos(midwindow.x, midwindow.y);
		}
		break;
	case WM_LBUTTONDOWN:
		MouseEventSystem.MouseDown(Sigma::event::BUTTON::LEFT, static_cast<float>(GET_X_LPARAM(lParam)) / static_cast<float>(windowWidth), static_cast<float>(GET_Y_LPARAM(lParam)) / static_cast<float>(windowHeight));
		break;
	case WM_LBUTTONUP:
		MouseEventSystem.MouseUp(Sigma::event::BUTTON::LEFT,static_cast<float>( GET_X_LPARAM(lParam)) / static_cast<float>(windowWidth), static_cast<float>(GET_Y_LPARAM(lParam)) / static_cast<float>(windowHeight));
		break;
	case WM_RBUTTONDOWN:
		ShowCursor(false);
		mouselook = true;
		ClientToScreen(hwnd, &midwindow);
		SetCursorPos(midwindow.x, midwindow.y);
		break;
	case WM_RBUTTONUP:
		ShowCursor(true);
		mouselook = false;
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	if (!mouseMoved) {
		MouseEventSystem.MouseMove(0.f, 0.f);
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}

bool win32::MessageLoop() {
	MSG msg;
	
	ZeroMemory(keyUp, sizeof(keyUp));
	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // If we have a message to process, process it
		if (msg.message == WM_QUIT) {
			return false;
		}
		else {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	Sleep(1);
	return true;
}

bool win32::SetupTimer() {
	LARGE_INTEGER li;
	if(!QueryPerformanceFrequency(&li)) {
		return false;
	}

	this->frequency = static_cast<double>(li.QuadPart)/1000.0;

	QueryPerformanceCounter(&li);
{'fraction_non_alphanumeric': 0.12968801858612677, 'fraction_numerical': 0.015930965814802522, 'mean_word_length': 5.1182741116751265, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 108, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5769898', 'n_tokens_mistral': 4833, 'n_tokens_neox': 4471, 'n_words': 864}