Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML\Graphics.hpp>
- #include <fstream>
- #include <iostream>
- #include <string>
- #define string std::string
- namespace App
- {
- string Map;
- string MapLoadedName;
- int CurrentSpriteOffset;
- sf::Sprite Player;
- sf::Texture PlayerTexture;
- bool PlayerLoaded = false;
- int PlayerPosX = 0;
- int PlayerPosY = 0;
- int MovementUnit = 10;
- int JumpMovementUnit = 16;
- string JumpVelocityDirection = "to-sky";
- int JumpState = 0;
- int MaxJumpState = 5;
- sf::Image TextureImageCache[10];
- char TextureImageCacheTable[10];
- const string MapDirectory = "D:/FUCKING_MARIO_MAPS/";
- const string MapExtension = ".txt";
- const string TextureDirectory = "D:/FUCKING_MARIO_TEXTURES/";
- const string TextureExtension = ".png";
- const int SpriteSizeX = 50;
- const int SpriteSizeY = 50;
- sf::RenderWindow* createWindow(int width, int height, char title[])
- {
- sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(width, height), title, sf::Style::Close);
- window->setFramerateLimit(60);
- return window;
- }
- void checkWindowEvent(sf::RenderWindow* window, sf::Event windowEvent)
- {
- if (windowEvent.type == sf::Event::Closed)
- {
- window->close();
- }
- }
- void print(string data)
- {
- std::cout << data << std::endl;
- }
- void print(int data)
- {
- std::cout << data << std::endl;
- }
- string getTexturePathBySign(char sign)
- {
- string value;
- switch (sign)
- {
- case 'w':
- value = "wood";
- break;
- case 'd':
- value = "dirt";
- break;
- default:
- value = "empty";
- }
- value = App::TextureDirectory + value + App::TextureExtension;
- return value;
- }
- sf::Image getTextureImageByPath(string path)
- {
- sf::Image textureImage;
- textureImage.loadFromFile(path);
- return textureImage;
- }
- void incrementSpritePositions(int& currentPosX, int& currentPosY)
- {
- if (App::CurrentSpriteOffset == 16)
- {
- currentPosY += App::SpriteSizeY;
- currentPosX = 0;
- App::CurrentSpriteOffset = 0;
- }
- else
- {
- currentPosX += App::SpriteSizeX;
- App::CurrentSpriteOffset++;
- }
- }
- int findTextureImageIndex(char sign)
- {
- int indexCount = 0;
- for (char signInTable : App::TextureImageCacheTable)
- {
- if (sign == signInTable)
- {
- return indexCount;
- }
- indexCount++;
- }
- }
- sf::Image getTextureImageFromCache(char sign)
- {
- int index = findTextureImageIndex(sign);
- App::print(index);
- return App::TextureImageCache[index];
- }
- void initTextureImageCacheTable()
- {
- App::TextureImageCache[0] = App::getTextureImageByPath(App::getTexturePathBySign('e'));
- App::TextureImageCacheTable[0] = 'e';
- App::TextureImageCache[1] = App::getTextureImageByPath(App::getTexturePathBySign('w'));
- App::TextureImageCacheTable[1] = 'w';
- App::TextureImageCache[2] = App::getTextureImageByPath(App::getTexturePathBySign('d'));
- App::TextureImageCacheTable[2] = 'd';
- }
- void drawMap(sf::RenderWindow* window)
- {
- int spritePosX = 0;
- int spritePosY = 0;
- for (char sign : App::Map)
- {
- sf::Sprite sprite;
- sf::Texture texture;
- texture.loadFromImage(App::getTextureImageFromCache(sign));
- sprite.setTexture(texture);
- sprite.setPosition(spritePosX, spritePosY);
- App::incrementSpritePositions(spritePosX, spritePosY);
- window->draw(sprite);
- }
- spritePosX, spritePosY = 0;
- }
- string readMap(string mapname)
- {
- string mapDir = App::MapDirectory;
- string mapExt = App::MapExtension;
- string filename = mapDir + mapname + mapExt;
- string line;
- string map;
- std::ifstream mapFile;
- mapFile.open(filename);
- while (std::getline(mapFile, line))
- {
- map += line;
- }
- return map;
- }
- void loadMap(string mapname)
- {
- if (App::MapLoadedName != mapname)
- {
- App::Map = App::readMap(mapname);
- App::initTextureImageCacheTable();
- App::MapLoadedName = mapname;
- }
- }
- string getPlayerTexturePath()
- {
- return App::TextureDirectory + "player" + App::TextureExtension;
- }
- void flipPlayer(string direction)
- {
- }
- void loadPlayer()
- {
- if (App::PlayerLoaded == false)
- {
- App::PlayerTexture.loadFromFile(App::getPlayerTexturePath());
- App::Player.setTexture(App::PlayerTexture);
- App::PlayerLoaded = true;
- }
- }
- bool checkPlayerPos(int posX, int posY)
- {
- return false;
- }
- void updatePlayerPosition()
- {
- int posX = App::PlayerPosX;
- int posY = App::PlayerPosY;
- if (App::checkPlayerPos(posX, posY))
- {
- // App::Player.setPosition(posX);
- }
- }
- void moveLeft()
- {
- App::flipPlayer("left");
- App::PlayerPosX -= App::MovementUnit;
- }
- void moveRight()
- {
- App::flipPlayer("right");
- App::PlayerPosX += App::MovementUnit;
- App::updatePlayerPosition();
- }
- void moveUp()
- {
- App::PlayerPosY -= App::MovementUnit;
- App::updatePlayerPosition();
- }
- void moveDown()
- {
- App::PlayerPosY += App::MovementUnit;
- App::updatePlayerPosition();
- }
- void jump()
- {
- if (App::JumpState == App::MaxJumpState)
- {
- App::JumpVelocityDirection = "to-ground";
- }
- if (App::JumpState == 0)
- {
- App::JumpVelocityDirection = "to-sky";
- }
- if(App::JumpVelocityDirection == "to-sky")
- {
- App::PlayerPosY -= App::JumpMovementUnit;
- App::JumpState++;
- }
- if (App::JumpVelocityDirection == "to-ground")
- {
- App::PlayerPosY += App::JumpMovementUnit;
- App::JumpState--;
- }
- App::updatePlayerPosition();
- }
- void checkControls()
- {
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- {
- App::moveLeft();
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
- {
- App::moveRight();
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
- {
- App::moveDown();
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
- {
- App::moveUp();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) | App::JumpState != 0)
- {
- App::jump();
- }
- }
- void drawPlayer(sf::RenderWindow* window)
- {
- window->draw(App::Player);
- }
- void pollingLoopCallback(sf::RenderWindow* window)
- {
- App::loadMap("default");
- App::drawMap(window);
- App::loadPlayer();
- App::checkControls();
- App::drawPlayer(window);
- }
- void initPollingLoop(sf::RenderWindow* window, void(*callback)(sf::RenderWindow*))
- {
- while (window->isOpen())
- {
- sf::Event windowEvent;
- while (window->pollEvent(windowEvent))
- {
- App::checkWindowEvent(window, windowEvent);
- }
- window->clear(sf::Color::White);
- callback(window);
- window->display();
- }
- }
- int init()
- {
- sf::RenderWindow* appWindow = App::createWindow(800, 600, "Mario Game");
- App::initPollingLoop(appWindow, App::pollingLoopCallback);
- return 0;
- }
- }
- int main()
- {
- App::init();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement