render_backend_gl_windows.odin 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #+build windows
  2. package karl2d
  3. import win32 "core:sys/windows"
  4. import gl "vendor:OpenGL"
  5. import "core:log"
  6. GL_Context :: win32.HGLRC
  7. _gl_get_context :: proc(window_handle: Window_Handle) -> (GL_Context, bool) {
  8. hdc := win32.GetWindowDC(win32.HWND(window_handle))
  9. pfd := win32.PIXELFORMATDESCRIPTOR {
  10. size_of(win32.PIXELFORMATDESCRIPTOR),
  11. 1,
  12. win32.PFD_DRAW_TO_WINDOW | win32.PFD_SUPPORT_OPENGL | win32.PFD_DOUBLEBUFFER, // Flags
  13. win32.PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
  14. 32, // Colordepth of the framebuffer.
  15. 0, 0, 0, 0, 0, 0,
  16. 0,
  17. 0,
  18. 0,
  19. 0, 0, 0, 0,
  20. 24, // Number of bits for the depthbuffer
  21. 8, // Number of bits for the stencilbuffer
  22. 0, // Number of Aux buffers in the framebuffer.
  23. win32.PFD_MAIN_PLANE,
  24. 0,
  25. 0, 0, 0,
  26. }
  27. fmt := win32.ChoosePixelFormat(hdc, &pfd)
  28. win32.SetPixelFormat(hdc, fmt, &pfd)
  29. dummy_ctx := win32.wglCreateContext(hdc)
  30. win32.wglMakeCurrent(hdc, dummy_ctx)
  31. win32.gl_set_proc_address(&win32.wglChoosePixelFormatARB, "wglChoosePixelFormatARB")
  32. win32.gl_set_proc_address(&win32.wglCreateContextAttribsARB, "wglCreateContextAttribsARB")
  33. win32.gl_set_proc_address(&win32.wglSwapIntervalEXT, "wglSwapIntervalEXT")
  34. if win32.wglChoosePixelFormatARB == nil {
  35. log.error("Failed fetching wglChoosePixelFormatARB")
  36. return {}, false
  37. }
  38. if win32.wglCreateContextAttribsARB == nil {
  39. log.error("Failed fetching wglCreateContextAttribsARB")
  40. return {}, false
  41. }
  42. if win32.wglSwapIntervalEXT == nil {
  43. log.error("Failed fetching wglSwapIntervalEXT")
  44. return {}, false
  45. }
  46. pixel_format_ilist := [?]i32 {
  47. win32.WGL_DRAW_TO_WINDOW_ARB, 1,
  48. win32.WGL_SUPPORT_OPENGL_ARB, 1,
  49. win32.WGL_DOUBLE_BUFFER_ARB, 1,
  50. win32.WGL_PIXEL_TYPE_ARB, win32.WGL_TYPE_RGBA_ARB,
  51. win32.WGL_COLOR_BITS_ARB, 32,
  52. win32.WGL_DEPTH_BITS_ARB, 24,
  53. win32.WGL_STENCIL_BITS_ARB, 8,
  54. 0,
  55. }
  56. pixel_format: i32
  57. num_formats: u32
  58. valid_pixel_format := win32.wglChoosePixelFormatARB(hdc, raw_data(pixel_format_ilist[:]),
  59. nil, 1, &pixel_format, &num_formats)
  60. if !valid_pixel_format {
  61. return {}, false
  62. }
  63. win32.SetPixelFormat(hdc, pixel_format, nil)
  64. ctx := win32.wglCreateContextAttribsARB(hdc, nil, nil)
  65. win32.wglMakeCurrent(hdc, ctx)
  66. win32.wglSwapIntervalEXT(1)
  67. return ctx, true
  68. }
  69. _gl_destroy_context :: proc(ctx: GL_Context) {
  70. win32.wglDeleteContext(ctx)
  71. }
  72. _gl_load_procs :: proc() {
  73. gl.load_up_to(3, 3, win32.gl_set_proc_address)
  74. }
  75. _gl_present :: proc(window_handle: Window_Handle) {
  76. hdc := win32.GetWindowDC(win32.HWND(window_handle))
  77. win32.SwapBuffers(hdc)
  78. }