grzesiek11

BitWorld #1

Jul 17th, 2018
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.81 KB | None | 0 0
  1. //main.cpp
  2.  
  3.  
  4. enum Rarity {COMMON = 0, UNCOMMON = 1, RARE = 2, EPIC = 3, LEGENDARY = 4, EVENT = 5};
  5.  
  6. #include <SFML/Graphics.hpp>
  7. #include <string>
  8. #include <chrono>
  9. #include <sstream>
  10. #include <vector>
  11. #include <fstream>
  12. #include <ctime>
  13. #include <iostream>
  14.  
  15. using namespace sf;
  16.  
  17. #include <block.h>
  18. #include <world.h>
  19. #include <entity.h>
  20. #include <settings.h>
  21.  
  22. int main()
  23. {
  24.     srand(time(NULL));
  25.  
  26.     World world;
  27.     Player player;
  28.     Settings settings;
  29.  
  30.     //Filling world with checkers board
  31.  
  32.     for (int i = 0; i < 100; i++)
  33.     {
  34.         for (int i2 = 0; i2 < 100; i2++)
  35.         {
  36.             if ((i + i2) % 2 == 0)
  37.             {
  38.                 world.setBlock(i, i2, new DirtBlock);
  39.             }
  40.         }
  41.     }
  42.  
  43.     std::ostringstream rStringStream;
  44.     rStringStream << std::rand() % 100;
  45.  
  46.     //Fonts
  47.  
  48.     Font pixel;
  49.     if (!pixel.loadFromFile(settings.res + "font/pixel_arial.ttf"))
  50.     {
  51.         return -1;
  52.     }
  53.  
  54.     Font boldPixel;
  55.     if (!boldPixel.loadFromFile(settings.res + "font/pixel_arial_b.ttf"))
  56.     {
  57.         return -1;
  58.     }
  59.  
  60.     //Player
  61.  
  62.     Texture textureEntityPlayer;
  63.     textureEntityPlayer.setSmooth(false);
  64.     if (!textureEntityPlayer.loadFromFile(player.texture()))
  65.     {
  66.         return -1;
  67.     }
  68.  
  69.     //Block
  70.  
  71.     Texture blockTexture;
  72.     blockTexture.setSmooth(false);
  73.     RectangleShape blockSprite(Vector2f(settings.blockSize, settings.blockSize));
  74.     blockSprite.setTexture(&blockTexture);
  75.  
  76.     //FPS tweaks
  77.  
  78.     RenderWindow window(VideoMode(settings.windowWidth, settings.windowHeight), "BitWorld v. " + settings.version, settings.windowMode);
  79.     if (!settings.maxFps == 0 && !settings.vSync)
  80.     {
  81.         window.setFramerateLimit(settings.maxFps);
  82.     }
  83.     window.setVerticalSyncEnabled(settings.vSync);
  84.  
  85.     Text fpsText;
  86.     fpsText.setFont(pixel);
  87.     fpsText.setCharacterSize(30);
  88.     fpsText.setString("");
  89.     fpsText.setFillColor(Color(255, 255, 255));
  90.  
  91.  
  92.     RectangleShape playerSprite(Vector2f(settings.blockSize, settings.blockSize));
  93.     playerSprite.setTexture(&textureEntityPlayer);
  94.  
  95.     //Game loop
  96.  
  97.     while (window.isOpen())
  98.     {
  99.         //FPS Counter start
  100.  
  101.         auto frameStart = std::chrono::system_clock::now();
  102.  
  103.         Event event;
  104.         while (window.pollEvent(event))
  105.         {
  106.             if (event.type == Event::Closed)
  107.             {
  108.                 window.close();
  109.             }
  110.             if (Keyboard::isKeyPressed(Keyboard::Escape))
  111.             {
  112.                 window.close();
  113.             }
  114.             if (Keyboard::isKeyPressed(Keyboard::F11))
  115.             {
  116.                 switch (settings.windowMode) {
  117.                 case 0:
  118.                     settings.windowMode = 7;
  119.                     break;
  120.  
  121.                 case 7:
  122.                     settings.windowMode = 8;
  123.                     break;
  124.  
  125.                 case 8:
  126.                     settings.windowMode = 0;
  127.                     break;
  128.  
  129.  
  130.                 }
  131.                 RenderWindow window(VideoMode(settings.windowWidth, settings.windowHeight), "BitWorld v. " + settings.version, settings.windowMode);
  132.                 window.setFramerateLimit(settings.maxFps);
  133.                 window.setVerticalSyncEnabled(settings.vSync);
  134.             }
  135.  
  136.         }
  137.  
  138.         std::ostringstream fpsStringStream;
  139.         fpsStringStream << settings.fps;
  140.         std::string fpsString = fpsStringStream.str() + " FPS, R: " + rStringStream.str();
  141.         fpsText.setString(fpsString);
  142.  
  143.         //gametick()
  144.  
  145.         window.clear();
  146.  
  147.         //Render
  148.  
  149.         for (int i = 0; i < settings.windowWidth/settings.blockSize+1; i++)
  150.         {
  151.             for (int i2 = 0; i2 < settings.windowHeight/settings.blockSize+1; i2++)
  152.             {
  153.                 if (!blockTexture.loadFromFile(settings.res + "textures/blocks/" + world.getBlock(i, i2).texture()))
  154.                 {
  155.                     if (!blockTexture.loadFromFile(settings.res + "textures/missing_texture.png"))
  156.                     {
  157.                         return -1;
  158.                     }
  159.                 }
  160.                 blockSprite.setPosition(Vector2f(i*settings.blockSize, i2*settings.blockSize));
  161.                 window.draw(blockSprite);
  162.             }
  163.         }
  164.  
  165.         //window.draw(playerSprite);
  166.         window.draw(fpsText);
  167.  
  168.         window.display();
  169.  
  170.         //FPS Counter update
  171.  
  172.         auto frameEnd = std::chrono::system_clock::now();
  173.         std::chrono::duration<long double> frameDelta = frameEnd - frameStart;
  174.         settings.fps = 1 / frameDelta.count();
  175.     }
  176.  
  177.     return 0;
  178. }
  179.  
  180.  
  181. //world.h
  182.  
  183.  
  184. class World
  185. {
  186.     public:
  187.         World()
  188.         {
  189.             for (int i = 0; i < 100; i++)
  190.             {
  191.                 for (int i2 = 0; i2 < 100; i2++)
  192.                 {
  193.                     setBlock(i, i2, new AirBlock());
  194.                 }
  195.             }
  196.         }
  197.         void setBlock(int x, int y, Block *p_block)
  198.         {
  199.             //delete p_world[x][y];
  200.             p_world[x][y] = p_block;
  201.         }
  202.         Block getBlock(int x, int y)
  203.         {
  204.             return *p_world[x][y];
  205.         }
  206.     private:
  207.         Block *p_world[100][100];
  208. };
  209.  
  210.  
  211. //block.h
  212.  
  213.  
  214. class Block
  215. {
  216.     public:
  217.         int ID()
  218.         {
  219.             return id;
  220.         }
  221.         std::string texture()
  222.         {
  223.             return txtr;
  224.         }
  225.     protected:
  226.         int id;
  227.         std::string txtr;
  228. };
  229.  
  230. class AirBlock : public Block
  231. {
  232.     public:
  233.         AirBlock() {id = 0; txtr = "air_block.png";}
  234. };
  235.  
  236. class DirtBlock : public Block
  237. {
  238.     public:
  239.         DirtBlock() {id = 1; txtr = "dirt_block.png";}
  240. };
  241.  
  242.  
  243. //entity.h
  244.  
  245.  
  246. class Entity
  247. {
  248.     public:
  249.         int ID()
  250.         {
  251.             return id;
  252.         }
  253.         std::string texture()
  254.         {
  255.             return txtr;
  256.         }
  257.         int x()
  258.         {
  259.             return xpos;
  260.         }
  261.         int y()
  262.         {
  263.             return ypos;
  264.         }
  265.     protected:
  266.         int id;
  267.         int xpos;
  268.         int ypos;
  269.         std::string txtr;
  270. };
  271.  
  272. class Player : public Entity
  273. {
  274.     public:
  275.         Player() {id = 1; txtr = "res/textures/entities/player.png"; xpos = 0; ypos = 0;}
  276. };
  277.  
  278.  
  279. //settings.h
  280.  
  281.  
  282. class Settings
  283. {
  284.     public:
  285.         const std::string version = "a1.0.0pre"; //("a" - alpha || "b" - beta)(number - huge update).(number - big uptate).(number - small update)("" || ".(number - patch)" - if patched)("" - normal version || "dev" - unstable, development version (not public) || "pre(number)" - unstable, development version (public))
  286.         int windowWidth = 1280;
  287.         int windowHeight = windowWidth / 16 * 9;
  288.         int fps = 0;
  289.         int maxFps = 60;
  290.         int windowMode = 7; //8 = fullscreen, 7 = windowed, 0 = windowed (without frame)
  291.         bool vSync = true;
  292.         int blockSize = 16;
  293.         std::string res = "res/";
  294. };
Advertisement
Add Comment
Please, Sign In to add comment