#2D Top-Down TexureAtlas & AssetManager

148 messages · Page 1 of 1 (latest)

sudden acorn
#

I want some opinion on this code:

// Block.h
#pragma once

#include <SFML/Graphics/RectangleShape.hpp>
#include <memory>

namespace mb {
    class Block {
    private:
        int x;
        int y;
        sf::RectangleShape shape;
    public:

        Block(int x, int y) : x(x), y(y) {};
        ~Block() noexcept = default;

        void create();

        [[nodiscard]] int getX() const noexcept;

        [[nodiscard]] int getY() const noexcept;

        const sf::RectangleShape &getShape() const;
    };
}```

```C++
// Chunk.h
#pragma once

#include <memory>
#include <SFML/Graphics/RenderWindow.hpp>
#include "block/Block.h"

namespace mb {
    class Chunk {
    public:
        Chunk(int x, int y);

        ~Chunk() noexcept;

        [[nodiscard]] int getX() const noexcept;

        [[nodiscard]] int getY() const noexcept;

        void render(sf::RenderWindow &window) const;

        static const int SIZE = 16;

    private:
        int x;
        int y;
        std::vector<std::vector<std::unique_ptr<Block>>> blocks;

        void generate();
    };
}```
#
// Game.h
#pragma once

#include <memory>
#include "SFML/Graphics.hpp"
#include "chunk/Chunk.h"

namespace mb {
    class Game {
    private:
        std::unique_ptr<sf::RenderWindow> window;
        sf::Event event{};
        sf::VideoMode videoMode;
        std::unique_ptr<Chunk> chunk;
        long long seed{}; // soon
    public:
        Game() noexcept = default;

        ~Game() noexcept;

        [[nodiscard]] bool isRunning() const noexcept;

        void pullEvents() noexcept;

        void create();

        void render() noexcept;

        void update(float dt) noexcept;
    };
}
// Block.cpp
#include <random>
#include "block/Block.h"

namespace mb {
    int Block::getX() const noexcept {
        return this->x;
    }

    int Block::getY() const noexcept {
        return this->y;
    }

    void Block::create() {
        this->shape.setSize(sf::Vector2f(50, 50));
        std::random_device rd;
        std::mt19937 mt(rd());
        std::uniform_int_distribution dist(0, 255);

        this->shape.setFillColor(sf::Color(dist(mt), dist(mt), dist(mt)));
        this->shape.setPosition(sf::Vector2f(
                static_cast<float>(this->getX()),
                static_cast<float>(this->getY())));
    }

    const sf::RectangleShape &Block::getShape() const {
        return this->shape;
    }
}
#
// Chunk.cpp
#include "chunk/Chunk.h"

namespace mb {
    Chunk::~Chunk() noexcept {
        this->x = 0;
        this->y = 0;
    }

    Chunk::Chunk(int x, int y) : x(x), y(y) {
        this->blocks.resize(Chunk::SIZE);
        for (auto& inner : this->blocks) {
            inner.resize(Chunk::SIZE);
        }
        this->generate();
    }

    void Chunk::generate() {
        for (int i = 0; i < Chunk::SIZE; ++i) {
            for (int j = 0; j < Chunk::SIZE; ++j) {
                this->blocks[i][j] = std::make_unique<Block>(getX() + i * 50, getY() + j * 50);
                this->blocks[i][j]->create();
            }
        }
    }

    int Chunk::getX() const noexcept {
        return this->x;
    }

    int Chunk::getY() const noexcept {
        return this->y;
    }

    void Chunk::render(sf::RenderWindow &window) const {
        for (auto & block : this->blocks) {
            for (const auto & j : block) {
                window.draw(j->getShape());
            }
        }
    }

}```
#
// Game.cPP
#include <SFML/Window.hpp>
#include "Game.h"
#include <chrono>

namespace mb {

    void Game::create() {
        this->videoMode = sf::VideoMode(800, 600);
        this->window = std::make_unique<sf::RenderWindow>(this->videoMode, "MiniBlock",
                                                          sf::Style::Titlebar | sf::Style::Close);
        this->seed = std::chrono::system_clock::now().time_since_epoch().count();
        this->chunk = std::make_unique<Chunk>(0, 0);
    }

    void Game::render() noexcept {
        this->window->clear();
        this->chunk->render(*this->window);
        this->window->display();
    }

    void Game::update(float dt) noexcept {
        this->pullEvents();

    }

    bool Game::isRunning() const noexcept {
        return this->window->isOpen();
    }

    void Game::pullEvents() noexcept {
        while (this->window->pollEvent(this->event)) {
            switch (this->event.type) {
                case sf::Event::Closed:
                    this->window->close();
                    break;
                case sf::Event::KeyPressed:
                    if (this->event.key.code == sf::Keyboard::Escape)
                        this->window->close();
                    break;
                default:
                    break;
            }
        }
    }

    Game::~Game() noexcept {
        this->window->close();
        this->window.reset();
        this->chunk.reset();
    }
}
warped merlin
#

you should probably put it on github

sudden acorn
#
// main.cPP
#include "Game.h"

int main() {
    mb::Game game;
    game.create();

    while (game.isRunning()) {
        game.update(0.0f);
        game.render();
    }

    return 0;
}
sudden acorn
warped merlin
#

all the this-> are unnecessary

sudden acorn
#

after speding so much time

#

lemme upload rq

warped merlin
#

Block::create() could probably be a constructor

#

the random should probably be done outside of Block::create() so that you only create a single random device

#

sf::Event event{}; that shouldn't be a member

#

sf::VideoMode videoMode; probably doesn't need to be a member either

#

long long seed{}; // soon I would avoid using long long and use int64_t instead

sudden acorn
#

here code

warped merlin
#

same for constructor, getters

#

same for the chunk

#

both chunk and block could inherit from sf::Drawable

#

instead of having those render/getShape functions

#

std::vector<std::vector<std::unique_ptr<Block>>> blocks; very bad, use a single vector to represent the 2d map

#

also why is it unique_ptr instead of being a value directly?

sudden acorn
warped merlin
warped merlin
sudden acorn
warped merlin
sudden acorn
warped merlin
#

ok

warped merlin
#

most advice on youtube is garbage

sudden acorn
warped merlin
warped merlin
sudden acorn
warped merlin
#

basically the same way

sudden acorn
warped merlin
#

you should probably start with that before making graphic applications

sudden acorn
#

but i don't know how to use vector

#

I was using blocks[][]

#

lol

warped merlin
sudden acorn
#

Cisco courses

#

Random things

#

and Informatic olympics

#

but really basic C++

#

not that advance guides ain't good newdays

warped merlin
#

sounds like outdated resources maybe

sudden acorn
#

Hmm

#

oke

sudden acorn
warped merlin
#

check the official SFML tutorials

sudden acorn
warped merlin
#

depends

#

I would say it's fine for now, but if the only goal was defered initialization maybe I would go for std::optional

sudden acorn
#

If anyone has better class management or anything like that I'm open to suggestions

warped merlin
#

still using a vector of vector

#

still shouldn't do the random in the block itself

#

otherwise I think it's a good start 👍

sudden acorn
#

chunk it's an 2D array grid

#

does vector have it?

#

Because I'm not sure if it's possible, I mean I'm not great with new C++ things, I'm like old fashion guy

sudden acorn
sudden acorn
sudden acorn
#

Hello

#

2D Top-Down TexureAtlas & AssetManager

#

I have added more things, such as TextureAltas, AssetManager, and Custom Block by Ids, can someone help me improve the code for better usability and code matiament?

warped merlin
#
GitHub

Contribute to sdxqw/MiniBlock-CPP development by creating an account on GitHub.

GitHub

Contribute to sdxqw/MiniBlock-CPP development by creating an account on GitHub.

#

Good job on the 2d -> 1d vector by the way 👍

#

actually maybe it's fine idk

sudden acorn
sudden acorn
sudden acorn
sudden acorn
warped merlin
warped merlin
sudden acorn
warped merlin
# sudden acorn what you mean?

like it will only run on each cmake ., so if you have add a new asset you have to remember to re-run the configuration

warped merlin
warped merlin
warped merlin
sudden acorn
warped merlin
warped merlin
sudden acorn
#

std::unordered_map<Block::Type, sf::IntRect> textureRects;

std::unordered_map<Block::Type, sf::IntRect> textureRects;
| ^~~~~
| clock

warped merlin
sudden acorn
warped merlin
#

too bad

sudden acorn
#

error: wrong number of template arguments (1, should be at least 2)
12 | std::unordered_map<Block::Type, sf::IntRect> textureRects;

warped merlin
#

probably missing an include

sudden acorn
#

no sytax error

#

maybe ide is bugged

warped merlin
#

did you add the right includes?

sudden acorn
#

#include <unordered_map>
#include "block/Block.h"

#

I think yes

#

it's doens't like the: Block::Type

#

oh god enums don't have the same logic as Java

warped merlin
#

ah it's because it's an class enum probably

sudden acorn
#

enum class Type {
Dirt = 2,
Grass = 3
};

#

yes

#

nvm

#

the problem is that they depens on each other

#

oh god more complexity

sudden acorn
#

I have updated the code, but seems it's doesn't draw anything expect for white thigns on screen

#

I think i did everything right but seems some refactorying just broke it

#

I do so much C-style errors lol, the things I read are so outdated

warped merlin
sudden acorn
#

you mean vector2i?

#

in the parameters