Main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <iostream>
  2. #include "rendering/wrapper/GLFWWrapper.h"
  3. #include "rendering/wrapper/GLWrapper.h"
  4. #include "rendering/WindowSize.h"
  5. #include "rendering/wrapper/Window.h"
  6. #include "utils/Clock.h"
  7. #include "rendering/wrapper/Shader.h"
  8. #include "input/Controller.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. void initCallbacks(Window& w, WindowSize& size, Controller& controller) {
  21. static WindowSize& cSize = size;
  22. static Controller& cController = controller;
  23. (void) cController;
  24. w.setFramebufferSizeCallback([](GLFWwindow*, int newWidth, int newHeight) {
  25. glViewport(0, 0, newWidth, newHeight);
  26. cSize.width = newWidth;
  27. cSize.height = newHeight;
  28. });
  29. /*w.setKeyCallback([](GLFWwindow*, int key, int, int action, int) {
  30. if(action == GLFW_PRESS) {
  31. cControl.keys.press(key);
  32. } else if(action == GLFW_RELEASE) {
  33. cControl.keys.release(key);
  34. }
  35. });*/
  36. }
  37. int main() {
  38. if(GLFWWrapper::hasError()) {
  39. return 0;
  40. }
  41. WindowSize size(800, 480);
  42. Window window(size, "Test");
  43. if(window.hasError() || initGLEW()) {
  44. return 0;
  45. }
  46. Shader shader("resources/shader/vertex.vs", "resources/shader/fragment.fs");
  47. if(shader.hasError()) {
  48. return 0;
  49. }
  50. Renderer renderer(size, shader);
  51. Controller controller;
  52. Clock fps;
  53. Clock tps;
  54. static Game game(controller, fps, tps);
  55. initCallbacks(window, size, controller);
  56. window.show();
  57. const u64 nanosPerTick = 50000000;
  58. u64 lag = 0;
  59. while(!window.shouldClose() && game.isRunning()) {
  60. GLWrapper::checkAndPrintError("GL-Error");
  61. lag += fps.update();
  62. while(lag >= nanosPerTick) {
  63. lag -= nanosPerTick;
  64. tps.update();
  65. controller.tick();
  66. game.tick();
  67. }
  68. Matrix view;
  69. view.translate(-1.0f, 1.0f);
  70. view.scale(2.0f / size.width, -2.0f / size.height);
  71. shader.setMatrix("view", view.getValues());
  72. game.render((float) lag / nanosPerTick, renderer);
  73. window.swapBuffers();
  74. glfwPollEvents();
  75. }
  76. /*for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
  77. String s("Joystick ");
  78. int present = glfwJoystickPresent(i);
  79. s.append(i).append(": ").append(present);
  80. if(present) {
  81. s.append(" ");
  82. int count = 0;
  83. const u8* buttons = glfwGetJoystickButtons(i, &count);
  84. for(int k = 0; k < count; k++) {
  85. s.append(buttons[k] ? '1' : '0');
  86. }
  87. }
  88. r.drawString(0, i * 10 + 10, s);
  89. }*/
  90. return 0;
  91. }