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.
20 // !!! (can be removed later) !!!
21 #define DO_GFX_SANITY_CHECK TRUE
23 // distance to screen edge in cells when scrolling the screen
24 #define SCROLL_EDGE_DISTANCE 4
26 // these can't be larger than 31, or they mess up utf8 coding or are the same as some ascii letter
27 #define GD_DOWN_CHAR 1
28 #define GD_LEFT_CHAR 2
30 #define GD_RIGHT_CHAR 4
32 #define GD_BALL_CHAR 5
33 #define GD_UNCHECKED_BOX_CHAR 6
34 #define GD_CHECKED_BOX_CHAR 7
36 #define GD_PLAYER_CHAR 8
37 #define GD_DIAMOND_CHAR 9
38 #define GD_SKELETON_CHAR 11
39 #define GD_KEY_CHAR 12
40 #define GD_COMMENT_CHAR 13
42 // pointer to tile bitmap (which may be prepared with level-specific colors)
43 static Bitmap *gd_tile_bitmap = NULL;
44 // pointer to reference tile bitmap (without level-specific colors)
45 static Bitmap *gd_tile_bitmap_reference = NULL;
47 // optional title screen bitmaps (foreground and background)
48 static Bitmap *gd_title_screen_bitmaps[2] = { NULL, NULL };
51 Bitmap *gd_screen_bitmap = NULL;
53 static int play_area_w = 0;
54 static int play_area_h = 0;
56 static int scroll_x, scroll_y;
58 // quit, global variable which is set to true if the application should quit
59 boolean gd_quit = FALSE;
61 static int cell_size = 0;
63 // graphic info for game objects/frames and players/actions/frames
64 struct GraphicInfo_BD graphic_info_bd_object[O_MAX_ALL][8];
65 // graphic info for game graphics template for level-specific colors
66 struct GraphicInfo_BD graphic_info_bd_color_template;
68 static inline int c64_png_colors(int r, int g, int b, int a)
70 static const int c64_png_cols[] =
74 /* 0000 */ 0, // transparent
82 /* 1000 */ 1, // black - background
83 /* 1001 */ 2, // red - foreg1
84 /* 1010 */ 5, // green - amoeba
85 /* 1011 */ 4, // yellow - foreg3
86 /* 1100 */ 6, // blue - slime
87 /* 1101 */ 3, // purple - foreg2
88 /* 1110 */ 7, // black around arrows (used in editor) is coded as cyan
89 /* 1111 */ 8, // white is the arrow
92 int c = ((a >> 7) * 8 +
97 return c64_png_cols[c];
100 void set_cell_size(int s)
105 void set_play_area(int w, int h)
111 void gd_init_play_area(void)
113 set_play_area(SXSIZE, SYSIZE);
117 logical_size: logical pixel size of playfield, usually larger than the screen.
118 physical_size: visible part. (remember: player_x-x1!)
120 center: the coordinates to scroll to.
121 exact: scroll exactly
122 start: start scrolling if difference is larger than
123 to: scroll to, if started, until difference is smaller than
126 desired: the function stores its data here
127 speed: the function stores its data here
129 cell_size: size of one cell. used to determine if the play field is only a
130 slightly larger than the screen, in that case no scrolling is desirable
132 static boolean cave_scroll(int logical_size, int physical_size, int center, boolean exact,
133 int *current, int *desired, int speed)
135 int max = MAX(0, logical_size - physical_size);
136 int edge_distance = SCROLL_EDGE_DISTANCE;
137 int cell_size = TILESIZE_VAR;
138 boolean changed = FALSE;
140 // start scrolling when player reaches certain distance to screen edge
141 int start = physical_size / 2 - cell_size * edge_distance;
143 // scroll so that the player is at the center; the allowed difference is this
146 // if cave size smaller than the screen, no scrolling req'd
147 if (logical_size < physical_size)
160 if (logical_size <= physical_size + cell_size)
162 // if cave size is only a slightly larger than the screen, also no scrolling
163 // scroll to the middle of the cell
170 // if exact scrolling, just go exactly to the center.
175 // hystheresis function.
176 // when scrolling left, always go a bit less left than player being at the middle.
177 // when scrolling right, always go a bit less to the right.
178 if (*current < center - start)
179 *desired = center - to;
180 if (*current > center + start)
181 *desired = center + to;
185 *desired = MIN(MAX(0, *desired), max);
187 if (*current < *desired)
189 *current = MIN(*current + speed, *desired);
194 if (*current > *desired)
196 *current = MAX(*current - speed, *desired);
204 // just set current viewport to upper left.
205 void gd_scroll_to_origin(void)
211 int get_scroll_x(void)
213 return scroll_x / cell_size;
216 int get_scroll_y(void)
218 return scroll_y / cell_size;
221 int get_play_area_w(void)
223 return play_area_w / cell_size;
226 int get_play_area_h(void)
228 return play_area_h / cell_size;
231 static boolean player_out_of_window(GdGame *game, int player_x, int player_y)
233 // if not yet born, we treat as visible. so cave will run.
234 // the user is unable to control an unborn player, so this is the right behaviour.
235 if (game->cave->player_state == GD_PL_NOT_YET)
238 // check if active player is outside drawing area. if yes, we should wait for scrolling
239 if ((player_x * cell_size) < scroll_x ||
240 (player_x * cell_size + cell_size - 1) > scroll_x + play_area_w)
242 // but only do the wait, if the player SHOULD BE visible, ie. he is inside
243 // the defined visible area of the cave
244 if (game->cave->player_x >= game->cave->x1 &&
245 game->cave->player_x <= game->cave->x2)
249 if ((player_y * cell_size) < scroll_y ||
250 (player_y * cell_size + cell_size - 1) > scroll_y + play_area_h)
252 // but only do the wait, if the player SHOULD BE visible, ie. he is inside
253 // the defined visible area of the cave
254 if (game->cave->player_y >= game->cave->y1 &&
255 game->cave->player_y <= game->cave->y2)
259 // player is inside visible window
266 scrolls to the player during game play.
268 returns true, if player is not visible-ie it is out of the visible size in the drawing area.
270 boolean gd_scroll(GdGame *game, boolean exact_scroll, boolean immediate)
272 static int scroll_desired_x = 0, scroll_desired_y = 0;
273 static int scroll_speed_last = -1;
274 int player_x, player_y, visible_x, visible_y;
277 // max scrolling speed depends on the speed of the cave.
278 // game moves cell_size_game * 1s / cave time pixels in a second.
279 // scrolling moves scroll speed * 1s / scroll_time in a second.
280 // these should be almost equal; scrolling speed a little slower.
281 // that way, the player might reach the border with a small probability,
282 // but the scrolling will not "oscillate", ie. turn on for little intervals as it has
283 // caught up with the desired position. smaller is better.
284 int scroll_speed = cell_size * 20 / game->cave->speed;
286 if (!setup.bd_scroll_delay)
290 scroll_speed = cell_size * MAX(game->cave->w, game->cave->h);
292 player_x = game->cave->player_x - game->cave->x1; // cell coordinates of player
293 player_y = game->cave->player_y - game->cave->y1;
295 // if player is outside visible playfield area, use faster scrolling
296 // (might happen when wrapping around the playfield, teleporting etc.)
297 if (player_out_of_window(game, player_x, player_y))
300 // if scrolling started with player outside visible playfield area, keep faster scrolling
301 if (scroll_speed_last > scroll_speed)
302 scroll_speed = scroll_speed_last;
304 // store current (potentially faster) scrolling speed for next time
305 scroll_speed_last = scroll_speed;
307 // pixel size of visible part of the cave (may be smaller in intermissions)
308 visible_x = (game->cave->x2 - game->cave->x1 + 1) * cell_size;
309 visible_y = (game->cave->y2 - game->cave->y1 + 1) * cell_size;
311 // cell_size contains the scaled size, but we need the original.
314 if (cave_scroll(visible_x, play_area_w, player_x * cell_size + cell_size / 2 - play_area_w / 2,
315 exact_scroll, &scroll_x, &scroll_desired_x, scroll_speed))
318 if (cave_scroll(visible_y, play_area_h, player_y * cell_size + cell_size / 2 - play_area_h / 2,
319 exact_scroll, &scroll_y, &scroll_desired_y, scroll_speed))
322 // if scrolling, we should update entire screen.
327 for (y = 0; y < game->cave->h; y++)
328 for (x = 0; x < game->cave->w; x++)
329 game->gfx_buffer[y][x] |= GD_REDRAW;
332 // if no scrolling required, reset last (potentially faster) scrolling speed
334 scroll_speed_last = -1;
336 // check if active player is visible at the moment.
337 return player_out_of_window(game, player_x, player_y);
340 // returns true, if the given surface seems to be a c64 imported image.
341 static boolean surface_has_c64_colors(SDL_Surface *surface)
343 boolean has_c64_colors = TRUE; // default: assume C64 colors
344 const unsigned char *p;
347 if (surface->format->BytesPerPixel != 4)
350 SDL_LockSurface(surface);
352 for (y = 0; y < surface->h && has_c64_colors; y++)
354 p = ((unsigned char *)surface->pixels) + y * surface->pitch;
356 for (x = 0; x < surface->w * surface->format->BytesPerPixel && has_c64_colors; x++)
357 if (p[x] != 0 && p[x] != 255)
358 has_c64_colors = FALSE;
361 SDL_UnlockSurface(surface);
363 return has_c64_colors;
366 // sets one of the colors in the indexed palette of an sdl surface to a GdColor.
367 static void set_surface_palette_color(SDL_Surface *surface, int index, GdColor col)
369 if (surface->format->BytesPerPixel != 1)
374 c.r = gd_color_get_r(col);
375 c.g = gd_color_get_g(col);
376 c.b = gd_color_get_b(col);
378 SDL_SetPaletteColors(surface->format->palette, &c, index, 1);
381 // takes a c64_gfx.png-coded 32-bit surface, and creates a paletted surface in our internal format.
382 static SDL_Surface *get_tile_surface_c64(SDL_Surface *surface, int scale_down_factor)
384 static SDL_Surface *tile_surface_c64 = NULL;
385 static unsigned char *pixels = NULL;
386 int width = surface->w;
387 int height = surface->h;
391 if (!surface_has_c64_colors(surface))
394 if (surface->format->BytesPerPixel != 4)
395 Fail("C64 style surface has wrong color depth -- should not happen");
397 if (tile_surface_c64 != NULL)
398 SDL_FreeSurface(tile_surface_c64);
400 checked_free(pixels);
402 pixels = checked_malloc(width * height);
404 SDL_LockSurface(surface);
406 for (y = 0; y < height; y++)
408 unsigned int *p = (unsigned int *)((char *)surface->pixels + y * surface->pitch);
410 for (x = 0; x < width; x++)
412 int r = (p[x] & surface->format->Rmask) >> surface->format->Rshift << surface->format->Rloss;
413 int g = (p[x] & surface->format->Gmask) >> surface->format->Gshift << surface->format->Gloss;
414 int b = (p[x] & surface->format->Bmask) >> surface->format->Bshift << surface->format->Bloss;
416 // a = (p[x]&surface->format->Amask) >> surface->format->Ashift << surface->format->Aloss;
417 // but we do not use the alpha channel in sdash, so we just use 255 (max alpha)
419 pixels[out++] = c64_png_colors(r, g, b, 255);
423 SDL_UnlockSurface(surface);
425 // create new surface from pixel data
427 SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 8, width, 0, 0, 0, 0);
429 if (tile_surface_c64 == NULL)
430 Fail("SDL_CreateRGBSurfaceFrom() failed: %s", SDL_GetError());
432 if (scale_down_factor > 1)
434 SDL_Surface *surface_old = tile_surface_c64;
435 int width_scaled = width / scale_down_factor;
436 int height_scaled = height / scale_down_factor;
438 // replace surface with scaled-down variant
439 tile_surface_c64 = SDLZoomSurface(surface_old, width_scaled, height_scaled);
441 // free previous (non-scaled) surface
442 SDL_FreeSurface(surface_old);
445 return tile_surface_c64;
448 static Bitmap *get_tile_bitmap_c64(GdCave *cave, SDL_Surface *surface)
450 static Bitmap *tile_bitmap_c64 = NULL;
455 if (tile_bitmap_c64 != NULL)
456 FreeBitmap(tile_bitmap_c64);
458 // set surface color palette to cave colors
459 set_surface_palette_color(surface, 0, 0);
460 set_surface_palette_color(surface, 1, gd_color_get_rgb(cave->color0));
461 set_surface_palette_color(surface, 2, gd_color_get_rgb(cave->color1));
462 set_surface_palette_color(surface, 3, gd_color_get_rgb(cave->color2));
463 set_surface_palette_color(surface, 4, gd_color_get_rgb(cave->color3));
464 set_surface_palette_color(surface, 5, gd_color_get_rgb(cave->color4));
465 set_surface_palette_color(surface, 6, gd_color_get_rgb(cave->color5));
466 set_surface_palette_color(surface, 7, 0);
467 set_surface_palette_color(surface, 8, 0);
469 // create bitmap from C64 surface
470 tile_bitmap_c64 = SDLGetBitmapFromSurface(surface);
472 return tile_bitmap_c64;
475 void gd_prepare_tile_bitmap(GdCave *cave, Bitmap *bitmap, int scale_down_factor)
477 static SDL_Surface *tile_surface_c64 = NULL;
478 static Bitmap *gd_tile_bitmap_original = NULL;
479 static int scale_down_factor_last = -1;
481 if (program.headless)
484 // check if tile bitmap has changed (different artwork or tile size selected)
485 if (bitmap != gd_tile_bitmap_original || scale_down_factor != scale_down_factor_last)
487 // check if tile bitmap has limited C64 style colors
488 tile_surface_c64 = get_tile_surface_c64(bitmap->surface, scale_down_factor);
490 // store original tile bitmap from current artwork set and scaling factor
491 gd_tile_bitmap_original = bitmap;
492 scale_down_factor_last = scale_down_factor;
494 // store reference tile bitmap from current artwork set (may be changed later)
495 gd_tile_bitmap_reference = bitmap;
498 // check if tile bitmap should be colored for next game
499 if (tile_surface_c64 != NULL)
501 // set tile bitmap to bitmap with level-specific colors
502 gd_tile_bitmap = get_tile_bitmap_c64(cave, tile_surface_c64);
506 // no special tile bitmap available for this artwork set
507 gd_tile_bitmap = NULL;
511 void gd_set_tile_bitmap_reference(Bitmap *bitmap)
513 gd_tile_bitmap_reference = bitmap;
516 Bitmap *gd_get_tile_bitmap(Bitmap *bitmap)
518 // if no special tile bitmap available, keep using default tile bitmap
519 if (gd_tile_bitmap == NULL)
522 // if default bitmap refers to tile bitmap, use special tile bitmap
523 if (bitmap == gd_tile_bitmap_reference)
524 return gd_tile_bitmap;
529 // returns true if the element is a player
530 static inline boolean el_player(const int element)
532 return (gd_elements[element & O_MASK].properties & P_PLAYER) != 0;
535 // returns true if the element is diggable
536 static inline boolean el_diggable(const int element)
538 return (gd_elements[element & O_MASK].properties & P_DIGGABLE) != 0;
542 // returns true if the element is collectible
543 static inline boolean el_collectible(const int element)
545 return (gd_elements[element & O_MASK].properties & P_COLLECTIBLE) != 0;
549 // returns true if the element is pushable
550 static inline boolean el_pushable(const int element)
552 return (gd_elements[element & O_MASK].properties & P_PUSHABLE) != 0;
555 // returns true if the element can move
556 static inline boolean el_can_move(const int element)
558 return (gd_elements[element & O_MASK].properties & P_CAN_MOVE) != 0;
561 // returns true if the element can fall
562 static inline boolean el_falling(const int element)
564 return (gd_elements[element & O_MASK].properties & P_FALLING) != 0;
567 // returns true if the element is growing
568 static inline boolean el_growing(const int element)
570 return (gd_elements[element & O_MASK].properties & P_GROWING) != 0;
573 // returns true if the element is exploding
574 static inline boolean el_explosion(const int element)
576 return (gd_elements[element & O_MASK].properties & P_EXPLOSION) != 0;
579 static void gd_drawcave_tile(Bitmap *dest, GdGame *game, int x, int y, boolean draw_masked)
581 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
582 (draw_masked ? BlitBitmapMasked : BlitBitmap);
583 GdCave *cave = game->cave;
584 int sx = x * cell_size - scroll_x;
585 int sy = y * cell_size - scroll_y;
586 int dir = game->dir_buffer[y][x];
587 int tile = game->element_buffer[y][x];
588 int frame = game->animcycle;
589 struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
590 Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
591 boolean is_movable = (el_can_move(tile) || el_falling(tile) || el_pushable(tile) ||
593 boolean is_movable_or_diggable = (is_movable || el_diggable(game->last_element_buffer[y][x]));
594 boolean is_moving = (is_movable_or_diggable && dir != GD_MV_STILL);
595 boolean use_smooth_movements = use_bd_smooth_movements();
597 // do not use smooth movement animation for growing or exploding game elements
598 if ((el_growing(tile) || el_explosion(tile)) && dir != GD_MV_STILL)
600 int dx = (dir == GD_MV_LEFT ? +1 : dir == GD_MV_RIGHT ? -1 : 0);
601 int dy = (dir == GD_MV_UP ? +1 : dir == GD_MV_DOWN ? -1 : 0);
602 int old_x = cave->getx(cave, x + dx, y + dy);
603 int old_y = cave->gety(cave, x + dx, y + dy);
604 int last_tile_from = game->last_element_buffer[old_y][old_x] & ~SKIPPED;
605 boolean old_is_player = el_player(last_tile_from);
607 // check special case of player running into enemy from top or left side
610 game->element_buffer[y][x] = (dir == GD_MV_LEFT ? O_PLAYER_LEFT :
611 dir == GD_MV_RIGHT ? O_PLAYER_RIGHT :
612 dir == GD_MV_UP ? O_PLAYER_UP :
613 dir == GD_MV_DOWN ? O_PLAYER_DOWN : O_PLAYER);
615 // draw player running into explosion (else player would disappear immediately)
616 gd_drawcave_tile(dest, game, x, y, draw_masked);
618 game->element_buffer[y][x] = tile;
621 use_smooth_movements = FALSE;
624 // do not use smooth movement animation for player entering exit (engine stopped)
625 if (cave->player_state == GD_PL_EXITED)
626 use_smooth_movements = FALSE;
628 #if DO_GFX_SANITY_CHECK
629 if (use_native_bd_graphics_engine() && !setup.small_game_graphics && !program.headless)
631 int old_x = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) % GD_NUM_OF_CELLS_X;
632 int old_y = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) / GD_NUM_OF_CELLS_X;
633 int new_x = g->src_x / g->width;
634 int new_y = g->src_y / g->height;
636 if (new_x != old_x || new_y != old_y)
638 printf("::: BAD ANIMATION FOR TILE %d, FRAME %d [NEW(%d, %d) != OLD(%d, %d)] ['%s']\n",
642 gd_elements[tile].name);
647 // if game element not moving (or no smooth movements requested), simply draw tile
648 if (!is_moving || !use_smooth_movements)
650 blit_bitmap(tile_bitmap, dest, g->src_x, g->src_y, cell_size, cell_size, sx, sy);
655 // draw smooth animation for game element moving between two cave tiles
657 if (!(game->last_element_buffer[y][x] & SKIPPED))
659 // redraw previous game element on the cave field the new element is moving to
660 int tile_last = game->last_element_buffer[y][x];
662 // only redraw previous game element if it is diggable (like dirt etc.)
663 if (!el_diggable(tile_last))
666 struct GraphicInfo_BD *g_old = &graphic_info_bd_object[tile_last][frame];
667 Bitmap *tile_bitmap_old = gd_get_tile_bitmap(g_old->bitmap);
669 blit_bitmap(tile_bitmap_old, dest, g_old->src_x, g_old->src_y, cell_size, cell_size, sx, sy);
672 // get cave field position the game element is moving from
673 int dx = (dir == GD_MV_LEFT ? +1 : dir == GD_MV_RIGHT ? -1 : 0);
674 int dy = (dir == GD_MV_UP ? +1 : dir == GD_MV_DOWN ? -1 : 0);
675 int old_x = cave->getx(cave, x + dx, y + dy);
676 int old_y = cave->gety(cave, x + dx, y + dy);
677 int tile_from = game->element_buffer[old_y][old_x] & ~SKIPPED; // should never be skipped
678 struct GraphicInfo_BD *g_from = &graphic_info_bd_object[tile_from][frame];
679 Bitmap *tile_bitmap_from = gd_get_tile_bitmap(g_from->bitmap);
680 boolean old_is_player = el_player(tile_from);
681 boolean old_is_moving = (game->dir_buffer[old_y][old_x] != GD_MV_STILL);
682 boolean old_is_visible = (old_x >= cave->x1 &&
688 if (!old_is_moving && !old_is_player)
690 // redraw game element on the cave field the element is moving from
691 blit_bitmap(tile_bitmap_from, dest, g_from->src_x, g_from->src_y, cell_size, cell_size,
692 sx + dx * cell_size, sy + dy * cell_size);
694 game->element_buffer[old_y][old_x] |= SKIPPED;
698 // if old tile also moving (like pushing player), do not redraw tile background
699 // (but redraw if tile and old tile are moving/falling into different directions)
700 if (game->dir_buffer[old_y][old_x] == game->dir_buffer[y][x])
701 game->last_element_buffer[old_y][old_x] |= SKIPPED;
705 // get shifted position between cave fields the game element is moving from/to
706 int itercycle = MIN(MAX(0, game->itermax - game->itercycle - 1), game->itermax);
707 int shift = cell_size * itercycle / game->itermax;
709 blit_bitmap(tile_bitmap, dest, g->src_x, g->src_y, cell_size, cell_size,
710 sx + dx * shift, sy + dy * shift);
712 // special case: redraw player snapping a game element
713 if (old_is_visible && old_is_player && !old_is_moving)
715 // redraw game element on the cave field the element is moving from
716 blit_bitmap(tile_bitmap_from, dest, g_from->src_x, g_from->src_y, cell_size, cell_size,
717 sx + dx * cell_size, sy + dy * cell_size);
721 int gd_drawcave(Bitmap *dest, GdGame *game, boolean force_redraw)
723 GdCave *cave = game->cave;
724 static int show_flash_count = 0;
725 boolean show_flash = FALSE;
726 boolean draw_masked = FALSE;
727 boolean redraw_all = force_redraw;
730 // force redraw if maximum number of cycles has changed (to redraw moving elements)
731 if (game->itermax != game->itermax_last)
734 if (!cave->gate_open_flash)
736 show_flash_count = 0;
740 if (show_flash_count++ < 4)
748 FillRectangle(dest, 0, 0, SXSIZE, SYSIZE, WHITE_PIXEL);
754 // here we draw all cells to be redrawn. we do not take scrolling area into
755 // consideration - sdl will do the clipping.
756 for (y = cave->y1; y <= cave->y2; y++)
758 for (x = cave->x1; x <= cave->x2; x++)
761 game->gfx_buffer[y][x] & GD_REDRAW ||
762 game->dir_buffer[y][x] != GD_MV_STILL)
764 // skip redrawing already drawn element with movement
765 if (game->element_buffer[y][x] & SKIPPED)
768 // now we have drawn it
769 game->gfx_buffer[y][x] = game->gfx_buffer[y][x] & ~GD_REDRAW;
771 gd_drawcave_tile(dest, game, x, y, draw_masked);
779 static SDL_Surface *get_surface_from_raw_data(const unsigned char *data, int size)
781 SDL_RWops *rwop = SDL_RWFromConstMem(data, size);
782 SDL_Surface *surface = IMG_Load_RW(rwop, 1); // 1 = automatically closes rwop
787 static SDL_Surface *get_surface_from_base64(const char *base64_data)
789 int decoded_data_size = base64_decoded_size(base64_data);
790 unsigned char *decoded_data = checked_malloc(decoded_data_size);
792 base64_decode(decoded_data, base64_data);
794 SDL_Surface *surface = get_surface_from_raw_data(decoded_data, decoded_data_size);
796 checked_free(decoded_data);
801 static SDL_Surface *get_title_screen_background_surface(SDL_Surface *tile)
806 SDL_Surface *foreground_surface = gd_title_screen_bitmaps[0]->surface_masked;
808 // if foreground image has no transparency, no background image needed
809 if (foreground_surface->format->Amask == 0)
812 // use foreground image size for background image size
813 int w = foreground_surface->w;
814 int h = foreground_surface->h + tile->h; // background is one scrolling tile higher
816 // create background surface
817 SDL_Surface *back = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
820 // fill background surface with tile
821 for (y = 0; y < h; y += tile->h)
822 for (x = 0; x < w; x += tile->w)
823 SDLBlitSurface(tile, back, 0, 0, tile->w, tile->h, x, y);
825 // background tile surface not needed anymore
826 SDL_FreeSurface(tile);
831 static SDL_Surface *get_title_screen_surface(int nr)
833 if (gd_caveset_data == NULL)
836 // do not use title screen background without foreground image
837 if (nr == 1 && gd_title_screen_bitmaps[0] == NULL)
840 char *data = (nr == 0 ? gd_caveset_data->title_screen : gd_caveset_data->title_screen_scroll);
845 SDL_Surface *surface = get_surface_from_base64(data);
850 return (nr == 0 ? surface : get_title_screen_background_surface(surface));
853 static void set_title_screen_bitmap(int nr)
855 if (gd_title_screen_bitmaps[nr] != NULL)
856 FreeBitmap(gd_title_screen_bitmaps[nr]);
858 gd_title_screen_bitmaps[nr] = NULL;
860 SDL_Surface *surface = get_title_screen_surface(nr);
865 int width_scaled = surface->w * 2;
866 int height_scaled = surface->h * 2;
867 SDL_Surface *surface_scaled = SDLZoomSurface(surface, width_scaled, height_scaled);
869 gd_title_screen_bitmaps[nr] = SDLGetBitmapFromSurface(surface_scaled);
871 SDL_FreeSurface(surface);
872 SDL_FreeSurface(surface_scaled);
875 static void set_title_screen_bitmaps(void)
879 for (i = 0; i < 2; i++)
880 set_title_screen_bitmap(i);
883 Bitmap **gd_get_title_screen_bitmaps(void)
885 static char *levelset_subdir_last = NULL;
887 if (gd_caveset_data == NULL || gd_caveset_data->title_screen == NULL)
890 // check if stored cave set is used as current level set (may be outdated)
891 if (!strEqual(gd_caveset_data->levelset_subdir, leveldir_current->subdir))
894 // check if stored cave set has changed
895 if (!strEqual(gd_caveset_data->levelset_subdir, levelset_subdir_last))
896 set_title_screen_bitmaps();
898 setString(&levelset_subdir_last, gd_caveset_data->levelset_subdir);
900 return gd_title_screen_bitmaps;