summaryrefslogtreecommitdiff
path: root/src/editor.c
blob: ac3889176a68d1f510b04141ff66d22551de3b78 (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
64
65
66
67
68
69
70
71
72
73
#include <raylib.h>
#include <stdio.h>

#include "tiled.h"

int selectedTile[2] = {0, 0};

void update(Tiled *tiled) {
    updateTiledCamera(tiled);
    Vector2 mousePos = GetMousePosition();
    Vector2 mapPos = translateTiledPosition(*tiled, mousePos);
    selectedTile[0] = mapPos.x;
    selectedTile[1] = mapPos.y;
}

void drawOverlay(Tiled tiled) {
    Vector2 screenPos = translateTiledScreenPosition(tiled, (Vector2){selectedTile[0], selectedTile[1]});
    
    DrawRectangleLinesEx((Rectangle) {
                screenPos.x,
                screenPos.y,
                tiled.zoom,
                tiled.zoom
            }, tiled.zoom/16, GREEN);
}

void modifyTile(Tiled *tiled, int i) {
    if (selectedTile[0] >= 0 && selectedTile[0] < tiled->tiledMap.width
        && selectedTile[1] >= 0 && selectedTile[1] < tiled->tiledMap.height) {
        int tile = getTiledMapTile(tiled->tiledMap, selectedTile);
        int tileCount = tiled->tiledMap.atlasSize[0] * tiled->tiledMap.atlasSize[1] + 1;
        tile = (tile + i) % (tileCount);
        setTiledMapTile(tiled->tiledMap, selectedTile, tile);
        redrawTiledMap(*tiled);
    }
}

int launchEditor(TiledMap tiledMap) {
    SetConfigFlags(FLAG_WINDOW_RESIZABLE);
    InitWindow(SCREEN_W, SCREEN_H, "tiled");

    Tiled tiled = initTiled(tiledMap);

    while (!WindowShouldClose()) {
        update(&tiled);

        if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
            modifyTile(&tiled, 1);

        if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
            modifyTile(&tiled, -1);

        BeginDrawing();

        ClearBackground(LIGHTGRAY);

        drawTiled(&tiled);
        drawOverlay(tiled);
        DrawFPS(16, 16);

        EndDrawing();
    }

    unloadTiled(&tiled);

    CloseWindow();
    return 0;
}

int main() {
    TiledMap tiledMap = loadTiledMap("map.tiles");
    launchEditor(tiledMap);
}