Advertisement
SilverTES

SFML TEST

Apr 15th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Network.hpp>
  4.  
  5. using namespace std;
  6.  
  7. sf::View view;
  8. int resX = 640;
  9. int resY = 360;
  10.  
  11. template <typename T> void console_log(T msg) {
  12.     cout << msg << endl;
  13. }
  14.  
  15. class Ball {
  16.     private:
  17.         int x, y;
  18.         int vx, vy;
  19.         int speed;
  20.         int a;
  21.         int top,left,bottom,right;
  22.  
  23.         sf::CircleShape shape;
  24.  
  25.     public:
  26.         Ball() {}
  27.         Ball(int x, int y, int r, int speed=1, int vx=0, int vy=0, sf::Color colorFill=sf::Color(255,255,255)) {
  28.  
  29.             this->x = x;
  30.             this->y = y;
  31.             this->speed = speed;
  32.             this->vx = vx;
  33.             this->vy = vy;
  34.             this->a = 0;
  35.             this->top = 0;
  36.             this->left = 0;
  37.             this->bottom = resX;
  38.             this->right = resY;
  39.  
  40.             this->shape.setRadius(r);
  41.             this->shape.setPointCount(6);
  42.  
  43.             this->shape.setOrigin(sf::Vector2f(r,r));
  44.  
  45.             this->shape.setFillColor(colorFill);
  46.             //this->shape.setOutlineColor(colorLine);
  47.             //this->shape.setOutlineThickness(2.f);
  48.         }
  49.         ~Ball() {}
  50.  
  51.         void update() {
  52.             x += vx;
  53.             y += vy;
  54.             a += 4;
  55.  
  56.             if (a>360) a=0;
  57.  
  58.             if (x<top)   vx = -vx;
  59.             if (x>resX)  vx = -vx;
  60.             if (y<top)    vy = -vy;
  61.             if (y>resY) vy = -vy;
  62.  
  63.             shape.setRotation(a);
  64.             shape.setPosition(sf::Vector2f(x,y));
  65.         }
  66.  
  67.         void draw(sf::RenderWindow &window) {
  68.             window.draw(shape);
  69.         }
  70.  
  71. };
  72.  
  73. sf::View getLetterboxView(sf::View view, int windowWidth, int windowHeight) {
  74.  
  75.     // Compares the aspect ratio of the window to the aspect ratio of the view,
  76.     // and sets the view's viewport accordingly in order to archieve a letterbox effect.
  77.     // A new view (with a new viewport set) is returned.
  78.  
  79.     float windowRatio = windowWidth / (float) windowHeight;
  80.     float viewRatio = view.getSize().x / (float) view.getSize().y;
  81.     float sizeX = 1;
  82.     float sizeY = 1;
  83.     float posX = 0;
  84.     float posY = 0;
  85.  
  86.     bool horizontalSpacing = true;
  87.     if (windowRatio < viewRatio)
  88.         horizontalSpacing = false;
  89.  
  90.     // If horizontalSpacing is true, the black bars will appear on the left and right side.
  91.     // Otherwise, the black bars will appear on the top and bottom.
  92.  
  93.     if (horizontalSpacing) {
  94.         sizeX = viewRatio / windowRatio;
  95.         posX = (1 - sizeX) / 2.0;
  96.         //view.zoom(1.f);
  97.     }
  98.  
  99.     else {
  100.         sizeY = windowRatio / viewRatio;
  101.         posY = (1 - sizeY) / 2.0;
  102.         //view.zoom(1.f);
  103.  
  104.     }
  105.  
  106.  
  107.     view.setViewport( sf::FloatRect(posX, posY, sizeX, sizeY) );
  108.  
  109.     return view;
  110. }
  111.  
  112. void getEvent(sf::Window &window);
  113.  
  114. int main() {
  115.  
  116. // Create a window
  117.  
  118.     sf::RenderWindow window( sf::VideoMode(resX, resY), "Letterbox", (sf::Style::Resize + sf::Style::Close) );
  119.  
  120.     view.setSize( resX, resY );
  121.     view.setCenter( view.getSize().x / 2, view.getSize().y / 2 );
  122.     view = getLetterboxView( view, resX, resY );
  123.  
  124.     window.setFramerateLimit(60);
  125.     //window.setVerticalSyncEnabled(false);
  126.  
  127.     sf::CircleShape shape(32.f,6);
  128.  
  129.     //shape.setRadius(32.f);
  130.     shape.setOrigin(sf::Vector2f(32,32));
  131.     shape.setFillColor(sf::Color(20,56,220));
  132.     shape.setOutlineColor(sf::Color(250,250,100));
  133.     shape.setOutlineThickness(2.f);
  134.  
  135.     //sf::Time myTime = sf::milliseconds(10);
  136.     //sf::Vector2f cPos = sf::Vector2f(100,100);
  137.  
  138.     const int MAX_BALL = 400;
  139.  
  140.     int x(100);
  141.     int y(100);
  142.  
  143.     int vx(1);
  144.     int vy(1);
  145.  
  146.     int a(0);
  147.  
  148.     vector<Ball> ballArray;
  149.  
  150.     srand(time(NULL));
  151.     for (int i=0; i<MAX_BALL; i++) {
  152.         int x = rand()% resX;
  153.         int y = rand()% resY;
  154.         int r = rand()% 16+1;
  155.         int speed = rand() % 1 + 1;
  156.         int vx = rand()% 1 + 1;
  157.         int vy = rand()% 1 + 1;
  158.  
  159.         int cr = rand()% 255+1;
  160.         int cg = rand()% 255+1;
  161.         int cb = rand()% 255+1;
  162.  
  163.         sf::Color colorFill(cr,cg,cb);
  164.  
  165.         Ball myBall(x,y,r,speed,vx,vy,colorFill);
  166.         ballArray.push_back(myBall);
  167.     }
  168.  
  169.     vector<Ball>::iterator indexBall = ballArray.begin();
  170.  
  171.     // création d'un tableau de 3 vertex définissant un triangle
  172.     sf::VertexArray triangle(sf::Triangles, 3);
  173.  
  174.     // on définit la position des sommets du triangle
  175.     triangle[0].position = sf::Vector2f(10, 10);
  176.     triangle[1].position = sf::Vector2f(100, 10);
  177.     triangle[2].position = sf::Vector2f(100, 100);
  178.  
  179.     // on définit la couleur des sommets du triangle
  180.     triangle[0].color = sf::Color::Red;
  181.     triangle[1].color = sf::Color::Blue;
  182.     triangle[2].color = sf::Color::Green;
  183.  
  184.     vector<sf::Vertex> vertices;
  185.  
  186.     int cr = rand()% 255+1, cg = rand()% 255+1, cb = rand()% 255+1;
  187.  
  188.     for (int i=0; i<1200; i++) {
  189.         int x = rand()% resX;
  190.         int y = rand()% resY;
  191.         int r = rand()% 16+1;
  192.  
  193.         sf::Vertex vertex;
  194.  
  195.         cr = rand()% 255+1;
  196.         cg = rand()% 255+1;
  197.         cb = rand()% 255+1;
  198.         vertex.position = sf::Vector2f(x,y-r);
  199.         vertex.color = sf::Color(cr,cg,cb);
  200.         vertices.push_back(vertex);
  201.  
  202.         cr = rand()% 255+1;
  203.         cg = rand()% 255+1;
  204.         cb = rand()% 255+1;
  205.         vertex.position = sf::Vector2f(x+r,y);
  206.         vertex.color = sf::Color(cr,cg,cb);
  207.         vertices.push_back(vertex);
  208.  
  209.         cr = rand()% 255+1;
  210.         cg = rand()% 255+1;
  211.         cb = rand()% 255+1;
  212.         vertex.position = sf::Vector2f(x,y+r);
  213.         vertex.color = sf::Color(cr,cg,cb);
  214.         vertices.push_back(vertex);
  215.  
  216.         cr = rand()% 255+1;
  217.         cg = rand()% 255+1;
  218.         cb = rand()% 255+1;
  219.         vertex.position = sf::Vector2f(x-r,y);
  220.         vertex.color = sf::Color(cr,cg,cb);
  221.         vertices.push_back(vertex);
  222.     }
  223.  
  224.     //window.setMouseCursorVisible(false);*
  225.  
  226.     sf::Texture myTexture;
  227.  
  228.     if (!myTexture.loadFromFile("test.png")) {
  229.         cout << "Loading Error : test.png " << endl ;
  230.     }
  231.     //myTexture.setSmooth(true);
  232.  
  233.     sf::Sprite mySprite;
  234.     mySprite.setTexture(myTexture);
  235.  
  236.     mySprite.setColor(sf::Color(0,255,0));
  237.     mySprite.setColor(sf::Color(255,255,255,128));
  238.  
  239.     mySprite.setPosition(sf::Vector2f(100,100));
  240.  
  241.  
  242. // Connexion à un serveur distant !
  243.  
  244.     sf::TcpSocket socket;
  245.     sf::Socket::Status status = socket.connect("127.0.0.1", 53000);
  246.     if (status != sf::Socket::Done){
  247.         cout <<" Connexion impossible..."<< endl;
  248.     }
  249.     cout <<" Adress du serveur : "<< socket.getRemoteAddress()<< endl;
  250.     cout <<" Port ouvert : "<< socket.getRemotePort()<< endl;
  251.  
  252.  
  253.  
  254.     while (window.isOpen()) {
  255.  
  256.         getEvent(window);
  257.  
  258.         indexBall = ballArray.begin();
  259.         int i=0;
  260.         while (indexBall != ballArray.end()) {
  261.             ballArray[i].update();
  262.             indexBall++;
  263.             i++;
  264.         }
  265.  
  266.         x += vx;
  267.         y += vy;
  268.         a += 4;
  269.  
  270.         if (a>360) a=0;
  271.  
  272.         if (x>resX) vx =-1;
  273.         if (x<0)   vx = 1;
  274.         if (y>resY) vy =-1;
  275.         if (y<0)   vy = 1;
  276.  
  277.         //shape.setPosition(cPos);
  278.         shape.setRotation(a);
  279.         shape.setPosition(sf::Vector2f(x,y));
  280.  
  281.         window.clear();
  282.         window.setView(view);
  283.  
  284.        window.draw(mySprite);
  285.  
  286.         //window.draw(triangle);
  287.         window.draw(&vertices[0], vertices.size(), sf::Quads);
  288.  
  289.         window.draw(shape);
  290.  
  291.         indexBall = ballArray.begin();
  292.         i=0;
  293.         while (indexBall != ballArray.end()) {
  294.             ballArray[i].draw(window);
  295.             indexBall++;
  296.             i++;
  297.         }
  298.  
  299.  
  300.  
  301.         window.display();
  302.     }
  303.  
  304.     return 0;
  305. }
  306.  
  307. void getEvent(sf::Window &window) {
  308.  
  309.     sf::Event event;
  310.  
  311.     while (window.pollEvent(event)) {
  312.         if (event.type == sf::Event::Closed)
  313.             window.close();
  314.  
  315.         if (event.type == sf::Event::KeyPressed) {
  316.             console_log("> Key was pressed !");
  317.             switch (event.key.code) {
  318.                 case sf::Keyboard::Escape:
  319.                     window.close();
  320.                     break;
  321.                 default:
  322.                     break;
  323.             }
  324.         }
  325.  
  326.         if (event.type == sf::Event::KeyReleased) {
  327.             console_log("> Key was released !");
  328.  
  329.             switch (event.key.code) {
  330.                 case sf::Keyboard::Return:
  331.                     console_log("> Return was released !");
  332.                     break;
  333.                 default:
  334.                     break;
  335.             }
  336.         }
  337.  
  338.  
  339.  
  340.         if (event.type == sf::Event::MouseMoved) {
  341. //            int mouseX = event.mouseMove.x;
  342. //            int mouseY = event.mouseMove.y;
  343. //
  344. //            cout << "mouse pos : " << mouseX << " | " << mouseY << endl;
  345.         }
  346.  
  347.  
  348.  
  349.         if (event.type == sf::Event::Resized)
  350.             view = getLetterboxView( view, event.size.width, event.size.height );
  351.  
  352.     }
  353.  
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement