Main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <iostream>
  2. #include <getopt.h>
  3. #include "Game.h"
  4. #include "gaming-core/utils/Clock.h"
  5. #include "gaming-core/wrapper/GL.h"
  6. #include "gaming-core/wrapper/GLEW.h"
  7. #include "gaming-core/wrapper/GLFW.h"
  8. #include "gaming-core/wrapper/Shader.h"
  9. #include "gaming-core/wrapper/Window.h"
  10. #include "gaming-core/wrapper/WindowOptions.h"
  11. #include "rendering/Renderer.h"
  12. bool parseArgs(int argAmount, char* const* args, WindowOptions& options) {
  13. while(true) {
  14. switch(getopt(argAmount, args, "fv")) {
  15. case '?': return true;
  16. case 'f': options.fullscreen = true; break;
  17. case 'v': options.vsync = true; break;
  18. case -1: return false;
  19. }
  20. }
  21. }
  22. int main(int argAmount, char* const* args) {
  23. WindowOptions options(3, 0, Size(800, 480), true, "Pigine");
  24. if(parseArgs(argAmount, args, options) || GLFW::init()) {
  25. return 0;
  26. }
  27. Window window(options);
  28. if(window.hasError() || GLEW::init()) {
  29. return 0;
  30. }
  31. Shader shader("resources/shader/vertex.vs", "resources/shader/fragment.fs");
  32. if(shader.hasError()) {
  33. return 0;
  34. }
  35. Renderer renderer(shader);
  36. Clock fps;
  37. Clock tps;
  38. Buttons buttons(window);
  39. Controller controller(buttons);
  40. static Game game(controller, fps, tps);
  41. window.show();
  42. const Clock::Nanos nanosPerTick = 10'000'000;
  43. Clock::Nanos lag = 0;
  44. while(!window.shouldClose() && game.isRunning()) {
  45. GL::checkAndPrintError("GL-Error");
  46. lag += fps.update();
  47. while(lag >= nanosPerTick) {
  48. lag -= nanosPerTick;
  49. tps.update();
  50. buttons.tick();
  51. game.tick();
  52. }
  53. Size size = window.getSize();
  54. GL::setViewport(size.width, size.height);
  55. Matrix view;
  56. view.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f));
  57. view.translate(Vector3(-1.0f, 1.0f, 0.0f));
  58. shader.setMatrix("view", view.getValues());
  59. game.render(static_cast<float>(lag) / nanosPerTick, renderer);
  60. window.swapBuffers();
  61. GL::clearFramebuffer();
  62. glfwPollEvents();
  63. }
  64. return 0;
  65. }