2
0

game.odin 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. when ODIN_OS == "windows" do import win32 "core:sys/windows.odin";
  2. when ODIN_OS == "windows" import wgl "core:sys/wgl.odin";
  3. import "core:fmt.odin";
  4. import "core:math.odin";
  5. import "core:os.odin";
  6. import gl "core:opengl.odin";
  7. TWO_HEARTS :: '💕';
  8. win32_perf_count_freq := win32.get_query_performance_frequency();
  9. time_now :: proc() -> f64 {
  10. assert(win32_perf_count_freq != 0);
  11. counter: i64;
  12. win32.query_performance_counter(&counter);
  13. return f64(counter) / f64(win32_perf_count_freq);
  14. }
  15. win32_print_last_error :: proc() {
  16. err_code := win32.get_last_error();
  17. if err_code != 0 {
  18. fmt.println("get_last_error: ", err_code);
  19. }
  20. }
  21. // Yuk!
  22. to_c_string :: proc(s: string) -> []u8 {
  23. c_str := make([]u8, len(s)+1);
  24. copy(c_str, cast([]u8)s);
  25. c_str[len(s)] = 0;
  26. return c_str;
  27. }
  28. Window :: struct {
  29. width, height: int,
  30. wc: win32.Wnd_Class_Ex_A,
  31. dc: win32.Hdc,
  32. hwnd: win32.Hwnd,
  33. opengl_context, rc: wgl.Hglrc,
  34. c_title: []u8,
  35. }
  36. make_window :: proc(title: string, msg, height: int, window_proc: win32.Wnd_Proc) -> (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[0];
  42. if title[len(title)-1] != 0 {
  43. w.c_title = to_c_string(title);
  44. } else {
  45. w.c_title = cast([]u8)title;
  46. }
  47. instance := get_module_handle_a(nil);
  48. w.wc = Wnd_Class_Ex_A{
  49. size = size_of(Wnd_Class_Ex_A),
  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. pfd := Pixel_Format_Descriptor{
  72. size = size_of(Pixel_Format_Descriptor),
  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. 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 that :: proc this is the end of attribs
  90. };
  91. wgl_str := "wglCreateContextAttribsARB\x00";
  92. wglCreateContextAttribsARB := cast(wgl.Create_Context_Attribs_ARB_Type)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. destroy_window :: proc(w: ^Window) {
  100. free(w.c_title);
  101. }
  102. display_window :: proc(w: ^Window) {
  103. win32.swap_buffers(w.dc);
  104. }
  105. run :: proc() {
  106. using math;
  107. win32_proc :: 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. window, window_success := make_window("Odin Language Demo", 854, 480, cast(win32.Wnd_Proc)win32_proc);
  116. if !window_success {
  117. return;
  118. }
  119. defer destroy_window(&window);
  120. gl.init();
  121. using win32;
  122. prev_time := time_now();
  123. running := true;
  124. pos := Vec2{100, 100};
  125. for running {
  126. curr_time := time_now();
  127. dt := f32(curr_time - prev_time);
  128. prev_time = curr_time;
  129. 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(Key_Code.Escape) {
  138. running = false;
  139. }
  140. {
  141. SPEED :: 500;
  142. v: Vec2;
  143. if is_key_down(Key_Code.Right) do v[0] += 1;
  144. if is_key_down(Key_Code.Left) do v[0] -= 1;
  145. if is_key_down(Key_Code.Up) do v[1] += 1;
  146. if is_key_down(Key_Code.Down) do 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. draw_rect :: proc(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. if ms_to_sleep := i32(16 - 1000*dt); ms_to_sleep > 0 {
  168. win32.sleep(ms_to_sleep);
  169. }
  170. }
  171. }
  172. main :: proc() {
  173. run();
  174. }