2
0

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. TWO_HEARTS :: '💕';
  8. win32_perf_count_freq := win32.GetQueryPerformanceFrequency();
  9. time_now :: proc() -> f64 {
  10. assert(win32_perf_count_freq != 0);
  11. counter: i64;
  12. win32.QueryPerformanceCounter(^counter);
  13. result := cast(f64)counter / cast(f64)win32_perf_count_freq;
  14. return result;
  15. }
  16. win32_print_last_error :: proc() {
  17. err_code := cast(int)win32.GetLastError();
  18. if err_code != 0 {
  19. fmt.println("GetLastError: %", err_code);
  20. }
  21. }
  22. // Yuk!
  23. to_c_string :: proc(s: string) -> []u8 {
  24. c_str := make([]u8, len(s)+1);
  25. copy(c_str, cast([]byte)s);
  26. c_str[len(s)] = 0;
  27. return c_str;
  28. }
  29. Window :: struct {
  30. width, height: int,
  31. wc: win32.WndClassExA,
  32. dc: win32.Hdc,
  33. hwnd: win32.Hwnd,
  34. opengl_context, rc: wgl.Hglrc,
  35. c_title: []u8,
  36. }
  37. make_window :: proc(title: string, msg, height: int, window_proc: win32.Wnd_Proc) -> (Window, bool) {
  38. using win32;
  39. w: Window;
  40. w.width, w.height = msg, height;
  41. class_name := "Win32-Odin-Window\x00";
  42. c_class_name := ^class_name[0];
  43. if title[len(title)-1] != 0 {
  44. w.c_title = to_c_string(title);
  45. } else {
  46. w.c_title = cast([]u8)title;
  47. }
  48. instance := GetModuleHandleA(nil);
  49. w.wc = WndClassExA{
  50. size = size_of(WndClassExA),
  51. style = CS_VREDRAW | CS_HREDRAW,
  52. instance = cast(Hinstance)instance,
  53. class_name = c_class_name,
  54. wnd_proc = window_proc,
  55. };
  56. if RegisterClassExA(^w.wc) == 0 {
  57. win32_print_last_error();
  58. return w, false;
  59. }
  60. w.hwnd = CreateWindowExA(0,
  61. c_class_name, ^w.c_title[0],
  62. WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  63. CW_USEDEFAULT, CW_USEDEFAULT,
  64. cast(i32)w.width, cast(i32)w.height,
  65. nil, nil, instance, nil);
  66. if w.hwnd == nil {
  67. win32_print_last_error();
  68. return w, false;
  69. }
  70. w.dc = GetDC(w.hwnd);
  71. {
  72. pfd := PIXELFORMATDESCRIPTOR{
  73. size = size_of(PIXELFORMATDESCRIPTOR),
  74. version = 1,
  75. flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
  76. pixel_type = PFD_TYPE_RGBA,
  77. color_bits = 32,
  78. alpha_bits = 8,
  79. depth_bits = 24,
  80. stencil_bits = 8,
  81. layer_type = PFD_MAIN_PLANE,
  82. };
  83. SetPixelFormat(w.dc, ChoosePixelFormat(w.dc, ^pfd), nil);
  84. w.opengl_context = wgl.CreateContext(w.dc);
  85. wgl.MakeCurrent(w.dc, w.opengl_context);
  86. attribs := [8]i32{
  87. wgl.CONTEXT_MAJOR_VERSION_ARB, 2,
  88. wgl.CONTEXT_MINOR_VERSION_ARB, 1,
  89. wgl.CONTEXT_PROFILE_MASK_ARB, wgl.CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
  90. 0, // NOTE(bill): tells the proc that this is the end of attribs
  91. };
  92. wgl_str := "wglCreateContextAttribsARB\x00";
  93. wglCreateContextAttribsARB := cast(wgl.Create_Context_Attribs_ARB_Type)wgl.GetProcAddress(^wgl_str[0]);
  94. w.rc = wglCreateContextAttribsARB(w.dc, nil, ^attribs[0]);
  95. wgl.MakeCurrent(w.dc, w.rc);
  96. SwapBuffers(w.dc);
  97. }
  98. return w, true;
  99. }
  100. destroy_window :: proc(w: ^Window) {
  101. free(w.c_title);
  102. }
  103. display_window :: proc(w: ^Window) {
  104. win32.SwapBuffers(w.dc);
  105. }
  106. run :: proc() {
  107. using win32;
  108. using math;
  109. win32_proc :: proc(hwnd: win32.Hwnd, msg: u32, wparam: win32.Wparam, lparam: win32.Lparam) -> win32.Lresult #no_inline {
  110. if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
  111. os.exit(0);
  112. return 0;
  113. }
  114. return DefWindowProcA(hwnd, msg, wparam, lparam);
  115. }
  116. window, window_success := make_window("Odin Language Demo", 854, 480, cast(Wnd_Proc)win32_proc);
  117. if !window_success {
  118. return;
  119. }
  120. defer destroy_window(^window);
  121. gl.init();
  122. prev_time := time_now();
  123. running := true;
  124. pos := Vec2{100, 100};
  125. for running {
  126. curr_time := time_now();
  127. dt := cast(f32)(curr_time - prev_time);
  128. prev_time = curr_time;
  129. msg: Msg;
  130. for PeekMessageA(^msg, nil, 0, 0, PM_REMOVE) > 0 {
  131. if msg.message == WM_QUIT {
  132. running = false;
  133. }
  134. TranslateMessage(^msg);
  135. DispatchMessageA(^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) { v[0] += 1; }
  144. if is_key_down(Key_Code.LEFT) { v[0] -= 1; }
  145. if is_key_down(Key_Code.UP) { v[1] += 1; }
  146. if is_key_down(Key_Code.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, cast(f64)window.width,
  154. 0, cast(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. ms_to_sleep := cast(i32)(16 - 1000*dt);
  168. if ms_to_sleep > 0 {
  169. win32.Sleep(ms_to_sleep);
  170. }
  171. }
  172. }
  173. main :: proc() {
  174. run();
  175. }