native.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //========================================================================
  2. // Win32 native handle attachment test
  3. // Copyright (c) Camilla Löwy <[email protected]>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. #define UNICODE
  26. #include <glad/glad.h>
  27. #include <GLFW/glfw3.h>
  28. #define GLFW_EXPOSE_NATIVE_WIN32
  29. #include <GLFW/glfw3native.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. static void error_callback(int error, const char* description)
  33. {
  34. fprintf(stderr, "Error: %s\n", description);
  35. }
  36. static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  37. {
  38. // This will only be used until glfwAttachWin32Window
  39. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  40. }
  41. int main(void)
  42. {
  43. GLFWwindow* window;
  44. WNDCLASSEX wc;
  45. HWND handle;
  46. ZeroMemory(&wc, sizeof(wc));
  47. wc.cbSize = sizeof(wc);
  48. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  49. wc.lpfnWndProc = (WNDPROC) windowProc;
  50. wc.hInstance = GetModuleHandleW(NULL);
  51. wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
  52. wc.lpszClassName = L"SomeKindOfWindowClassName";
  53. if (!RegisterClassExW(&wc))
  54. exit(EXIT_FAILURE);
  55. handle = CreateWindowExW(WS_EX_APPWINDOW,
  56. L"SomeKindOfWindowClassName",
  57. L"HWND attachment test",
  58. WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  59. CW_USEDEFAULT, CW_USEDEFAULT,
  60. 600, 400,
  61. NULL,
  62. NULL,
  63. GetModuleHandleW(NULL),
  64. NULL);
  65. if (!handle)
  66. exit(EXIT_FAILURE);
  67. glfwSetErrorCallback(error_callback);
  68. if (!glfwInit())
  69. {
  70. DestroyWindow(handle);
  71. exit(EXIT_FAILURE);
  72. }
  73. window = glfwAttachWin32Window(handle, NULL);
  74. if (!window)
  75. {
  76. glfwTerminate();
  77. DestroyWindow(handle);
  78. exit(EXIT_FAILURE);
  79. }
  80. glfwMakeContextCurrent(window);
  81. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  82. glfwSwapInterval(1);
  83. while (!glfwWindowShouldClose(window))
  84. {
  85. glClear(GL_COLOR_BUFFER_BIT);
  86. glfwSwapBuffers(window);
  87. glfwWaitEvents();
  88. }
  89. glfwTerminate();
  90. DestroyWindow(handle);
  91. exit(EXIT_SUCCESS);
  92. }