Advertisement
Guest User

SFMLApp.cpp

a guest
Nov 25th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include "SFMLApp.h"
  2. #include "Tile.h"
  3.  
  4. #include <ctime>
  5. #include <cstdlib>
  6.  
  7. TextureManager* SFMLApp::textureManager = new TextureManager();
  8.  
  9. SFMLApp::SFMLApp() {
  10.    
  11. }
  12.  
  13. SFMLApp::~SFMLApp() {
  14.     delete this->window;
  15.     delete this->level;
  16. }
  17.  
  18. bool SFMLApp::Init() {
  19.     // Initialize the rendering window.
  20.     this->window = new sf::RenderWindow(sf::VideoMode(SFMLApp::WINDOW_WIDTH, SFMLApp::WINDOW_HEIGHT, 32), "SFML Tiles");
  21.     this->viewport.reset(sf::FloatRect(0, 0, SFMLApp::WINDOW_WIDTH, SFMLApp::WINDOW_HEIGHT));
  22.  
  23.     // Load the textures into the texture manager.
  24.     SFMLApp::textureManager->AddTexture("WoodTexture01.png", "Wood01");
  25.     SFMLApp::textureManager->AddTexture("FogMesh01.png", "FogMesh");
  26.  
  27.     // Set the default size for the map and generate a new level.
  28.     this->level = new Level(50, 50);
  29.     this->GenerateLevel();
  30.  
  31.     // Tells the Execute() method whether or not the window was successfully initialized.
  32.     return (this->window != NULL);
  33. }
  34.  
  35. void SFMLApp::HandleEvent(sf::Event* event) {
  36.     // If the player closes the window, set the running flag to false.
  37.     if(event->type == sf::Event::Closed) {
  38.         this->running = false;
  39.     }
  40.  
  41.     if(event->type == sf::Event::KeyPressed) {
  42.         // If the Escape key is pressed, set the running flag to false.
  43.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) this->running = false;
  44.  
  45.         // If the arrow keys are pressed, flag them.
  46.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) this->keys[0] = true;
  47.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) this->keys[1] = true;
  48.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) this->keys[2] = true;
  49.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) this->keys[3] = true;
  50.  
  51.         // If the space bar is pressed, generate a new level.
  52.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
  53.             delete this->level;
  54.             this->level = new Level(50, 50);
  55.             this->GenerateLevel();
  56.         }
  57.     }
  58.  
  59.     if(event->type == sf::Event::KeyReleased) {
  60.         // If the arrow keys are released, unflag them.
  61.         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up) && this->keys[0]) this->keys[0] = false;
  62.         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right) && this->keys[1]) this->keys[1] = false;
  63.         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down) && this->keys[2]) this->keys[2] = false;
  64.         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left) && this->keys[3]) this->keys[3] = false;
  65.     }
  66. }
  67.  
  68. void SFMLApp::Update(sf::Time elapsed) {
  69.     // If the arrow keys are pressed, move the viewport.
  70.     if(this->keys[0]) this->viewport.move(0.0f, 2.0f);
  71.     if(this->keys[1]) this->viewport.move(-2.0f, 0.0f);
  72.     if(this->keys[2]) this->viewport.move(0.0f, -2.0f);
  73.     if(this->keys[3]) this->viewport.move(2.0f, 0.0f);
  74. }
  75.  
  76. void SFMLApp::Render() {
  77.     this->window->clear(sf::Color::Black);
  78.  
  79.     this->window->setView(this->viewport);
  80.     this->level->DrawTiles(this->window);
  81.     this->window->setView(this->window->getDefaultView());
  82.  
  83.     this->window->display();
  84. }
  85.  
  86. void SFMLApp::Cleanup() {
  87.    
  88. }
  89.  
  90. void SFMLApp::GenerateLevel() {
  91.     srand(time(NULL));
  92.  
  93.     sf::Vector2f position = sf::Vector2f((SFMLApp::WINDOW_WIDTH / 2), (SFMLApp::WINDOW_HEIGHT / 2));
  94.  
  95.     int cX = 9;
  96.     int cY = 14;
  97.  
  98.     int xDir = 0;
  99.     int yDir = -1;
  100.  
  101.     Tile startTile = Tile(position.x, position.y, SFMLApp::textureManager->GetTexture("Wood01"));
  102.     startTile.ChangeState(TileState::VISIBLE);
  103.     this->level->AddTile(cX, cY, &startTile);
  104.  
  105.     for(int i = 0; i < 120; i++) {
  106.         bool turn = (((rand() % 20) + 1) > 9);
  107.         if(turn) {
  108.             int direction = (((rand() % 2) == 0)?-1:1);
  109.             if(xDir != 0) {
  110.                 xDir = 0;
  111.                 yDir = direction;
  112.             } else {
  113.                 yDir = 0;
  114.                 xDir = direction;
  115.             }
  116.         }
  117.  
  118.         xDir = ((((cX + xDir) >= 0) && ((cX + xDir) < this->level->GetWidth()))?xDir:-xDir);
  119.         yDir = ((((cY + yDir) >= 0) && ((cY + yDir) < this->level->GetHeight()))?yDir:-yDir);
  120.         cX += xDir;
  121.         cY += yDir;
  122.         position.x += ((float)xDir * Tile::TILE_SIZE);
  123.         position.y += ((float)yDir * Tile::TILE_SIZE);
  124.  
  125.         Tile temp = Tile(position.x, position.y, SFMLApp::textureManager->GetTexture("Wood01"));
  126.         temp.ChangeState(TileState::REVEALED);
  127.         this->level->AddTile(cX, cY, &temp);
  128.     }
  129.  
  130.     this->viewport.setCenter(startTile.GetPosition());
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement