main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <iostream>
  2. #include <SDL2/SDL.h>
  3. #include <box2d/box2d.h>
  4. #include "texturer.h"
  5. #include "Input.h"
  6. using namespace std;
  7. int main() {
  8. // Start
  9. cout << "SDL2 app is loading..." << endl;
  10. // Useful data
  11. const int SCREEN_WIDTH = 1200;
  12. const int SCREEN_HEIGHT = 700;
  13. // Init video
  14. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  15. logSDLError(cout, "SDL_Init(SDL_INIT_VIDEO)");
  16. return 1;
  17. }
  18. // Opening window
  19. SDL_Window *win = SDL_CreateWindow("Hello Box2D!",
  20. SDL_WINDOWPOS_UNDEFINED,
  21. SDL_WINDOWPOS_UNDEFINED,
  22. SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  23. if (win == nullptr) {
  24. logSDLError(cout, "SDL_CreateWindow");
  25. SDL_Quit();
  26. return 1;
  27. }
  28. // Create a renderer
  29. SDL_Renderer *ren = SDL_CreateRenderer(win, -1,
  30. SDL_RENDERER_ACCELERATED |
  31. SDL_RENDERER_PRESENTVSYNC);
  32. if (ren == nullptr) {
  33. SDL_DestroyWindow(win);
  34. logSDLError(cout, "SDL_CreateRenderer");
  35. SDL_Quit();
  36. return 1;
  37. }
  38. // Events
  39. Input myInput;
  40. // Time
  41. Uint32 frameRate(60);
  42. Uint32 prevTime(0);
  43. Uint32 waitTime(1000 / frameRate);
  44. float timeStep(1.0f / frameRate);
  45. int32 velocityIterations = 6;
  46. int32 positionIterations = 2;
  47. /// Physic
  48. // World
  49. b2Vec2 gravity(0.0f, 10.0f);
  50. b2World world(gravity);
  51. float zoom = 50.0f;
  52. // Defs
  53. b2BodyDef myBodyDef;
  54. b2PolygonShape myBox;
  55. // Ground body
  56. myBodyDef.position.Set(10.0f, 17.0f);
  57. b2Body* groundBody(world.CreateBody(&myBodyDef));
  58. myBox.SetAsBox(50.0f, 5.0f);
  59. groundBody->CreateFixture(&myBox, 0.0f); // Warning : simple function (no fixture)
  60. // Crate body
  61. myBodyDef.type = b2_dynamicBody;
  62. myBodyDef.position.Set(10.0f, -4.0f);
  63. b2Body* crateBody(world.CreateBody(&myBodyDef));
  64. myBox.SetAsBox(1.0f, 1.0f);
  65. b2FixtureDef myFixtureDef;
  66. myFixtureDef.shape = &myBox;
  67. myFixtureDef.density = 1.0f;
  68. myFixtureDef.friction = 0.3f;
  69. myFixtureDef.restitution = 0.3f;
  70. crateBody->CreateFixture(&myFixtureDef);
  71. while (!myInput.isFinished()) {
  72. // Update events
  73. myInput.updateEvents();
  74. // Update physic
  75. world.Step(timeStep, velocityIterations, positionIterations);
  76. // Reset renderer
  77. SDL_SetRenderDrawColor(ren, 0xFF, 0xFF, 0xFF, 0xFF);
  78. SDL_RenderClear(ren);
  79. // Foreground
  80. b2Vec2 pos(crateBody->GetPosition());
  81. float angle(crateBody->GetAngle());
  82. SDL_Rect fillRect = {(int)(pos.x * zoom), (int)(pos.y * zoom), 100, 100};
  83. SDL_SetRenderDrawColor(ren, 0xFF, 0x00, 0x00, 0xFF);
  84. SDL_RenderFillRect(ren, &fillRect);
  85. // Render
  86. SDL_RenderPresent(ren);
  87. // Pause
  88. if (SDL_GetTicks() < prevTime + waitTime)
  89. SDL_Delay(waitTime + prevTime - SDL_GetTicks());
  90. prevTime = SDL_GetTicks();
  91. }
  92. // End
  93. SDL_DestroyRenderer(ren);
  94. SDL_DestroyWindow(win);
  95. SDL_Quit();
  96. return 0;
  97. }