Debian Codegraph @

src/dinkini.cpp

raw
1 /**
2 * Dink.ini - loading graphics
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2008, 2009, 2010, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include "paths.h"
30 #include "dinkini.h"
31 #include "str_util.h"
32 #include "log.h"
33
34 // TODO: break deps
35 #include "bgm.h"
36
37 #include "gfx_sprites.h"
38
39 struct idata
40 {
41 enum idata_type type;
42 int seq;
43 int frame;
44 int xoffset, yoffset;
45 rect hardbox;
46 };
47 static struct idata *id;
48 int nb_idata = 0;
49
50 /**
51 * Allocate idata
52 */
53 void dinkini_init(int nb_idata_param)
54 {
55 nb_idata = nb_idata_param;
56 id = (struct idata*)calloc(sizeof(struct idata), nb_idata);
57 }
58
59 /**
60 * Deallocate idata
61 */
62 void dinkini_quit()
63 {
64 if (id != NULL)
65 free(id);
66 id = NULL;
67 }
68
69 /**
70 * Store sprite instructions from dink.ini
71 */
72 void make_idata(enum idata_type type, int myseq, int myframe, int xoffset, int yoffset, rect crect)
73 {
74 int i;
75 for (i = 1; i < nb_idata; i++)
76 {
77 if (id[i].type == IDATA_EMPTY)
78 {
79 //found empty one
80 id[i].type = type;
81 id[i].seq = myseq;
82 id[i].frame = myframe;
83 id[i].xoffset = xoffset;
84 id[i].yoffset = yoffset;
85 rect_copy(&id[i].hardbox, &crect);
86
87 return;
88 }
89 }
90
91 log_error("Out of idata spots (max is %d), no more sprite corrections can be allowed.", nb_idata);
92 }
93
94 /**
95 * Interpret/execute sprite instructions
96 */
97 void program_idata(void)
98 {
99 int i;
100 for (i = 1; i < nb_idata; i++)
101 {
102 if (id[i].type == IDATA_EMPTY)
103 return;
104
105 if (id[i].type == IDATA_SPRITE_INFO)
106 {
107 k[seq[id[i].seq].frame[id[i].frame]].xoffset = id[i].xoffset;
108 k[seq[id[i].seq].frame[id[i].frame]].yoffset = id[i].yoffset;
109 rect_copy(&k[seq[id[i].seq].frame[id[i].frame]].hardbox, &id[i].hardbox);
110
111 // Msg("Programming idata type %d in %d...Seq %d Frame %d (Hardbox is %d %d %d %d)", id[i].type, i,
112 // id[i].seq, id[i].frame, id[i].hardbox.left,id[i].hardbox.right, id[i].hardbox.top, id[i].hardbox.bottom);
113 }
114
115 if (id[i].type == IDATA_FRAME_SPECIAL)
116 {
117 //set special
118 seq[id[i].seq].special[id[i].frame] = id[i].xoffset;
119 }
120 if (id[i].type == IDATA_FRAME_DELAY)
121 {
122 //set delay
123 seq[id[i].seq].delay[id[i].frame] = id[i].xoffset;
124 }
125
126 if (id[i].type == IDATA_FRAME_FRAME)
127 {
128 if (id[i].xoffset == -1)
129 seq[id[i].seq].frame[id[i].frame] = -1;
130 else
131 seq[id[i].seq].frame[id[i].frame] = seq[id[i].xoffset].frame[id[i].yoffset];
132 }
133 }
134 }
135
136
137 /**
138 * Parse a dink.ini line, and store instructions for later processing
139 * (used in game initialization through 'load_batch')
140 */
141 void pre_figure_out(char* line, bool playmidi)
142 {
143 int i;
144 char* ev[10];
145 memset(&ev, 0, sizeof(ev));
146 for (i = 0; i < 10; i++)
147 ev[i] = separate_string(line, i+1, ' ');
148 char *command = ev[0];
149
150 // PLAYMIDI filename
151 if (compare(command, "playmidi"))
152 {
153 char* midi_filename = ev[1];
154 if (playmidi)
155 PlayMidi(midi_filename);
156 }
157
158 // LOAD_SEQUENCE_NOW path seq BLACK
159 // LOAD_SEQUENCE_NOW path seq LEFTALIGN
160 // LOAD_SEQUENCE_NOW path seq NOTANIM
161 // LOAD_SEQUENCE_NOW path seq speed
162 // LOAD_SEQUENCE_NOW path seq speed offsetx offsety hard.left hard.top hard.right hard.bottom
163 else if (compare(command, "LOAD_SEQUENCE_NOW"))
164 {
165 rect hardbox;
166 memset(&hardbox, 0, sizeof(rect));
167
168 int myseq = atol(ev[2]);
169 seq[myseq].is_active = 1;
170 seq_set_ini(myseq, line);
171
172 int flags = 0;
173 if (compare(ev[3], "BLACK"))
174 {
175 flags = DINKINI_NOTANIM | DINKINI_BLACK;
176 }
177 else if (compare(ev[3], "LEFTALIGN"))
178 {
179 flags = DINKINI_LEFTALIGN;
180 }
181 else if (compare(ev[3], "NOTANIM"))
182 {
183 //not an animation!
184 flags = 0;
185 }
186 else
187 {
188 //yes, an animation!
189 hardbox.left = atol(ev[6]);
190 hardbox.top = atol(ev[7]);
191 hardbox.right = atol(ev[8]);
192 hardbox.bottom = atol(ev[9]);
193
194 flags = DINKINI_NOTANIM;
195 }
196
197 load_sprites(ev[1],atol(ev[2]),atol(ev[3]),atol(ev[4]),atol(ev[5]),
198 hardbox, flags);
199
200
201 /* In the original engine, due to a bug, make_idata() modifies
202 unused sequence #0, but this isn't really important because
203 sequence was already configured in was already done in
204 'load_sprites'. This is consistent with 'figure_out', which
205 doesn't call 'make_idata' at all. */
206 /* We still call 'make_idata' for compatibility, to use the same
207 number of idata, hence preserving the same max_idata. */
208 make_idata(IDATA_SPRITE_INFO, 0,0, 0,0, hardbox);
209 }
210
211 // LOAD_SEQUENCE path seq BLACK
212 // LOAD_SEQUENCE path seq LEFTALIGN
213 // LOAD_SEQUENCE path seq NOTANIM
214 // LOAD_SEQUENCE path seq speed
215 // LOAD_SEQUENCE path seq speed offsetx offsety hard.left hard.top hard.right hard.bottom
216 else if (compare(command, "LOAD_SEQUENCE"))
217 {
218 int myseq = atol(ev[2]);
219 seq_set_ini(myseq, line);
220 seq[myseq].is_active = 1;
221 }
222
223 else if (compare(command, "SET_SPRITE_INFO"))
224 {
225 // name seq speed offsetx offsety hardx hardy
226 //if (k[seq[myseq].frame[myframe]].frame = 0) Msg("Changing sprite that doesn't exist...");
227
228 rect hardbox;
229 int myseq = atol(ev[1]);
230 int myframe = atol(ev[2]);
231 rect_set(&hardbox, atol(ev[5]), atol(ev[6]), atol(ev[7]), atol(ev[8]));
232 make_idata(IDATA_SPRITE_INFO, myseq, myframe,atol(ev[3]), atol(ev[4]),hardbox);
233 }
234
235 else if (compare(command, "SET_FRAME_SPECIAL"))
236 {
237 rect hardbox;
238 int myseq = atol(ev[1]);
239 int myframe = atol(ev[2]);
240 int special = atol(ev[3]);
241 make_idata(IDATA_FRAME_SPECIAL, myseq, myframe, special, 0, hardbox);
242 }
243
244 else if (compare(command, "SET_FRAME_DELAY"))
245 {
246 rect hardbox;
247 int myseq = atol(ev[1]);
248 int myframe = atol(ev[2]);
249 int delay = atol(ev[3]);
250 make_idata(IDATA_FRAME_DELAY, myseq, myframe, delay, 0, hardbox);
251 }
252
253 // SET_FRAME_FRAME seq frame new_seq new_frame
254 // SET_FRAME_FRAME seq frame -1
255 else if (compare(command, "SET_FRAME_FRAME"))
256 {
257 rect hardbox;
258 int myseq = atol(ev[1]);
259 int myframe = atol(ev[2]);
260 int new_seq = atol(ev[3]);
261 int new_frame = atol(ev[4]);
262
263 make_idata(IDATA_FRAME_FRAME, myseq, myframe, new_seq, new_frame, hardbox);
264 }
265
266 /* Clean-up */
267 for (i = 0; i < 10; i++)
268 free(ev[i]);
269 }
270
271
272 /* Parse dink.ini */
273 void load_batch(bool playmidi)
274 {
275 FILE *in = NULL;
276 char line[255];
277
278 log_info("Loading dink.ini");
279 /* Open the text file in binary mode, so it's read the same way
280 under different OSes (Unix has no text mode) */
281 if ((in = paths_dmodfile_fopen("dink.ini", "rb")) == NULL)
282 log_error("Error opening dink.ini for reading.");
283 else
284 {
285 while(fgets(line, 255, in) != NULL)
286 {
287 pre_figure_out(line, playmidi);
288 /* printf("[pre_figure_out] %s", line); */
289 }
290 fclose(in);
291 }
292
293 program_idata();
294 }
295
296
297
298 /**
299 * Parse a delayed seq[].ini or a DinkC init("...") , and act
300 * immediately
301 */
302 void figure_out(char* line)
303 {
304 int myseq = 0, myframe = 0;
305 int special = 0;
306 int special2 = 0;
307 int i;
308 char* ev[10];
309 memset(&ev, 0, sizeof(ev));
310 for (i = 0; i < 10; i++)
311 {
312 ev[i] = separate_string(line, i+1, ' ');
313 if (ev[i] == NULL)
314 ev[i] = strdup("");
315 }
316 char *command = ev[0];
317
318 // LOAD_SEQUENCE_NOW path seq BLACK
319 // LOAD_SEQUENCE_NOW path seq LEFTALIGN
320 // LOAD_SEQUENCE_NOW path seq NOTANIM
321 // LOAD_SEQUENCE_NOW path seq speed offsetx offsety hard.left hard.top hard.right hard.bottom
322 if (compare(command, "LOAD_SEQUENCE_NOW") ||
323 compare(command, "LOAD_SEQUENCE"))
324 {
325 rect hardbox;
326 memset(&hardbox, 0, sizeof(rect));
327
328 int myseq = atol(ev[2]);
329 seq[myseq].is_active = 1;
330 seq_set_ini(myseq, line);
331
332 int flags = 0;
333
334 if (compare(ev[3], "BLACK"))
335 {
336 flags = DINKINI_NOTANIM | DINKINI_BLACK;
337 }
338 else if (compare(ev[3], "LEFTALIGN"))
339 {
340 flags = DINKINI_LEFTALIGN;
341 }
342 else if (compare(ev[3], "NOTANIM"))
343 {
344 //not an animation!
345 flags = 0;
346 }
347 else
348 {
349 //yes, an animation!
350 hardbox.left = atol(ev[6]);
351 hardbox.top = atol(ev[7]);
352 hardbox.right = atol(ev[8]);
353 hardbox.bottom = atol(ev[9]);
354
355 flags = DINKINI_NOTANIM;
356 }
357
358 load_sprites(ev[1],atol(ev[2]),atol(ev[3]),atol(ev[4]),atol(ev[5]),
359 hardbox, flags);
360
361 program_idata();
362 }
363
364 else if (compare(command, "SET_SPRITE_INFO"))
365 {
366 // name seq speed offsetx offsety hardx hardy
367 myseq = atol(ev[1]);
368 myframe = atol(ev[2]);
369 k[seq[myseq].frame[myframe]].xoffset = atol(ev[3]);
370 k[seq[myseq].frame[myframe]].yoffset = atol(ev[4]);
371 k[seq[myseq].frame[myframe]].hardbox.left = atol(ev[5]);
372 k[seq[myseq].frame[myframe]].hardbox.top = atol(ev[6]);
373 k[seq[myseq].frame[myframe]].hardbox.right = atol(ev[7]);
374 k[seq[myseq].frame[myframe]].hardbox.bottom = atol(ev[8]);
375 }
376
377 else if (compare(command, "SET_FRAME_SPECIAL"))
378 {
379 // name seq speed offsetx offsety hardx hardy
380 myseq = atol(ev[1]);
381 myframe = atol(ev[2]);
382 special = atol(ev[3]);
383
384 seq[myseq].special[myframe] = special;
385 log_debug("Set special. %d %d %d", myseq, myframe, special);
386 }
387
388 else if (compare(command, "SET_FRAME_DELAY"))
389 {
390 // name seq speed offsetx offsety hardx hardy
391 myseq = atol(ev[1]);
392 myframe = atol(ev[2]);
393 special = atol(ev[3]);
394
395 seq[myseq].delay[myframe] = special;
396 log_debug("Set delay. %d %d %d",myseq, myframe, special);
397 }
398
399 else if (compare(command, "SET_FRAME_FRAME"))
400 {
401 // name seq speed offsetx offsety hardx hardy
402 myseq = atol(ev[1]);
403 myframe = atol(ev[2]);
404 special = atol(ev[3]);
405 special2 = atol(ev[4]);
406
407 if (special == -1)
408 seq[myseq].frame[myframe] = special;
409 else
410 seq[myseq].frame[myframe] = seq[special].frame[special2];
411 log_debug("Set frame. %d %d %d", myseq, myframe, special);
412 }
413
414 /* Clean-up */
415 for (i = 0; i < 10; i++)
416 free(ev[i]);
417 }
418
419
420 /**
421 * Load sequence in memory if not already, using cached dink.ini info
422 */
423 void check_seq_status(int seq_no)
424 {
425 if (seq_no > 0 && seq_no < MAX_SEQUENCES)
426 {
427 /* Skip empty/unused sequences */
428 if (!seq[seq_no].is_active)
429 return;
430
431 if (seq[seq_no].frame[1] == 0 || GFX_k[seq[seq_no].frame[1]].k == NULL)
432 figure_out(seq[seq_no].ini);
433 }
434 else if (seq_no > 0)
435 {
436 log_error("Warning: check_seq_status: invalid sequence %d", seq_no);
437 }
438 }
439
440 /* Editor only */
441 void check_frame_status(int h, int frame)
442 {
443 if (!seq[h].is_active) return;
444
445 if (h > 0)
446 {
447 // Msg("Smartload: Loading seq %d..", spr[h].seq);
448 if (seq[h].frame[1] == 0 || GFX_k[seq[h].frame[1]].k == NULL)
449 {
450 figure_out(seq[h].ini);
451 }
452 else
453 {
454 //it's been loaded before.. is it lost or still there?
455 }
456 }
457 }
458
459 /**
460 * Load all +1->+9 sequences from base sequence 'base' in memory,
461 * useful to load all of a moving sprite sequences
462 */
463 void check_base(int base)
464 {
465 int i;
466 for (i = 1; i < 10; i++)
467 if (seq[base+i].is_active)
468 check_seq_status(base+i);
469 }