AllenYuan

Untitled

Jun 13th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <math.h>
  3. using namespace sf;
  4.  
  5. int width = 1024;
  6. int height = 768;
  7. int roadW = 2000;
  8. int segL = 200; // segment length
  9. float camD = 0.84; // camera depth
  10.  
  11. struct Line
  12. {
  13. float x,y,z; // center of line in the 3d world
  14. float X,Y,W; // parameterization of the line on the screen
  15. float scale, curve;
  16.  
  17. Line() { curve=x=y=z=0; }
  18.  
  19. // project onto the given camera position
  20. void project(int camX, int camY, int camZ)
  21. {
  22. scale = camD/(z-camZ);
  23. X = (1 + scale*(x - camX)) * width/2;
  24. Y = (1 - scale*(y - camY)) * height/2;
  25. W = scale * roadW * width/2;
  26. }
  27. };
  28.  
  29. // draw a quadtrilateral parameterized by two midpoints (x1,y1), (x2,y2) and 2 widths w1, w2.
  30. void drawQuad(RenderWindow &w, Color c, int x1, int y1, int w1, int x2, int y2, int w2)
  31. {
  32. ConvexShape shape(4);
  33. shape.setFillColor(c);
  34. shape.setPoint(0, Vector2f(x1-w1,y1));
  35. shape.setPoint(1, Vector2f(x2-w2, y2));
  36. shape.setPoint(2, Vector2f(x2+w2, y2));
  37. shape.setPoint(3, Vector2f(x1+w1, y1));
  38.  
  39. w.draw(shape);
  40. }
  41.  
  42. int main()
  43. {
  44. RenderWindow app(VideoMode(width, height), "Outrun Racing!");
  45. app.setFramerateLimit(60);
  46.  
  47. std::vector<Line> lines;
  48.  
  49. for (int i = 0; i < 1600; i++)
  50. {
  51. Line line;
  52. line.z = i * segL;
  53.  
  54. if (i > 300 && i < 700) line.curve = 0.5;
  55.  
  56. if (i > 750) line.y = sin(i/30.0) * 1500; // add wavy terrain to the end of the track
  57.  
  58. lines.push_back(line);
  59. }
  60.  
  61. int N = lines.size();
  62. int pos = 0;
  63. int playerX = 0;
  64.  
  65. while(app.isOpen())
  66. {
  67. Event e;
  68. while (app.pollEvent(e))
  69. {
  70. if (e.type == Event::Closed)
  71. app.close();
  72. }
  73.  
  74. if (Keyboard::isKeyPressed(Keyboard::Right)) playerX += 200;
  75. if (Keyboard::isKeyPressed(Keyboard::Left)) playerX -= 200;
  76. if (Keyboard::isKeyPressed(Keyboard::Up)) pos += 200;
  77. if (Keyboard::isKeyPressed(Keyboard::Down)) pos -= 200;
  78.  
  79. // loop the track
  80. while (pos >= N*segL) pos -= N*segL;
  81. while (pos < 0) pos += N*segL;
  82.  
  83. app.clear();
  84. int startPos = pos/segL;
  85. int camH = 1500 + lines[startPos].y;
  86. float x = 0, dx = 0; // curvature offset, dx = rate of change of the curvature
  87. int maxy = height;
  88.  
  89. for (int n = startPos; n < startPos+300; n++)
  90. {
  91. Line &l = lines[n%N];
  92. l.project(playerX - x, camH, pos - (n>=N?N*segL:0));
  93. x += dx;
  94. dx += l.curve;
  95.  
  96. if (l.Y >= maxy) continue;
  97. maxy = l.Y;
  98.  
  99. Color grass = (n/3)%2?Color(16,200,16):Color(0,154,0);
  100. Color rumble = (n/3)%2?Color(255,255,255):Color(0,0,0);
  101. Color road = (n/3)%2?Color(107,107,107):Color(105,105,105);
  102.  
  103. Line p = lines[(n-1)%N]; // prev line
  104.  
  105. drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
  106. drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
  107. drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
  108. }
  109.  
  110. app.display();
  111. }
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment