but someone on opengl irc helped me out and to never have to deal with this mess again here's the code
(i started from the original game demo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #import "fmt.odin"; #import "math.odin"; #import "os.odin"; #import gl "opengl.odin"; #import win32 "sys/windows.odin"; main :: proc(){ run(); } win32_print_last_error :: proc() { err_code := cast(int)win32.GetLastError(); if err_code != 0 { fmt.println("GetLastError: %", err_code); } } // Yuk! to_c_string :: proc(s: string) -> []u8 { c_str := new_slice(u8, s.count+1); copy(c_str, cast([]byte)s); c_str[s.count] = 0; return c_str; } Window :: struct { width: int, height: int, wc: win32.WNDCLASSEXA, dc: win32.HDC, hwnd: win32.HWND, opengl_context: win32.HGLRC, rc: win32.HGLRC, c_title: []u8 } make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC) -> (Window, bool) { using win32; w: Window; w.width, w.height = msg, height; class_name := "Win32-Odin-Window\x00"; c_class_name := class_name.data; if title[title.count-1] != 0 { w.c_title = to_c_string(title); } else { w.c_title = cast([]u8)title; } instance := GetModuleHandleA(nil); w.wc = WNDCLASSEXA{ size = cast(u32)size_of(WNDCLASSEXA), style = CS_VREDRAW | CS_HREDRAW, instance = cast(HINSTANCE)instance, class_name = c_class_name, wnd_proc = window_proc, }; if RegisterClassExA(^w.wc) == 0 { win32_print_last_error(); return w, false; } w.hwnd = CreateWindowExA(0, c_class_name, w.c_title.data, WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, cast(i32)w.width, cast(i32)w.height, nil, nil, instance, nil); if w.hwnd == nil { win32_print_last_error(); return w, false; } w.dc = GetDC(w.hwnd); { pfd := PIXELFORMATDESCRIPTOR{ size = cast(u32)size_of(PIXELFORMATDESCRIPTOR), version = 1, flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, pixel_type = PFD_TYPE_RGBA, color_bits = 32, alpha_bits = 8, depth_bits = 24, stencil_bits = 8, layer_type = PFD_MAIN_PLANE, }; SetPixelFormat(w.dc, ChoosePixelFormat(w.dc, ^pfd), nil); w.opengl_context = wglCreateContext(w.dc); wglMakeCurrent(w.dc, w.opengl_context); attribs := [8]i32{ //WGL_SAMPLES_ARB, 4, WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0, // NOTE(bill): tells the proc that this is the end of attribs }; wglCreateContextAttribsARB := cast(wglCreateContextAttribsARBType)wglGetProcAddress((cast(string)"wglCreateContextAttribsARB\x00").data); w.rc = wglCreateContextAttribsARB(w.dc, nil, ^attribs[0]); wglMakeCurrent(w.dc, w.rc); SwapBuffers(w.dc); } return w, true; } destroy_window :: proc(w: ^Window) { free(w.c_title.data); } display_window :: proc(w: ^Window) { win32.SwapBuffers(w.dc); } run :: proc() { using win32; using math; win32_procA :: proc(hwnd: win32.HWND, msg: u32, wparam: win32.WPARAM, lparam: win32.LPARAM) -> win32.LRESULT #no_inline { if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT { os.exit(0); return 0; } return DefWindowProcA(hwnd, msg, wparam, lparam); } win32_proc := cast(WNDPROC)win32_procA; window, window_success := make_window("Odin gl Demo", 500, 450, win32_proc); if !window_success { return; } defer destroy_window(^window); //gl gl.init(); vao : u32; gl.GenVertexArrays(1, ^vao); gl.BindVertexArray(vao); vertexData := []f32{ -1,-1, 0, 1,-1, 0, 0, 1, 0 }; vertexBuffer : u32; gl.GenBuffers(1, ^vertexBuffer); gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.BufferData(gl.ARRAY_BUFFER, 9*size_of(f32)/* <-is there a better way?*/, vertexData.data, gl.STATIC_DRAW); //loop running := true; for running { msg: MSG; for PeekMessageA(^msg, nil, 0, 0, PM_REMOVE) > 0 { if msg.message == WM_QUIT { running = false; } TranslateMessage(^msg); DispatchMessageA(^msg); } //---------------------// gl.EnableVertexAttribArray(0); gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.VertexAttribPointer(0,3,gl.FLOAT,gl.FALSE,0,nil); gl.DrawArrays(gl.TRIANGLES,0,3); gl.DisableVertexAttribArray(0); //---------------------// display_window(^window); } } |