AllenYuan

Untitled

Jun 12th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. using namespace sf;
  3.  
  4. int width = 1024;
  5. int height = 768;
  6. int roadW = 2000; // road width
  7. int segL = 200; // segment length
  8. float camD = 0.84; // base scale factor for distant objects
  9.  
  10. // a line in the 3d world w/ a 'project' method to draw it on the 2d screen
  11. struct Line
  12. {
  13. float x,y,z; // 3d center of line
  14. float X,Y,W; // paramterization of line on screen
  15. float scale;
  16.  
  17. Line() { x=y=z=0; }
  18.  
  19. // project the line from 3d world onto 2d screen. X=horizontal, Y=vertical, Z=depth
  20. void project(int camX, int camY, int camZ)
  21. {
  22. scale = camD/(z-camZ); // scale factor for the line. An object looks bigger when its closer (smaller z) and smaller when its farther (large z)
  23. // lines are closer to the center and smaller when they are far away.
  24. X = (1 + scale*(x - camX)) * width/2;
  25. Y = (1 - scale*(y - camY)) * height/2;
  26. W = scale * roadW * width/2;
  27. }
  28. };
  29.  
  30. // draw a quadrilateral in w with color c, parameterized by two center points of opposing sides
  31. // (x1, y1), (x2, y2) and 2 radiuses w1, w2.
  32. // You may wonder why we parameterize with w1 and w2 instead of 2 more points...this paramterization
  33. // makes the math for perspectives easier
  34. void drawQuad(RenderWindow &w, Color c, int x1, int y1, int w1, int x2, int y2, int w2)
  35. {
  36. ConvexShape shape(4);
  37. shape.setFillColor(c);
  38. shape.setPoint(0, Vector2f(x1-w1, y1));
  39. shape.setPoint(1, Vector2f(x2-w2, y2));
  40. shape.setPoint(2, Vector2f(x2+w2, y2));
  41. shape.setPoint(3, Vector2f(x1+w1, y1));
  42. w.draw(shape);
  43. }
  44.  
  45. int main()
  46. {
  47. RenderWindow app(VideoMode(width, height), "Outrun Racing!");
  48. app.setFramerateLimit(60);
  49.  
  50. // data for 1600 segments (side of the road)
  51. std::vector<Line> lines;
  52. for (int i = 0; i < 1600; i++)
  53. {
  54. Line line;
  55. line.z = i * segL;
  56.  
  57. lines.push_back(line);
  58. }
  59.  
  60. int N = lines.size();
  61. int pos = 0;
  62.  
  63.  
  64. while (app.isOpen())
  65. {
  66. Event e;
  67. while (app.pollEvent(e))
  68. {
  69. if (e.type == Event::Closed)
  70. app.close();
  71. }
  72.  
  73. // move the racer
  74. if (Keyboard::isKeyPressed(Keyboard::Up)) pos += 200;
  75. if (Keyboard::isKeyPressed(Keyboard::Down)) pos -= 200;
  76.  
  77. app.clear();
  78. int startPos = pos/segL;
  79.  
  80. // draw road
  81. for (int n = startPos; n < startPos + 300; n++)
  82. {
  83. Line &l = lines[n%N]; // select line to render
  84. l.project(0, 1500, pos);
  85.  
  86. // alternate grass/rumble/road color every 6 lines
  87. Color grass = (n/3)%2 ? Color(16,200,16) : Color(0,154,0);
  88. Color rumble = (n/3)%2 ? Color(255,255,255) : Color(0,0,0);
  89. Color road = (n/3)%2 ? Color(107,107,107) : Color(105,105,105);
  90.  
  91. Line p = lines[(n-1)%N]; // previous line
  92.  
  93. drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width); // draw grass across the whole screen
  94. drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2); // draw rumble across the middle of the screen
  95. drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W); // draw road in center of screen
  96. }
  97.  
  98. app.display();
  99. }
  100.  
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment