Main.cpp 2.1 KB

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