AllenYuan

Untitled

Jun 13th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 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. Texture bg;
  48. bg.loadFromFile("/Users/alleyuan/code-snippets/16_games/08 Outrun/images/bg.png");
  49. bg.setRepeated(true);
  50. Sprite sBackground(bg);
  51. sBackground.setTextureRect(IntRect(0,0,5000,411));
  52. sBackground.setPosition(-2000,0);
  53.  
  54. std::vector<Line> lines;
  55.  
  56. for (int i = 0; i < 1600; i++)
  57. {
  58. Line line;
  59. line.z = i * segL;
  60.  
  61. if (i > 300 && i < 700) line.curve = 0.5;
  62.  
  63. if (i > 750) line.y = sin(i/30.0) * 1500; // add wavy terrain to the end of the track
  64.  
  65. lines.push_back(line);
  66. }
  67.  
  68. int N = lines.size();
  69. int pos = 0;
  70. int playerX = 0;
  71.  
  72. while(app.isOpen())
  73. {
  74. Event e;
  75. while (app.pollEvent(e))
  76. {
  77. if (e.type == Event::Closed)
  78. app.close();
  79. }
  80.  
  81. if (Keyboard::isKeyPressed(Keyboard::Right)) playerX += 200;
  82. if (Keyboard::isKeyPressed(Keyboard::Left)) playerX -= 200;
  83. if (Keyboard::isKeyPressed(Keyboard::Up)) pos += 200;
  84. if (Keyboard::isKeyPressed(Keyboard::Down)) pos -= 200;
  85.  
  86. // loop the track
  87. while (pos >= N*segL) pos -= N*segL;
  88. while (pos < 0) pos += N*segL;
  89.  
  90. app.clear(Color(105,205,4));
  91. app.draw(sBackground);
  92. int startPos = pos/segL;
  93. int camH = 1500 + lines[startPos].y;
  94. float x = 0, dx = 0; // curvature offset, dx = rate of change of the curvature
  95. int maxy = height;
  96.  
  97. for (int n = startPos; n < startPos+300; n++)
  98. {
  99. Line &l = lines[n%N];
  100. l.project(playerX - x, camH, pos - (n>=N?N*segL:0));
  101. x += dx;
  102. dx += l.curve;
  103.  
  104. if (l.Y >= maxy) continue;
  105. maxy = l.Y;
  106.  
  107. Color grass = (n/3)%2?Color(16,200,16):Color(0,154,0);
  108. Color rumble = (n/3)%2?Color(255,255,255):Color(0,0,0);
  109. Color road = (n/3)%2?Color(107,107,107):Color(105,105,105);
  110.  
  111. Line p = lines[(n-1)%N]; // prev line
  112.  
  113. drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
  114. drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
  115. drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
  116. }
  117.  
  118. app.display();
  119. }
  120.  
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment