aboutsummaryrefslogtreecommitdiffstats
path: root/src/input.h
blob: c80c78f6cad51458edc810d4c854b95109470670 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Warning ugly code on line 248 turn back now if u 
want to keep your joy and happens.
*/

void show_error(const char * label) {
	ok_button->show();
	ok_button->labelsize(ok_button->w() / 5);
	error_msg->show();
	error_msg->value(label);

	if (!g_active)
		startb->do_callback();

	act_boxs(false);
}

void error_cb(Fl_Widget * w, void * d) {
	ok_button->hide();
	error_msg->hide();
	act_boxs(true);
}

class MapBrowser : public Fl_File_Browser {
	public:
		MapBrowser(int _x, int _y, int _w, int _h, const char * l=0) :
			Fl_File_Browser(_x, _y, _w, _h, l) {
				sprintf(mapdir, dir_to_map_prf, MAINDIR);

				load(mapdir);
				type(FL_HOLD_BROWSER);
				hide();
				callback(map_browser_cb, (void*)this);

				int bw, bh;
				bw = w() / 5;
				bh = h() / 5;

				close_button = new Fl_Button(x(), y() - bh, bw, bh,
						"close");
				close_button->callback(close_cb, (void*)this);
				close_button->labelsize(bw / 3);
				close_button->hide();

				if (text(1))
					if (!strcmp(text(1), "../"))
						remove(1);
			}

		static void map_browser_cb(Fl_Widget * w, void * d) {
			MapBrowser * br = (MapBrowser*)d;
			br->real_cb();
		}

		void show_all() {
			close_button->show();
			show();
			act_boxs(false);
		}

		void hide_all() {
			close_button->hide();
			hide();

			if (g_active)
				act_boxs(true);
		}

		bool is_visible() {
			if (close_button->visible())
				return true;
			else
				return false;
		}

		static void close_cb(Fl_Widget * w, void * d) {
			MapBrowser * br = (MapBrowser*)d;
			br->hide_all();
		}

		void handle_resize() {
			close_button->labelsize(close_button->w() / 3);
		}

		void real_cb();
	private:
		Fl_Button * close_button;
		int last_clicked;
		char mapdir[255];
};

void MapBrowser::real_cb() {

	if (!text(value()))
		return;
	else if (last_clicked != value()) {
		last_clicked = value();
		return;
	}

	last_clicked = -1;
	map_browser->hide_all();

	// Loading json.
	char filename[255];
	sprintf(filename, "%s%s", mapdir, text(value()));

	FILE * fp = fopen(filename, "rt");

	if (!fp) {
		show_error("File not found");
		fclose(fp);
		return;
	}

	long int file_size = findsize(fp);
	char file_buf[file_size];
	file_buf[0] = '\0';
	file_buf[file_size] = '\0';
	int a_w, a_h, c, r;

	fread(file_buf, file_size, 1, fp);
	fclose(fp);

	json_value * _value = json_parse(file_buf, file_size);

	if (!_value) {
		show_error("Can't read map");
		return;
	}

	if (get_jsonf_size(_value, &a_w, &a_h)) {
		show_error("Bad formatt");
		return;
	}

	// Making 2d array.
	unsigned char ** the_map = new unsigned char*[a_h];
	unsigned char i;

	if (the_map == NULL) {
		show_error("Can't malloc data");
		delete [] the_map;
		return;
	}

	for (i = 0; i < a_h; i++)
		the_map[i] = new unsigned char[a_w];

	// Reading data.
	if (read_data_jsonf(_value, a_w, a_h, the_map)) {
		show_error("Bad formatt");

		// Deleting memory.
		for (i = 0; i < a_h; i++) {
			delete [] the_map[i];
		}

		delete [] the_map;

		return;
	}

	// Settings Max blocks.
	if (a_w > a_h)
		MBLOCKS = a_w;
	else
		MBLOCKS = a_h;
	
	if (a_w > max_map || a_h > max_map) {
		// Deleting memory.
		for (i = 0; i < a_h; i++) {
			delete [] the_map[i];
		}
		
		delete [] the_map;

		loaded_map = false;
		map_init();
		add_boxs();

		show_error("Map is to big");
		return;
	}

	map_init(the_map, a_w, a_h);

	// Deleting memory.
	for (i = 0; i < a_h; i++) {
		delete [] the_map[i];
	}
	
	delete [] the_map;
	loaded_map = true;

	add_boxs();

	// Activate game.
	if (!g_active)
		startb->do_callback();
	g_active = true;
}

void startb_cb(Fl_Widget * w, void * d) {
	int bw, by;
	bw = bside - (bside / 3);
	by = bside / 5;
	ext_used = false;

	if (!g_active) {
		g_active = true;
		startb->label("Restart");
		startb->labelsize(bw / 5);
		startb->resize(10, 10, exit_button->w(),
				exit_button->h());
		startb->color(FL_GRAY);
		startb->labelcolor(FL_BLACK);
		startb->box(FL_UP_BOX);
		act_boxs(true);
	}

	overlap_wid->resize(0, 0, 0, 0);
	map_init();
	add_boxs();

	game_over->hide();
}

void exit_cb(Fl_Widget * w, void * d) {
	_window->hide();
}

void fullsc_cb(Fl_Widget * w, void * d) {
	if (_window->fullscreen_active())
		_window->fullscreen_off();
	else
		_window->fullscreen();
}

void browse_cb(Fl_Widget * w, void * d) {
	/*
	My compiler was being stupid. The extistence of 
	this code makes me angery.
	It should look like this:
	game_over->hide();
	Why do I have to do this!
	*/
	if (game_over->visible())
		startb->do_callback();
	// The saddness is over for now.
	
	map_browser->show_all();
}