Advertisement
vovan333

MarioGame/main.cpp

Apr 29th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SFML\Graphics.hpp>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #define string std::string
  6.  
  7. namespace App
  8. {
  9.     string Map;
  10.     string MapLoadedName;
  11.     int CurrentSpriteOffset;
  12.     sf::Sprite Player;
  13.     sf::Texture PlayerTexture;
  14.     bool PlayerLoaded = false;
  15.     int PlayerPosX = 0;
  16.     int PlayerPosY = 0;
  17.     int MovementUnit = 10;
  18.     int JumpMovementUnit = 16;
  19.     string JumpVelocityDirection = "to-sky";
  20.     int JumpState = 0;
  21.     int MaxJumpState = 5;
  22.     sf::Image TextureImageCache[10];
  23.     char TextureImageCacheTable[10];
  24.     const string MapDirectory = "D:/FUCKING_MARIO_MAPS/";
  25.     const string MapExtension = ".txt";
  26.     const string TextureDirectory = "D:/FUCKING_MARIO_TEXTURES/";
  27.     const string TextureExtension = ".png";
  28.     const int SpriteSizeX = 50;
  29.     const int SpriteSizeY = 50;
  30.  
  31.     sf::RenderWindow* createWindow(int width, int height, char title[])
  32.     {
  33.         sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(width, height), title, sf::Style::Close);
  34.         window->setFramerateLimit(60);
  35.         return window;
  36.     }
  37.  
  38.     void checkWindowEvent(sf::RenderWindow* window, sf::Event windowEvent)
  39.     {
  40.         if (windowEvent.type == sf::Event::Closed)
  41.         {
  42.             window->close();
  43.         }
  44.     }
  45.  
  46.     void print(string data)
  47.     {
  48.         std::cout << data << std::endl;
  49.     }
  50.  
  51.     void print(int data)
  52.     {
  53.         std::cout << data << std::endl;
  54.     }
  55.  
  56.     string getTexturePathBySign(char sign)
  57.     {
  58.         string value;
  59.         switch (sign)
  60.         {
  61.         case 'w':
  62.             value = "wood";
  63.             break;
  64.         case 'd':
  65.             value = "dirt";
  66.             break;
  67.         default:
  68.             value = "empty";
  69.         }
  70.         value = App::TextureDirectory + value + App::TextureExtension;
  71.         return value;
  72.     }
  73.  
  74.     sf::Image getTextureImageByPath(string path)
  75.     {
  76.         sf::Image textureImage;
  77.         textureImage.loadFromFile(path);
  78.         return textureImage;
  79.     }
  80.  
  81.     void incrementSpritePositions(int& currentPosX, int& currentPosY)
  82.     {
  83.         if (App::CurrentSpriteOffset == 16)
  84.         {
  85.             currentPosY += App::SpriteSizeY;
  86.             currentPosX = 0;
  87.             App::CurrentSpriteOffset = 0;
  88.         }
  89.         else
  90.         {
  91.             currentPosX += App::SpriteSizeX;
  92.             App::CurrentSpriteOffset++;
  93.         }
  94.     }
  95.  
  96.     int findTextureImageIndex(char sign)
  97.     {
  98.         int indexCount = 0;
  99.         for (char signInTable : App::TextureImageCacheTable)
  100.         {
  101.             if (sign == signInTable)
  102.             {
  103.                 return indexCount;
  104.             }
  105.             indexCount++;
  106.         }
  107.     }
  108.  
  109.     sf::Image getTextureImageFromCache(char sign)
  110.     {
  111.         int index = findTextureImageIndex(sign);
  112.         App::print(index);
  113.         return App::TextureImageCache[index];
  114.     }
  115.  
  116.     void initTextureImageCacheTable()
  117.     {
  118.         App::TextureImageCache[0] = App::getTextureImageByPath(App::getTexturePathBySign('e'));
  119.         App::TextureImageCacheTable[0] = 'e';
  120.  
  121.         App::TextureImageCache[1] = App::getTextureImageByPath(App::getTexturePathBySign('w'));
  122.         App::TextureImageCacheTable[1] = 'w';
  123.  
  124.         App::TextureImageCache[2] = App::getTextureImageByPath(App::getTexturePathBySign('d'));
  125.         App::TextureImageCacheTable[2] = 'd';
  126.     }
  127.  
  128.     void drawMap(sf::RenderWindow* window)
  129.     {
  130.         int spritePosX = 0;
  131.         int spritePosY = 0;
  132.  
  133.         for (char sign : App::Map)
  134.         {
  135.             sf::Sprite sprite;
  136.             sf::Texture texture;
  137.             texture.loadFromImage(App::getTextureImageFromCache(sign));
  138.             sprite.setTexture(texture);
  139.             sprite.setPosition(spritePosX, spritePosY);
  140.  
  141.             App::incrementSpritePositions(spritePosX, spritePosY);
  142.             window->draw(sprite);
  143.         }
  144.  
  145.         spritePosX, spritePosY = 0;
  146.     }
  147.  
  148.     string readMap(string mapname)
  149.     {
  150.         string mapDir = App::MapDirectory;
  151.         string mapExt = App::MapExtension;
  152.         string filename = mapDir + mapname + mapExt;
  153.         string line;
  154.         string map;
  155.  
  156.         std::ifstream mapFile;
  157.         mapFile.open(filename);
  158.  
  159.         while (std::getline(mapFile, line))
  160.         {
  161.             map += line;
  162.         }
  163.  
  164.         return map;
  165.     }
  166.  
  167.     void loadMap(string mapname)
  168.     {
  169.         if (App::MapLoadedName != mapname)
  170.         {
  171.             App::Map = App::readMap(mapname);
  172.             App::initTextureImageCacheTable();
  173.             App::MapLoadedName = mapname;
  174.         }
  175.     }
  176.  
  177.     string getPlayerTexturePath()
  178.     {
  179.         return App::TextureDirectory + "player" + App::TextureExtension;
  180.     }
  181.  
  182.     void flipPlayer(string direction)
  183.     {
  184.  
  185.     }
  186.  
  187.     void loadPlayer()
  188.     {
  189.         if (App::PlayerLoaded == false)
  190.         {
  191.             App::PlayerTexture.loadFromFile(App::getPlayerTexturePath());
  192.             App::Player.setTexture(App::PlayerTexture);
  193.             App::PlayerLoaded = true;
  194.         }
  195.     }
  196.  
  197.     bool checkPlayerPos(int posX, int posY)
  198.     {
  199.         return false;
  200.     }
  201.  
  202.     void updatePlayerPosition()
  203.     {
  204.         int posX = App::PlayerPosX;
  205.         int posY = App::PlayerPosY;
  206.  
  207.         if (App::checkPlayerPos(posX, posY))
  208.         {
  209. //          App::Player.setPosition(posX);
  210.         }
  211.     }
  212.  
  213.     void moveLeft()
  214.     {
  215.         App::flipPlayer("left");
  216.         App::PlayerPosX -= App::MovementUnit;
  217.     }
  218.  
  219.     void moveRight()
  220.     {
  221.         App::flipPlayer("right");
  222.         App::PlayerPosX += App::MovementUnit;
  223.         App::updatePlayerPosition();
  224.     }
  225.  
  226.     void moveUp()
  227.     {
  228.         App::PlayerPosY -= App::MovementUnit;
  229.         App::updatePlayerPosition();
  230.     }
  231.  
  232.     void moveDown()
  233.     {
  234.         App::PlayerPosY += App::MovementUnit;
  235.         App::updatePlayerPosition();
  236.     }
  237.  
  238.     void jump()
  239.     {
  240.         if (App::JumpState == App::MaxJumpState)
  241.         {
  242.             App::JumpVelocityDirection = "to-ground";
  243.         }
  244.         if (App::JumpState == 0)
  245.         {
  246.             App::JumpVelocityDirection = "to-sky";
  247.         }
  248.         if(App::JumpVelocityDirection == "to-sky")
  249.         {
  250.             App::PlayerPosY -= App::JumpMovementUnit;
  251.             App::JumpState++;
  252.         }
  253.         if (App::JumpVelocityDirection == "to-ground")
  254.         {
  255.             App::PlayerPosY += App::JumpMovementUnit;
  256.             App::JumpState--;
  257.         }
  258.         App::updatePlayerPosition();
  259.     }
  260.  
  261.     void checkControls()
  262.     {
  263.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  264.         {
  265.             App::moveLeft();
  266.         }
  267.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
  268.         {
  269.             App::moveRight();
  270.         }
  271.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
  272.         {
  273.             App::moveDown();
  274.         }
  275.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
  276.         {
  277.             App::moveUp();
  278.         }
  279.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) | App::JumpState != 0)
  280.         {
  281.             App::jump();
  282.         }
  283.     }
  284.  
  285.     void drawPlayer(sf::RenderWindow* window)
  286.     {
  287.         window->draw(App::Player);
  288.     }
  289.  
  290.     void pollingLoopCallback(sf::RenderWindow* window)
  291.     {
  292.         App::loadMap("default");
  293.         App::drawMap(window);
  294.         App::loadPlayer();
  295.         App::checkControls();
  296.         App::drawPlayer(window);
  297.     }
  298.  
  299.     void initPollingLoop(sf::RenderWindow* window, void(*callback)(sf::RenderWindow*))
  300.     {
  301.         while (window->isOpen())
  302.         {
  303.             sf::Event windowEvent;
  304.             while (window->pollEvent(windowEvent))
  305.             {
  306.                 App::checkWindowEvent(window, windowEvent);
  307.             }
  308.             window->clear(sf::Color::White);
  309.             callback(window);
  310.             window->display();
  311.         }
  312.     }
  313.  
  314.     int init()
  315.     {
  316.         sf::RenderWindow* appWindow = App::createWindow(800, 600, "Mario Game");
  317.         App::initPollingLoop(appWindow, App::pollingLoopCallback);
  318.         return 0;
  319.     }
  320. }
  321.  
  322. int main()
  323. {
  324.     App::init();
  325.     return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement