<commit_before>#include "../Bacon.h"
#include "../BaconInternal.h"
#include "Platform.h"

#include <EGL/egl.h>
#include <EGL/eglext.h>

#include <unordered_map>
using namespace std;

HWND g_hWnd = NULL;
LPCTSTR WndClass = TEXT("BaconWnd");
int g_Width = 640, g_Height = 480;
bool g_Fullscreen = false;
static WINDOWPLACEMENT g_SavedWindowPlacement;

EGLDisplay g_Display;
EGLContext g_Context;
EGLSurface g_Surface;

static unordered_map<UINT, int> s_KeyMap;

static void InitKeyMap();
static void OnSize(int width, int height);
static void OnKey(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void OnMouseButton(HWND hWnd, UINT uMsg, WPARAM wParam, short x, short y);

static void GetWindowFrameSizeForContentSize(int& width, int& height, int windowStyle)
{
    RECT windowRect;
    windowRect.left = 0;
    windowRect.top = 0;
    windowRect.right = width;
    windowRect.bottom = height;
    AdjustWindowRect(&windowRect, windowStyle, FALSE);
    width = windowRect.right - windowRect.left;
    height = windowRect.bottom - windowRect.top;
}
    
int Bacon_SetWindowSize(int width, int height)
{
    g_Width = width;
    g_Height = height;
    if (g_hWnd)
    {
        GetWindowFrameSizeForContentSize(width, height, GetWindowLong(g_hWnd, GWL_STYLE));
        SetWindowPos(g_hWnd, NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
    }
    return Bacon_Error_None;
}



int Bacon_SetWindowFullscreen(int fullscreen)
{
    // http://blogs.msdn.com/b/oldnewthing/archive/2010/04/12/9994016.aspx

    if ((bool)fullscreen == g_Fullscreen)
        return Bacon_Error_None;

    g_Fullscreen = (bool)fullscreen;

    DWORD dwStyle = GetWindowLong(g_hWnd, GWL_STYLE);
    if (fullscreen) 
    {
        MONITORINFO mi = { sizeof(mi) };
        if (GetWindowPlacement(g_hWnd, &g_SavedWindowPlacement) &&
            GetMonitorInfo(MonitorFromWindow(g_hWnd,
            MONITOR_DEFAULTTOPRIMARY), &mi)) 
        {
                SetWindowLong(g_hWnd, GWL_STYLE,
                    dwStyle & ~WS_OVERLAPPEDWINDOW);
                SetWindowPos(g_hWnd, HWND_TOP,
                    mi.rcMonitor.left, mi.rcMonitor.top,
                    mi.rcMonitor.right - mi.rcMonitor.left,
                    mi.rcMonitor.bottom - mi.rcMonitor.top,
                    SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
        }
    } 
    else 
    {
        SetWindowLong(g_hWnd, GWL_STYLE,
            dwStyle | WS_OVERLAPPEDWINDOW);
        SetWindowPlacement(g_hWnd, &g_SavedWindowPlacement);
        SetWindowPos(g_hWnd, NULL, 0, 0, 0, 0,
            SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
            SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
    }
    return Bacon_Error_None;
}

LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{
    switch (uMsg) 
    { 
    case WM_CREATE:
        break;

    case WM_SIZE:
        OnSize(lParam & 0xffff, (lParam >> 16) & 0xffff);
        // Fallthrough

    case WM_PAINT:
        if (g_Context)
        {
            Graphics_BeginFrame(g_Width, g_Height);
            Bacon_InternalTick();
            Graphics_EndFrame();

            eglSwapBuffers(g_Display, g_Surface);
            ValidateRect(g_hWnd, NULL);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);             
        break; 

    case WM_KEYDOWN:
    case WM_KEYUP:
        OnKey(hWnd, uMsg, wParam, lParam);
        return 0;

    case WM_LBUTTONDOWN:
    case WM_MBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_LBUTTONUP:
    case WM_MBUTTONUP:
    case WM_RBUTTONUP:
        Mouse_SetMousePosition((float)LOWORD(lParam), (float)HIWORD(lParam));
        OnMouseButton(hWnd, uMsg, wParam, LOWORD(lParam), HIWORD(lParam));
        return 0;

    case WM_MOUSEMOVE:
        Mouse_SetMousePosition((float)LOWORD(lParam), (float)HIWORD(lParam));
        return 0;

    case WM_MOUSEWHEEL:
        Mouse_SetMousePosition((float)LOWORD(lParam), (float)HIWORD(lParam));
        Mouse_OnMouseScrolled(0.f, (float)HIWORD(wParam) / WHEEL_DELTA);
        return 0;
        
    default: 
        return DefWindowProc (hWnd, uMsg, wParam, lParam); 
    } 

    return 1; 
}

static int Platform_CreateWindow()
{
    WNDCLASS wndclass = { 0 }; 
    DWORD    wStyle   = 0;
    HINSTANCE hInstance = GetModuleHandle(NULL);

    wndclass.style         = CS_OWNDC;
    wndclass.lpfnWndProc   = (WNDPROC)WndProc; 
    wndclass.hInstance     = hInstance; 
    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 
    wndclass.lpszClassName = WndClass;
    if (!RegisterClass (&wndclass) ) 
        return FALSE; 

    wStyle = WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_SIZEBOX;

    // Adjust the window rectangle so that the client area has
    // the correct number of pixels
    int windowWidth = g_Width;
    int windowHeight = g_Height;
    GetWindowFrameSizeForContentSize(windowWidth, windowHeight, wStyle);

    g_hWnd = CreateWindow(
        WndClass,
        TEXT("Bacon"),
        wStyle,
        0,
        0,
        windowWidth,
        windowHeight,
        NULL,
        NULL,
        hInstance,
        NULL);

    if (!g_hWnd)
        return Bacon_Error_Unknown;
    
    return Bacon_Error_None;
}

static int Platform_CreateEGLContext()
{
    EGLint numConfigs;
    EGLint majorVersion;
    EGLint minorVersion;
    EGLConfig config;
    EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
    EGLint configAttribList[] =
    {
        EGL_RED_SIZE,       8,
        EGL_GREEN_SIZE,     8,
        EGL_BLUE_SIZE,      8,
        EGL_ALPHA_SIZE,     EGL_DONT_CARE,
        EGL_DEPTH_SIZE,     EGL_DONT_CARE,
        EGL_STENCIL_SIZE,   EGL_DONT_CARE,
        EGL_SAMPLE_BUFFERS, 0,<commit_msg>Windows startup fullscreen<commit_after>#include "../Bacon.h"
#include "../BaconInternal.h"
#include "Platform.h"

#include <EGL/egl.h>
#include <EGL/eglext.h>

#include <unordered_map>
using namespace std;

HWND g_hWnd = NULL;
LPCTSTR WndClass = TEXT("BaconWnd");
int g_Width = 640, g_Height = 480;
bool g_Fullscreen = false;
static WINDOWPLACEMENT g_SavedWindowPlacement;

EGLDisplay g_Display;
EGLContext g_Context;
EGLSurface g_Surface;

static unordered_map<UINT, int> s_KeyMap;

static void InitKeyMap();
static void OnSize(int width, int height);
static void OnKey(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void OnMouseButton(HWND hWnd, UINT uMsg, WPARAM wParam, short x, short y);

static void GetWindowFrameSizeForContentSize(int& width, int& height, int windowStyle)
{
    RECT windowRect;
    windowRect.left = 0;
    windowRect.top = 0;
    windowRect.right = width;
    windowRect.bottom = height;
    AdjustWindowRect(&windowRect, windowStyle, FALSE);
    width = windowRect.right - windowRect.left;
    height = windowRect.bottom - windowRect.top;
}
    
int Bacon_SetWindowSize(int width, int height)
{
    g_Width = width;
    g_Height = height;
    if (g_hWnd)
    {
        GetWindowFrameSizeForContentSize(width, height, GetWindowLong(g_hWnd, GWL_STYLE));
        SetWindowPos(g_hWnd, NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
    }
    return Bacon_Error_None;
}

static void SetWindowFullscreen(bool fullscreen)
{
    // http://blogs.msdn.com/b/oldnewthing/archive/2010/04/12/9994016.aspx
    DWORD dwStyle = GetWindowLong(g_hWnd, GWL_STYLE);
    if (fullscreen) 
    {
        MONITORINFO mi = { sizeof(mi) };
        if (GetWindowPlacement(g_hWnd, &g_SavedWindowPlacement) &&
            GetMonitorInfo(MonitorFromWindow(g_hWnd,
            MONITOR_DEFAULTTOPRIMARY), &mi)) 
        {
                SetWindowLong(g_hWnd, GWL_STYLE,
                    dwStyle & ~WS_OVERLAPPEDWINDOW);
                SetWindowPos(g_hWnd, HWND_TOP,
                    mi.rcMonitor.left, mi.rcMonitor.top,
                    mi.rcMonitor.right - mi.rcMonitor.left,
                    mi.rcMonitor.bottom - mi.rcMonitor.top,
                    SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
        }
    } 
    else 
    {
        SetWindowLong(g_hWnd, GWL_STYLE,
            dwStyle | WS_OVERLAPPEDWINDOW);
        SetWindowPlacement(g_hWnd, &g_SavedWindowPlacement);
        SetWindowPos(g_hWnd, NULL, 0, 0, 0, 0,
            SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
            SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
    }
}

int Bacon_SetWindowFullscreen(int fullscreen)
{
    if ((bool)fullscreen == g_Fullscreen)
        return Bacon_Error_None;

    g_Fullscreen = (bool)fullscreen;
    if (g_hWnd)
        SetWindowFullscreen((bool)fullscreen);
    return Bacon_Error_None;
}

LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{
    switch (uMsg) 
    { 
    case WM_CREATE:
        break;

    case WM_SIZE:
        OnSize(lParam & 0xffff, (lParam >> 16) & 0xffff);
        // Fallthrough

    case WM_PAINT:
        if (g_Context)
        {
            Graphics_BeginFrame(g_Width, g_Height);
            Bacon_InternalTick();
            Graphics_EndFrame();

            eglSwapBuffers(g_Display, g_Surface);
            ValidateRect(g_hWnd, NULL);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);             
        break; 

    case WM_KEYDOWN:
    case WM_KEYUP:
        OnKey(hWnd, uMsg, wParam, lParam);
        return 0;

    case WM_LBUTTONDOWN:
    case WM_MBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_LBUTTONUP:
    case WM_MBUTTONUP:
    case WM_RBUTTONUP:
        Mouse_SetMousePosition((float)LOWORD(lParam), (float)HIWORD(lParam));
        OnMouseButton(hWnd, uMsg, wParam, LOWORD(lParam), HIWORD(lParam));
        return 0;

    case WM_MOUSEMOVE:
        Mouse_SetMousePosition((float)LOWORD(lParam), (float)HIWORD(lParam));
        return 0;

    case WM_MOUSEWHEEL:
        Mouse_SetMousePosition((float)LOWORD(lParam), (float)HIWORD(lParam));
        Mouse_OnMouseScrolled(0.f, (float)HIWORD(wParam) / WHEEL_DELTA);
        return 0;
        
    default: 
        return DefWindowProc (hWnd, uMsg, wParam, lParam); 
    } 

    return 1; 
}

static int Platform_CreateWindow()
{
    WNDCLASS wndclass = { 0 }; 
    DWORD    wStyle   = 0;
    HINSTANCE hInstance = GetModuleHandle(NULL);

    wndclass.style         = CS_OWNDC;
    wndclass.lpfnWndProc   = (WNDPROC)WndProc; 
    wndclass.hInstance     = hInstance; 
    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 
    wndclass.lpszClassName = WndClass;
    if (!RegisterClass (&wndclass) ) 
        return FALSE; 

    wStyle = WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_SIZEBOX;

    // Adjust the window rectangle so that the client area has
    // the correct number of pixels
    int windowWidth = g_Width;
    int windowHeight = g_Height;
    GetWindowFrameSizeForContentSize(windowWidth, windowHeight, wStyle);

    g_hWnd = CreateWindow(
        WndClass,
        TEXT("Bacon"),
        wStyle,
        0,
        0,
        windowWidth,
        windowHeight,
        NULL,
        NULL,
        hInstance,
        NULL);

    if (!g_hWnd)
        return Bacon_Error_Unknown;
    
    if (g_Fullscreen)
        SetWindowFullscreen(true);

    return Bacon_Error_None;
}

static int Platform_CreateEGLContext()
{
    EGLint numConfigs;
    EGLint majorVersion;
    EGLint minorVersion;
    EGLConfig config;
    EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
    EGLint configAttribList[] =
    {
        EGL_RED_SIZE,       8,
        EGL_GREEN_SIZE,     8,
        EGL_BLUE_SIZE,      8,
        EGL_ALPHA_SIZE,     EGL_DONT_CARE,
        EGL_DEPTH_SIZE,     EGL_DONT_CARE,
        EGL_STENCIL_SIZE,   EGL_DONT_CARE,
        EGL_SAMPLE_BUFFERS, 0,
{'fraction_non_alphanumeric': 0.0915807560137457, 'fraction_numerical': 0.008419243986254295, 'mean_word_length': 2.6118523115110146, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '2544541', 'n_tokens_mistral': 4274, 'n_tokens_neox': 4080, 'n_words': 791}