Advertisement
Necto

15 tiles sfml

Aug 6th, 2022 (edited)
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. int WinMain()
  7. {
  8.     std::srand(std::time(nullptr));
  9.     sf::RenderWindow gameWindow;
  10.     sf::RenderWindow picWindow;
  11.     gameWindow.create(sf::VideoMode(600, 600), "");
  12.     picWindow.create(sf::VideoMode(600, 600), "");
  13.     gameWindow.setFramerateLimit(60);
  14.     sf::Event event;
  15.  
  16.     sf::Image img;
  17.     img.loadFromFile("test.jpg");
  18.     sf::Texture fullimgtext;
  19.     fullimgtext.loadFromImage(img);
  20.     sf::Sprite fullsprite;
  21.     fullsprite.setTexture(fullimgtext);
  22.     int factor = img.getSize().x / 4;
  23.     fullsprite.setScale((600.f / 4) / factor, (600.f / 4) / factor);
  24.    
  25.     std::vector <sf::Sprite> sprite_tiles(16);
  26.     std::vector <sf::Texture> text_tiles(16);
  27.  
  28.     sf::Image small_img;
  29.  
  30.     small_img.create(factor, factor, sf::Color::Green);
  31.  
  32.     for (int m = 0; m < 4; m++) {
  33.         for (int n = 0; n < 4; n++) {
  34.             for (int i = 0; i < factor; ++i) {
  35.                 for (int j = 0; j < factor; ++j) {
  36.                     small_img.setPixel(i, j, img.getPixel(i + n * factor, j + m * factor));
  37.                 }
  38.             }
  39.             text_tiles[n*4+m].loadFromImage(small_img);
  40.             sprite_tiles[n * 4 + m].setTexture(text_tiles[n * 4 + m]);
  41.             sprite_tiles[n * 4 + m].setScale((600.f / 4) / factor, (600.f / 4) / factor);
  42.             sf::Vector2f pos;
  43.             pos.x = n*150.f;
  44.             pos.y = m*150.f;           
  45.             sprite_tiles[n * 4 + m].setPosition(pos);
  46.             sprite_tiles.push_back(sprite_tiles[n * 4 + m]);
  47.         }
  48.     }
  49.  
  50.     sf::Image img_void;
  51.     img_void.create(600, 600, sf::Color::Green);
  52.     sf::Texture tex_void;
  53.     tex_void.loadFromImage(img_void);
  54.     int void_index = 15;
  55.     text_tiles[15].swap(tex_void);
  56.  
  57.     sf::Vector2i winpos;
  58.     winpos.x = 355.f;
  59.     winpos.y = 240.f;
  60.     gameWindow.setPosition(winpos);
  61.     winpos.x = 955.f;
  62.     picWindow.setPosition(winpos);
  63.  
  64.     sf::Text text;
  65.     sf::Font font;
  66.     font.loadFromFile("calibri.ttf");
  67.     text.setFont(font);
  68.     text.setString("press f2 to mix tiles");
  69.  
  70.     while (gameWindow.isOpen() || picWindow.isOpen()) {
  71.         while (gameWindow.pollEvent(event) || picWindow.pollEvent(event)) {
  72.             if (event.type == sf::Event::Closed) {
  73.                 gameWindow.close();
  74.                 picWindow.close();
  75.             }
  76.             if (event.key.code == sf::Keyboard::F2) {
  77.                 for (int i = 0; i < 50; ++i) {
  78.                     int a = std::rand() % 15;
  79.                     int b = std::rand() % 15;
  80.                     text_tiles[a].swap(text_tiles[b]);
  81.                 }
  82.             }
  83.             if (event.type == sf::Event::KeyReleased)
  84.             {
  85.  
  86.  
  87.                 if (event.key.code == sf::Keyboard::Left && void_index / 4 != 3) {
  88.                     if (void_index < 16)
  89.                     {
  90.                         text_tiles[void_index + 4].swap(text_tiles[void_index]);
  91.                         void_index = void_index + 4;
  92.                     }
  93.                 }
  94.                 if (event.key.code == sf::Keyboard::Right && void_index / 4 != 0) {
  95.                     if (void_index >= 0)
  96.                     {
  97.                         text_tiles[void_index - 4].swap(text_tiles[void_index]);
  98.                         void_index = void_index - 4;
  99.                     }
  100.                 }
  101.                 if (event.key.code == sf::Keyboard::Up && void_index % 4 != 3) {
  102.                     if (void_index < 16)
  103.                     {
  104.                         text_tiles[void_index + 1].swap(text_tiles[void_index]);
  105.                         void_index = void_index + 1;
  106.                     }
  107.                 }
  108.                 if (event.key.code == sf::Keyboard::Down && void_index % 4 != 0) {
  109.                     if (void_index >= 0)
  110.                     {
  111.                         text_tiles[void_index -1].swap(text_tiles[void_index]);
  112.                         void_index = void_index - 1;
  113.                     }
  114.                 }
  115.                
  116.             }
  117.  
  118.         }
  119.  
  120.         gameWindow.clear();
  121.         picWindow.clear();
  122.  
  123.         for (int i = 0; i < 16; ++i)
  124.             gameWindow.draw(sprite_tiles[i]);
  125.         picWindow.draw(fullsprite);
  126.         picWindow.draw(text);
  127.  
  128.         gameWindow.display();
  129.         picWindow.display();
  130.     }
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement