summaryrefslogtreecommitdiff
path: root/editor.c
blob: 86f75273a4c93c6280cba8f9dbadcf17c493feac (plain) (blame)
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
74
75
#include "asset.h"
#include "editor.h"
#include "gui.h"
#include "map.h"
#include "plat.h"
#include "render.h"

#define tile_display_size 32

static GUI_Scroll_State map_ss = { 0 };

void draw_map_scrollable(
	GUI* g,
	Renderer* r,
	const Rect* rect,
	const GUI_Scroll_State* ss
) {
	const Map* m = g->uptr;
	const Map_Tile* tiles = map_tilesc(m);
	int x, y;
	Rect re = { 0 };
	re.w = tile_display_size;
	re.h = tile_display_size;
	for (y = 0; y < m->h; y++) {
		for (x = 0; x < m->w; x++) {
			re.x = rect->x + x * tile_display_size - ss->x;
			re.y = rect->y + y * tile_display_size - ss->y;
			if (tiles[x + y * m->w])
				ren_texture(
					r,
					&re,
					get_texture(asset_id_brick_texture)
				);
			else
				ren_rect(
					r,
					make_black(),
					&re
				);
		}
	}
	x = (g->app->mx - rect->x) / tile_display_size;
	y = (g->app->my - rect->y) / tile_display_size;
	if (
		x >= 0 &&
		y >= 0 &&
		x < m->w &&
		y < m->h
	) {
		re.x = x * tile_display_size + rect->x;
		re.y = y * tile_display_size + rect->y;
		ren_rect(
			r,
			make_colour(0xffffff, 120),
			&re
		);
	}
}

void edit_map(Editor* e, GUI* g, Map* m) {
	Rect b = gui_viewport(g);
	Rect t = gui_cut_up(&b, gui_text_height() + 4);
	int mcs[2];
	(void)e;
	mcs[0] = m->w * tile_display_size;
	mcs[1] = m->h * tile_display_size;
	gui_btn(g, gui_cut_left(&t, gui_text_width("Load", 4) + 4), "Save");
	gui_btn(g, gui_cut_left(&t, gui_text_width("Save", 4) + 4), "Load");
	if (gui_btn(g, gui_cut_left(&t, gui_text_width("Bake Lights", 11) + 4), "Bake Lights")) {
		bake_map(m);
	}
	g->uptr = m;
	gui_scrollable(g, b, mcs, &map_ss, draw_map_scrollable);
}