karl2d_windows.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 D3D "vendor:directx/d3d_compiler"
  8. import "core:strings"
  9. import "core:log"
  10. import "core:math/linalg"
  11. _ :: log
  12. //import "core:math"
  13. _init :: proc(width: int, height: int, title: string,
  14. allocator := context.allocator, loc := #caller_location) -> ^State {
  15. s = new(State, allocator, loc)
  16. s.custom_context = context
  17. CLASS_NAME :: "karl2d"
  18. instance := win.HINSTANCE(win.GetModuleHandleW(nil))
  19. s.run = true
  20. s.width = width
  21. s.height = height
  22. cls := win.WNDCLASSW {
  23. lpfnWndProc = window_proc,
  24. lpszClassName = CLASS_NAME,
  25. hInstance = instance,
  26. hCursor = win.LoadCursorA(nil, win.IDC_ARROW),
  27. }
  28. _ = cls
  29. class := win.RegisterClassW(&cls)
  30. _ = class
  31. r: win.RECT
  32. r.right = i32(width)
  33. r.bottom = i32(height)
  34. style := win.WS_OVERLAPPEDWINDOW | win.WS_VISIBLE
  35. win.AdjustWindowRect(&r, style, false)
  36. hwnd := win.CreateWindowW(CLASS_NAME,
  37. win.utf8_to_wstring(title),
  38. style,
  39. 100, 10, r.right - r.left, r.bottom - r.top,
  40. nil, nil, instance, nil)
  41. feature_levels := [?]D3D11.FEATURE_LEVEL{ ._11_0 }
  42. base_device: ^D3D11.IDevice
  43. base_device_context: ^D3D11.IDeviceContext
  44. D3D11.CreateDevice(
  45. nil,
  46. .HARDWARE,
  47. nil,
  48. {.BGRA_SUPPORT},
  49. &feature_levels[0], len(feature_levels),
  50. D3D11.SDK_VERSION, &base_device, nil, &base_device_context)
  51. device: ^D3D11.IDevice
  52. base_device->QueryInterface(D3D11.IDevice_UUID, (^rawptr)(&device))
  53. base_device_context->QueryInterface(D3D11.IDeviceContext_UUID, (^rawptr)(&s.device_context))
  54. dxgi_device: ^DXGI.IDevice
  55. device->QueryInterface(DXGI.IDevice_UUID, (^rawptr)(&dxgi_device))
  56. dxgi_adapter: ^DXGI.IAdapter
  57. dxgi_device->GetAdapter(&dxgi_adapter)
  58. dxgi_factory: ^DXGI.IFactory2
  59. dxgi_adapter->GetParent(DXGI.IFactory2_UUID, (^rawptr)(&dxgi_factory))
  60. swapchain_desc := DXGI.SWAP_CHAIN_DESC1 {
  61. Format = .B8G8R8A8_UNORM,
  62. SampleDesc = {
  63. Count = 1,
  64. },
  65. BufferUsage = {.RENDER_TARGET_OUTPUT},
  66. BufferCount = 2,
  67. Scaling = .STRETCH,
  68. SwapEffect = .DISCARD,
  69. }
  70. dxgi_factory->CreateSwapChainForHwnd(device, hwnd, &swapchain_desc, nil, nil, &s.swapchain)
  71. framebuffer: ^D3D11.ITexture2D
  72. s.swapchain->GetBuffer(0, D3D11.ITexture2D_UUID, (^rawptr)(&framebuffer))
  73. device->CreateRenderTargetView(framebuffer, nil, &s.framebuffer_view)
  74. depth_buffer_desc: D3D11.TEXTURE2D_DESC
  75. framebuffer->GetDesc(&depth_buffer_desc)
  76. depth_buffer_desc.Format = .D24_UNORM_S8_UINT
  77. depth_buffer_desc.BindFlags = {.DEPTH_STENCIL}
  78. depth_buffer: ^D3D11.ITexture2D
  79. device->CreateTexture2D(&depth_buffer_desc, nil, &depth_buffer)
  80. device->CreateDepthStencilView(depth_buffer, nil, &s.depth_buffer_view)
  81. //////////
  82. vs_blob: ^D3D11.IBlob
  83. D3D.Compile(raw_data(shader_hlsl), len(shader_hlsl), "shader.hlsl", nil, nil, "vs_main", "vs_5_0", 0, 0, &vs_blob, nil)
  84. assert(vs_blob != nil)
  85. device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nil, &vertex_shader)
  86. input_element_desc := [?]D3D11.INPUT_ELEMENT_DESC{
  87. { "POS", 0, .R32G32B32_FLOAT, 0, 0, .VERTEX_DATA, 0 },
  88. }
  89. device->CreateInputLayout(&input_element_desc[0], len(input_element_desc), vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), &input_layout)
  90. ps_blob: ^D3D11.IBlob
  91. D3D.Compile(raw_data(shader_hlsl), len(shader_hlsl), "shader.hlsl", nil, nil, "ps_main", "ps_5_0", 0, 0, &ps_blob, nil)
  92. device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), nil, &pixel_shader)
  93. //////////
  94. rasterizer_desc := D3D11.RASTERIZER_DESC{
  95. FillMode = .SOLID,
  96. CullMode = .BACK,
  97. }
  98. device->CreateRasterizerState(&rasterizer_desc, &rasterizer_state)
  99. depth_stencil_desc := D3D11.DEPTH_STENCIL_DESC{
  100. DepthEnable = true,
  101. DepthWriteMask = .ALL,
  102. DepthFunc = .LESS,
  103. }
  104. device->CreateDepthStencilState(&depth_stencil_desc, &depth_stencil_state)
  105. constant_buffer_desc := D3D11.BUFFER_DESC{
  106. ByteWidth = size_of(Constants),
  107. Usage = .DYNAMIC,
  108. BindFlags = {.CONSTANT_BUFFER},
  109. CPUAccessFlags = {.WRITE},
  110. }
  111. device->CreateBuffer(&constant_buffer_desc, nil, &constant_buffer)
  112. vertex_data := [?]Vec3 {
  113. {0, 320, 0.1},
  114. {320, 320, 0.1},
  115. {0, 0, 0.1},
  116. }
  117. index_data := [?]u32 {
  118. 0, 1, 2,
  119. }
  120. vertex_buffer_desc := D3D11.BUFFER_DESC{
  121. ByteWidth = size_of(vertex_data),
  122. Usage = .IMMUTABLE,
  123. BindFlags = {.VERTEX_BUFFER},
  124. }
  125. device->CreateBuffer(&vertex_buffer_desc, &D3D11.SUBRESOURCE_DATA{pSysMem = &vertex_data[0], SysMemPitch = size_of(vertex_data)}, &s.vertex_buffer)
  126. index_buffer_desc := D3D11.BUFFER_DESC{
  127. ByteWidth = size_of(index_data),
  128. Usage = .IMMUTABLE,
  129. BindFlags = {.INDEX_BUFFER},
  130. }
  131. device->CreateBuffer(&index_buffer_desc, &D3D11.SUBRESOURCE_DATA{pSysMem = &index_data[0], SysMemPitch = size_of(index_data)}, &s.index_buffer)
  132. s.proj_matrix = make_default_projection(s.width, s.height)
  133. return s
  134. }
  135. Vec3 :: [3]f32
  136. shader_hlsl :: #load("shader.hlsl")
  137. s: ^State
  138. constant_buffer: ^D3D11.IBuffer
  139. vertex_shader: ^D3D11.IVertexShader
  140. pixel_shader: ^D3D11.IPixelShader
  141. depth_stencil_state: ^D3D11.IDepthStencilState
  142. rasterizer_state: ^D3D11.IRasterizerState
  143. input_layout: ^D3D11.IInputLayout
  144. State :: struct {
  145. swapchain: ^DXGI.ISwapChain1,
  146. framebuffer_view: ^D3D11.IRenderTargetView,
  147. depth_buffer_view: ^D3D11.IDepthStencilView,
  148. device_context: ^D3D11.IDeviceContext,
  149. vertex_buffer: ^D3D11.IBuffer,
  150. index_buffer: ^D3D11.IBuffer,
  151. run: bool,
  152. custom_context: runtime.Context,
  153. width: int,
  154. height: int,
  155. keys_went_down: #sparse [Keyboard_Key]bool,
  156. keys_went_up: #sparse [Keyboard_Key]bool,
  157. keys_is_held: #sparse [Keyboard_Key]bool,
  158. proj_matrix: matrix[4,4]f32,
  159. }
  160. VK_MAP := [255]Keyboard_Key {
  161. win.VK_A = .A,
  162. win.VK_B = .B,
  163. win.VK_C = .C,
  164. win.VK_D = .D,
  165. win.VK_E = .E,
  166. win.VK_F = .F,
  167. win.VK_G = .G,
  168. win.VK_H = .H,
  169. win.VK_I = .I,
  170. win.VK_J = .J,
  171. win.VK_K = .K,
  172. win.VK_L = .L,
  173. win.VK_M = .M,
  174. win.VK_N = .N,
  175. win.VK_O = .O,
  176. win.VK_P = .P,
  177. win.VK_Q = .Q,
  178. win.VK_R = .R,
  179. win.VK_S = .S,
  180. win.VK_T = .T,
  181. win.VK_U = .U,
  182. win.VK_V = .V,
  183. win.VK_W = .W,
  184. win.VK_X = .X,
  185. win.VK_Y = .Y,
  186. win.VK_Z = .Z,
  187. win.VK_LEFT = .Left,
  188. win.VK_RIGHT = .Right,
  189. win.VK_UP = .Up,
  190. win.VK_DOWN = .Down,
  191. }
  192. window_proc :: proc "stdcall" (hwnd: win.HWND, msg: win.UINT, wparam: win.WPARAM, lparam: win.LPARAM) -> win.LRESULT {
  193. context = s.custom_context
  194. switch msg {
  195. case win.WM_DESTROY:
  196. win.PostQuitMessage(0)
  197. s.run = false
  198. case win.WM_CLOSE:
  199. s.run = false
  200. case win.WM_KEYDOWN:
  201. key := VK_MAP[wparam]
  202. s.keys_went_down[key] = true
  203. s.keys_is_held[key] = true
  204. case win.WM_KEYUP:
  205. key := VK_MAP[wparam]
  206. s.keys_is_held[key] = false
  207. s.keys_went_up[key] = true
  208. }
  209. return win.DefWindowProcW(hwnd, msg, wparam, lparam)
  210. }
  211. _shutdown :: proc() {
  212. }
  213. _set_internal_state :: proc(new_state: ^State) {
  214. s = new_state
  215. }
  216. Color_F32 :: [4]f32
  217. f32_color_from_color :: proc(color: Color) -> Color_F32 {
  218. return {
  219. f32(color.r) / 255,
  220. f32(color.g) / 255,
  221. f32(color.b) / 255,
  222. f32(color.a) / 255,
  223. }
  224. }
  225. _clear :: proc(color: Color) {
  226. c := f32_color_from_color(color)
  227. s.device_context->ClearRenderTargetView(s.framebuffer_view, &c)
  228. s.device_context->ClearDepthStencilView(s.depth_buffer_view, {.DEPTH}, 1, 0)
  229. }
  230. _load_texture :: proc(filename: string) -> Texture {
  231. return {}
  232. }
  233. _destroy_texture :: proc(tex: Texture) {
  234. }
  235. _draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE) {
  236. }
  237. _draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
  238. }
  239. _draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rot: f32, tint := WHITE) {
  240. }
  241. _draw_rectangle :: proc(rect: Rect, color: Color) {
  242. }
  243. _draw_rectangle_outline :: proc(rect: Rect, thickness: f32, color: Color) {
  244. }
  245. _draw_circle :: proc(center: Vec2, radius: f32, color: Color) {
  246. }
  247. _draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) {
  248. }
  249. _get_screen_width :: proc() -> int {
  250. return 0
  251. }
  252. _get_screen_height :: proc() -> int {
  253. return 0
  254. }
  255. _key_went_down :: proc(key: Keyboard_Key) -> bool {
  256. return s.keys_went_down[key]
  257. }
  258. _key_went_up :: proc(key: Keyboard_Key) -> bool {
  259. return s.keys_went_up[key]
  260. }
  261. _key_is_held :: proc(key: Keyboard_Key) -> bool {
  262. return s.keys_is_held[key]
  263. }
  264. _window_should_close :: proc() -> bool {
  265. return !s.run
  266. }
  267. _draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  268. }
  269. _mouse_button_pressed :: proc(button: Mouse_Button) -> bool {
  270. return false
  271. }
  272. _mouse_button_released :: proc(button: Mouse_Button) -> bool {
  273. return false
  274. }
  275. _mouse_button_held :: proc(button: Mouse_Button) -> bool {
  276. return false
  277. }
  278. _mouse_wheel_delta :: proc() -> f32 {
  279. return 0
  280. }
  281. _mouse_position :: proc() -> Vec2 {
  282. return {}
  283. }
  284. _enable_scissor :: proc(x, y, w, h: int) {
  285. }
  286. _disable_scissor :: proc() {
  287. }
  288. _set_window_size :: proc(width: int, height: int) {
  289. }
  290. _set_window_position :: proc(x: int, y: int) {
  291. }
  292. _screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  293. return pos
  294. }
  295. _set_camera :: proc(camera: Maybe(Camera)) {
  296. if c, c_ok := camera.?; c_ok {
  297. s.proj_matrix = make_default_projection(s.width, s.height)
  298. s.proj_matrix[0, 0] *= c.zoom
  299. s.proj_matrix[1, 1] *= c.zoom
  300. } else {
  301. s.proj_matrix = make_default_projection(s.width, s.height)
  302. }
  303. }
  304. _set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  305. }
  306. _set_shader :: proc(shader: Maybe(Shader)) {
  307. }
  308. _process_events :: proc() {
  309. s.keys_went_up = {}
  310. s.keys_went_down = {}
  311. msg: win.MSG
  312. for win.PeekMessageW(&msg, nil, 0, 0, win.PM_REMOVE) {
  313. win.TranslateMessage(&msg)
  314. win.DispatchMessageW(&msg)
  315. }
  316. }
  317. _flush :: proc() {
  318. }
  319. Constants :: struct #align (16) {
  320. projection: matrix[4, 4]f32,
  321. }
  322. make_default_projection :: proc(w, h: int) -> matrix[4,4]f32 {
  323. return linalg.matrix_ortho3d_f32(0, f32(w), f32(h), 0, 0.001, 2)
  324. }
  325. _present :: proc(do_flush := true) {
  326. viewport := D3D11.VIEWPORT{
  327. 0, 0,
  328. f32(s.width), f32(s.height),
  329. 0, 1,
  330. }
  331. dc := s.device_context
  332. mapped_subresource: D3D11.MAPPED_SUBRESOURCE
  333. // matrix[0.002, 0, -0, -1; 0, 0.002, -0, -1; 0, 0, -1.0005003, -1.0010005; 0, 0, -0, 1]
  334. // matrix[0.002, 0, -0, -1; 0, 0.002, -0, 1; 0, 0, 1.0005002, -1.0010005; 0, 0, -0, 1]
  335. dc->Map(constant_buffer, 0, .WRITE_DISCARD, {}, &mapped_subresource)
  336. {
  337. constants := (^Constants)(mapped_subresource.pData)
  338. constants.projection = s.proj_matrix
  339. }
  340. dc->Unmap(constant_buffer, 0)
  341. dc->IASetPrimitiveTopology(.TRIANGLELIST)
  342. dc->IASetInputLayout(input_layout)
  343. vertex_buffer_offset := u32(0)
  344. vertex_buffer_stride := u32(3*4)
  345. dc->IASetVertexBuffers(0, 1, &s.vertex_buffer, &vertex_buffer_stride, &vertex_buffer_offset)
  346. dc->IASetIndexBuffer(s.index_buffer, .R32_UINT, 0)
  347. dc->VSSetShader(vertex_shader, nil, 0)
  348. dc->VSSetConstantBuffers(0, 1, &constant_buffer)
  349. dc->RSSetViewports(1, &viewport)
  350. dc->RSSetState(rasterizer_state)
  351. dc->PSSetShader(pixel_shader, nil, 0)
  352. dc->OMSetRenderTargets(1, &s.framebuffer_view, s.depth_buffer_view)
  353. dc->OMSetDepthStencilState(depth_stencil_state, 0)
  354. dc->OMSetBlendState(nil, nil, ~u32(0)) // use default blend mode (i.e. disable)
  355. dc->DrawIndexed(3, 0, 0)
  356. s.swapchain->Present(1, {})
  357. }
  358. _load_shader :: proc(vs: string, fs: string) -> Shader {
  359. return {}
  360. }
  361. _destroy_shader :: proc(shader: Shader) {
  362. }
  363. _get_shader_location :: proc(shader: Shader, uniform_name: string) -> int {
  364. return 0
  365. }
  366. _set_shader_value_f32 :: proc(shader: Shader, loc: int, val: f32) {
  367. }
  368. _set_shader_value_vec2 :: proc(shader: Shader, loc: int, val: Vec2) {
  369. }
  370. temp_cstring :: proc(str: string) -> cstring {
  371. return strings.clone_to_cstring(str, context.temp_allocator)
  372. }