sokol_glue.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #if defined(SOKOL_IMPL) && !defined(SOKOL_GLUE_IMPL)
  2. #define SOKOL_GLUE_IMPL
  3. #endif
  4. #ifndef SOKOL_GLUE_INCLUDED
  5. /*
  6. sokol_glue.h -- glue helper functions for sokol headers
  7. Project URL: https://github.com/floooh/sokol
  8. Do this:
  9. #define SOKOL_IMPL or
  10. #define SOKOL_GLUE_IMPL
  11. before you include this file in *one* C or C++ file to create the
  12. implementation.
  13. ...optionally provide the following macros to override defaults:
  14. SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
  15. SOKOL_GLUE_API_DECL - public function declaration prefix (default: extern)
  16. SOKOL_API_DECL - same as SOKOL_GLUE_API_DECL
  17. SOKOL_API_IMPL - public function implementation prefix (default: -)
  18. If sokol_glue.h is compiled as a DLL, define the following before
  19. including the declaration or implementation:
  20. SOKOL_DLL
  21. On Windows, SOKOL_DLL will define SOKOL_GLUE_API_DECL as __declspec(dllexport)
  22. or __declspec(dllimport) as needed.
  23. OVERVIEW
  24. ========
  25. sokol_glue.h provides glue helper functions between sokol_gfx.h and sokol_app.h,
  26. so that sokol_gfx.h doesn't need to depend on sokol_app.h but can be
  27. used with different window system glue libraries.
  28. PROVIDED FUNCTIONS
  29. ==================
  30. sg_environment sglue_environment(void)
  31. Returns an sg_environment struct initialized by calling sokol_app.h
  32. functions. Use this in the sg_setup() call like this:
  33. sg_setup(&(sg_desc){
  34. .environment = sglue_environment(),
  35. ...
  36. });
  37. sg_swapchain sglue_swapchain(void)
  38. Returns an sg_swapchain struct initialized by calling sokol_app.h
  39. functions. Use this in sg_begin_pass() for a 'swapchain pass' like
  40. this:
  41. sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain(), ... });
  42. LICENSE
  43. =======
  44. zlib/libpng license
  45. Copyright (c) 2018 Andre Weissflog
  46. This software is provided 'as-is', without any express or implied warranty.
  47. In no event will the authors be held liable for any damages arising from the
  48. use of this software.
  49. Permission is granted to anyone to use this software for any purpose,
  50. including commercial applications, and to alter it and redistribute it
  51. freely, subject to the following restrictions:
  52. 1. The origin of this software must not be misrepresented; you must not
  53. claim that you wrote the original software. If you use this software in a
  54. product, an acknowledgment in the product documentation would be
  55. appreciated but is not required.
  56. 2. Altered source versions must be plainly marked as such, and must not
  57. be misrepresented as being the original software.
  58. 3. This notice may not be removed or altered from any source
  59. distribution.
  60. */
  61. #define SOKOL_GLUE_INCLUDED
  62. #if defined(SOKOL_API_DECL) && !defined(SOKOL_GLUE_API_DECL)
  63. #define SOKOL_GLUE_API_DECL SOKOL_API_DECL
  64. #endif
  65. #ifndef SOKOL_GLUE_API_DECL
  66. #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GLUE_IMPL)
  67. #define SOKOL_GLUE_API_DECL __declspec(dllexport)
  68. #elif defined(_WIN32) && defined(SOKOL_DLL)
  69. #define SOKOL_GLUE_API_DECL __declspec(dllimport)
  70. #else
  71. #define SOKOL_GLUE_API_DECL extern
  72. #endif
  73. #endif
  74. #ifndef SOKOL_GFX_INCLUDED
  75. #error "Please include sokol_gfx.h before sokol_glue.h"
  76. #endif
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. SOKOL_GLUE_API_DECL sg_environment sglue_environment(void);
  81. SOKOL_GLUE_API_DECL sg_swapchain sglue_swapchain(void);
  82. #ifdef __cplusplus
  83. } /* extern "C" */
  84. #endif
  85. #endif /* SOKOL_GLUE_INCLUDED */
  86. /*-- IMPLEMENTATION ----------------------------------------------------------*/
  87. #ifdef SOKOL_GLUE_IMPL
  88. #define SOKOL_GLUE_IMPL_INCLUDED (1)
  89. #include <string.h> /* memset */
  90. #ifndef SOKOL_APP_INCLUDED
  91. #error "Please include sokol_app.h before the sokol_glue.h implementation"
  92. #endif
  93. #ifndef SOKOL_API_IMPL
  94. #define SOKOL_API_IMPL
  95. #endif
  96. SOKOL_API_IMPL sg_environment sglue_environment(void) {
  97. sg_environment env;
  98. memset(&env, 0, sizeof(env));
  99. env.defaults.color_format = (sg_pixel_format) sapp_color_format();
  100. env.defaults.depth_format = (sg_pixel_format) sapp_depth_format();
  101. env.defaults.sample_count = sapp_sample_count();
  102. env.metal.device = sapp_metal_get_device();
  103. env.d3d11.device = sapp_d3d11_get_device();
  104. env.d3d11.device_context = sapp_d3d11_get_device_context();
  105. env.wgpu.device = sapp_wgpu_get_device();
  106. return env;
  107. }
  108. SOKOL_API_IMPL sg_swapchain sglue_swapchain(void) {
  109. sg_swapchain swapchain;
  110. memset(&swapchain, 0, sizeof(swapchain));
  111. swapchain.width = sapp_width();
  112. swapchain.height = sapp_height();
  113. swapchain.sample_count = sapp_sample_count();
  114. swapchain.color_format = (sg_pixel_format)sapp_color_format();
  115. swapchain.depth_format = (sg_pixel_format)sapp_depth_format();
  116. swapchain.metal.current_drawable = sapp_metal_get_current_drawable();
  117. swapchain.metal.depth_stencil_texture = sapp_metal_get_depth_stencil_texture();
  118. swapchain.metal.msaa_color_texture = sapp_metal_get_msaa_color_texture();
  119. swapchain.d3d11.render_view = sapp_d3d11_get_render_view();
  120. swapchain.d3d11.resolve_view = sapp_d3d11_get_resolve_view();
  121. swapchain.d3d11.depth_stencil_view = sapp_d3d11_get_depth_stencil_view();
  122. swapchain.wgpu.render_view = sapp_wgpu_get_render_view();
  123. swapchain.wgpu.resolve_view = sapp_wgpu_get_resolve_view();
  124. swapchain.wgpu.depth_stencil_view = sapp_wgpu_get_depth_stencil_view();
  125. swapchain.gl.framebuffer = sapp_gl_get_framebuffer();
  126. return swapchain;
  127. }
  128. #endif /* SOKOL_GLUE_IMPL */