2 * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
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.
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.
22 // !!! (can be removed later) !!!
23 #define DO_GFX_SANITY_CHECK TRUE
25 /* distance to screen edge in cells when scrolling the screen */
26 #define SCROLL_EDGE_DISTANCE 4
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
33 #define GD_RIGHT_CHAR 4
35 #define GD_BALL_CHAR 5
36 #define GD_UNCHECKED_BOX_CHAR 6
37 #define GD_CHECKED_BOX_CHAR 7
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
46 Bitmap *gd_screen_bitmap = NULL;
48 static int play_area_w = 0;
49 static int play_area_h = 0;
51 static int scroll_x, scroll_y;
53 /* quit, global variable which is set to true if the application should quit */
54 boolean gd_quit = FALSE;
56 const guint8 *gd_keystate;
58 static int cell_size = 0;
60 /* graphic info for game objects/frames and players/actions/frames */
61 struct GraphicInfo_BD graphic_info_bd_object[O_MAX_ALL][8];
63 void set_cell_size(int s)
68 void set_play_area(int w, int h)
74 void gd_init_keystate(void)
76 set_play_area(SXSIZE, SYSIZE);
78 gd_keystate = SDL_GetKeyboardState(NULL);
82 logical_size: logical pixel size of playfield, usually larger than the screen.
83 physical_size: visible part. (remember: player_x-x1!)
85 center: the coordinates to scroll to.
87 start: start scrolling if difference is larger than
88 to: scroll to, if started, until difference is smaller than
91 desired: the function stores its data here
92 speed: the function stores its data here
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
97 static boolean cave_scroll(int logical_size, int physical_size, int center, boolean exact,
98 int *current, int *desired, int speed)
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;
105 /* start scrolling when player reaches certain distance to screen edge */
106 int start = physical_size / 2 - cell_size * edge_distance;
108 /* scroll so that the player is at the center; the allowed difference is this */
111 /* if cave size smaller than the screen, no scrolling req'd */
112 if (logical_size < physical_size)
125 if (logical_size <= physical_size + cell_size)
127 /* if cave size is only a slightly larger than the screen, also no scrolling */
128 /* scroll to the middle of the cell */
135 /* if exact scrolling, just go exactly to the center. */
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;
150 *desired = MIN(MAX(0, *desired), max);
152 if (*current < *desired)
154 *current = MIN(*current + speed, *desired);
159 if (*current > *desired)
161 *current = MAX(*current - speed, *desired);
169 /* just set current viewport to upper left. */
170 void gd_scroll_to_origin(void)
176 int get_scroll_x(void)
178 return scroll_x / cell_size;
181 int get_scroll_y(void)
183 return scroll_y / cell_size;
186 int get_play_area_w(void)
188 return play_area_w / cell_size;
191 int get_play_area_h(void)
193 return play_area_h / cell_size;
198 * scrolls to the player during game play.
200 * returns true, if player is not visible-ie it is out of the visible size in the drawing area.
202 boolean gd_scroll(GdGame *game, boolean exact_scroll, boolean immediate)
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;
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;
220 scroll_speed = cell_size * MAX(game->cave->w, game->cave->h);
222 player_x = game->cave->player_x - game->cave->x1; /* cell coordinates of player */
223 player_y = game->cave->player_y - game->cave->y1;
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;
229 /* cell_size contains the scaled size, but we need the original. */
232 /* some sort of scrolling speed.
233 with larger cells, the divisor must be smaller, so the scrolling faster. */
234 scroll_divisor = 256 / cell_size;
236 /* fine scrolling is 50hz (normal would be 25hz only) */
239 if (cave_scroll(visible_x, play_area_w, player_x * cell_size + cell_size / 2 - play_area_w / 2,
240 exact_scroll, &scroll_x, &scroll_desired_x, scroll_speed))
243 if (cave_scroll(visible_y, play_area_h, player_y * cell_size + cell_size / 2 - play_area_h / 2,
244 exact_scroll, &scroll_y, &scroll_desired_y, scroll_speed))
247 /* if scrolling, we should update entire screen. */
252 for (y = 0; y < game->cave->h; y++)
253 for (x = 0; x < game->cave->w; x++)
254 game->gfx_buffer[y][x] |= GD_REDRAW;
257 /* check if active player is visible at the moment. */
258 out_of_window = FALSE;
260 /* check if active player is outside drawing area. if yes, we should wait for scrolling */
261 if ((player_x * cell_size) < scroll_x ||
262 (player_x * cell_size + cell_size - 1) > scroll_x + play_area_w)
264 /* but only do the wait, if the player SHOULD BE visible, ie. he is inside
265 the defined visible area of the cave */
266 if (game->cave->player_x >= game->cave->x1 &&
267 game->cave->player_x <= game->cave->x2)
268 out_of_window = TRUE;
271 if ((player_y * cell_size) < scroll_y ||
272 (player_y * cell_size + cell_size - 1) > scroll_y + play_area_h)
273 /* but only do the wait, if the player SHOULD BE visible, ie. he is inside
274 the defined visible area of the cave */
275 if (game->cave->player_y >= game->cave->y1 &&
276 game->cave->player_y <= game->cave->y2)
277 out_of_window = TRUE;
279 /* if not yet born, we treat as visible. so cave will run.
280 the user is unable to control an unborn player, so this is the right behaviour. */
281 if (game->cave->player_state == GD_PL_NOT_YET)
284 return out_of_window;
287 #if DO_GFX_SANITY_CHECK
288 /* workaround to prevent variable name scope problem */
289 static boolean use_native_bd_graphics_engine(void)
291 return game.use_native_bd_graphics_engine;
295 int gd_drawcave(Bitmap *dest, GdGame *game, boolean force_redraw)
297 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) = BlitBitmap;
298 static int show_flash_count = 0;
299 boolean show_flash = FALSE;
300 boolean redraw_all = force_redraw;
301 int scroll_y_aligned = scroll_y;
304 if (!game->cave->gate_open_flash)
306 show_flash_count = 0;
310 if (show_flash_count++ < 4)
318 FillRectangle(dest, 0, 0, SXSIZE, SYSIZE, WHITE_PIXEL);
320 blit_bitmap = BlitBitmapMasked;
324 /* here we draw all cells to be redrawn. we do not take scrolling area into
325 consideration - sdl will do the clipping. */
326 for (y = game->cave->y1, yd = 0; y <= game->cave->y2; y++, yd++)
328 for (x = game->cave->x1, xd = 0; x <= game->cave->x2; x++, xd++)
330 if (redraw_all || game->gfx_buffer[y][x] & GD_REDRAW)
332 /* if it needs to be redrawn */
335 /* sdl_blitsurface destroys offset, so we have to set y here, too.
336 (ie. in every iteration) */
337 offset.y = y * cell_size - scroll_y_aligned;
338 offset.x = x * cell_size - scroll_x;
340 /* now we have drawn it */
341 game->gfx_buffer[y][x] = game->gfx_buffer[y][x] & ~GD_REDRAW;
343 int tile = game->element_buffer[y][x];
344 int frame = game->animcycle;
345 struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
346 int width = g->width * TILESIZE_VAR / TILESIZE;
347 int height = g->height * TILESIZE_VAR / TILESIZE;
349 blit_bitmap(g->bitmap, dest, g->src_x, g->src_y, width, height, offset.x, offset.y);
351 #if DO_GFX_SANITY_CHECK
352 if (use_native_bd_graphics_engine() && !setup.small_game_graphics && !program.headless)
354 int old_x = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) % GD_NUM_OF_CELLS_X;
355 int old_y = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) / GD_NUM_OF_CELLS_X;
356 int new_x = g->src_x / g->width;
357 int new_y = g->src_y / g->height;
359 if (new_x != old_x || new_y != old_y)
361 printf("::: BAD ANIMATION FOR TILE %d, FRAME %d [NEW(%d, %d) != OLD(%d, %d)] ['%s']\n",
365 gd_elements[tile].name);