Guest User

Untitled

a guest
Aug 19th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.94 KB | None | 0 0
  1.  
  2. const int paOsWindowEvent_GainFocus = 1;
  3. const int paOsWindowEvent_LoseFocus = 2;
  4. const int paOsWindowEvent_Close = 3;
  5. const int paOsWindowEvent_Minimize = 4;
  6. const int paOsWindowEvent_Maximize = 5;
  7. const int paOsWindowEvent_KeyDown = 6;
  8. const int paOsWindowEvent_KeyRepeat = 7;
  9. const int paOsWindowEvent_KeyUp = 8;
  10. const int paOsWindowEvent_Char = 9;
  11. const int paOsWindowEvent_MousePos = 10;
  12. const int paOsWindowEvent_MouseDown = 11;
  13. const int paOsWindowEvent_MouseUp = 12;
  14. const int paOsWindowEvent_MouseWheel = 13;
  15.  
  16. const int paOsWindowMode_Windowed = 0;
  17. const int paOsWindowMode_BorderlessWindowed = 1;
  18. const int paOsWindowMode_Fullscreen = 2;
  19.  
  20. HWND paWindow = 0;
  21. HDC paWindowDc = 0;
  22. HGLRC paWindowContext = 0;
  23.  
  24. //gl extensions
  25.  
  26. PROC wglCreateContextAttribsARB;
  27. PROC wglSwapIntervalEXT;
  28.  
  29. void GetWglExtensions() {
  30.     wglCreateContextAttribsARB = wglGetProcAddress("wglCreateContextAttribsARB");
  31.     wglSwapIntervalEXT = wglGetProcAddress("wglSwapIntervalEXT");
  32. }
  33.  
  34. //window events
  35.  
  36. int (*paOsWindowEventCallback)(int,int,int);
  37.  
  38. int paTranslateVirtualKey(WPARAM wParam, LPARAM lParam) {
  39.  
  40.     int extendedBit = lParam & (1 << 24);
  41.     int scancode = (lParam >> 16) & 0xFF;
  42.  
  43.     if (extendedBit) {
  44.         switch(wParam) {
  45.             case VK_CONTROL: return VK_RCONTROL;
  46.             case VK_MENU: return VK_RMENU;
  47.             case VK_RETURN: return 95;
  48.         }
  49.     } else {
  50.         switch((lParam >> 16) & 0xFF) {
  51.             case 42: return VK_LSHIFT;
  52.             case 54: return VK_RSHIFT;
  53.             case 82: return VK_NUMPAD0;
  54.             case 79: return VK_NUMPAD1;
  55.             case 80: return VK_NUMPAD2;
  56.             case 81: return VK_NUMPAD3;
  57.             case 75: return VK_NUMPAD4;
  58.             case 76: return VK_NUMPAD5;
  59.             case 77: return VK_NUMPAD6;
  60.             case 71: return VK_NUMPAD7;
  61.             case 72: return VK_NUMPAD8;
  62.             case 73: return VK_NUMPAD9;
  63.             case 83: return VK_DECIMAL;
  64.         }
  65.     }
  66.  
  67.     switch(wParam) {
  68.         case VK_CONTROL: return VK_LCONTROL;
  69.         case VK_MENU: return VK_LMENU;
  70.     }
  71.  
  72.     return wParam;
  73.  
  74. }
  75.  
  76. LRESULT CALLBACK paOsWindowCallback(HWND window, UINT message, WPARAM wParam, LPARAM lParam) {
  77.     switch(message){
  78.  
  79.         case WM_SYSCOMMAND:
  80.             if (wParam == SC_CLOSE) { paOsWindowEventCallback(paOsWindowEvent_Close,0,0); return 0; }
  81.             return DefWindowProc(window,message,wParam,lParam);
  82.  
  83.         case WM_SETFOCUS:
  84.             paOsWindowEventCallback(paOsWindowEvent_GainFocus,0,0);
  85.             return 0;
  86.  
  87.         case WM_DESTROY:
  88.         case WM_KILLFOCUS:
  89.              paOsWindowEventCallback(paOsWindowEvent_LoseFocus,0,0);
  90.              return 0;
  91.  
  92.         case WM_KEYDOWN:
  93.         case WM_SYSKEYDOWN:
  94.             if (lParam & (1 << 30)) {
  95.                 paOsWindowEventCallback(paOsWindowEvent_KeyRepeat,paTranslateVirtualKey(wParam,lParam),0);
  96.             } else {
  97.                 paOsWindowEventCallback(paOsWindowEvent_KeyDown,paTranslateVirtualKey(wParam,lParam),0);
  98.             }
  99.             return 0;
  100.  
  101.         case WM_KEYUP:
  102.         case WM_SYSKEYUP:
  103.             paOsWindowEventCallback(paOsWindowEvent_KeyUp,paTranslateVirtualKey(wParam,lParam),0);
  104.             return 0;
  105.  
  106.         case WM_CHAR:
  107.             paOsWindowEventCallback(paOsWindowEvent_Char,wParam,0);
  108.             return 0;
  109.  
  110.         case WM_MOUSEMOVE:
  111.             paOsWindowEventCallback(paOsWindowEvent_MousePos,LOWORD(lParam),HIWORD(lParam));
  112.             return 0;
  113.  
  114.         case WM_LBUTTONDOWN:
  115.         case WM_RBUTTONDOWN:
  116.         case WM_MBUTTONDOWN:
  117.         case 0x020B:
  118.             SetCapture(paWindow);
  119.             switch(message) {
  120.                 case WM_LBUTTONDOWN: paOsWindowEventCallback(paOsWindowEvent_MouseDown,1,0); return 0;
  121.                 case WM_RBUTTONDOWN: paOsWindowEventCallback(paOsWindowEvent_MouseDown,2,0); return 0;
  122.                 case WM_MBUTTONDOWN: paOsWindowEventCallback(paOsWindowEvent_MouseDown,3,0); return 0;
  123.                 case 0x020B:
  124.                     if (wParam & 0x0020) { paOsWindowEventCallback(paOsWindowEvent_MouseDown,4,0); return 0; }
  125.                     if (wParam & 0x0040) { paOsWindowEventCallback(paOsWindowEvent_MouseDown,5,0); return 0; }
  126.                     return 1;
  127.             }
  128.  
  129.         case WM_LBUTTONUP:
  130.         case WM_RBUTTONUP:
  131.         case WM_MBUTTONUP:
  132.         case 0x020C:
  133.             ReleaseCapture();
  134.             switch(message) {
  135.                 case WM_LBUTTONUP: paOsWindowEventCallback(paOsWindowEvent_MouseUp,1,0); return 0;
  136.                 case WM_RBUTTONUP: paOsWindowEventCallback(paOsWindowEvent_MouseUp,2,0); return 0;
  137.                 case WM_MBUTTONUP: paOsWindowEventCallback(paOsWindowEvent_MouseUp,3,0); return 0;
  138.                 case 0x020C:
  139.                     if (wParam & 0x0020) { paOsWindowEventCallback(paOsWindowEvent_MouseUp,4,0); return 0; }
  140.                     if (wParam & 0x0040) { paOsWindowEventCallback(paOsWindowEvent_MouseUp,5,0); return 0; }
  141.                     return 1;
  142.             }
  143.  
  144.         case WM_MOUSEWHEEL:
  145.             paOsWindowEventCallback(paOsWindowEvent_MouseWheel,GET_WHEEL_DELTA_WPARAM(wParam)/120,0);
  146.             return 0;
  147.  
  148.         default:
  149.             return DefWindowProc(window,message,wParam,lParam);
  150.     }
  151. }
  152.  
  153. void paDispatchOsWindowEvents() {
  154.     MSG message;
  155.     while(PeekMessage(&message,0,0,0,PM_REMOVE)) {
  156.         TranslateMessage(&message);
  157.         DispatchMessage(&message);
  158.     }
  159. }
  160.  
  161. void paInitializeOsWindow() {
  162.     if (paWindow) return 0;
  163.  
  164.     //create window class
  165.     WNDCLASS windowClass = {0};
  166.     windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  167.     windowClass.lpfnWndProc = paOsWindowCallback;
  168.     windowClass.hInstance = GetModuleHandle(0);
  169.     windowClass.lpszClassName = "PAWindowClass";
  170.     windowClass.hCursor = (HCURSOR)LoadCursor(0,IDC_ARROW);
  171.     RegisterClass(&windowClass);
  172.  
  173.     //create window
  174.     HWND window = CreateWindowEx(0,windowClass.lpszClassName,"PA",WS_POPUP,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,GetModuleHandle(0),0);
  175.     if (!window) return 0;
  176.  
  177.     //get device context
  178.     HDC dc = GetDC(window);
  179.  
  180.     //build pixel format descriptor
  181.     PIXELFORMATDESCRIPTOR pfd = {0};
  182.     pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  183.     pfd.nVersion = 1;
  184.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  185.     pfd.iPixelType = PFD_TYPE_RGBA;
  186.     pfd.cColorBits = 1;
  187.     pfd.cDepthBits = 1;
  188.     pfd.cStencilBits = 1;
  189.  
  190.     //get pixel format
  191.     int pf = ChoosePixelFormat(dc,&pfd);
  192.     if (!pf) { DestroyWindow(window); return 0; }
  193.  
  194.     //set device context's pixel format and descriptor
  195.     SetPixelFormat(dc,pf,&pfd);
  196.  
  197.     //create simple OpenGL context
  198.     HGLRC context = wglCreateContext(dc);
  199.     if (!context) { DestroyWindow(window); return 0; }
  200.     wglMakeCurrent(dc,context);
  201.  
  202.     //get extensions
  203.     GetWglExtensions();
  204.  
  205.     //delete context
  206.     wglDeleteContext(context);
  207.  
  208.     //create upgraded OpenGL context
  209.     context = wglCreateContextAttribsARB(dc,0,0);
  210.     wglMakeCurrent(dc,context);
  211.  
  212.     //set global state
  213.     paWindow = window;
  214.     paWindowDc = dc;
  215.     paWindowContext = context;
  216.  
  217.     return 1;
  218. }
  219.  
  220. int paTerminateOsWindow() {
  221.     if (!paWindow) return 0;
  222.  
  223.     //delete context
  224.     wglDeleteContext(paWindowContext);
  225.  
  226.     //destroy window
  227.     DestroyWindow(paWindow);
  228.  
  229.     //set state
  230.     paWindow = 0;
  231.     paWindowDc = 0;
  232.     paWindowContext = 0;
  233.  
  234.     return 1;
  235. }
  236.  
  237. int paSetOsWindowSettings(int mode, int x, int y, int width, int height) {
  238.     if (!paWindow) return 0;
  239.  
  240.     //get style
  241.     DWORD style,exStyle;
  242.     if (mode == paOsWindowMode_Windowed) {
  243.         style = WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
  244.         exStyle = 0;
  245.     } else if (mode == paOsWindowMode_BorderlessWindowed) {
  246.         style =  WS_POPUP | WS_VISIBLE;
  247.         exStyle = 0;
  248.     } else if (mode == paOsWindowMode_Fullscreen) {
  249.         style = WS_POPUP | WS_VISIBLE;
  250.         exStyle = 0;
  251.     }
  252.  
  253.     //adjust size
  254.     RECT rect;
  255.     rect.left = x;
  256.     rect.top = y;
  257.     rect.right = x+width;
  258.     rect.bottom = y+height;
  259.     AdjustWindowRectEx(&rect,style,0,exStyle);
  260.     x = rect.left;
  261.     y = rect.top;
  262.     width = rect.right-rect.left;
  263.     height = rect.bottom-rect.top;
  264.  
  265.     //set configuration
  266.     SetWindowLongPtr(paWindow,GWL_STYLE,style);
  267.     SetWindowLongPtr(paWindow,GWL_EXSTYLE,exStyle);
  268.  
  269.     //reposition (needs to be done after setting configuration)
  270.     SetWindowPos(paWindow,HWND_BOTTOM,x,y,width,height,SWP_FRAMECHANGED);
  271.  
  272.     return 1;
  273. }
  274.  
  275. int paMinimizeOsWindow() {
  276.     if (!paWindow) return 0;
  277.     ShowWindow(paWindow,SW_MINIMIZE);
  278.     return 1;
  279. }
  280.  
  281. int paRestoreOsWindow() {
  282.     if (!paWindow) return 0;
  283.     ShowWindow(paWindow,SW_RESTORE);
  284.     return 1;
  285. }
  286.  
  287. int paSetOsWindowSwapInterval(int interval) {
  288.     if (!paWindow) return 0;
  289.     wglSwapIntervalEXT(interval);
  290.     return 1;
  291. }
  292.  
  293. int paSwapOsWindow(int interval) {
  294.     if (!paWindow) return 0;
  295.     SwapBuffers(paWindowDc);
  296.     return 1;
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment