3471c9d2598509d5d97d1573461509f96ee820aa
[rocksndiamonds.git] / src / game_bd / bd_graphics.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "main_bd.h"
18
19
20 // !!! (can be removed later) !!!
21 #define DO_GFX_SANITY_CHECK     TRUE
22
23 /* distance to screen edge in cells when scrolling the screen */
24 #define SCROLL_EDGE_DISTANCE    4
25
26 /* these can't be larger than 31, or they mess up utf8 coding or are the same
27    as some ascii letter */
28 #define GD_DOWN_CHAR            1
29 #define GD_LEFT_CHAR            2
30 #define GD_UP_CHAR              3
31 #define GD_RIGHT_CHAR           4
32
33 #define GD_BALL_CHAR            5
34 #define GD_UNCHECKED_BOX_CHAR   6
35 #define GD_CHECKED_BOX_CHAR     7
36
37 #define GD_PLAYER_CHAR          8
38 #define GD_DIAMOND_CHAR         9
39 #define GD_SKELETON_CHAR        11
40 #define GD_KEY_CHAR             12
41 #define GD_COMMENT_CHAR         13
42
43 /* screen area */
44 Bitmap *gd_screen_bitmap = NULL;
45
46 static int play_area_w = 0;
47 static int play_area_h = 0;
48
49 static int scroll_x, scroll_y;
50
51 /* quit, global variable which is set to true if the application should quit */
52 boolean gd_quit = FALSE;
53
54 const byte *gd_keystate;
55
56 static int cell_size = 0;
57
58 /* graphic info for game objects/frames and players/actions/frames */
59 struct GraphicInfo_BD graphic_info_bd_object[O_MAX_ALL][8];
60
61 void set_cell_size(int s)
62 {
63   cell_size = s;
64 }
65
66 void set_play_area(int w, int h)
67 {
68   play_area_w = w;
69   play_area_h = h;
70 }
71
72 void gd_init_keystate(void)
73 {
74   set_play_area(SXSIZE, SYSIZE);
75
76   gd_keystate = SDL_GetKeyboardState(NULL);
77 }
78
79 /*
80   logical_size: logical pixel size of playfield, usually larger than the screen.
81   physical_size: visible part. (remember: player_x-x1!)
82
83   center: the coordinates to scroll to.
84   exact: scroll exactly
85   start: start scrolling if difference is larger than
86   to: scroll to, if started, until difference is smaller than
87   current
88
89   desired: the function stores its data here
90   speed: the function stores its data here
91
92   cell_size: size of one cell. used to determine if the play field is only a
93   slightly larger than the screen, in that case no scrolling is desirable
94 */
95 static boolean cave_scroll(int logical_size, int physical_size, int center, boolean exact,
96                            int *current, int *desired, int speed)
97 {
98   int max = MAX(0, logical_size - physical_size);
99   int edge_distance = SCROLL_EDGE_DISTANCE;
100   int cell_size = TILESIZE_VAR;
101   boolean changed = FALSE;
102
103   /* start scrolling when player reaches certain distance to screen edge */
104   int start = physical_size / 2 - cell_size * edge_distance;
105
106   /* scroll so that the player is at the center; the allowed difference is this */
107   int to = cell_size;
108
109   /* if cave size smaller than the screen, no scrolling req'd */
110   if (logical_size < physical_size)
111   {
112     *desired = 0;
113
114     if (*current != 0)
115     {
116       *current = 0;
117       changed = TRUE;
118     }
119
120     return changed;
121   }
122
123   if (logical_size <= physical_size + cell_size)
124   {
125     /* if cave size is only a slightly larger than the screen, also no scrolling */
126     /* scroll to the middle of the cell */
127     *desired = max / 2;
128   }
129   else
130   {
131     if (exact)
132     {
133       /* if exact scrolling, just go exactly to the center. */
134       *desired = center;
135     }
136     else
137     {
138       /* hystheresis function.
139        * when scrolling left, always go a bit less left than player being at the middle.
140        * when scrolling right, always go a bit less to the right. */
141       if (*current < center - start)
142         *desired = center - to;
143       if (*current > center + start)
144         *desired = center + to;
145     }
146   }
147
148   *desired = MIN(MAX(0, *desired), max);
149
150   if (*current < *desired)
151   {
152     *current = MIN(*current + speed, *desired);
153
154     changed = TRUE;
155   }
156
157   if (*current > *desired)
158   {
159     *current = MAX(*current - speed, *desired);
160
161     changed = TRUE;
162   }
163
164   return changed;
165 }
166
167 /* just set current viewport to upper left. */
168 void gd_scroll_to_origin(void)
169 {
170   scroll_x = 0;
171   scroll_y = 0;
172 }
173
174 int get_scroll_x(void)
175 {
176   return scroll_x / cell_size;
177 }
178
179 int get_scroll_y(void)
180 {
181   return scroll_y / cell_size;
182 }
183
184 int get_play_area_w(void)
185 {
186   return play_area_w / cell_size;
187 }
188
189 int get_play_area_h(void)
190 {
191   return play_area_h / cell_size;
192 }
193
194 /* SCROLLING
195  *
196  * scrolls to the player during game play.
197  * called by drawcave
198  * returns true, if player is not visible-ie it is out of the visible size in the drawing area.
199  */
200 boolean gd_scroll(GdGame *game, boolean exact_scroll, boolean immediate)
201 {
202   static int scroll_desired_x = 0, scroll_desired_y = 0;
203   boolean out_of_window;
204   int player_x, player_y, visible_x, visible_y;
205   boolean changed;
206
207   /* max scrolling speed depends on the speed of the cave. */
208   /* game moves cell_size_game * 1s / cave time pixels in a second. */
209   /* scrolling moves scroll speed * 1s / scroll_time in a second. */
210   /* these should be almost equal; scrolling speed a little slower. */
211   /* that way, the player might reach the border with a small probability, */
212   /* but the scrolling will not "oscillate", ie. turn on for little intervals as it has */
213   /* caught up with the desired position. smaller is better. */
214   int scroll_speed = cell_size * 20 / game->cave->speed;
215
216   if (!setup.bd_scroll_delay)
217     exact_scroll = TRUE;
218
219   if (immediate)
220     scroll_speed = cell_size * MAX(game->cave->w, game->cave->h);
221
222   player_x = game->cave->player_x - game->cave->x1; /* cell coordinates of player */
223   player_y = game->cave->player_y - game->cave->y1;
224
225   /* pixel size of visible part of the cave (may be smaller in intermissions) */
226   visible_x = (game->cave->x2 - game->cave->x1 + 1) * cell_size;
227   visible_y = (game->cave->y2 - game->cave->y1 + 1) * cell_size;
228
229   /* cell_size contains the scaled size, but we need the original. */
230   changed = FALSE;
231
232   if (cave_scroll(visible_x, play_area_w, player_x * cell_size + cell_size / 2 - play_area_w / 2,
233                   exact_scroll, &scroll_x, &scroll_desired_x, scroll_speed))
234     changed = TRUE;
235
236   if (cave_scroll(visible_y, play_area_h, player_y * cell_size + cell_size / 2 - play_area_h / 2,
237                   exact_scroll, &scroll_y, &scroll_desired_y, scroll_speed))
238     changed = TRUE;
239
240   /* if scrolling, we should update entire screen. */
241   if (changed)
242   {
243     int x, y;
244
245     for (y = 0; y < game->cave->h; y++)
246       for (x = 0; x < game->cave->w; x++)
247         game->gfx_buffer[y][x] |= GD_REDRAW;
248   }
249
250   /* check if active player is visible at the moment. */
251   out_of_window = FALSE;
252
253   /* check if active player is outside drawing area. if yes, we should wait for scrolling */
254   if ((player_x * cell_size) < scroll_x ||
255       (player_x * cell_size + cell_size - 1) > scroll_x + play_area_w)
256   {
257     /* but only do the wait, if the player SHOULD BE visible, ie. he is inside
258        the defined visible area of the cave */
259     if (game->cave->player_x >= game->cave->x1 &&
260         game->cave->player_x <= game->cave->x2)
261       out_of_window = TRUE;
262   }
263
264   if ((player_y * cell_size) < scroll_y ||
265       (player_y * cell_size + cell_size - 1) > scroll_y + play_area_h)
266     /* but only do the wait, if the player SHOULD BE visible, ie. he is inside
267        the defined visible area of the cave */
268     if (game->cave->player_y >= game->cave->y1 &&
269         game->cave->player_y <= game->cave->y2)
270       out_of_window = TRUE;
271
272   /* if not yet born, we treat as visible. so cave will run.
273      the user is unable to control an unborn player, so this is the right behaviour. */
274   if (game->cave->player_state == GD_PL_NOT_YET)
275     return FALSE;
276
277   return out_of_window;
278 }
279
280 #if DO_GFX_SANITY_CHECK
281 /* workaround to prevent variable name scope problem */
282 static boolean use_native_bd_graphics_engine(void)
283 {
284   return game.use_native_bd_graphics_engine;
285 }
286 #endif
287
288 /* returns true if the element is a player */
289 static inline boolean is_player(const int element)
290 {
291   return (gd_elements[element & O_MASK].properties & P_PLAYER) != 0;
292 }
293
294 /* returns true if the element is collectible */
295 static inline boolean is_collectible(const int element)
296 {
297   return (gd_elements[element & O_MASK].properties & P_COLLECTIBLE) != 0;
298 }
299
300 /* returns true if the element is exploding */
301 static inline boolean is_explosion(const int element)
302 {
303   return (gd_elements[element & O_MASK].properties & P_EXPLOSION) != 0;
304 }
305
306 static void gd_drawcave_tile(Bitmap *dest, GdGame *game, int x, int y, boolean draw_masked)
307 {
308   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
309     (draw_masked ? BlitBitmapMasked : BlitBitmap);
310   GdCave *cave = game->cave;
311   int sx = x * cell_size - scroll_x;
312   int sy = y * cell_size - scroll_y;
313   int dir = game->dir_buffer[y][x];
314   int tile = game->element_buffer[y][x];
315   int frame = game->animcycle;
316   struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
317   boolean use_smooth_movements =
318     ((setup.bd_smooth_movements == TRUE) ||
319      (setup.bd_smooth_movements == AUTO && !use_native_bd_graphics_engine()));
320
321   // do not use smooth movement animation for exploding game elements (like player)
322   if (is_explosion(tile) && dir != GD_MV_STILL)
323     use_smooth_movements = FALSE;
324
325   // do not use smooth movement animation for player entering exit (engine stopped)
326   if (cave->player_state == GD_PL_EXITED)
327     use_smooth_movements = FALSE;
328
329 #if DO_GFX_SANITY_CHECK
330   if (use_native_bd_graphics_engine() && !setup.small_game_graphics && !program.headless)
331   {
332     int old_x = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) % GD_NUM_OF_CELLS_X;
333     int old_y = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) / GD_NUM_OF_CELLS_X;
334     int new_x = g->src_x / g->width;
335     int new_y = g->src_y / g->height;
336
337     if (new_x != old_x || new_y != old_y)
338     {
339       printf("::: BAD ANIMATION FOR TILE %d, FRAME %d [NEW(%d, %d) != OLD(%d, %d)] ['%s']\n",
340              tile, frame,
341              new_x, new_y,
342              old_x, old_y,
343              gd_elements[tile].name);
344     }
345   }
346 #endif
347
348   /* if game element not moving (or no smooth movements requested), simply draw tile */
349   if (dir == GD_MV_STILL || !use_smooth_movements)
350   {
351     blit_bitmap(g->bitmap, dest, g->src_x, g->src_y, cell_size, cell_size, sx, sy);
352
353     return;
354   }
355
356   /* draw smooth animation for game element moving between two cave tiles */
357
358   if (!(game->last_element_buffer[y][x] & SKIPPED))
359   {
360     /* redraw previous game element on the cave field the new element is moving to */
361     int tile_last = game->last_element_buffer[y][x];
362
363     /* only redraw previous game element if it is not collectible (like dirt etc.) */
364     if (is_collectible(tile_last))
365       tile_last = O_SPACE;
366
367     struct GraphicInfo_BD *g_old = &graphic_info_bd_object[tile_last][frame];
368
369     blit_bitmap(g_old->bitmap, dest, g_old->src_x, g_old->src_y, cell_size, cell_size, sx, sy);
370   }
371
372   /* get cave field position the game element is moving from */
373   int dx = (dir == GD_MV_LEFT ? +1 : dir == GD_MV_RIGHT ? -1 : 0);
374   int dy = (dir == GD_MV_UP   ? +1 : dir == GD_MV_DOWN  ? -1 : 0);
375   int old_x = cave->getx(cave, x + dx, y + dy);
376   int old_y = cave->gety(cave, x + dx, y + dy);
377   int tile_from = game->element_buffer[old_y][old_x];
378   struct GraphicInfo_BD *g_from = &graphic_info_bd_object[tile_from][frame];
379   boolean old_is_player = is_player(tile_from);
380   boolean old_is_moving = (game->dir_buffer[old_y][old_x] != GD_MV_STILL);
381   boolean old_is_visible = (old_x >= cave->x1 &&
382                             old_x <= cave->x2 &&
383                             old_y >= cave->y1 &&
384                             old_y <= cave->y2);
385
386   if (old_is_visible)
387   {
388     if (!old_is_moving && !old_is_player)
389     {
390       /* redraw game element on the cave field the element is moving from */
391       blit_bitmap(g_from->bitmap, dest, g_from->src_x, g_from->src_y, cell_size, cell_size,
392                   sx + dx * cell_size, sy + dy * cell_size);
393
394       game->element_buffer[old_y][old_x] |= SKIPPED;
395     }
396     else
397     {
398       /* if old tile also moving (like pushing player), do not redraw tile background */
399       game->last_element_buffer[old_y][old_x] |= SKIPPED;
400     }
401   }
402
403   /* get shifted position between cave fields the game element is moving from/to */
404   int itercycle = MIN(MAX(0, game->itermax - game->itercycle - 1), game->itermax);
405   int shift = cell_size * itercycle / game->itermax;
406
407   blit_bitmap(g->bitmap, dest, g->src_x, g->src_y, cell_size, cell_size,
408               sx + dx * shift, sy + dy * shift);
409
410   /* special case: redraw player snapping a game element */
411   if (old_is_visible && old_is_player && !old_is_moving)
412   {
413     /* redraw game element on the cave field the element is moving from */
414     blit_bitmap(g_from->bitmap, dest, g_from->src_x, g_from->src_y, cell_size, cell_size,
415                 sx + dx * cell_size, sy + dy * cell_size);
416   }
417 }
418
419 int gd_drawcave(Bitmap *dest, GdGame *game, boolean force_redraw)
420 {
421   GdCave *cave = game->cave;
422   static int show_flash_count = 0;
423   boolean show_flash = FALSE;
424   boolean draw_masked = FALSE;
425   boolean redraw_all = force_redraw;
426   int x, y;
427
428   /* force redraw if maximum number of cycles has changed (to redraw moving elements) */
429   if (game->itermax != game->itermax_last)
430     redraw_all = TRUE;
431
432   if (!cave->gate_open_flash)
433   {
434     show_flash_count = 0;
435   }
436   else
437   {
438     if (show_flash_count++ < 4)
439       show_flash = TRUE;
440
441     redraw_all = TRUE;
442   }
443
444   if (show_flash)
445   {
446     FillRectangle(dest, 0, 0, SXSIZE, SYSIZE, WHITE_PIXEL);
447
448     draw_masked = TRUE;
449     redraw_all = TRUE;
450   }
451
452   /* here we draw all cells to be redrawn. we do not take scrolling area into
453      consideration - sdl will do the clipping. */
454   for (y = cave->y1; y <= cave->y2; y++)
455   {
456     for (x = cave->x1; x <= cave->x2; x++)
457     {
458       if (redraw_all ||
459           game->gfx_buffer[y][x] & GD_REDRAW ||
460           game->dir_buffer[y][x] != GD_MV_STILL)
461       {
462         /* skip redrawing already drawn element with movement */
463         if (game->element_buffer[y][x] & SKIPPED)
464           continue;
465
466         /* now we have drawn it */
467         game->gfx_buffer[y][x] = game->gfx_buffer[y][x] & ~GD_REDRAW;
468
469         gd_drawcave_tile(dest, game, x, y, draw_masked);
470       }
471     }
472   }
473
474   return 0;
475 }