Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SFMLApp.h"
- #include "Tile.h"
- #include <ctime>
- #include <cstdlib>
- TextureManager* SFMLApp::textureManager = new TextureManager();
- SFMLApp::SFMLApp() {
- }
- SFMLApp::~SFMLApp() {
- delete this->window;
- delete this->level;
- }
- bool SFMLApp::Init() {
- // Initialize the rendering window.
- this->window = new sf::RenderWindow(sf::VideoMode(SFMLApp::WINDOW_WIDTH, SFMLApp::WINDOW_HEIGHT, 32), "SFML Tiles");
- this->viewport.reset(sf::FloatRect(0, 0, SFMLApp::WINDOW_WIDTH, SFMLApp::WINDOW_HEIGHT));
- // Load the textures into the texture manager.
- SFMLApp::textureManager->AddTexture("WoodTexture01.png", "Wood01");
- SFMLApp::textureManager->AddTexture("FogMesh01.png", "FogMesh");
- // Set the default size for the map and generate a new level.
- this->level = new Level(50, 50);
- this->GenerateLevel();
- // Tells the Execute() method whether or not the window was successfully initialized.
- return (this->window != NULL);
- }
- void SFMLApp::HandleEvent(sf::Event* event) {
- // If the player closes the window, set the running flag to false.
- if(event->type == sf::Event::Closed) {
- this->running = false;
- }
- if(event->type == sf::Event::KeyPressed) {
- // If the Escape key is pressed, set the running flag to false.
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) this->running = false;
- // If the arrow keys are pressed, flag them.
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) this->keys[0] = true;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) this->keys[1] = true;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) this->keys[2] = true;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) this->keys[3] = true;
- // If the space bar is pressed, generate a new level.
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
- delete this->level;
- this->level = new Level(50, 50);
- this->GenerateLevel();
- }
- }
- if(event->type == sf::Event::KeyReleased) {
- // If the arrow keys are released, unflag them.
- if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up) && this->keys[0]) this->keys[0] = false;
- if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right) && this->keys[1]) this->keys[1] = false;
- if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down) && this->keys[2]) this->keys[2] = false;
- if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left) && this->keys[3]) this->keys[3] = false;
- }
- }
- void SFMLApp::Update(sf::Time elapsed) {
- // If the arrow keys are pressed, move the viewport.
- if(this->keys[0]) this->viewport.move(0.0f, 2.0f);
- if(this->keys[1]) this->viewport.move(-2.0f, 0.0f);
- if(this->keys[2]) this->viewport.move(0.0f, -2.0f);
- if(this->keys[3]) this->viewport.move(2.0f, 0.0f);
- }
- void SFMLApp::Render() {
- this->window->clear(sf::Color::Black);
- this->window->setView(this->viewport);
- this->level->DrawTiles(this->window);
- this->window->setView(this->window->getDefaultView());
- this->window->display();
- }
- void SFMLApp::Cleanup() {
- }
- void SFMLApp::GenerateLevel() {
- srand(time(NULL));
- sf::Vector2f position = sf::Vector2f((SFMLApp::WINDOW_WIDTH / 2), (SFMLApp::WINDOW_HEIGHT / 2));
- int cX = 9;
- int cY = 14;
- int xDir = 0;
- int yDir = -1;
- Tile startTile = Tile(position.x, position.y, SFMLApp::textureManager->GetTexture("Wood01"));
- startTile.ChangeState(TileState::VISIBLE);
- this->level->AddTile(cX, cY, &startTile);
- for(int i = 0; i < 120; i++) {
- bool turn = (((rand() % 20) + 1) > 9);
- if(turn) {
- int direction = (((rand() % 2) == 0)?-1:1);
- if(xDir != 0) {
- xDir = 0;
- yDir = direction;
- } else {
- yDir = 0;
- xDir = direction;
- }
- }
- xDir = ((((cX + xDir) >= 0) && ((cX + xDir) < this->level->GetWidth()))?xDir:-xDir);
- yDir = ((((cY + yDir) >= 0) && ((cY + yDir) < this->level->GetHeight()))?yDir:-yDir);
- cX += xDir;
- cY += yDir;
- position.x += ((float)xDir * Tile::TILE_SIZE);
- position.y += ((float)yDir * Tile::TILE_SIZE);
- Tile temp = Tile(position.x, position.y, SFMLApp::textureManager->GetTexture("Wood01"));
- temp.ChangeState(TileState::REVEALED);
- this->level->AddTile(cX, cY, &temp);
- }
- this->viewport.setCenter(startTile.GetPosition());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement