main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. _global gs_resource( gs_uniform ) u_model = {0};
  8. _global gs_resource( gs_uniform ) u_view = {0};
  9. _global gs_resource( gs_uniform ) u_proj = {0};
  10. const char* v_src = "\n"
  11. "#version 330 core\n"
  12. "layout( location = 0 ) in vec3 a_pos;\n"
  13. "uniform mat4 u_model;\n"
  14. "uniform mat4 u_view;\n"
  15. "uniform mat4 u_proj;\n"
  16. "void main()\n"
  17. "{\n"
  18. " gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0);\n"
  19. "}";
  20. const char* f_src = "\n"
  21. "#version 330 core\n"
  22. "uniform vec4 u_color;\n"
  23. "out vec4 frag_color;\n"
  24. "void main()\n"
  25. "{\n"
  26. " frag_color = u_color;\n"
  27. "}";
  28. // Forward Decls.
  29. gs_result app_init(); // Use to init your application
  30. gs_result app_update(); // Use to update your application
  31. gs_result app_shutdown(); // Use to shutdown your appliaction
  32. int main( int argc, char** argv )
  33. {
  34. gs_application_desc app = {0};
  35. app.window_title = "Simple Cube";
  36. app.window_width = 800;
  37. app.window_height = 600;
  38. app.init = &app_init;
  39. app.update = &app_update;
  40. app.shutdown = &app_shutdown;
  41. // Construct internal instance of our engine
  42. gs_engine* engine = gs_engine_construct( app );
  43. // Run the internal engine loop until completion
  44. gs_result res = engine->run();
  45. // Check result of engine after exiting loop
  46. if ( res != gs_result_success )
  47. {
  48. gs_println( "Error: Engine did not successfully finish running." );
  49. return -1;
  50. }
  51. gs_println( "Gunslinger exited successfully." );
  52. return 0;
  53. }
  54. // Here, we'll initialize all of our application data, which in this case is our graphics resources
  55. gs_result app_init()
  56. {
  57. // Cache instance of graphics api from engine
  58. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  59. // Construct command buffer ( the command buffer is used to allow for immediate drawing at any point in our program )
  60. g_cb = gfx->construct_command_buffer();
  61. // Construct shader from our source above
  62. g_shader = gfx->construct_shader( v_src, f_src );
  63. // Construct uniform for shader
  64. u_color = gfx->construct_uniform( g_shader, "u_color", gs_uniform_type_vec4 );
  65. u_view = gfx->construct_uniform( g_shader, "u_view", gs_uniform_type_mat4 );
  66. u_model = gfx->construct_uniform( g_shader, "u_model", gs_uniform_type_mat4 );
  67. u_proj = gfx->construct_uniform( g_shader, "u_proj", gs_uniform_type_mat4 );
  68. // Vertex data layout for our mesh (for this shader, it's a single float3 attribute for position)
  69. gs_vertex_attribute_type layout[] =
  70. {
  71. gs_vertex_attribute_float3
  72. };
  73. // Count of our vertex attribute array
  74. u32 layout_count = sizeof( layout ) / sizeof( gs_vertex_attribute_type );
  75. // Vertex data for cube
  76. float v_data[] = {
  77. // Position
  78. -0.5f, -0.5f, -0.5f, // First face
  79. 0.5f, -0.5f, -0.5f,
  80. 0.5f, 0.5f, -0.5f,
  81. 0.5f, 0.5f, -0.5f,
  82. -0.5f, 0.5f, -0.5f,
  83. -0.5f, -0.5f, -0.5f,
  84. -0.5f, -0.5f, 0.5f, // Second face
  85. 0.5f, -0.5f, 0.5f,
  86. 0.5f, 0.5f, 0.5f,
  87. 0.5f, 0.5f, 0.5f,
  88. -0.5f, 0.5f, 0.5f,
  89. -0.5f, -0.5f, 0.5f,
  90. -0.5f, 0.5f, 0.5f, // Third face
  91. -0.5f, 0.5f, -0.5f,
  92. -0.5f, -0.5f, -0.5f,
  93. -0.5f, -0.5f, -0.5f,
  94. -0.5f, -0.5f, 0.5f,
  95. -0.5f, 0.5f, 0.5f,
  96. 0.5f, 0.5f, 0.5f, // Fourth face
  97. 0.5f, 0.5f, -0.5f,
  98. 0.5f, -0.5f, -0.5f,
  99. 0.5f, -0.5f, -0.5f,
  100. 0.5f, -0.5f, 0.5f,
  101. 0.5f, 0.5f, 0.5f,
  102. -0.5f, -0.5f, -0.5f, // Fifth face
  103. 0.5f, -0.5f, -0.5f,
  104. 0.5f, -0.5f, 0.5f,
  105. 0.5f, -0.5f, 0.5f,
  106. -0.5f, -0.5f, 0.5f,
  107. -0.5f, -0.5f, -0.5f,
  108. -0.5f, 0.5f, -0.5f, // Sixth face
  109. 0.5f, 0.5f, -0.5f,
  110. 0.5f, 0.5f, 0.5f,
  111. 0.5f, 0.5f, 0.5f,
  112. -0.5f, 0.5f, 0.5f,
  113. -0.5f, 0.5f, -0.5f
  114. };
  115. // Construct vertex buffer
  116. g_vbo = gfx->construct_vertex_buffer( layout, layout_count, v_data, sizeof(v_data) );
  117. return gs_result_success;
  118. }
  119. gs_result app_update()
  120. {
  121. // Grab global instance of engine
  122. gs_engine* engine = gs_engine_instance();
  123. // Platform api
  124. gs_platform_i* platform = engine->ctx.platform;
  125. // If we press the escape key, exit the application
  126. if ( platform->key_pressed( gs_keycode_esc ) )
  127. {
  128. return gs_result_success;
  129. }
  130. const f32 dt = platform->time.delta;
  131. const f32 t = platform->elapsed_time();
  132. /*===============
  133. // Render scene
  134. ================*/
  135. // Graphics api instance
  136. gs_graphics_i* gfx = engine->ctx.graphics;
  137. // Main window size
  138. gs_vec2 ws = platform->window_size( platform->main_window() );
  139. gs_vec2 fbs = platform->frame_buffer_size( platform->main_window() );
  140. // Set clear color and clear screen
  141. f32 clear_color[4] = { 0.2f, 0.2f, 0.2f, 1.f };
  142. gfx->set_view_clear( g_cb, clear_color );
  143. gfx->set_view_port( g_cb, fbs.x, fbs.y );
  144. gfx->set_depth_enabled( g_cb, true );
  145. gfx->set_face_culling( g_cb, gs_face_culling_disabled );
  146. // Bind shader
  147. gfx->bind_shader( g_cb, g_shader );
  148. // Bind uniform for triangle color
  149. f32 color[4] = { sin(t * 0.001f), cos(t * 0.002f), 0.1f, 1.f };
  150. gfx->bind_uniform( g_cb, u_color, &color );
  151. // Create model/view/projection matrices
  152. gs_mat4 view_mtx = gs_mat4_mul(gs_mat4_rotate(15.f, (gs_vec3){1.f, 0.f, 0.f}),
  153. gs_mat4_translate((gs_vec3){0.f, -0.9f, -2.5f}));
  154. gs_mat4 proj_mtx = gs_mat4_perspective(60.f, ws.x / ws.y, 0.01f, 1000.f);
  155. gs_mat4 model_mtx = gs_mat4_rotate(t * 0.01f, (gs_vec3){0.f, 1.f, 0.f});
  156. // Bind matrix uniforms
  157. gfx->bind_uniform( g_cb, u_proj, &proj_mtx );
  158. gfx->bind_uniform( g_cb, u_view, &view_mtx );
  159. gfx->bind_uniform( g_cb, u_model, &model_mtx );
  160. // Bind vertex buffer
  161. gfx->bind_vertex_buffer( g_cb, g_vbo );
  162. // Draw
  163. gfx->draw( g_cb, 0, 36 );
  164. // Submit command buffer for rendering
  165. gfx->submit_command_buffer( g_cb );
  166. return gs_result_in_progress;
  167. }
  168. gs_result app_shutdown()
  169. {
  170. gs_println( "Goodbye, Gunslinger." );
  171. return gs_result_success;
  172. }