AllenYuan

Untitled

Jun 13th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 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. app.clear();
  80. int startPos = pos/segL;
  81. int camH = 1500 + lines[startPos].y;
  82. float x = 0, dx = 0; // curvature offset, dx = rate of change of the curvature
  83. int maxy = height;
  84.  
  85. for (int n = startPos; n < startPos+300; n++)
  86. {
  87. Line &l = lines[n%N];
  88. l.project(playerX - x, camH, pos);
  89. x += dx;
  90. dx += l.curve;
  91.  
  92. if (l.Y >= maxy) continue;
  93. maxy = l.Y;
  94.  
  95. Color grass = (n/3)%2?Color(16,200,16):Color(0,154,0);
  96. Color rumble = (n/3)%2?Color(255,255,255):Color(0,0,0);
  97. Color road = (n/3)%2?Color(107,107,107):Color(105,105,105);
  98.  
  99. Line p = lines[(n-1)%N]; // prev line
  100.  
  101. drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
  102. drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
  103. drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
  104. }
  105.  
  106. app.display();
  107. }
  108.  
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment