A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hello-triangle.cc 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <cygnet/Window.h>
  2. #include <cygnet/GlProgram.h>
  3. #include <iostream>
  4. const char *vertexSource = R"(
  5. attribute vec4 position;
  6. void main() {
  7. gl_Position = vec4(position.xyz, 1.0);
  8. }
  9. )";
  10. const char *fragmentSource = R"(
  11. precision mediump float;
  12. void main() {
  13. gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
  14. }
  15. )";
  16. int main() {
  17. SDL_Init(SDL_INIT_VIDEO);
  18. Cygnet::Window win("Hello Triangle", 640, 480);
  19. Cygnet::GlShader vertex(vertexSource, Cygnet::GlShader::Type::VERTEX);
  20. Cygnet::GlShader fragment(fragmentSource, Cygnet::GlShader::Type::FRAGMENT);
  21. Cygnet::GlProgram program({ vertex, fragment });
  22. GLuint positionAttrib = program.getLocation("position");
  23. program.use();
  24. GLfloat vertixes[] = {
  25. 0.0, 0.5, 0.0,
  26. -0.5, -0.5, 0.0,
  27. 0.5, -0.5, 0.0,
  28. };
  29. // Draw loop
  30. while (true) {
  31. SDL_Event evt;
  32. while (SDL_PollEvent(&evt)) {
  33. switch (evt.type) {
  34. case SDL_QUIT:
  35. goto exit;
  36. break;
  37. }
  38. }
  39. win.clear();
  40. glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, vertixes);
  41. glEnableVertexAttribArray(positionAttrib);
  42. glDrawArrays(GL_TRIANGLES, 0, 3);
  43. win.flip();
  44. }
  45. exit:
  46. SDL_Quit();
  47. }