main.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <gs.h>
  2. // Forward Decls.
  3. gs_result app_init(); // Use to init your application
  4. gs_result app_update(); // Use to update your application
  5. gs_result app_shutdown(); // Use to shutdown your appliaction
  6. _global b32 g_app_running = true;
  7. _global gs_resource( gs_audio_source ) g_music_src = {0};
  8. _global gs_resource( gs_audio_instance ) g_music = {0};
  9. int main( int argc, char** argv )
  10. {
  11. // This is our app description. It gives internal hints to our engine for various things like
  12. // window size, title, as well as update, init, and shutdown functions to be run.
  13. gs_application_desc app = {0};
  14. app.window_title = "Simple Audio";
  15. app.window_width = 800;
  16. app.window_height = 600;
  17. app.init = &app_init;
  18. app.update = &app_update;
  19. app.shutdown = &app_shutdown;
  20. // Construct internal instance of our engine
  21. gs_engine* engine = gs_engine_construct( app );
  22. // Run the internal engine loop until completion
  23. gs_result res = engine->run();
  24. // Check result of engine after exiting loop
  25. if ( res != gs_result_success )
  26. {
  27. gs_println( "Error: Engine did not successfully finish running." );
  28. return -1;
  29. }
  30. gs_println( "Gunslinger exited successfully." );
  31. return 0;
  32. }
  33. void app_close_window_callback( void* window )
  34. {
  35. g_app_running = false;
  36. }
  37. gs_result app_init()
  38. {
  39. // Cache apis
  40. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  41. gs_audio_i* audio = gs_engine_instance()->ctx.audio;
  42. // Set callback for when window close button is pressed
  43. platform->set_window_close_callback( platform->main_window(), &app_close_window_callback );
  44. // Constuct audio resource to play
  45. g_music_src = audio->load_audio_source_from_file( platform->file_exists( "./assets/cold_morning_tx.mp3" ) ?
  46. "./assets/cold_morning_tx.mp3" : "./../assets/cold_morning_tx.mp3" );
  47. // Construct instance source and play on loop. Forever.
  48. gs_audio_instance_data_t inst = gs_audio_instance_data_new( g_music_src );
  49. inst.volume = 0.8f; // Range from [0.f, 1.f]
  50. inst.loop = true; // Tell whether or not audio should loop
  51. inst.persistent = true; // Whether or not instance should stick in memory after completing
  52. g_music = audio->play( inst );
  53. return gs_result_success;
  54. }
  55. gs_result app_update()
  56. {
  57. // Grab global instance of engine
  58. gs_engine* engine = gs_engine_instance();
  59. gs_platform_i* platform = engine->ctx.platform;
  60. gs_audio_i* audio = engine->ctx.audio;
  61. // If we press the escape key, exit the application
  62. if ( platform->key_pressed( gs_keycode_esc ) || !g_app_running )
  63. {
  64. return gs_result_success;
  65. }
  66. // Otherwise, continue
  67. return gs_result_in_progress;
  68. }
  69. gs_result app_shutdown()
  70. {
  71. gs_println( "Goodbye, Gunslinger." );
  72. return gs_result_success;
  73. }