main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <gs.h>
  2. // Globals
  3. _global gs_resource( gs_vertex_buffer ) g_vbo = {0};
  4. _global gs_resource( gs_command_buffer ) g_cb = {0};
  5. _global gs_resource( gs_shader ) g_shader = {0};
  6. _global gs_resource( gs_uniform ) u_color = {0};
  7. const char* v_src = "\n"
  8. "#version 330 core\n"
  9. "layout( location = 0 ) in vec3 a_pos;\n"
  10. "void main()\n"
  11. "{\n"
  12. " gl_Position = vec4(a_pos, 1.0);\n"
  13. "}";
  14. const char* f_src = "\n"
  15. "#version 330 core\n"
  16. "uniform vec4 u_color;\n"
  17. "out vec4 frag_color;\n"
  18. "void main()\n"
  19. "{\n"
  20. " frag_color = u_color;\n"
  21. "}";
  22. // Forward Decls.
  23. gs_result app_init(); // Use to init your application
  24. gs_result app_update(); // Use to update your application
  25. gs_result app_shutdown(); // Use to shutdown your appliaction
  26. int main( int argc, char** argv )
  27. {
  28. gs_application_desc app = {0};
  29. app.window_title = "Simple Triangle";
  30. app.window_width = 800;
  31. app.window_height = 600;
  32. app.init = &app_init;
  33. app.update = &app_update;
  34. app.shutdown = &app_shutdown;
  35. // Construct internal instance of our engine
  36. gs_engine* engine = gs_engine_construct( app );
  37. // Run the internal engine loop until completion
  38. gs_result res = engine->run();
  39. // Check result of engine after exiting loop
  40. if ( res != gs_result_success )
  41. {
  42. gs_println( "Error: Engine did not successfully finish running." );
  43. return -1;
  44. }
  45. gs_println( "Gunslinger exited successfully." );
  46. return 0;
  47. }
  48. // Here, we'll initialize all of our application data, which in this case is our graphics resources
  49. gs_result app_init()
  50. {
  51. // Cache instance of graphics api from engine
  52. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  53. // Construct command buffer ( the command buffer is used to allow for immediate drawing at any point in our program )
  54. g_cb = gfx->construct_command_buffer();
  55. // Construct shader from our source above
  56. g_shader = gfx->construct_shader( v_src, f_src );
  57. // Construct uniform for shader
  58. u_color = gfx->construct_uniform( g_shader, "u_color", gs_uniform_type_vec4 );
  59. // Vertex data layout for our mesh (for this shader, it's a single float3 attribute for position)
  60. gs_vertex_attribute_type layout[] =
  61. {
  62. gs_vertex_attribute_float3
  63. };
  64. // Count of our vertex attribute array
  65. u32 layout_count = sizeof( layout ) / sizeof( gs_vertex_attribute_type );
  66. // Vertex data for triangle
  67. f32 v_data[] =
  68. {
  69. 0.0f, 0.5f, 0.0f,
  70. -0.5f, -0.5f, 0.0f,
  71. 0.5f, -0.5f, 0.0f
  72. };
  73. // Construct vertex buffer
  74. g_vbo = gfx->construct_vertex_buffer( layout, layout_count, v_data, sizeof(v_data) );
  75. return gs_result_success;
  76. }
  77. gs_result app_update()
  78. {
  79. // Grab global instance of engine
  80. gs_engine* engine = gs_engine_instance();
  81. // If we press the escape key, exit the application
  82. if ( engine->ctx.platform->key_pressed( gs_keycode_esc ) )
  83. {
  84. return gs_result_success;
  85. }
  86. /*===============
  87. // Render scene
  88. ================*/
  89. /*
  90. Note:
  91. You'll notice that 'g_cb', our command buffer handle, is the first argument for every one of these api calls.
  92. All graphics api functions for immediate rendering will require a command buffer.
  93. Every call you make adds these commands into this buffer and then will be processed later on in the frame
  94. for drawing. This allows you to make these calls at ANY point in your code before the final rendering submission occurs.
  95. */
  96. // Graphics api instance
  97. gs_graphics_i* gfx = engine->ctx.graphics;
  98. gs_platform_i* platform = engine->ctx.platform;
  99. const gs_vec2 ws = platform->window_size(platform->main_window());
  100. const gs_vec2 fbs = platform->frame_buffer_size(platform->main_window());
  101. // Set clear color and clear screen
  102. f32 clear_color[4] = { 0.2f, 0.2f, 0.2f, 1.f };
  103. gfx->set_view_clear( g_cb, clear_color );
  104. gfx->set_view_port( g_cb, fbs.x, fbs.y );
  105. // Bind shader
  106. gfx->bind_shader( g_cb, g_shader );
  107. // Bind uniform for triangle color
  108. f32 tri_color[4] = { 1.f, 0.6f, 0.1f, 1.f };
  109. gfx->bind_uniform( g_cb, u_color, &tri_color );
  110. // Bind vertex buffer
  111. gfx->bind_vertex_buffer( g_cb, g_vbo );
  112. // Draw
  113. gfx->draw( g_cb, 0, 3 );
  114. // Submit command buffer for rendering
  115. gfx->submit_command_buffer( g_cb );
  116. return gs_result_in_progress;
  117. }
  118. gs_result app_shutdown()
  119. {
  120. gs_println( "Goodbye, Gunslinger." );
  121. return gs_result_success;
  122. }