main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*================================================================
  2. * Copyright: 2020 John Jackson
  3. * simple_triangle
  4. The purpose of this example is to demonstrate how to explicitly construct a
  5. GPU resources to use for your application to render a basic triangle.
  6. Included:
  7. * Construct vertex/index buffers from user defined declarations
  8. * Construct shaders from source
  9. * Rendering via command buffers
  10. Press `esc` to exit the application.
  11. ================================================================*/
  12. #include <gs.h>
  13. // Globals
  14. gs_global gs_command_buffer_t g_cb = {0};
  15. gs_global gs_vertex_buffer_t g_vbo = {0};
  16. gs_global gs_shader_t g_shader = {0};
  17. gs_global gs_uniform_t u_color = {0};
  18. const char* v_src = "\n"
  19. "#version 330 core\n"
  20. "layout(location = 0) in vec3 a_pos;\n"
  21. "void main()\n"
  22. "{\n"
  23. " gl_Position = vec4(a_pos, 1.0);\n"
  24. "}";
  25. const char* f_src = "\n"
  26. "#version 330 core\n"
  27. "uniform vec4 u_color;\n"
  28. "out vec4 frag_color;\n"
  29. "void main()\n"
  30. "{\n"
  31. " frag_color = u_color;\n"
  32. "}";
  33. // Forward Decls.
  34. gs_result app_init(); // Use to init your application
  35. gs_result app_update(); // Use to update your application
  36. int main(int argc, char** argv)
  37. {
  38. gs_application_desc_t app = {0};
  39. app.window_title = "Simple Triangle";
  40. app.window_width = 800;
  41. app.window_height = 600;
  42. app.init = &app_init;
  43. app.update = &app_update;
  44. // Construct internal instance of our engine
  45. gs_engine_t* engine = gs_engine_construct(app);
  46. // Run the internal engine loop until completion
  47. gs_result res = engine->run();
  48. // Check result of engine after exiting loop
  49. if (res != gs_result_success)
  50. {
  51. gs_println("Error: Engine did not successfully finish running.");
  52. return -1;
  53. }
  54. gs_println("Gunslinger exited successfully.");
  55. return 0;
  56. }
  57. // Here, we'll initialize all of our application data, which in this case is our graphics resources
  58. gs_result app_init()
  59. {
  60. // Cache instance of graphics api from engine
  61. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  62. // Construct command buffer (the command buffer is used to allow for immediate drawing at any point in our program)
  63. g_cb = gs_command_buffer_new();
  64. // Construct shader from our source above
  65. g_shader = gfx->construct_shader(v_src, f_src);
  66. // Construct uniform for shader
  67. u_color = gfx->construct_uniform(g_shader, "u_color", gs_uniform_type_vec4);
  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. gs_vertex_attribute_float3
  71. };
  72. // Vertex data for triangle
  73. f32 v_data[] = {
  74. 0.0f, 0.5f, 0.0f,
  75. -0.5f, -0.5f, 0.0f,
  76. 0.5f, -0.5f, 0.0f
  77. };
  78. // Construct vertex buffer
  79. g_vbo = gfx->construct_vertex_buffer(layout, sizeof(layout), v_data, sizeof(v_data));
  80. return gs_result_success;
  81. }
  82. gs_result app_update()
  83. {
  84. // Grab global instance of engine
  85. gs_engine_t* engine = gs_engine_instance();
  86. // If we press the escape key, exit the application
  87. if (engine->ctx.platform->key_pressed(gs_keycode_esc))
  88. {
  89. return gs_result_success;
  90. }
  91. /*===============
  92. // Render scene
  93. ================*/
  94. /*
  95. Note:
  96. You'll notice that 'g_cb', our command buffer handle, is the first argument for every one of these api calls.
  97. All graphics api functions for immediate rendering will require a command buffer.
  98. Every call you make adds these commands into this buffer and then will be processed later on in the frame
  99. for drawing. This allows you to make these calls at ANY point in your code before the final rendering submission occurs.
  100. */
  101. // Graphics api instance
  102. gs_graphics_i* gfx = engine->ctx.graphics;
  103. gs_platform_i* platform = engine->ctx.platform;
  104. const gs_vec2 ws = platform->window_size(platform->main_window());
  105. const gs_vec2 fbs = platform->frame_buffer_size(platform->main_window());
  106. gs_command_buffer_t* cb = &g_cb;
  107. // Set clear color and clear screen
  108. f32 clear_color[4] = { 0.2f, 0.2f, 0.2f, 1.f };
  109. gfx->set_view_clear(cb, clear_color);
  110. gfx->set_viewport(cb, 0.f, 0.f, fbs.x, fbs.y);
  111. // Bind shader
  112. gfx->bind_shader(cb, g_shader);
  113. // Bind uniform for triangle color
  114. f32 tri_color[4] = { 1.f, 0.6f, 0.1f, 1.f };
  115. gfx->bind_uniform(cb, u_color, &tri_color);
  116. // Bind vertex buffer
  117. gfx->bind_vertex_buffer(cb, g_vbo);
  118. // Draw
  119. gfx->draw(cb, 0, 3);
  120. // Submit command buffer for rendering
  121. gfx->submit_command_buffer(cb);
  122. return gs_result_in_progress;
  123. }
  124. /*
  125. gs_graphics_immediate_mode_i* gmi = &gfx->immediate;
  126. // Pass in a transform and do things with stuff!
  127. gmi->triangle(cb, transform);
  128. // Need to get 3d model loading
  129. */