Main.cpp 2.3 KB

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