game.odin 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import win32 "sys/windows.odin" when ODIN_OS == "windows";
  2. import wgl "sys/wgl.odin" when ODIN_OS == "windows";
  3. import "fmt.odin";
  4. import "math.odin";
  5. import "os.odin";
  6. import gl "opengl.odin";
  7. const TWO_HEARTS = '💕';
  8. var win32_perf_count_freq = win32.get_query_performance_frequency();
  9. proc time_now() -> f64 {
  10. assert(win32_perf_count_freq != 0);
  11. var counter: i64;
  12. win32.query_performance_counter(&counter);
  13. return f64(counter) / f64(win32_perf_count_freq);
  14. }
  15. proc win32_print_last_error() {
  16. var err_code = win32.get_last_error();
  17. if err_code != 0 {
  18. fmt.println("get_last_error: ", err_code);
  19. }
  20. }
  21. // Yuk!
  22. proc to_c_string(s: string) -> []u8 {
  23. var c_str = make([]u8, len(s)+1);
  24. copy(c_str, []u8(s));
  25. c_str[len(s)] = 0;
  26. return c_str;
  27. }
  28. type Window struct {
  29. width, height: int,
  30. wc: win32.WndClassExA,
  31. dc: win32.Hdc,
  32. hwnd: win32.Hwnd,
  33. opengl_context, rc: wgl.Hglrc,
  34. c_title: []u8,
  35. }
  36. proc make_window(title: string, msg, height: int, window_proc: win32.WndProc) -> (Window, bool) {
  37. using win32;
  38. var w: Window;
  39. w.width, w.height = msg, height;
  40. var class_name = "Win32-Odin-Window\x00";
  41. var c_class_name = &class_name[0];
  42. if title[len(title)-1] != 0 {
  43. w.c_title = to_c_string(title);
  44. } else {
  45. w.c_title = []u8(title);
  46. }
  47. var instance = get_module_handle_a(nil);
  48. w.wc = WndClassExA{
  49. size = size_of(WndClassExA),
  50. style = CS_VREDRAW | CS_HREDRAW,
  51. instance = Hinstance(instance),
  52. class_name = c_class_name,
  53. wnd_proc = window_proc,
  54. };
  55. if register_class_ex_a(&w.wc) == 0 {
  56. win32_print_last_error();
  57. return w, false;
  58. }
  59. w.hwnd = create_window_ex_a(0,
  60. c_class_name, &w.c_title[0],
  61. WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  62. CW_USEDEFAULT, CW_USEDEFAULT,
  63. i32(w.width), i32(w.height),
  64. nil, nil, instance, nil);
  65. if w.hwnd == nil {
  66. win32_print_last_error();
  67. return w, false;
  68. }
  69. w.dc = get_dc(w.hwnd);
  70. {
  71. var pfd = PixelFormatDescriptor{
  72. size = size_of(PixelFormatDescriptor),
  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. set_pixel_format(w.dc, choose_pixel_format(w.dc, &pfd), nil);
  83. w.opengl_context = wgl.create_context(w.dc);
  84. wgl.make_current(w.dc, w.opengl_context);
  85. var 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. var wgl_str = "wglCreateContextAttribsARB\x00";
  92. var wglCreateContextAttribsARB = wgl.CreateContextAttribsARBType(wgl.get_proc_address(&wgl_str[0]));
  93. w.rc = wglCreateContextAttribsARB(w.dc, nil, &attribs[0]);
  94. wgl.make_current(w.dc, w.rc);
  95. swap_buffers(w.dc);
  96. }
  97. return w, true;
  98. }
  99. proc destroy_window(w: ^Window) {
  100. free(w.c_title);
  101. }
  102. proc display_window(w: ^Window) {
  103. win32.swap_buffers(w.dc);
  104. }
  105. proc run() {
  106. using math;
  107. proc win32_proc(hwnd: win32.Hwnd, msg: u32, wparam: win32.Wparam, lparam: win32.Lparam) -> win32.Lresult #no_inline {
  108. using win32;
  109. if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
  110. os.exit(0);
  111. return 0;
  112. }
  113. return def_window_proc_a(hwnd, msg, wparam, lparam);
  114. }
  115. var window, window_success = make_window("Odin Language Demo", 854, 480, win32.WndProc(win32_proc));
  116. if !window_success {
  117. return;
  118. }
  119. defer destroy_window(&window);
  120. gl.init();
  121. using win32;
  122. var prev_time = time_now();
  123. var running = true;
  124. var pos = Vec2{100, 100};
  125. for running {
  126. var curr_time = time_now();
  127. var dt = f32(curr_time - prev_time);
  128. prev_time = curr_time;
  129. var msg: Msg;
  130. for peek_message_a(&msg, nil, 0, 0, PM_REMOVE) > 0 {
  131. if msg.message == WM_QUIT {
  132. running = false;
  133. }
  134. translate_message(&msg);
  135. dispatch_message_a(&msg);
  136. }
  137. if is_key_down(KeyCode.Escape) {
  138. running = false;
  139. }
  140. {
  141. const SPEED = 500;
  142. var v: Vec2;
  143. if is_key_down(KeyCode.Right) { v[0] += 1; }
  144. if is_key_down(KeyCode.Left) { v[0] -= 1; }
  145. if is_key_down(KeyCode.Up) { v[1] += 1; }
  146. if is_key_down(KeyCode.Down) { v[1] -= 1; }
  147. v = norm(v);
  148. pos += v * Vec2{SPEED * dt};
  149. }
  150. gl.ClearColor(0.5, 0.7, 1.0, 1.0);
  151. gl.Clear(gl.COLOR_BUFFER_BIT);
  152. gl.LoadIdentity();
  153. gl.Ortho(0, f64(window.width),
  154. 0, f64(window.height), 0, 1);
  155. proc draw_rect(x, y, w, h: f32) {
  156. gl.Begin(gl.TRIANGLES);
  157. defer gl.End();
  158. gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0);
  159. gl.Color3f(0, 1, 0); gl.Vertex3f(x+w, y, 0);
  160. gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0);
  161. gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0);
  162. gl.Color3f(1, 1, 0); gl.Vertex3f(x, y+h, 0);
  163. gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0);
  164. }
  165. draw_rect(pos.x, pos.y, 50, 50);
  166. display_window(&window);
  167. var ms_to_sleep = i32(16 - 1000*dt);
  168. if ms_to_sleep > 0 {
  169. win32.sleep(ms_to_sleep);
  170. }
  171. }
  172. }
  173. proc main() {
  174. run();
  175. }