karl2d_windows.odin 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #+build windows
  2. package karl2d
  3. import "base:runtime"
  4. import win "core:sys/windows"
  5. import D3D11 "vendor:directx/d3d11"
  6. import DXGI "vendor:directx/dxgi"
  7. import "core:strings"
  8. import "core:log"
  9. _ :: log
  10. //import "core:math"
  11. _init :: proc(width: int, height: int, title: string,
  12. allocator := context.allocator, loc := #caller_location) -> ^State {
  13. s = new(State, allocator, loc)
  14. s.custom_context = context
  15. CLASS_NAME :: "karl2d"
  16. instance := win.HINSTANCE(win.GetModuleHandleW(nil))
  17. s.run = true
  18. cls := win.WNDCLASSW {
  19. lpfnWndProc = window_proc,
  20. lpszClassName = CLASS_NAME,
  21. hInstance = instance,
  22. hCursor = win.LoadCursorA(nil, win.IDC_ARROW),
  23. }
  24. _ = cls
  25. class := win.RegisterClassW(&cls)
  26. _ = class
  27. hwnd := win.CreateWindowW(CLASS_NAME,
  28. win.utf8_to_wstring(title),
  29. win.WS_OVERLAPPEDWINDOW | win.WS_VISIBLE,
  30. 100, 100, i32(width), i32(height),
  31. nil, nil, instance, nil)
  32. feature_levels := [?]D3D11.FEATURE_LEVEL{ ._11_0 }
  33. base_device: ^D3D11.IDevice
  34. base_device_context: ^D3D11.IDeviceContext
  35. D3D11.CreateDevice(
  36. nil,
  37. .HARDWARE,
  38. nil,
  39. {.BGRA_SUPPORT},
  40. &feature_levels[0], len(feature_levels),
  41. D3D11.SDK_VERSION, &base_device, nil, &base_device_context)
  42. device: ^D3D11.IDevice
  43. base_device->QueryInterface(D3D11.IDevice_UUID, (^rawptr)(&device))
  44. base_device_context->QueryInterface(D3D11.IDeviceContext_UUID, (^rawptr)(&s.device_context))
  45. dxgi_device: ^DXGI.IDevice
  46. device->QueryInterface(DXGI.IDevice_UUID, (^rawptr)(&dxgi_device))
  47. dxgi_adapter: ^DXGI.IAdapter
  48. dxgi_device->GetAdapter(&dxgi_adapter)
  49. dxgi_factory: ^DXGI.IFactory2
  50. dxgi_adapter->GetParent(DXGI.IFactory2_UUID, (^rawptr)(&dxgi_factory))
  51. swapchain_desc := DXGI.SWAP_CHAIN_DESC1 {
  52. Format = .B8G8R8A8_UNORM,
  53. SampleDesc = {
  54. Count = 1,
  55. },
  56. BufferUsage = {.RENDER_TARGET_OUTPUT},
  57. BufferCount = 2,
  58. Scaling = .STRETCH,
  59. SwapEffect = .DISCARD,
  60. }
  61. dxgi_factory->CreateSwapChainForHwnd(device, hwnd, &swapchain_desc, nil, nil, &s.swapchain)
  62. framebuffer: ^D3D11.ITexture2D
  63. s.swapchain->GetBuffer(0, D3D11.ITexture2D_UUID, (^rawptr)(&framebuffer))
  64. device->CreateRenderTargetView(framebuffer, nil, &s.framebuffer_view)
  65. depth_buffer_desc: D3D11.TEXTURE2D_DESC
  66. framebuffer->GetDesc(&depth_buffer_desc)
  67. depth_buffer_desc.Format = .D24_UNORM_S8_UINT
  68. depth_buffer_desc.BindFlags = {.DEPTH_STENCIL}
  69. depth_buffer: ^D3D11.ITexture2D
  70. device->CreateTexture2D(&depth_buffer_desc, nil, &depth_buffer)
  71. device->CreateDepthStencilView(depth_buffer, nil, &s.depth_buffer_view)
  72. return s
  73. }
  74. s: ^State
  75. State :: struct {
  76. swapchain: ^DXGI.ISwapChain1,
  77. framebuffer_view: ^D3D11.IRenderTargetView,
  78. depth_buffer_view: ^D3D11.IDepthStencilView,
  79. device_context: ^D3D11.IDeviceContext,
  80. run: bool,
  81. custom_context: runtime.Context,
  82. keys_went_down: #sparse [Keyboard_Key]bool,
  83. keys_went_up: #sparse [Keyboard_Key]bool,
  84. keys_is_held: #sparse [Keyboard_Key]bool,
  85. }
  86. VK_MAP := [255]Keyboard_Key {
  87. win.VK_A = .A,
  88. win.VK_B = .B,
  89. win.VK_C = .C,
  90. win.VK_D = .D,
  91. win.VK_E = .E,
  92. win.VK_F = .F,
  93. win.VK_G = .G,
  94. win.VK_H = .H,
  95. win.VK_I = .I,
  96. win.VK_J = .J,
  97. win.VK_K = .K,
  98. win.VK_L = .L,
  99. win.VK_M = .M,
  100. win.VK_N = .N,
  101. win.VK_O = .O,
  102. win.VK_P = .P,
  103. win.VK_Q = .Q,
  104. win.VK_R = .R,
  105. win.VK_S = .S,
  106. win.VK_T = .T,
  107. win.VK_U = .U,
  108. win.VK_V = .V,
  109. win.VK_W = .W,
  110. win.VK_X = .X,
  111. win.VK_Y = .Y,
  112. win.VK_Z = .Z,
  113. win.VK_LEFT = .Left,
  114. win.VK_RIGHT = .Right,
  115. win.VK_UP = .Up,
  116. win.VK_DOWN = .Down,
  117. }
  118. window_proc :: proc "stdcall" (hwnd: win.HWND, msg: win.UINT, wparam: win.WPARAM, lparam: win.LPARAM) -> win.LRESULT {
  119. context = s.custom_context
  120. switch msg {
  121. case win.WM_DESTROY:
  122. win.PostQuitMessage(0)
  123. s.run = false
  124. case win.WM_CLOSE:
  125. s.run = false
  126. case win.WM_KEYDOWN:
  127. key := VK_MAP[wparam]
  128. s.keys_went_down[key] = true
  129. s.keys_is_held[key] = true
  130. case win.WM_KEYUP:
  131. key := VK_MAP[wparam]
  132. s.keys_is_held[key] = false
  133. s.keys_went_up[key] = true
  134. }
  135. return win.DefWindowProcW(hwnd, msg, wparam, lparam)
  136. }
  137. _shutdown :: proc() {
  138. }
  139. _set_internal_state :: proc(new_state: ^State) {
  140. s = new_state
  141. }
  142. Color_F32 :: [4]f32
  143. f32_color_from_color :: proc(color: Color) -> Color_F32 {
  144. return {
  145. f32(color.r) / 255,
  146. f32(color.g) / 255,
  147. f32(color.b) / 255,
  148. f32(color.a) / 255,
  149. }
  150. }
  151. _clear :: proc(color: Color) {
  152. c := f32_color_from_color(color)
  153. s.device_context->ClearRenderTargetView(s.framebuffer_view, &c)
  154. s.device_context->ClearDepthStencilView(s.depth_buffer_view, {.DEPTH}, 1, 0)
  155. }
  156. _load_texture :: proc(filename: string) -> Texture {
  157. return {}
  158. }
  159. _destroy_texture :: proc(tex: Texture) {
  160. }
  161. _draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE) {
  162. }
  163. _draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
  164. }
  165. _draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rot: f32, tint := WHITE) {
  166. }
  167. _draw_rectangle :: proc(rect: Rect, color: Color) {
  168. }
  169. _draw_rectangle_outline :: proc(rect: Rect, thickness: f32, color: Color) {
  170. }
  171. _draw_circle :: proc(center: Vec2, radius: f32, color: Color) {
  172. }
  173. _draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) {
  174. }
  175. _get_screen_width :: proc() -> int {
  176. return 0
  177. }
  178. _get_screen_height :: proc() -> int {
  179. return 0
  180. }
  181. _key_went_down :: proc(key: Keyboard_Key) -> bool {
  182. return s.keys_went_down[key]
  183. }
  184. _key_went_up :: proc(key: Keyboard_Key) -> bool {
  185. return s.keys_went_up[key]
  186. }
  187. _key_is_held :: proc(key: Keyboard_Key) -> bool {
  188. return s.keys_is_held[key]
  189. }
  190. _window_should_close :: proc() -> bool {
  191. return !s.run
  192. }
  193. _draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  194. }
  195. _mouse_button_pressed :: proc(button: Mouse_Button) -> bool {
  196. return false
  197. }
  198. _mouse_button_released :: proc(button: Mouse_Button) -> bool {
  199. return false
  200. }
  201. _mouse_button_held :: proc(button: Mouse_Button) -> bool {
  202. return false
  203. }
  204. _mouse_wheel_delta :: proc() -> f32 {
  205. return 0
  206. }
  207. _mouse_position :: proc() -> Vec2 {
  208. return {}
  209. }
  210. _enable_scissor :: proc(x, y, w, h: int) {
  211. }
  212. _disable_scissor :: proc() {
  213. }
  214. _set_window_size :: proc(width: int, height: int) {
  215. }
  216. _set_window_position :: proc(x: int, y: int) {
  217. }
  218. _screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  219. return pos
  220. }
  221. _set_camera :: proc(camera: Maybe(Camera)) {
  222. }
  223. _set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  224. }
  225. _set_shader :: proc(shader: Maybe(Shader)) {
  226. }
  227. _process_events :: proc() {
  228. s.keys_went_up = {}
  229. s.keys_went_down = {}
  230. msg: win.MSG
  231. for win.PeekMessageW(&msg, nil, 0, 0, win.PM_REMOVE) {
  232. win.TranslateMessage(&msg)
  233. win.DispatchMessageW(&msg)
  234. }
  235. }
  236. _flush :: proc() {
  237. }
  238. _present :: proc(do_flush := true) {
  239. s.swapchain->Present(1, {})
  240. }
  241. _load_shader :: proc(vs: string, fs: string) -> Shader {
  242. return {}
  243. }
  244. _destroy_shader :: proc(shader: Shader) {
  245. }
  246. _get_shader_location :: proc(shader: Shader, uniform_name: string) -> int {
  247. return 0
  248. }
  249. _set_shader_value_f32 :: proc(shader: Shader, loc: int, val: f32) {
  250. }
  251. _set_shader_value_vec2 :: proc(shader: Shader, loc: int, val: Vec2) {
  252. }
  253. temp_cstring :: proc(str: string) -> cstring {
  254. return strings.clone_to_cstring(str, context.temp_allocator)
  255. }