render_backend_gl_windows.odin 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if win32.wglChoosePixelFormatARB == nil {
  34. log.error("Failed fetching wglChoosePixelFormatARB")
  35. return {}, false
  36. }
  37. if win32.wglCreateContextAttribsARB == nil {
  38. log.error("Failed fetching wglCreateContextAttribsARB")
  39. return {}, false
  40. }
  41. pixel_format_ilist := [?]i32 {
  42. win32.WGL_DRAW_TO_WINDOW_ARB, 1,
  43. win32.WGL_SUPPORT_OPENGL_ARB, 1,
  44. win32.WGL_DOUBLE_BUFFER_ARB, 1,
  45. win32.WGL_PIXEL_TYPE_ARB, win32.WGL_TYPE_RGBA_ARB,
  46. win32.WGL_COLOR_BITS_ARB, 32,
  47. win32.WGL_DEPTH_BITS_ARB, 24,
  48. win32.WGL_STENCIL_BITS_ARB, 8,
  49. 0,
  50. }
  51. pixel_format: i32
  52. num_formats: u32
  53. valid_pixel_format := win32.wglChoosePixelFormatARB(hdc, raw_data(pixel_format_ilist[:]),
  54. nil, 1, &pixel_format, &num_formats)
  55. if !valid_pixel_format {
  56. return {}, false
  57. }
  58. win32.SetPixelFormat(hdc, pixel_format, nil)
  59. ctx := win32.wglCreateContextAttribsARB(hdc, nil, nil)
  60. win32.wglMakeCurrent(hdc, ctx)
  61. return ctx, true
  62. }
  63. _gl_destroy_context :: proc(ctx: GL_Context) {
  64. win32.wglDeleteContext(ctx)
  65. }
  66. _gl_load_procs :: proc() {
  67. gl.load_up_to(3, 3, win32.gl_set_proc_address)
  68. }
  69. _gl_present :: proc(window_handle: Window_Handle) {
  70. hdc := win32.GetWindowDC(win32.HWND(window_handle))
  71. win32.SwapBuffers(hdc)
  72. }