karl2d_windows.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. import "core:slice"
  12. import "core:mem"
  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. win.RegisterClassW(&cls)
  29. r: win.RECT
  30. r.right = i32(width)
  31. r.bottom = i32(height)
  32. style := win.WS_OVERLAPPEDWINDOW | win.WS_VISIBLE
  33. win.AdjustWindowRect(&r, style, false)
  34. hwnd := win.CreateWindowW(CLASS_NAME,
  35. win.utf8_to_wstring(title),
  36. style,
  37. 100, 10, r.right - r.left, r.bottom - r.top,
  38. nil, nil, instance, nil)
  39. assert(hwnd != nil, "Failed creating window")
  40. feature_levels := [?]D3D11.FEATURE_LEVEL{
  41. ._11_1,
  42. ._11_0,
  43. }
  44. base_device: ^D3D11.IDevice
  45. base_device_context: ^D3D11.IDeviceContext
  46. device_flags := D3D11.CREATE_DEVICE_FLAGS {
  47. .BGRA_SUPPORT,
  48. }
  49. when ODIN_DEBUG {
  50. device_flags += { .DEBUG }
  51. }
  52. ch(D3D11.CreateDevice(
  53. nil,
  54. .HARDWARE,
  55. nil,
  56. device_flags,
  57. &feature_levels[0], len(feature_levels),
  58. D3D11.SDK_VERSION, &base_device, nil, &base_device_context))
  59. ch(base_device->QueryInterface(D3D11.IInfoQueue_UUID, (^rawptr)(&s.info_queue)))
  60. ch(base_device->QueryInterface(D3D11.IDevice_UUID, (^rawptr)(&s.device)))
  61. ch(base_device_context->QueryInterface(D3D11.IDeviceContext_UUID, (^rawptr)(&s.device_context)))
  62. dxgi_device: ^DXGI.IDevice
  63. ch(s.device->QueryInterface(DXGI.IDevice_UUID, (^rawptr)(&dxgi_device)))
  64. base_device->Release()
  65. base_device_context->Release()
  66. dxgi_adapter: ^DXGI.IAdapter
  67. ch(dxgi_device->GetAdapter(&dxgi_adapter))
  68. dxgi_device->Release()
  69. dxgi_factory: ^DXGI.IFactory2
  70. ch(dxgi_adapter->GetParent(DXGI.IFactory2_UUID, (^rawptr)(&dxgi_factory)))
  71. swapchain_desc := DXGI.SWAP_CHAIN_DESC1 {
  72. Format = .B8G8R8A8_UNORM,
  73. SampleDesc = {
  74. Count = 1,
  75. },
  76. BufferUsage = {.RENDER_TARGET_OUTPUT},
  77. BufferCount = 2,
  78. Scaling = .STRETCH,
  79. SwapEffect = .DISCARD,
  80. }
  81. ch(dxgi_factory->CreateSwapChainForHwnd(s.device, hwnd, &swapchain_desc, nil, nil, &s.swapchain))
  82. ch(s.swapchain->GetBuffer(0, D3D11.ITexture2D_UUID, (^rawptr)(&s.framebuffer)))
  83. ch(s.device->CreateRenderTargetView(s.framebuffer, nil, &s.framebuffer_view))
  84. depth_buffer_desc: D3D11.TEXTURE2D_DESC
  85. s.framebuffer->GetDesc(&depth_buffer_desc)
  86. depth_buffer_desc.Format = .D24_UNORM_S8_UINT
  87. depth_buffer_desc.BindFlags = {.DEPTH_STENCIL}
  88. ch(s.device->CreateTexture2D(&depth_buffer_desc, nil, &s.depth_buffer))
  89. ch(s.device->CreateDepthStencilView(s.depth_buffer, nil, &s.depth_buffer_view))
  90. vs_blob: ^D3D11.IBlob
  91. vs_blob_errors: ^D3D11.IBlob
  92. ch(D3D.Compile(raw_data(shader_hlsl), len(shader_hlsl), "shader.hlsl", nil, nil, "vs_main", "vs_5_0", 0, 0, &vs_blob, &vs_blob_errors))
  93. if vs_blob_errors != nil {
  94. log.error("Failed compiling shader:")
  95. log.error(strings.string_from_ptr((^u8)(vs_blob_errors->GetBufferPointer()), int(vs_blob_errors->GetBufferSize())))
  96. }
  97. ch(s.device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nil, &s.vertex_shader))
  98. input_element_desc := [?]D3D11.INPUT_ELEMENT_DESC{
  99. { "POS", 0, .R32G32_FLOAT, 0, 0, .VERTEX_DATA, 0 },
  100. { "COL", 0, .R8G8B8A8_UNORM , 0, D3D11.APPEND_ALIGNED_ELEMENT, .VERTEX_DATA, 0 },
  101. }
  102. ch(s.device->CreateInputLayout(&input_element_desc[0], len(input_element_desc), vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), &s.input_layout))
  103. ps_blob: ^D3D11.IBlob
  104. ps_blob_errors: ^D3D11.IBlob
  105. ch(D3D.Compile(raw_data(shader_hlsl), len(shader_hlsl), "shader.hlsl", nil, nil, "ps_main", "ps_5_0", 0, 0, &ps_blob, &ps_blob_errors))
  106. if ps_blob_errors != nil {
  107. log.error("Failed compiling shader:")
  108. log.error(strings.string_from_ptr((^u8)(ps_blob_errors->GetBufferPointer()), int(ps_blob_errors->GetBufferSize())))
  109. }
  110. ch(s.device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), nil, &s.pixel_shader))
  111. rasterizer_desc := D3D11.RASTERIZER_DESC{
  112. FillMode = .SOLID,
  113. CullMode = .BACK,
  114. }
  115. ch(s.device->CreateRasterizerState(&rasterizer_desc, &s.rasterizer_state))
  116. depth_stencil_desc := D3D11.DEPTH_STENCIL_DESC{
  117. DepthEnable = false,
  118. DepthWriteMask = .ALL,
  119. DepthFunc = .LESS,
  120. }
  121. ch(s.device->CreateDepthStencilState(&depth_stencil_desc, &s.depth_stencil_state))
  122. constant_buffer_desc := D3D11.BUFFER_DESC{
  123. ByteWidth = size_of(Constants),
  124. Usage = .DYNAMIC,
  125. BindFlags = {.CONSTANT_BUFFER},
  126. CPUAccessFlags = {.WRITE},
  127. }
  128. ch(s.device->CreateBuffer(&constant_buffer_desc, nil, &s.constant_buffer))
  129. vertex_buffer_desc := D3D11.BUFFER_DESC{
  130. ByteWidth = VERTEX_BUFFER_MAX * size_of(Vertex),
  131. Usage = .DYNAMIC,
  132. BindFlags = {.VERTEX_BUFFER},
  133. CPUAccessFlags = {.WRITE},
  134. }
  135. ch(s.device->CreateBuffer(&vertex_buffer_desc, nil, &s.vertex_buffer_gpu))
  136. s.vertex_buffer_cpu = make([]Vertex, VERTEX_BUFFER_MAX)
  137. s.proj_matrix = make_default_projection(s.width, s.height)
  138. return s
  139. }
  140. shader_hlsl :: #load("shader.hlsl")
  141. Vertex :: struct {
  142. pos: Vec2,
  143. color: Color,
  144. }
  145. s: ^State
  146. VERTEX_BUFFER_MAX :: 10000
  147. State :: struct {
  148. swapchain: ^DXGI.ISwapChain1,
  149. framebuffer_view: ^D3D11.IRenderTargetView,
  150. depth_buffer_view: ^D3D11.IDepthStencilView,
  151. device_context: ^D3D11.IDeviceContext,
  152. constant_buffer: ^D3D11.IBuffer,
  153. vertex_shader: ^D3D11.IVertexShader,
  154. pixel_shader: ^D3D11.IPixelShader,
  155. depth_stencil_state: ^D3D11.IDepthStencilState,
  156. rasterizer_state: ^D3D11.IRasterizerState,
  157. input_layout: ^D3D11.IInputLayout,
  158. device: ^D3D11.IDevice,
  159. depth_buffer: ^D3D11.ITexture2D,
  160. framebuffer: ^D3D11.ITexture2D,
  161. info_queue: ^D3D11.IInfoQueue,
  162. vertex_buffer_gpu: ^D3D11.IBuffer,
  163. vertex_buffer_cpu: []Vertex,
  164. vertex_buffer_cpu_count: int,
  165. run: bool,
  166. custom_context: runtime.Context,
  167. width: int,
  168. height: int,
  169. keys_went_down: #sparse [Keyboard_Key]bool,
  170. keys_went_up: #sparse [Keyboard_Key]bool,
  171. keys_is_held: #sparse [Keyboard_Key]bool,
  172. proj_matrix: matrix[4,4]f32,
  173. }
  174. VK_MAP := [255]Keyboard_Key {
  175. win.VK_A = .A,
  176. win.VK_B = .B,
  177. win.VK_C = .C,
  178. win.VK_D = .D,
  179. win.VK_E = .E,
  180. win.VK_F = .F,
  181. win.VK_G = .G,
  182. win.VK_H = .H,
  183. win.VK_I = .I,
  184. win.VK_J = .J,
  185. win.VK_K = .K,
  186. win.VK_L = .L,
  187. win.VK_M = .M,
  188. win.VK_N = .N,
  189. win.VK_O = .O,
  190. win.VK_P = .P,
  191. win.VK_Q = .Q,
  192. win.VK_R = .R,
  193. win.VK_S = .S,
  194. win.VK_T = .T,
  195. win.VK_U = .U,
  196. win.VK_V = .V,
  197. win.VK_W = .W,
  198. win.VK_X = .X,
  199. win.VK_Y = .Y,
  200. win.VK_Z = .Z,
  201. win.VK_LEFT = .Left,
  202. win.VK_RIGHT = .Right,
  203. win.VK_UP = .Up,
  204. win.VK_DOWN = .Down,
  205. }
  206. window_proc :: proc "stdcall" (hwnd: win.HWND, msg: win.UINT, wparam: win.WPARAM, lparam: win.LPARAM) -> win.LRESULT {
  207. context = s.custom_context
  208. switch msg {
  209. case win.WM_DESTROY:
  210. win.PostQuitMessage(0)
  211. s.run = false
  212. case win.WM_CLOSE:
  213. s.run = false
  214. case win.WM_KEYDOWN:
  215. key := VK_MAP[wparam]
  216. s.keys_went_down[key] = true
  217. s.keys_is_held[key] = true
  218. case win.WM_KEYUP:
  219. key := VK_MAP[wparam]
  220. s.keys_is_held[key] = false
  221. s.keys_went_up[key] = true
  222. }
  223. return win.DefWindowProcW(hwnd, msg, wparam, lparam)
  224. }
  225. _shutdown :: proc() {
  226. s.framebuffer_view->Release()
  227. s.depth_buffer_view->Release()
  228. s.depth_buffer->Release()
  229. s.framebuffer->Release()
  230. s.device_context->Release()
  231. s.vertex_buffer_gpu->Release()
  232. s.constant_buffer->Release()
  233. s.vertex_shader->Release()
  234. s.pixel_shader->Release()
  235. s.depth_stencil_state->Release()
  236. s.rasterizer_state->Release()
  237. s.input_layout->Release()
  238. s.swapchain->Release()
  239. when ODIN_DEBUG {
  240. debug: ^D3D11.IDebug
  241. if ch(s.device->QueryInterface(D3D11.IDebug_UUID, (^rawptr)(&debug))) >= 0 {
  242. ch(debug->ReportLiveDeviceObjects({.DETAIL, .IGNORE_INTERNAL}))
  243. log_messages()
  244. }
  245. debug->Release()
  246. }
  247. s.device->Release()
  248. s.info_queue->Release()
  249. }
  250. _set_internal_state :: proc(new_state: ^State) {
  251. s = new_state
  252. }
  253. Color_F32 :: [4]f32
  254. f32_color_from_color :: proc(color: Color) -> Color_F32 {
  255. return {
  256. f32(color.r) / 255,
  257. f32(color.g) / 255,
  258. f32(color.b) / 255,
  259. f32(color.a) / 255,
  260. }
  261. }
  262. _clear :: proc(color: Color) {
  263. c := f32_color_from_color(color)
  264. s.device_context->ClearRenderTargetView(s.framebuffer_view, &c)
  265. s.device_context->ClearDepthStencilView(s.depth_buffer_view, {.DEPTH}, 1, 0)
  266. }
  267. _load_texture :: proc(filename: string) -> Texture {
  268. return {}
  269. }
  270. _destroy_texture :: proc(tex: Texture) {
  271. }
  272. _draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE) {
  273. _draw_texture_ex(
  274. tex,
  275. {0, 0, f32(tex.width), f32(tex.height)},
  276. {pos.x, pos.y, f32(tex.width), f32(tex.height)},
  277. {},
  278. 0,
  279. tint,
  280. )
  281. }
  282. _draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
  283. _draw_texture_ex(
  284. tex,
  285. rect,
  286. {pos.x, pos.y, rect.w, rect.h},
  287. {},
  288. 0,
  289. tint,
  290. )
  291. }
  292. add_vertex :: proc(v: Vec2, color: Color) {
  293. if s.vertex_buffer_cpu_count == len(s.vertex_buffer_cpu) {
  294. panic("Must dispatch here")
  295. }
  296. s.vertex_buffer_cpu[s.vertex_buffer_cpu_count] = {
  297. pos = v,
  298. color = color,
  299. }
  300. s.vertex_buffer_cpu_count += 1
  301. }
  302. _draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rot: f32, tint := WHITE) {
  303. p := Vec2 {
  304. dst.x, dst.y,
  305. }
  306. p -= origin
  307. add_vertex({p.x, p.y}, tint)
  308. add_vertex({p.x + dst.w, p.y}, tint)
  309. add_vertex({p.x + dst.w, p.y + dst.h}, tint)
  310. add_vertex({p.x, p.y}, tint)
  311. add_vertex({p.x + dst.w, p.y + dst.h}, tint)
  312. add_vertex({p.x, p.y + dst.h}, tint)
  313. }
  314. _draw_rectangle :: proc(r: Rect, color: Color) {
  315. add_vertex({r.x, r.y}, color)
  316. add_vertex({r.x + r.w, r.y}, color)
  317. add_vertex({r.x + r.w, r.y + r.h}, color)
  318. add_vertex({r.x, r.y}, color)
  319. add_vertex({r.x + r.w, r.y + r.h}, color)
  320. add_vertex({r.x, r.y + r.h}, color)
  321. }
  322. _draw_rectangle_outline :: proc(rect: Rect, thickness: f32, color: Color) {
  323. }
  324. _draw_circle :: proc(center: Vec2, radius: f32, color: Color) {
  325. }
  326. _draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) {
  327. }
  328. _get_screen_width :: proc() -> int {
  329. return 0
  330. }
  331. _get_screen_height :: proc() -> int {
  332. return 0
  333. }
  334. _key_went_down :: proc(key: Keyboard_Key) -> bool {
  335. return s.keys_went_down[key]
  336. }
  337. _key_went_up :: proc(key: Keyboard_Key) -> bool {
  338. return s.keys_went_up[key]
  339. }
  340. _key_is_held :: proc(key: Keyboard_Key) -> bool {
  341. return s.keys_is_held[key]
  342. }
  343. _window_should_close :: proc() -> bool {
  344. return !s.run
  345. }
  346. _draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  347. }
  348. _mouse_button_pressed :: proc(button: Mouse_Button) -> bool {
  349. return false
  350. }
  351. _mouse_button_released :: proc(button: Mouse_Button) -> bool {
  352. return false
  353. }
  354. _mouse_button_held :: proc(button: Mouse_Button) -> bool {
  355. return false
  356. }
  357. _mouse_wheel_delta :: proc() -> f32 {
  358. return 0
  359. }
  360. _mouse_position :: proc() -> Vec2 {
  361. return {}
  362. }
  363. _enable_scissor :: proc(x, y, w, h: int) {
  364. }
  365. _disable_scissor :: proc() {
  366. }
  367. _set_window_size :: proc(width: int, height: int) {
  368. }
  369. _set_window_position :: proc(x: int, y: int) {
  370. }
  371. _screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  372. return pos
  373. }
  374. _set_camera :: proc(camera: Maybe(Camera)) {
  375. if c, c_ok := camera.?; c_ok {
  376. s.proj_matrix = make_default_projection(s.width, s.height)
  377. s.proj_matrix[0, 0] *= c.zoom
  378. s.proj_matrix[1, 1] *= c.zoom
  379. } else {
  380. s.proj_matrix = make_default_projection(s.width, s.height)
  381. }
  382. }
  383. _set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  384. }
  385. _set_shader :: proc(shader: Maybe(Shader)) {
  386. }
  387. _process_events :: proc() {
  388. s.keys_went_up = {}
  389. s.keys_went_down = {}
  390. msg: win.MSG
  391. for win.PeekMessageW(&msg, nil, 0, 0, win.PM_REMOVE) {
  392. win.TranslateMessage(&msg)
  393. win.DispatchMessageW(&msg)
  394. }
  395. }
  396. _flush :: proc() {
  397. }
  398. Constants :: struct #align (16) {
  399. projection: matrix[4, 4]f32,
  400. }
  401. make_default_projection :: proc(w, h: int) -> matrix[4,4]f32 {
  402. return linalg.matrix_ortho3d_f32(0, f32(w), f32(h), 0, 0.001, 2)
  403. }
  404. _present :: proc(do_flush := true) {
  405. viewport := D3D11.VIEWPORT{
  406. 0, 0,
  407. f32(s.width), f32(s.height),
  408. 0, 1,
  409. }
  410. dc := s.device_context
  411. vb_data: D3D11.MAPPED_SUBRESOURCE
  412. ch(dc->Map(s.vertex_buffer_gpu, 0, .WRITE_NO_OVERWRITE, {}, &vb_data))
  413. {
  414. gpu_map := slice.from_ptr((^Vertex)(vb_data.pData), VERTEX_BUFFER_MAX)
  415. copy(gpu_map, s.vertex_buffer_cpu[:s.vertex_buffer_cpu_count])
  416. }
  417. dc->Unmap(s.vertex_buffer_gpu, 0)
  418. cb_data: D3D11.MAPPED_SUBRESOURCE
  419. ch(dc->Map(s.constant_buffer, 0, .WRITE_DISCARD, {}, &cb_data))
  420. {
  421. constants := (^Constants)(cb_data.pData)
  422. constants.projection = s.proj_matrix
  423. }
  424. dc->Unmap(s.constant_buffer, 0)
  425. dc->IASetPrimitiveTopology(.TRIANGLELIST)
  426. dc->IASetInputLayout(s.input_layout)
  427. vertex_buffer_offset := u32(0)
  428. vertex_buffer_stride := u32(size_of(Vertex))
  429. dc->IASetVertexBuffers(0, 1, &s.vertex_buffer_gpu, &vertex_buffer_stride, &vertex_buffer_offset)
  430. dc->VSSetShader(s.vertex_shader, nil, 0)
  431. dc->VSSetConstantBuffers(0, 1, &s.constant_buffer)
  432. dc->RSSetViewports(1, &viewport)
  433. dc->RSSetState(s.rasterizer_state)
  434. dc->PSSetShader(s.pixel_shader, nil, 0)
  435. dc->OMSetRenderTargets(1, &s.framebuffer_view, s.depth_buffer_view)
  436. dc->OMSetDepthStencilState(s.depth_stencil_state, 0)
  437. dc->OMSetBlendState(nil, nil, ~u32(0)) // use default blend mode (i.e. disable)
  438. dc->Draw(u32(s.vertex_buffer_cpu_count), 0)
  439. log_messages()
  440. ch(s.swapchain->Present(1, {}))
  441. s.vertex_buffer_cpu_count = 0
  442. }
  443. _load_shader :: proc(vs: string, fs: string) -> Shader {
  444. return {}
  445. }
  446. _destroy_shader :: proc(shader: Shader) {
  447. }
  448. _get_shader_location :: proc(shader: Shader, uniform_name: string) -> int {
  449. return 0
  450. }
  451. _set_shader_value_f32 :: proc(shader: Shader, loc: int, val: f32) {
  452. }
  453. _set_shader_value_vec2 :: proc(shader: Shader, loc: int, val: Vec2) {
  454. }
  455. temp_cstring :: proc(str: string, loc := #caller_location) -> cstring {
  456. return strings.clone_to_cstring(str, context.temp_allocator)
  457. }
  458. // CHeck win errors and print message log if there is any error
  459. ch :: proc(hr: win.HRESULT, loc := #caller_location) -> win.HRESULT {
  460. if hr >= 0 {
  461. return hr
  462. }
  463. log.errorf("D3D11 error: %0x", u32(hr), location = loc)
  464. log_messages(loc)
  465. return hr
  466. }
  467. log_messages :: proc(loc := #caller_location) {
  468. iq := s.info_queue
  469. if iq == nil {
  470. return
  471. }
  472. n := iq->GetNumStoredMessages()
  473. longest_msg: D3D11.SIZE_T
  474. for i in 0..=n {
  475. msglen: D3D11.SIZE_T
  476. iq->GetMessage(i, nil, &msglen)
  477. if msglen > longest_msg {
  478. longest_msg = msglen
  479. }
  480. }
  481. if longest_msg > 0 {
  482. msg_raw_ptr, _ := (mem.alloc(int(longest_msg), allocator = context.temp_allocator))
  483. for i in 0..=n {
  484. msglen: D3D11.SIZE_T
  485. iq->GetMessage(i, nil, &msglen)
  486. if msglen > 0 {
  487. msg := (^D3D11.MESSAGE)(msg_raw_ptr)
  488. iq->GetMessage(i, msg, &msglen)
  489. log.error(msg.pDescription, location = loc)
  490. }
  491. }
  492. }
  493. iq->ClearStoredMessages()
  494. }