game.odin 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #import "win32.odin" when ODIN_OS == "windows";
  2. #import "fmt.odin";
  3. #import "math.odin";
  4. #import "os.odin";
  5. #import gl "opengl.odin";
  6. TWO_HEARTS :: '💕';
  7. win32_perf_count_freq := win32.GetQueryPerformanceFrequency();
  8. time_now :: proc() -> f64 {
  9. assert(win32_perf_count_freq != 0);
  10. counter: i64;
  11. win32.QueryPerformanceCounter(^counter);
  12. result := counter as f64 / win32_perf_count_freq as f64;
  13. return result;
  14. }
  15. win32_print_last_error :: proc() {
  16. err_code := win32.GetLastError() as int;
  17. if err_code != 0 {
  18. fmt.println("GetLastError: %", err_code);
  19. }
  20. }
  21. // Yuk!
  22. to_c_string :: proc(s: string) -> []u8 {
  23. c_str := new_slice(u8, s.count+1);
  24. copy(c_str, s as []byte);
  25. c_str[s.count] = 0;
  26. return c_str;
  27. }
  28. Window :: struct {
  29. width, height: int;
  30. wc: win32.WNDCLASSEXA;
  31. dc: win32.HDC;
  32. hwnd: win32.HWND;
  33. opengl_context, rc: win32.HGLRC;
  34. c_title: []u8;
  35. }
  36. make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC) -> (Window, bool) {
  37. using win32;
  38. w: Window;
  39. w.width, w.height = msg, height;
  40. class_name := "Win32-Odin-Window\x00";
  41. c_class_name := class_name.data;
  42. if title[title.count-1] != 0 {
  43. w.c_title = to_c_string(title);
  44. } else {
  45. w.c_title = title as []u8;
  46. }
  47. instance := GetModuleHandleA(nil);
  48. w.wc = WNDCLASSEXA{
  49. size = size_of(WNDCLASSEXA) as u32,
  50. style = CS_VREDRAW | CS_HREDRAW,
  51. instance = instance as HINSTANCE,
  52. class_name = c_class_name,
  53. wnd_proc = window_proc,
  54. };
  55. if RegisterClassExA(^w.wc) == 0 {
  56. win32_print_last_error();
  57. return w, false;
  58. }
  59. w.hwnd = CreateWindowExA(0,
  60. c_class_name, w.c_title.data,
  61. WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  62. CW_USEDEFAULT, CW_USEDEFAULT,
  63. w.width as i32, w.height as i32,
  64. nil, nil, instance, nil);
  65. if w.hwnd == nil {
  66. win32_print_last_error();
  67. return w, false;
  68. }
  69. w.dc = GetDC(w.hwnd);
  70. {
  71. pfd := PIXELFORMATDESCRIPTOR{
  72. size = size_of(PIXELFORMATDESCRIPTOR) as u32,
  73. version = 1,
  74. flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
  75. pixel_type = PFD_TYPE_RGBA,
  76. color_bits = 32,
  77. alpha_bits = 8,
  78. depth_bits = 24,
  79. stencil_bits = 8,
  80. layer_type = PFD_MAIN_PLANE,
  81. };
  82. SetPixelFormat(w.dc, ChoosePixelFormat(w.dc, ^pfd), nil);
  83. w.opengl_context = wglCreateContext(w.dc);
  84. wglMakeCurrent(w.dc, w.opengl_context);
  85. attribs := [8]i32{
  86. WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
  87. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  88. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
  89. 0, // NOTE(bill): tells the proc that this is the end of attribs
  90. };
  91. wglCreateContextAttribsARB := wglGetProcAddress(("wglCreateContextAttribsARB\x00" as string).data) as wglCreateContextAttribsARBType;
  92. w.rc = wglCreateContextAttribsARB(w.dc, 0, ^attribs[0]);
  93. wglMakeCurrent(w.dc, w.rc);
  94. SwapBuffers(w.dc);
  95. }
  96. return w, true;
  97. }
  98. destroy_window :: proc(w: ^Window) {
  99. free(w.c_title.data);
  100. }
  101. display_window :: proc(w: ^Window) {
  102. win32.SwapBuffers(w.dc);
  103. }
  104. run :: proc() {
  105. using win32;
  106. using math;
  107. win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
  108. if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
  109. os.exit(0);
  110. return 0;
  111. }
  112. return DefWindowProcA(hwnd, msg, wparam, lparam);
  113. }
  114. window, window_success := make_window("Odin Language Demo", 854, 480, win32_proc);
  115. if !window_success {
  116. return;
  117. }
  118. defer destroy_window(^window);
  119. gl.init();
  120. prev_time := time_now();
  121. running := true;
  122. pos := Vec2{100, 100};
  123. for running {
  124. curr_time := time_now();
  125. dt := (curr_time - prev_time) as f32;
  126. prev_time = curr_time;
  127. msg: MSG;
  128. for PeekMessageA(^msg, nil, 0, 0, PM_REMOVE) > 0 {
  129. if msg.message == WM_QUIT {
  130. running = false;
  131. }
  132. TranslateMessage(^msg);
  133. DispatchMessageA(^msg);
  134. }
  135. if is_key_down(Key_Code.ESCAPE) {
  136. running = false;
  137. }
  138. {
  139. SPEED :: 500;
  140. v: Vec2;
  141. if is_key_down(Key_Code.RIGHT) { v[0] += 1; }
  142. if is_key_down(Key_Code.LEFT) { v[0] -= 1; }
  143. if is_key_down(Key_Code.UP) { v[1] += 1; }
  144. if is_key_down(Key_Code.DOWN) { v[1] -= 1; }
  145. v = vec2_norm0(v);
  146. pos += v * Vec2{SPEED * dt};
  147. }
  148. gl.ClearColor(0.5, 0.7, 1.0, 1.0);
  149. gl.Clear(gl.COLOR_BUFFER_BIT);
  150. gl.LoadIdentity();
  151. gl.Ortho(0, window.width as f64,
  152. 0, window.height as f64, 0, 1);
  153. draw_rect :: proc(x, y, w, h: f32) {
  154. gl.Begin(gl.TRIANGLES);
  155. defer gl.End();
  156. gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0);
  157. gl.Color3f(0, 1, 0); gl.Vertex3f(x+w, y, 0);
  158. gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0);
  159. gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0);
  160. gl.Color3f(1, 1, 0); gl.Vertex3f(x, y+h, 0);
  161. gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0);
  162. }
  163. draw_rect(pos.x, pos.y, 50, 50);
  164. display_window(^window);
  165. ms_to_sleep := (16 - 1000*dt) as i32;
  166. if ms_to_sleep > 0 {
  167. win32.Sleep(ms_to_sleep);
  168. }
  169. }
  170. }