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