Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- typedef void (*TKW_WINDOW_RESIZE_CALLBACK)(int, int);
- typedef void (*TKW_WINDOW_KEY_CALLBACK)(int, int);
- typedef void (*TKW_WINDOW_MOUSE_CALLBACK)(int, int);
- typedef void (*TKW_WINDOW_ACTIVATE_CALLBACK)();
- typedef struct
- {
- int width;
- int height;
- HWND handle;
- TKW_WINDOW_RESIZE_CALLBACK on_window_resize_callback;
- TKW_WINDOW_KEY_CALLBACK on_window_key_callback;
- TKW_WINDOW_MOUSE_CALLBACK on_window_mouse_callback;
- TKW_WINDOW_ACTIVATE_CALLBACK on_window_activate_callback;
- } TKW_WINDOW;
- static TKW_WINDOW tkw_window = {0};
- static void tkw_load_gl_extensions();
- static void tkw_display_window();
- static void tkw_display_window()
- {
- HDC dc = GetDC(tkw_window.handle);
- SwapBuffers(dc);
- ReleaseDC(tkw_window.handle, dc);
- }
- static LRESULT CALLBACK tkw_window_proc(HWND window, UINT message, WPARAM w_param, LPARAM l_param)
- {
- LRESULT result = 0;
- switch(message)
- {
- case WM_SIZE:
- {
- RECT client_rect;
- GetClientRect(window, &client_rect);
- int width = client_rect.right - client_rect.left;
- int height = client_rect.bottom - client_rect.top;
- tkw_window.width = width;
- tkw_window.height = height;
- if(tkw_window.on_window_resize_callback)
- {
- tkw_window.on_window_resize_callback(width, height);
- }
- } break;
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- } break;
- case WM_CLOSE:
- {
- PostQuitMessage(0);
- } break;
- case WM_ACTIVATEAPP:
- {
- if(tkw_window.on_window_activate_callback)
- {
- tkw_window.on_window_activate_callback();
- }
- } break;
- case WM_KEYDOWN:
- {
- if(tkw_window.on_window_key_callback)
- {
- tkw_window.on_window_key_callback((int)w_param, 1);
- }
- } break;
- case WM_KEYUP:
- {
- if(tkw_window.on_window_key_callback)
- {
- tkw_window.on_window_key_callback((int)w_param, 0);
- }
- } break;
- case WM_LBUTTONDOWN:
- {
- if(tkw_window.on_window_mouse_callback)
- {
- tkw_window.on_window_mouse_callback(0, 1);
- }
- } break;
- case WM_LBUTTONUP:
- {
- if(tkw_window.on_window_mouse_callback)
- {
- tkw_window.on_window_mouse_callback(0, 0);
- }
- } break;
- case WM_RBUTTONDOWN:
- {
- if(tkw_window.on_window_mouse_callback)
- {
- tkw_window.on_window_mouse_callback(1, 1);
- }
- } break;
- case WM_RBUTTONUP:
- {
- if(tkw_window.on_window_mouse_callback)
- {
- tkw_window.on_window_mouse_callback(1, 0);
- }
- } break;
- // case WM_PAINT:
- // {
- // PAINTSTRUCT ps;
- // HDC dc = BeginPaint(window, &ps);
- // tkw_display_window(dc);
- // EndPaint(window, &ps);
- // } break;
- default:
- {
- result = DefWindowProc(window, message, w_param, l_param);
- } break;
- }
- return result;
- }
- static int tkw_create_window(
- int width,
- int height,
- TKW_WINDOW_RESIZE_CALLBACK on_window_resize_callback,
- TKW_WINDOW_KEY_CALLBACK on_window_key_callback,
- TKW_WINDOW_MOUSE_CALLBACK on_window_mouse_callback,
- TKW_WINDOW_ACTIVATE_CALLBACK on_window_activate_callback
- )
- {
- int result = 0;
- WNDCLASS window_class = {0};
- HINSTANCE hInstance = GetModuleHandle(0);
- window_class.style = CS_HREDRAW|CS_VREDRAW;
- window_class.lpfnWndProc = tkw_window_proc;
- window_class.hInstance = hInstance;
- window_class.lpszClassName = "some_window_class";
- RegisterClass(&window_class);
- HWND window = CreateWindowEx(
- 0,
- window_class.lpszClassName,
- "Learn",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- CW_USEDEFAULT, CW_USEDEFAULT, width, height,
- NULL,
- NULL,
- hInstance,
- NULL
- );
- if(window)
- {
- tkw_window.handle = window;
- tkw_window.on_window_resize_callback = on_window_resize_callback;
- tkw_window.on_window_key_callback = on_window_key_callback;
- tkw_window.on_window_mouse_callback = on_window_mouse_callback;
- tkw_window.on_window_activate_callback = on_window_activate_callback;
- }
- else
- {
- result = 0;
- }
- return result;
- }
- static int tkw_handle_messages()
- {
- MSG msg;
- while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- if(msg.message == WM_QUIT)
- {
- return 0;
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement