summaryrefslogtreecommitdiff
path: root/core/src/cpp/map.h
blob: 668305bb73f942de44b6e9bce49894955fb14cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include <cassert>

#include <SDL.h>

#include "size2d.h"
#include "sprite.h"

struct Map {
    explicit Map(SDL_Renderer* renderer);

    // draw the level in the renderer window
    void draw();

    int get(const int i, const int j) const { // retreive the cell, transform character to texture index
        assert(i >= 0 && j >= 0 && i < size.w && j < size.h);
        return level[i + j * size.w] - '0';
    }

    bool is_empty(const int i, const int j) const {
        assert(i >= 0 && j >= 0 && i < size.w && j < size.h);
        return level[i + j * size.w] == ' ';
    }

    /*!
     * \brief renderer - draw here
     */
    SDL_Renderer* renderer{nullptr};
    /*!
     * \brief tile_size - tile size in the renderer window
     */
    size2d<int> tile_size{0, 0};

    /*!
     * \brief textures - textures to be drawn
     */
    const Sprite textures;

    /*!
     * \brief size - overall map dimensions
     */
    static constexpr size2d<int> size{16, 12};

    /*!
     * \brief level - the array level[] has the length
     * w*h+1 (+1 for the null character), space character for empty tiles,
     * digits indicate the texture index to be used per tile
     */
    static constexpr char level[size.w * size.h + 1] = " 123451234012340"
                                                       "5              5"
                                                       "0              0"
                                                       "5          5   5"
                                                       "0          0   0"
                                                       "512340   12345 5"
                                                       "0              0"
                                                       "5             51"
                                                       "0     50      12"
                                                       "5          51234"
                                                       "0          12345"
                                                       "1234012345052500";
};