gs_platform.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #ifndef __GS_PLATFORM_H__
  2. #define __GS_PLATFORM_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "common/gs_types.h"
  7. #include "math/gs_math.h"
  8. #include "common/gs_containers.h"
  9. #include "common/gs_util.h"
  10. #if ( defined __APPLE__ || defined _APPLE )
  11. #define GS_PLATFORM_APPLE
  12. #elif ( defined _WIN32 || defined _WIN64 )
  13. #define GS_PLATFORM_WIN
  14. #include <windows.h>
  15. #define WIN32_LEAN_AND_MEAN
  16. #elif ( defined linux || defined _linux || defined __linux__ )
  17. #define GS_PLATFORM_LINUX
  18. #endif
  19. // Forward Decl.
  20. struct gs_uuid;
  21. struct gs_platform_input;
  22. struct gs_platform_window;
  23. /*============================================================
  24. // Platform Time
  25. ============================================================*/
  26. typedef struct gs_platform_time
  27. {
  28. f64 max_fps;
  29. f64 current;
  30. f64 previous;
  31. f64 update;
  32. f64 render;
  33. f64 delta;
  34. f64 frame;
  35. } gs_platform_time;
  36. /*============================================================
  37. // Platform UUID
  38. ============================================================*/
  39. #define gs_uuid_str_size_constant 32
  40. // 33 characters, all set to 0
  41. #define gs_uuid_temp_str_buffer()\
  42. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  43. typedef struct
  44. gs_uuid
  45. {
  46. const char* id;
  47. u8 bytes[ 16 ];
  48. } gs_uuid;
  49. // /*============================================================
  50. // // Platform Window
  51. // ============================================================*/
  52. #define gs_window_flags_resizable 0x01
  53. #define gs_window_flags_fullscreen 0x02
  54. #define gs_window_flags_default (gs_window_flags_resizable | gs_window_flags_resizable)
  55. // // Forward Decl
  56. struct gs_platform_window;
  57. typedef void* gs_platform_window_ptr;
  58. // Internal handle for windows
  59. typedef u32 gs_resource_handle;
  60. // Declare slot array
  61. gs_slot_array_decl( gs_platform_window_ptr );
  62. typedef enum gs_platform_cursor
  63. {
  64. gs_platform_cursor_arrow,
  65. gs_platform_cursor_ibeam,
  66. gs_platform_cursor_size_nw_se,
  67. gs_platform_cursor_size_ne_sw,
  68. gs_platform_cursor_size_ns,
  69. gs_platform_cursor_size_we,
  70. gs_platform_cursor_size_all,
  71. gs_platform_cursor_hand,
  72. gs_platform_cursor_no,
  73. gs_platform_cursor_count
  74. } gs_platform_cursor;
  75. // /*============================================================
  76. // // Platform Input
  77. // ============================================================*/
  78. typedef enum gs_platform_keycode
  79. {
  80. gs_keycode_a,
  81. gs_keycode_b,
  82. gs_keycode_c,
  83. gs_keycode_d,
  84. gs_keycode_e,
  85. gs_keycode_f,
  86. gs_keycode_g,
  87. gs_keycode_h,
  88. gs_keycode_i,
  89. gs_keycode_j,
  90. gs_keycode_k,
  91. gs_keycode_l,
  92. gs_keycode_m,
  93. gs_keycode_n,
  94. gs_keycode_o,
  95. gs_keycode_p,
  96. gs_keycode_q,
  97. gs_keycode_r,
  98. gs_keycode_s,
  99. gs_keycode_t,
  100. gs_keycode_u,
  101. gs_keycode_v,
  102. gs_keycode_w,
  103. gs_keycode_x,
  104. gs_keycode_y,
  105. gs_keycode_z,
  106. gs_keycode_lshift,
  107. gs_keycode_rshift,
  108. gs_keycode_lalt,
  109. gs_keycode_ralt,
  110. gs_keycode_lctrl,
  111. gs_keycode_rctrl,
  112. gs_keycode_bspace,
  113. gs_keycode_bslash,
  114. gs_keycode_qmark,
  115. gs_keycode_tilde,
  116. gs_keycode_comma,
  117. gs_keycode_period,
  118. gs_keycode_esc,
  119. gs_keycode_space,
  120. gs_keycode_left,
  121. gs_keycode_up,
  122. gs_keycode_right,
  123. gs_keycode_down,
  124. gs_keycode_zero,
  125. gs_keycode_one,
  126. gs_keycode_two,
  127. gs_keycode_three,
  128. gs_keycode_four,
  129. gs_keycode_five,
  130. gs_keycode_six,
  131. gs_keycode_seven,
  132. gs_keycode_eight,
  133. gs_keycode_nine,
  134. gs_keycode_npzero,
  135. gs_keycode_npone,
  136. gs_keycode_nptwo,
  137. gs_keycode_npthree,
  138. gs_keycode_npfour,
  139. gs_keycode_npfive,
  140. gs_keycode_npsix,
  141. gs_keycode_npseven,
  142. gs_keycode_npeight,
  143. gs_keycode_npnine,
  144. gs_keycode_caps,
  145. gs_keycode_delete,
  146. gs_keycode_end,
  147. gs_keycode_f1,
  148. gs_keycode_f2,
  149. gs_keycode_f3,
  150. gs_keycode_f4,
  151. gs_keycode_f5,
  152. gs_keycode_f6,
  153. gs_keycode_f7,
  154. gs_keycode_f8,
  155. gs_keycode_f9,
  156. gs_keycode_f10,
  157. gs_keycode_f11,
  158. gs_keycode_f12,
  159. gs_keycode_home,
  160. gs_keycode_plus,
  161. gs_keycode_minus,
  162. gs_keycode_lbracket,
  163. gs_keycode_rbracket,
  164. gs_keycode_semi_colon,
  165. gs_keycode_enter,
  166. gs_keycode_insert,
  167. gs_keycode_pgup,
  168. gs_keycode_pgdown,
  169. gs_keycode_numlock,
  170. gs_keycode_tab,
  171. gs_keycode_npmult,
  172. gs_keycode_npdiv,
  173. gs_keycode_npplus,
  174. gs_keycode_npminus,
  175. gs_keycode_npenter,
  176. gs_keycode_npdel,
  177. gs_keycode_mute,
  178. gs_keycode_volup,
  179. gs_keycode_voldown,
  180. gs_keycode_pause,
  181. gs_keycode_print,
  182. gs_keycode_count
  183. } gs_platform_keycode;
  184. typedef enum gs_platform_mouse_button_code
  185. {
  186. gs_mouse_lbutton,
  187. gs_mouse_rbutton,
  188. gs_mouse_mbutton,
  189. gs_mouse_button_code_count
  190. } gs_platform_mouse_button_code;
  191. typedef struct gs_platform_mouse
  192. {
  193. b32 button_map[ gs_mouse_button_code_count ];
  194. b32 prev_button_map[ gs_mouse_button_code_count ];
  195. gs_vec2 position;
  196. gs_vec2 prev_position;
  197. gs_vec2 wheel;
  198. b32 moved_this_frame;
  199. } gs_platform_mouse;
  200. typedef struct gs_platform_input
  201. {
  202. b32 key_map[ gs_keycode_count ];
  203. b32 prev_key_map[ gs_keycode_count ];
  204. gs_platform_mouse mouse;
  205. } gs_platform_input;
  206. /*===============================================================================================
  207. // Platform API Struct
  208. ===============================================================================================*/
  209. // Enumeration of all platform type
  210. typedef enum gs_platform_type
  211. {
  212. gs_platform_type_unknown = 0,
  213. gs_platform_type_windows,
  214. gs_platform_type_linux,
  215. gs_platform_type_mac
  216. } gs_platform_type;
  217. typedef enum gs_platform_video_driver_type
  218. {
  219. gs_platform_video_driver_type_none = 0,
  220. gs_platform_video_driver_type_opengl,
  221. gs_platform_video_driver_type_opengles,
  222. gs_platform_video_driver_type_directx,
  223. gs_platform_video_driver_type_vulkan,
  224. gs_platform_video_driver_type_metal,
  225. gs_platform_video_driver_type_software
  226. } gs_platform_video_driver_type;
  227. typedef enum gs_opengl_compatibility_flags
  228. {
  229. gs_opengl_compatibility_flags_legacy = 0,
  230. gs_opengl_compatibility_flags_core = 1 << 1,
  231. gs_opengl_compatibility_flags_compatibility = 1 << 2,
  232. gs_opengl_compatibility_flags_forward = 1 << 3,
  233. gs_opengl_compatibility_flags_es = 1 << 4,
  234. } gs_opengl_compatibility_flags;
  235. // A structure that contains OpenGL video settings
  236. typedef struct gs_opengl_video_settings
  237. {
  238. gs_opengl_compatibility_flags compability_flags;
  239. u32 major_version;
  240. u32 minor_version;
  241. u8 multi_sampling_count;
  242. void* ctx;
  243. } gs_opengl_video_settings;
  244. typedef union gs_graphics_api_settings
  245. {
  246. gs_opengl_video_settings opengl;
  247. s32 dummy;
  248. } gs_graphics_api_settings;
  249. typedef struct gs_platform_video_settings
  250. {
  251. gs_graphics_api_settings graphics;
  252. gs_platform_video_driver_type driver;
  253. u32 vsync_enabled;
  254. } gs_platform_video_settings;
  255. typedef struct gs_platform_settings
  256. {
  257. gs_platform_video_settings video;
  258. } gs_platform_settings;
  259. typedef void ( * dropped_files_callback_t )( void*, s32 count, const char** file_paths );
  260. typedef void ( * window_close_callback_t )( void* );
  261. // General API for platform
  262. typedef struct gs_platform_i
  263. {
  264. /*============================================================
  265. // Platform Initilization / De-Initialization
  266. ============================================================*/
  267. gs_result ( * init )( struct gs_platform_i* );
  268. gs_result ( * shutdown )( struct gs_platform_i* );
  269. /*============================================================
  270. // Platform Util
  271. ============================================================*/
  272. void ( * sleep )( f32 ms ); // Sleeps platform for time in ms
  273. f64 ( * elapsed_time )(); // Returns time in ms since initialization of platform
  274. /*============================================================
  275. // Platform Video
  276. ============================================================*/
  277. void ( * enable_vsync )( b32 enabled );
  278. /*============================================================
  279. // Platform UUID
  280. ============================================================*/
  281. struct gs_uuid ( * generate_uuid )();
  282. void ( * uuid_to_string )( char* temp_buffer, const struct gs_uuid* uuid ); // Expects a temp buffer with at leat 32 bytes
  283. u32 ( * hash_uuid )( const struct gs_uuid* uuid );
  284. /*============================================================
  285. // Platform Input
  286. ============================================================*/
  287. gs_result ( * process_input )();
  288. void ( * update_input )();
  289. void ( * press_key )( gs_platform_keycode code );
  290. void ( * release_key )( gs_platform_keycode code );
  291. b32 ( * was_key_down )( gs_platform_keycode code );
  292. b32 ( * key_pressed )( gs_platform_keycode code );
  293. b32 ( * key_down )( gs_platform_keycode code );
  294. b32 ( * key_released )( gs_platform_keycode code );
  295. void ( * press_mouse_button )( gs_platform_mouse_button_code code );
  296. void ( * release_mouse_button )( gs_platform_mouse_button_code code );
  297. b32 ( * was_mouse_down )( gs_platform_mouse_button_code code );
  298. b32 ( * mouse_pressed )( gs_platform_mouse_button_code code );
  299. b32 ( * mouse_down )( gs_platform_mouse_button_code code );
  300. b32 ( * mouse_released )( gs_platform_mouse_button_code code );
  301. void ( * set_mouse_position )( gs_resource_handle, f64, f64 );
  302. gs_vec2 ( * mouse_delta )();
  303. gs_vec2 ( * mouse_position )();
  304. void ( * mouse_position_x_y )( f32* x, f32* y );
  305. void ( * mouse_wheel )( f32* x, f32* y );
  306. b32 (* mouse_moved )();
  307. /*============================================================
  308. // Platform Window
  309. ============================================================*/
  310. gs_resource_handle ( * create_window )( const char* title, u32 width, u32 height );
  311. void* ( * create_window_internal )( const char* title, u32 width, u32 height );
  312. void ( * window_swap_buffer )( gs_resource_handle handle );
  313. gs_vec2 ( * window_size )( gs_resource_handle handle );
  314. void ( * set_window_size )( gs_resource_handle handle, s32 width, s32 height );
  315. void ( * window_size_w_h )( gs_resource_handle handle, s32* width, s32* height );
  316. void ( * set_cursor )( gs_resource_handle handle, gs_platform_cursor cursor );
  317. gs_resource_handle ( *main_window )();
  318. void ( * set_dropped_files_callback )( gs_resource_handle, dropped_files_callback_t );
  319. void ( * set_window_close_callback )( gs_resource_handle, window_close_callback_t );
  320. void* ( *raw_window_handle )( gs_resource_handle ); // Do not call unless you know what you're doing
  321. gs_vec2 ( * frame_buffer_size )( gs_resource_handle handle );
  322. void ( * frame_buffer_size_w_h )( gs_resource_handle handle, s32* w, s32* h );
  323. /*============================================================
  324. // Platform File IO
  325. ============================================================*/
  326. // Will return a null buffer if file does not exist or allocation fails
  327. char* ( * read_file_contents )( const char* file_path, const char* mode, s32* sz );
  328. gs_result ( * write_file_contents )( const char* file_path, const char* mode, void* data, usize data_type_size, usize data_size );
  329. gs_result ( * write_str_to_file )( const char* contents, const char* mode, usize sz, const char* output_path );
  330. b32 ( * file_exists )( const char* file_path );
  331. s32 ( * file_size_in_bytes )( const char* file_path );
  332. void ( * file_extension )( char* buffer, usize buffer_sz, const char* file_path );
  333. // Settings for platform, including video, audio
  334. gs_platform_settings settings;
  335. // Time
  336. gs_platform_time time;
  337. // Input
  338. gs_platform_input input;
  339. // For now, just keep a window here as main window...
  340. gs_slot_array( gs_platform_window_ptr ) windows;
  341. gs_dyn_array( gs_resource_handle ) active_window_handles;
  342. // Cursors
  343. void* cursors[ gs_platform_cursor_count ];
  344. } gs_platform_i;
  345. /*===============================
  346. // Platform User Provided Funcs
  347. ===============================*/
  348. extern struct gs_platform_i* gs_platform_construct();
  349. /*============================
  350. // Platform Default Funcs
  351. ============================*/
  352. void __gs_default_init_platform();
  353. void __gs_verify_platform_correctness( struct gs_platform_i* platform );
  354. /*============================
  355. // Platform Input
  356. ============================*/
  357. void __gs_platform_update_input( );
  358. b32 __gs_platform_was_key_down( gs_platform_keycode code );
  359. b32 __gs_platform_key_down( gs_platform_keycode code );
  360. b32 __gs_platform_key_pressed( gs_platform_keycode code );
  361. b32 __gs_platform_key_released( gs_platform_keycode code );
  362. b32 __gs_platform_was_mouse_down( gs_platform_mouse_button_code code );
  363. void __gs_platform_press_mouse_button( gs_platform_mouse_button_code code );
  364. void __gs_platform_release_mouse_button( gs_platform_mouse_button_code code );
  365. b32 __gs_platform_mouse_down( gs_platform_mouse_button_code code );
  366. b32 __gs_platform_mouse_pressed( gs_platform_mouse_button_code code );
  367. b32 __gs_platform_mouse_released( gs_platform_mouse_button_code code );
  368. gs_vec2 __gs_platform_mouse_delta();
  369. gs_vec2 __gs_platform_mouse_position();
  370. void __gs_platform_mouse_position_x_y( f32* x, f32* y );
  371. void __gs_platform_mouse_wheel( f32* x, f32* y );
  372. void __gs_platform_press_key( gs_platform_keycode code );
  373. void __gs_platform_release_key( gs_platform_keycode code );
  374. /*============================
  375. // Platform Window
  376. ============================*/
  377. gs_resource_handle __gs_platform_create_window( const char* title, u32 width, u32 height );
  378. gs_resource_handle __gs_platform_main_window();
  379. /*============================
  380. // Platform File IO
  381. ============================*/
  382. b32 __gs_platform_file_exists( const char* file_path );
  383. char* __gs_platform_read_file_contents_into_string_null_term( const char* file_path, const char* mode, s32* sz );
  384. gs_result __gs_platform_write_str_to_file( const char* contents, const char* mode, usize sz, const char* output_path );
  385. void __gs_platform_file_extension( char* buffer, usize buffer_sz, const char* file_path );
  386. /*============================
  387. // Platform Util
  388. ============================*/
  389. struct gs_uuid __gs_platform_generate_uuid();
  390. void __gs_platform_uuid_to_string( char* temp_buffer, const struct gs_uuid* uuid ); // Expects a temp buffer with at leat 32 bytes
  391. u32 __gs_platform_hash_uuid( const struct gs_uuid* uuid );
  392. #ifdef __cplusplus
  393. }
  394. #endif // c++
  395. #endif // __GS_PLATFORM_H__