Debian Codegraph @

src/live_screen.cpp

raw
1 /**
2 * Displayed screen
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2005, 2007, 2008, 2009, 2014, 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 "game_engine.h"
29 #include "live_sprites_manager.h"
30 #include "live_screen.h"
31 #include "editor_screen.h"
32 #include "hardness_tiles.h"
33 #include "IOGfxPrimitives.h"
34 #include "gfx.h"
35 #include "gfx_sprites.h"
36 #include "log.h"
37 #include "dinkini.h"
38
39 /* base editor screen */
40 struct editor_screen cur_ed_screen;
41
42 /* hardness */
43 unsigned char screen_hitmap[600+1][400+1]; /* hit_map */
44
45 int playx = 620;
46 int playl = 20;
47 int playy = 400;
48
49 /* Screen transition */
50 static int move_screen = 0;
51 static int move_counter = 0;
52 /*bool*/int transition_in_progress = /*false*/0;
53
54
55 void live_screen_init() {
56 memset(&screen_hitmap, 0, sizeof(screen_hitmap));
57 }
58
59 /**
60 * Fills a int[MAX_SPRITES_AT_ONCE] with the index of the current
61 * screen's sprites, sorted by ascending height/queue.
62 */
63 void screen_rank_game_sprites(int* rank)
64 {
65 memset(rank, 0, MAX_SPRITES_AT_ONCE * sizeof(int));
66
67 int r1 = 0;
68 int already_checked[MAX_SPRITES_AT_ONCE+1];
69 memset(already_checked, 0, sizeof(already_checked));
70 for (r1 = 0; r1 < last_sprite_created; r1++)
71 {
72 int highest_sprite = 22000; //more than it could ever be
73 rank[r1] = 0;
74
75 int h1;
76 for (h1 = 1; h1 <= last_sprite_created; h1++)
77 {
78 if (already_checked[h1] == 0 && spr[h1].active)
79 {
80 int height;
81 if (spr[h1].que != 0)
82 height = spr[h1].que;
83 else
84 height = spr[h1].y;
85
86 if (height < highest_sprite)
87 {
88 highest_sprite = height;
89 rank[r1] = h1;
90 }
91 }
92 }
93 if (rank[r1] != 0)
94 already_checked[rank[r1]] = 1;
95 }
96 }
97
98 void fill_hard_sprites()
99 {
100 int rank[MAX_SPRITES_AT_ONCE];
101 screen_rank_game_sprites(rank);
102
103 int r1 = 0;
104 for (; r1 < last_sprite_created && rank[r1] > 0; r1++)
105 {
106 int h = rank[r1];
107 if (spr[h].active)
108 {
109 // Msg("proccesing sprite %d", h);
110 if (spr[h].sp_index != 0)
111 {
112 //Msg("has spindex of %d is_warp is %d",spr[h].sp_index,cur_ed_screen.sprite[spr[h].sp_index].is_warp);
113 if (cur_ed_screen.sprite[spr[h].sp_index].hard == 0)
114 {
115 add_hardness(h,100+spr[h].sp_index);
116 //Msg("added warp hardness for %d", spr[h].sp_index);
117 }
118 }
119 else
120 {
121 if (spr[h].hard == 0)
122 {
123 //Msg("adding a new sprite hardness %d (not from editor)", h);
124 add_hardness(h, 1);
125 }
126 }
127 }
128 }
129 }
130
131
132 void fill_whole_hard(void)
133 {
134 int til;
135 for (til=0; til < 96; til++)
136 {
137 int offx = (til * 50 - ((til / 12) * 600));
138 int offy = (til / 12) * 50;
139 int x, y;
140 for (x = 0; x < 50; x++)
141 for (y = 0; y < 50; y++)
142 screen_hitmap[offx +x][offy+y] = hmap.htile[ realhard(til) ].hm[x][y];
143 }
144 }
145
146
147 //add hardness from a sprite
148 void add_hardness (int sprite, int num)
149 {
150 int xx;
151 for (xx = spr[sprite].x + k[getpic(sprite)].hardbox.left; xx < spr[sprite].x + k[getpic(sprite)].hardbox.right; xx++)
152 {
153 int yy;
154 for (yy = spr[sprite].y + k[getpic(sprite)].hardbox.top; yy < spr[sprite].y + k[getpic(sprite)].hardbox.bottom; yy++)
155 {
156 if ( (xx-20 > 600) | (xx-20 < 0)| (yy > 400) | (yy < 0)) {
157 /* Nothing */
158 } else {
159 screen_hitmap[xx-20][yy] = num;
160 }
161 }
162 }
163 }
164
165
166
167 unsigned char get_hard_map(int x1, int y1)
168 {
169
170
171 if ((x1 < 0) || (y1 < 0)) return(0);
172 if ((x1 > 599) ) return(0);
173 if (y1 > 399) return(0);
174
175
176 int til = (x1 / 50) + ( ((y1 / 50)) * 12);
177 //til++;
178
179 int offx = x1 - ((x1 / 50) * 50);
180
181
182 int offy = y1 - ((y1 / 50) * 50);
183
184 //Msg("tile %d ",til);
185
186 return( hmap.htile[ realhard(til ) ].hm[offx][offy]);
187
188 }
189
190
191
192 void fill_hardxy(rect box)
193 {
194 int x1, y1;
195 //Msg("filling hard of %d %d %d %d", box.top, box.left, box.right, box.bottom);
196
197 if (box.right > 600) box.right = 600;
198 if (box.top < 0) box.top = 0;
199 if (box.bottom > 400) box.bottom = 400;
200 if (box.left < 0) box.left = 0;
201
202 for (x1 = box.left; x1 < box.right; x1++)
203 for (y1 = box.top; y1 < box.bottom; y1++)
204 screen_hitmap[x1][y1] = get_hard_map(x1,y1);
205 }
206
207
208
209 /**
210 * Check whether planned new position (x1,y1) is solid
211 *
212 * Only used in 'check_if_move_is_legal'
213 *
214 * Returns: 0 if not solid, !0 otherwise
215 */
216 unsigned char get_hard(int x1, int y1, int screenlock)
217 {
218 // Fix "going diag on screen border + screenlock avoids hardness"
219 // TODO: same for screen with no adjacent screen
220 if (screenlock)
221 {
222 if (x1 < 0) x1 = 0;
223 else if (x1 > 599) x1 = 599;
224
225 if (y1 < 0) y1 = 0;
226 else if (y1 > 399) y1 = 399;
227 }
228 if ((x1 < 0) || (y1 < 0) || (x1 > 599) || (y1 > 399))
229 return 0;
230
231 int value = screen_hitmap[x1][y1];
232 return(value);
233 }
234
235
236 /*bool*/int get_box (int h, rect * box_scaled, rect * box_real, bool skip_screen_clipping)
237 {
238 int x_offset, y_offset;
239
240 int mplayx = playx;
241 int mplayl = playl;
242 int mplayy = playy;
243
244 if (spr[h].noclip)
245 {
246 mplayx = 640;
247 mplayl = 0;
248 mplayy = 480;
249 }
250
251 // added to fix frame-not-in-memory immediately
252 if (getpic(h) < 1)
253 {
254 if (spr[h].pseq != 0)
255 check_seq_status(spr[h].pseq);
256 }
257
258 // if frame is still not in memory:
259 if (getpic(h) < 1)
260 {
261 log_warn("Yo, sprite %d has a bad pic. Seq %d, Frame %d",
262 h, spr[h].pseq, spr[h].pframe);
263 goto nodraw;
264 }
265
266 *box_real = k[getpic(h)].box;
267
268 /* This doesn't really make sense, but that's the way the game was
269 released, so we keep it for compatibility */
270 {
271 rect krect;
272 rect_copy(&krect, &k[getpic(h)].box);
273
274 double size_ratio = spr[h].size / 100.0;
275 int x_compat = krect.right * (size_ratio - 1) / 2;
276 int y_compat = krect.bottom * (size_ratio - 1) / 2;
277
278 int center_x = k[getpic(h)].xoffset;
279 int center_y = k[getpic(h)].yoffset;
280 box_scaled->left = spr[h].x - center_x - x_compat;
281 box_scaled->top = spr[h].y - center_y - y_compat;
282
283 box_scaled->right = box_scaled->left + krect.right * size_ratio;
284 box_scaled->bottom = box_scaled->top + krect.bottom * size_ratio;
285 }
286
287 if (spr[h].alt.right != 0 || spr[h].alt.left != 0 || spr[h].alt.top != 0)
288 {
289 // checks for correct box stuff
290 if (spr[h].alt.left < 0)
291 spr[h].alt.left = 0;
292 if (spr[h].alt.left > k[getpic(h)].box.right)
293 spr[h].alt.left = k[getpic(h)].box.right;
294
295 if (spr[h].alt.top < 0)
296 spr[h].alt.top = 0;
297 if (spr[h].alt.top > k[getpic(h)].box.bottom)
298 spr[h].alt.top = k[getpic(h)].box.bottom;
299
300 if (spr[h].alt.right < 0)
301 spr[h].alt.right = 0;
302 if (spr[h].alt.right > k[getpic(h)].box.right)
303 spr[h].alt.right = k[getpic(h)].box.right;
304
305 if (spr[h].alt.bottom < 0)
306 spr[h].alt.bottom = 0;
307 if (spr[h].alt.bottom > k[getpic(h)].box.bottom)
308 spr[h].alt.bottom = k[getpic(h)].box.bottom;
309
310 box_scaled->left += spr[h].alt.left;
311 box_scaled->top += spr[h].alt.top;
312 box_scaled->right = box_scaled->right - (k[getpic(h)].box.right - spr[h].alt.right);
313 box_scaled->bottom = box_scaled->bottom - (k[getpic(h)].box.bottom - spr[h].alt.bottom);
314
315 rect_copy(box_real, &spr[h].alt);
316 }
317
318 //********* Check to see if they need to be cut down and do clipping
319
320 if (spr[h].size == 0)
321 spr[h].size = 100;
322
323 if (skip_screen_clipping)
324 goto do_draw;
325
326 if (box_scaled->left < mplayl)
327 {
328 x_offset = box_scaled->left * (-1) + mplayl;
329 box_scaled->left = mplayl;
330
331 if (spr[h].size == 100)
332 box_real->left += x_offset;
333 else
334 box_real->left += (x_offset * 100) / spr[h].size;
335
336 if (box_scaled->right - 1 < mplayl)
337 goto nodraw;
338 }
339
340 if (box_scaled->top < 0)
341 {
342 y_offset = box_scaled->top * (-1);
343 box_scaled->top = 0;
344
345 if (spr[h].size == 100)
346 box_real->top += y_offset;
347 else
348 box_real->top += (y_offset * 100) / spr[h].size;
349
350 if (box_scaled->bottom-1 < 0)
351 goto nodraw;
352 }
353
354 if (box_scaled->right > mplayx)
355 {
356 x_offset = (box_scaled->right) - mplayx;
357 box_scaled->right = mplayx;
358
359 if (spr[h].size == 100)
360 box_real->right -= x_offset;
361 else
362 box_real->right -= (x_offset * 100) / spr[h].size;
363
364 if (box_scaled->left+1 > mplayx)
365 goto nodraw;
366 }
367
368 if (box_scaled->bottom > mplayy)
369 {
370 y_offset = (box_scaled->bottom) - mplayy;
371 box_scaled->bottom = mplayy;
372
373 if (spr[h].size == 100)
374 box_real->bottom -= y_offset;
375 else
376 box_real->bottom -= (y_offset * 100) / spr[h].size;
377
378 if (box_scaled->top+1 > mplayy)
379 goto nodraw;
380 }
381
382 do_draw:
383 return(/*true*/1);
384
385 nodraw:
386 return(/*false*/0);
387 }
388
389 void draw_sprite_game(IOGfxSurface *GFX_lpdest, int h)
390 {
391 if (spr[h].brain == 8)
392 return; // text
393 if (spr[h].nodraw == 1)
394 return; // invisible
395
396 rect box_crap,box_real;
397
398 if (get_box(h, &box_crap, &box_real, false))
399 {
400 /* Generic scaling */
401 /* Not perfectly accurate yet: move a 200% sprite to the border
402 of the screen to it is clipped: it's scaled size will slighly
403 vary. Maybe we need to clip the source zone before scaling
404 it.. */
405 // error checking for invalid rectangle
406 if (box_crap.left >= box_crap.right || box_crap.top >= box_crap.bottom)
407 return;
408
409 SDL_Rect src, dst;
410 int retval = 0;
411 src.x = box_real.left;
412 src.y = box_real.top;
413 src.w = box_real.right - box_real.left;
414 src.h = box_real.bottom - box_real.top;
415 dst.x = box_crap.left;
416 dst.y = box_crap.top;
417 dst.w = box_crap.right - box_crap.left;
418 dst.h = box_crap.bottom - box_crap.top;
419
420 retval = GFX_lpdest->blitStretch(GFX_k[getpic(h)].k, &src, &dst);
421
422 if (retval < 0) {
423 log_error("Could not draw sprite %d: %s", getpic(h), SDL_GetError());
424 /* If we failed, then maybe the sprite was actually loaded
425 yet, let's try now */
426 if (spr[h].pseq != 0)
427 check_seq_status(spr[h].pseq);
428 }
429 }
430 }
431
432
433
434
435
436 /**
437 * Screen transition with scrolling effect.
438 * Returns 0 when transition is finished.
439 */
440 bool transition(int fps_final)
441 {
442 SDL_Rect src, dst;
443
444 //we need to do our fancy screen transition
445 int dumb = fps_final * 2;
446
447 move_counter += dumb;
448
449
450 if (move_screen == 4) {
451 if (move_counter > 598)
452 move_counter = 598;
453
454 src.x = 0;
455 src.y = 0;
456 src.w = 600 - move_counter;
457 src.h = 400;
458 dst.x = 20 + move_counter;
459 dst.y = 0;
460 IOGFX_backbuffer->blit(IOGFX_tmp1, &src, &dst);
461
462 src.x = 600 - move_counter;
463 src.y = 0;
464 src.w = move_counter;
465 src.h = 400;
466 dst.x = 20;
467 dst.y = 0;
468 IOGFX_backbuffer->blit(IOGFX_tmp2, &src, &dst);
469
470 if (move_counter >= 595) {
471 transition_in_progress = 0;
472 move_screen = 0;
473 move_counter = 0;
474 //draw_map();
475 return false;
476 }
477
478 return true;
479 }
480
481
482 if (move_screen == 6) {
483 if (move_counter > 598)
484 move_counter = 598;
485
486 src.x = move_counter;
487 src.y = 0;
488 src.w = 600 - move_counter;
489 src.h = 400;
490 dst.x = 20;
491 dst.y = 0;
492 IOGFX_backbuffer->blit(IOGFX_tmp1, &src, &dst);
493
494 src.x = 0;
495 src.y = 0;
496 src.w = move_counter;
497 src.h = 400;
498 dst.x = 620 - move_counter;
499 dst.y = 0;
500 IOGFX_backbuffer->blit(IOGFX_tmp2, &src, &dst);
501
502 if (move_counter >= 595) {
503 transition_in_progress = 0;
504 move_screen = 0;
505 move_counter = 0;
506 //draw_map();
507 return false;
508 }
509
510 return true;
511 }
512
513
514 if (move_screen == 8) {
515 if (move_counter > 398)
516 move_counter = 398;
517
518 src.x = 0;
519 src.y = 0;
520 src.w = 600;
521 src.h = 400 - move_counter;
522 dst.x = 20;
523 dst.y = move_counter;
524 IOGFX_backbuffer->blit(IOGFX_tmp1, &src, &dst);
525
526 src.x = 0;
527 src.y = 400 - move_counter;
528 src.w = 600;
529 src.h = move_counter;
530 dst.x = 20;
531 dst.y = 0;
532 IOGFX_backbuffer->blit(IOGFX_tmp2, &src, &dst);
533
534 if (move_counter >= 398) {
535 transition_in_progress = 0;
536 move_screen = 0;
537 move_counter = 0;
538 //draw_map();
539 return false;
540 }
541
542 return true;
543 }
544
545
546 if (move_screen == 2) {
547 if (move_counter > 398)
548 move_counter = 398;
549
550 src.x = 0;
551 src.y = move_counter;
552 src.w = 600;
553 src.h = 400 - move_counter;
554 dst.x = 20;
555 dst.y = 0;
556 IOGFX_backbuffer->blit(IOGFX_tmp1, &src, &dst);
557
558 src.x = 0;
559 src.y = 0;
560 src.w = 600;
561 src.h = move_counter;
562 dst.x = 20;
563 dst.y = 400 - move_counter;
564 IOGFX_backbuffer->blit(IOGFX_tmp2, &src, &dst);
565
566 if (move_counter >= 398) {
567 transition_in_progress = 0;
568 move_screen = 0;
569 move_counter = 0;
570 //draw_map();
571 return false;
572 }
573
574 return true;
575 }
576
577 return false;
578 }
579
580 /* Capture the current's backbuffer game zone for screen transition */
581 void grab_trick(int dir) {
582 SDL_Rect src, dst;
583 src.x = playl;
584 src.y = 0;
585 src.w = 620 - playl;
586 src.h = 400;
587 dst.x = dst.y = 0;
588 IOGFX_tmp1->blit(IOGFX_backbuffer, &src, &dst);
589
590 move_screen = dir;
591 move_counter = 0;
592 }