Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- enum Rarity {COMMON = 0, UNCOMMON = 1, RARE = 2, EPIC = 3, LEGENDARY = 4, EVENT = 5};
- #include <SFML/Graphics.hpp>
- #include <string>
- #include <chrono>
- #include <sstream>
- #include <vector>
- #include <fstream>
- #include <ctime>
- #include <iostream>
- using namespace sf;
- #include <block.h>
- #include <world.h>
- #include <entity.h>
- #include <settings.h>
- int main()
- {
- srand(time(NULL));
- World world;
- Player player;
- Settings settings;
- //Filling world with checkers board
- for (int i = 0; i < 100; i++)
- {
- for (int i2 = 0; i2 < 100; i2++)
- {
- if ((i + i2) % 2 == 0)
- {
- world.setBlock(i, i2, new DirtBlock);
- }
- }
- }
- std::ostringstream rStringStream;
- rStringStream << std::rand() % 100;
- //Fonts
- Font pixel;
- if (!pixel.loadFromFile(settings.res + "font/pixel_arial.ttf"))
- {
- return -1;
- }
- Font boldPixel;
- if (!boldPixel.loadFromFile(settings.res + "font/pixel_arial_b.ttf"))
- {
- return -1;
- }
- //Player
- Texture textureEntityPlayer;
- textureEntityPlayer.setSmooth(false);
- if (!textureEntityPlayer.loadFromFile(player.texture()))
- {
- return -1;
- }
- //Block
- Texture blockTexture;
- blockTexture.setSmooth(false);
- RectangleShape blockSprite(Vector2f(settings.blockSize, settings.blockSize));
- blockSprite.setTexture(&blockTexture);
- //FPS tweaks
- RenderWindow window(VideoMode(settings.windowWidth, settings.windowHeight), "BitWorld v. " + settings.version, settings.windowMode);
- if (!settings.maxFps == 0 && !settings.vSync)
- {
- window.setFramerateLimit(settings.maxFps);
- }
- window.setVerticalSyncEnabled(settings.vSync);
- Text fpsText;
- fpsText.setFont(pixel);
- fpsText.setCharacterSize(30);
- fpsText.setString("");
- fpsText.setFillColor(Color(255, 255, 255));
- RectangleShape playerSprite(Vector2f(settings.blockSize, settings.blockSize));
- playerSprite.setTexture(&textureEntityPlayer);
- //Game loop
- while (window.isOpen())
- {
- //FPS Counter start
- auto frameStart = std::chrono::system_clock::now();
- Event event;
- while (window.pollEvent(event))
- {
- if (event.type == Event::Closed)
- {
- window.close();
- }
- if (Keyboard::isKeyPressed(Keyboard::Escape))
- {
- window.close();
- }
- if (Keyboard::isKeyPressed(Keyboard::F11))
- {
- switch (settings.windowMode) {
- case 0:
- settings.windowMode = 7;
- break;
- case 7:
- settings.windowMode = 8;
- break;
- case 8:
- settings.windowMode = 0;
- break;
- }
- RenderWindow window(VideoMode(settings.windowWidth, settings.windowHeight), "BitWorld v. " + settings.version, settings.windowMode);
- window.setFramerateLimit(settings.maxFps);
- window.setVerticalSyncEnabled(settings.vSync);
- }
- }
- std::ostringstream fpsStringStream;
- fpsStringStream << settings.fps;
- std::string fpsString = fpsStringStream.str() + " FPS, R: " + rStringStream.str();
- fpsText.setString(fpsString);
- //gametick()
- window.clear();
- //Render
- for (int i = 0; i < settings.windowWidth/settings.blockSize+1; i++)
- {
- for (int i2 = 0; i2 < settings.windowHeight/settings.blockSize+1; i2++)
- {
- if (!blockTexture.loadFromFile(settings.res + "textures/blocks/" + world.getBlock(i, i2).texture()))
- {
- if (!blockTexture.loadFromFile(settings.res + "textures/missing_texture.png"))
- {
- return -1;
- }
- }
- blockSprite.setPosition(Vector2f(i*settings.blockSize, i2*settings.blockSize));
- window.draw(blockSprite);
- }
- }
- //window.draw(playerSprite);
- window.draw(fpsText);
- window.display();
- //FPS Counter update
- auto frameEnd = std::chrono::system_clock::now();
- std::chrono::duration<long double> frameDelta = frameEnd - frameStart;
- settings.fps = 1 / frameDelta.count();
- }
- return 0;
- }
- //world.h
- class World
- {
- public:
- World()
- {
- for (int i = 0; i < 100; i++)
- {
- for (int i2 = 0; i2 < 100; i2++)
- {
- setBlock(i, i2, new AirBlock());
- }
- }
- }
- void setBlock(int x, int y, Block *p_block)
- {
- //delete p_world[x][y];
- p_world[x][y] = p_block;
- }
- Block getBlock(int x, int y)
- {
- return *p_world[x][y];
- }
- private:
- Block *p_world[100][100];
- };
- //block.h
- class Block
- {
- public:
- int ID()
- {
- return id;
- }
- std::string texture()
- {
- return txtr;
- }
- protected:
- int id;
- std::string txtr;
- };
- class AirBlock : public Block
- {
- public:
- AirBlock() {id = 0; txtr = "air_block.png";}
- };
- class DirtBlock : public Block
- {
- public:
- DirtBlock() {id = 1; txtr = "dirt_block.png";}
- };
- //entity.h
- class Entity
- {
- public:
- int ID()
- {
- return id;
- }
- std::string texture()
- {
- return txtr;
- }
- int x()
- {
- return xpos;
- }
- int y()
- {
- return ypos;
- }
- protected:
- int id;
- int xpos;
- int ypos;
- std::string txtr;
- };
- class Player : public Entity
- {
- public:
- Player() {id = 1; txtr = "res/textures/entities/player.png"; xpos = 0; ypos = 0;}
- };
- //settings.h
- class Settings
- {
- public:
- 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))
- int windowWidth = 1280;
- int windowHeight = windowWidth / 16 * 9;
- int fps = 0;
- int maxFps = 60;
- int windowMode = 7; //8 = fullscreen, 7 = windowed, 0 = windowed (without frame)
- bool vSync = true;
- int blockSize = 16;
- std::string res = "res/";
- };
Advertisement
Add Comment
Please, Sign In to add comment