Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <SFML/Network.hpp>
- using namespace std;
- sf::View view;
- int resX = 640;
- int resY = 360;
- template <typename T> void console_log(T msg) {
- cout << msg << endl;
- }
- class Ball {
- private:
- int x, y;
- int vx, vy;
- int speed;
- int a;
- int top,left,bottom,right;
- sf::CircleShape shape;
- public:
- Ball() {}
- Ball(int x, int y, int r, int speed=1, int vx=0, int vy=0, sf::Color colorFill=sf::Color(255,255,255)) {
- this->x = x;
- this->y = y;
- this->speed = speed;
- this->vx = vx;
- this->vy = vy;
- this->a = 0;
- this->top = 0;
- this->left = 0;
- this->bottom = resX;
- this->right = resY;
- this->shape.setRadius(r);
- this->shape.setPointCount(6);
- this->shape.setOrigin(sf::Vector2f(r,r));
- this->shape.setFillColor(colorFill);
- //this->shape.setOutlineColor(colorLine);
- //this->shape.setOutlineThickness(2.f);
- }
- ~Ball() {}
- void update() {
- x += vx;
- y += vy;
- a += 4;
- if (a>360) a=0;
- if (x<top) vx = -vx;
- if (x>resX) vx = -vx;
- if (y<top) vy = -vy;
- if (y>resY) vy = -vy;
- shape.setRotation(a);
- shape.setPosition(sf::Vector2f(x,y));
- }
- void draw(sf::RenderWindow &window) {
- window.draw(shape);
- }
- };
- sf::View getLetterboxView(sf::View view, int windowWidth, int windowHeight) {
- // Compares the aspect ratio of the window to the aspect ratio of the view,
- // and sets the view's viewport accordingly in order to archieve a letterbox effect.
- // A new view (with a new viewport set) is returned.
- float windowRatio = windowWidth / (float) windowHeight;
- float viewRatio = view.getSize().x / (float) view.getSize().y;
- float sizeX = 1;
- float sizeY = 1;
- float posX = 0;
- float posY = 0;
- bool horizontalSpacing = true;
- if (windowRatio < viewRatio)
- horizontalSpacing = false;
- // If horizontalSpacing is true, the black bars will appear on the left and right side.
- // Otherwise, the black bars will appear on the top and bottom.
- if (horizontalSpacing) {
- sizeX = viewRatio / windowRatio;
- posX = (1 - sizeX) / 2.0;
- //view.zoom(1.f);
- }
- else {
- sizeY = windowRatio / viewRatio;
- posY = (1 - sizeY) / 2.0;
- //view.zoom(1.f);
- }
- view.setViewport( sf::FloatRect(posX, posY, sizeX, sizeY) );
- return view;
- }
- void getEvent(sf::Window &window);
- int main() {
- // Create a window
- sf::RenderWindow window( sf::VideoMode(resX, resY), "Letterbox", (sf::Style::Resize + sf::Style::Close) );
- view.setSize( resX, resY );
- view.setCenter( view.getSize().x / 2, view.getSize().y / 2 );
- view = getLetterboxView( view, resX, resY );
- window.setFramerateLimit(60);
- //window.setVerticalSyncEnabled(false);
- sf::CircleShape shape(32.f,6);
- //shape.setRadius(32.f);
- shape.setOrigin(sf::Vector2f(32,32));
- shape.setFillColor(sf::Color(20,56,220));
- shape.setOutlineColor(sf::Color(250,250,100));
- shape.setOutlineThickness(2.f);
- //sf::Time myTime = sf::milliseconds(10);
- //sf::Vector2f cPos = sf::Vector2f(100,100);
- const int MAX_BALL = 400;
- int x(100);
- int y(100);
- int vx(1);
- int vy(1);
- int a(0);
- vector<Ball> ballArray;
- srand(time(NULL));
- for (int i=0; i<MAX_BALL; i++) {
- int x = rand()% resX;
- int y = rand()% resY;
- int r = rand()% 16+1;
- int speed = rand() % 1 + 1;
- int vx = rand()% 1 + 1;
- int vy = rand()% 1 + 1;
- int cr = rand()% 255+1;
- int cg = rand()% 255+1;
- int cb = rand()% 255+1;
- sf::Color colorFill(cr,cg,cb);
- Ball myBall(x,y,r,speed,vx,vy,colorFill);
- ballArray.push_back(myBall);
- }
- vector<Ball>::iterator indexBall = ballArray.begin();
- // création d'un tableau de 3 vertex définissant un triangle
- sf::VertexArray triangle(sf::Triangles, 3);
- // on définit la position des sommets du triangle
- triangle[0].position = sf::Vector2f(10, 10);
- triangle[1].position = sf::Vector2f(100, 10);
- triangle[2].position = sf::Vector2f(100, 100);
- // on définit la couleur des sommets du triangle
- triangle[0].color = sf::Color::Red;
- triangle[1].color = sf::Color::Blue;
- triangle[2].color = sf::Color::Green;
- vector<sf::Vertex> vertices;
- int cr = rand()% 255+1, cg = rand()% 255+1, cb = rand()% 255+1;
- for (int i=0; i<1200; i++) {
- int x = rand()% resX;
- int y = rand()% resY;
- int r = rand()% 16+1;
- sf::Vertex vertex;
- cr = rand()% 255+1;
- cg = rand()% 255+1;
- cb = rand()% 255+1;
- vertex.position = sf::Vector2f(x,y-r);
- vertex.color = sf::Color(cr,cg,cb);
- vertices.push_back(vertex);
- cr = rand()% 255+1;
- cg = rand()% 255+1;
- cb = rand()% 255+1;
- vertex.position = sf::Vector2f(x+r,y);
- vertex.color = sf::Color(cr,cg,cb);
- vertices.push_back(vertex);
- cr = rand()% 255+1;
- cg = rand()% 255+1;
- cb = rand()% 255+1;
- vertex.position = sf::Vector2f(x,y+r);
- vertex.color = sf::Color(cr,cg,cb);
- vertices.push_back(vertex);
- cr = rand()% 255+1;
- cg = rand()% 255+1;
- cb = rand()% 255+1;
- vertex.position = sf::Vector2f(x-r,y);
- vertex.color = sf::Color(cr,cg,cb);
- vertices.push_back(vertex);
- }
- //window.setMouseCursorVisible(false);*
- sf::Texture myTexture;
- if (!myTexture.loadFromFile("test.png")) {
- cout << "Loading Error : test.png " << endl ;
- }
- //myTexture.setSmooth(true);
- sf::Sprite mySprite;
- mySprite.setTexture(myTexture);
- mySprite.setColor(sf::Color(0,255,0));
- mySprite.setColor(sf::Color(255,255,255,128));
- mySprite.setPosition(sf::Vector2f(100,100));
- // Connexion à un serveur distant !
- sf::TcpSocket socket;
- sf::Socket::Status status = socket.connect("127.0.0.1", 53000);
- if (status != sf::Socket::Done){
- cout <<" Connexion impossible..."<< endl;
- }
- cout <<" Adress du serveur : "<< socket.getRemoteAddress()<< endl;
- cout <<" Port ouvert : "<< socket.getRemotePort()<< endl;
- while (window.isOpen()) {
- getEvent(window);
- indexBall = ballArray.begin();
- int i=0;
- while (indexBall != ballArray.end()) {
- ballArray[i].update();
- indexBall++;
- i++;
- }
- x += vx;
- y += vy;
- a += 4;
- if (a>360) a=0;
- if (x>resX) vx =-1;
- if (x<0) vx = 1;
- if (y>resY) vy =-1;
- if (y<0) vy = 1;
- //shape.setPosition(cPos);
- shape.setRotation(a);
- shape.setPosition(sf::Vector2f(x,y));
- window.clear();
- window.setView(view);
- window.draw(mySprite);
- //window.draw(triangle);
- window.draw(&vertices[0], vertices.size(), sf::Quads);
- window.draw(shape);
- indexBall = ballArray.begin();
- i=0;
- while (indexBall != ballArray.end()) {
- ballArray[i].draw(window);
- indexBall++;
- i++;
- }
- window.display();
- }
- return 0;
- }
- void getEvent(sf::Window &window) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::KeyPressed) {
- console_log("> Key was pressed !");
- switch (event.key.code) {
- case sf::Keyboard::Escape:
- window.close();
- break;
- default:
- break;
- }
- }
- if (event.type == sf::Event::KeyReleased) {
- console_log("> Key was released !");
- switch (event.key.code) {
- case sf::Keyboard::Return:
- console_log("> Return was released !");
- break;
- default:
- break;
- }
- }
- if (event.type == sf::Event::MouseMoved) {
- // int mouseX = event.mouseMove.x;
- // int mouseY = event.mouseMove.y;
- //
- // cout << "mouse pos : " << mouseX << " | " << mouseY << endl;
- }
- if (event.type == sf::Event::Resized)
- view = getLetterboxView( view, event.size.width, event.size.height );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement