/* Create a masked surface from the given transparent sprite and mask. The sprite must be at least as wide as the mask plus mask offset. */ static SDL_Surface * create_masked_transp_surface(const sprite_t *sprite, const sprite_t *mask, int mask_off) { void *s_data = (uint8_t *)sprite + sizeof(sprite_t); size_t s_width = le16toh(sprite->w); size_t s_height = le16toh(sprite->h); /* Unpack */ size_t unpack_size = s_width * s_height; uint8_t *unpack = new uint8_t[unpack_size]; if (unpack == NULL) abort(); data_unpack_transparent_sprite(unpack, s_data, unpack_size, 0); size_t m_width = le16toh(mask->w); size_t m_height = le16toh(mask->h); uint8_t *s_copy = new uint8_t[m_width * m_height]; if (s_copy == NULL) abort(); size_t to_copy = m_width * min(m_height, s_height); uint8_t *copy_dest = s_copy; uint8_t *copy_src = unpack + mask_off; while (to_copy) { memcpy(copy_dest, copy_src, m_width * sizeof(uint8_t)); to_copy -= m_width; copy_dest += m_width; copy_src += s_width; } free(unpack); /* Mask */ void *m_data = (uint8_t *)mask + sizeof(sprite_t); /* Unpack mask */ size_t m_unpack_size = m_width * m_height; uint8_t *m_unpack = new uint8_t[m_unpack_size]; if (m_unpack == NULL) abort(); data_unpack_mask_sprite(m_unpack, m_data, m_unpack_size); /* Fill alpha value from mask data */ for (int y = 0; y < m_height; y++) { for (int x = 0; x < m_width; x++) { if (!m_unpack[y*m_width+x]) { *(s_copy + y * m_width + x) = 0; } } } free(m_unpack); SDL_Surface *surf = create_surface_from_data(s_copy, (int)m_width, (int)m_height, 1); free(s_copy); return surf; }
static SDL_Surface * create_surface_from_sprite(const sprite_t *sprite) { void *data = (uint8_t *)sprite + sizeof(sprite_t); uint width = sprite->width; uint height = sprite->height; return create_surface_from_data(data, width, height); }
void sdl_draw_waves_sprite(const sprite_t *sprite, const sprite_t *mask, int x, int y, int mask_off, frame_t *dest) { x += le16toh(sprite->x) + dest->clip.x; y += le16toh(sprite->y) + dest->clip.y; const surface_id_t id = { .sprite = sprite, .mask = mask, .offset = 0 }; surface_t **surface = surface_ht_store(&transp_sprite_cache, &id); if (*surface == NULL) { *surface = new surface_t; if (*surface == NULL) abort(); if (mask != NULL) { (*surface)->surf = create_masked_transp_surface(sprite, mask, mask_off); } else { (*surface)->surf = create_transp_surface(sprite, 0); } } SDL_Surface *surf = (*surface)->surf; SDL_Rect dest_rect = { x, y, 0, 0 }; SDL_SetClipRect(dest->surf, &dest->clip); /* Blit sprite */ int r = SDL_BlitSurface(surf, NULL, dest->surf, &dest_rect); if (r < 0) { LOGE("sdl-video", "BlitSurface error: %s.", SDL_GetError()); } #if 0 /* Bounding box */ sdl_draw_rect(x, y, surf->w, surf->h, 41, dest); #endif } static SDL_Surface * create_sprite_surface(const sprite_t *sprite) { void *data = (uint8_t *)sprite + sizeof(sprite_t); int width = le16toh(sprite->w); int height = le16toh(sprite->h); return create_surface_from_data(data, width, height, 0); }
static SDL_Surface * create_transp_surface(const sprite_t *sprite, int offset) { void *data = (uint8_t *)sprite + sizeof(sprite_t); int width = le16toh(sprite->w); int height = le16toh(sprite->h); /* Unpack */ size_t unpack_size = width * height; uint8_t *unpack = new uint8_t[unpack_size]; if (unpack == NULL) abort(); data_unpack_transparent_sprite(unpack, data, unpack_size, offset); SDL_Surface *surf = create_surface_from_data(unpack, width, height, 1); free(unpack); return surf; }
void sdl_draw_overlay_sprite(const sprite_t *sprite, int x, int y, int y_off, frame_t *dest) { int r; x += le16toh(sprite->x) + dest->clip.x; y += le16toh(sprite->y) + dest->clip.y; const surface_id_t id = { .sprite = sprite, .mask = NULL, .offset = 0 }; surface_t **surface = surface_ht_store(&overlay_sprite_cache, &id); if (*surface == NULL) { *surface = new surface_t; if (*surface == NULL) abort(); (*surface)->surf = create_overlay_surface(sprite); } SDL_Surface *surf = (*surface)->surf; SDL_Rect src_rect = { 0, y_off, surf->w, surf->h - y_off }; SDL_Rect dest_rect = { x, y + y_off, 0, 0 }; SDL_SetClipRect(dest->surf, &dest->clip); /* Blit sprite */ r = SDL_BlitSurface(surf, &src_rect, dest->surf, &dest_rect); if (r < 0) { LOGE("sdl-video", "BlitSurface error: %s.", SDL_GetError()); } #if 0 /* Bounding box */ sdl_draw_rect(x, y + y_off, surf->w, surf->h - y_off, 1, dest); #endif } static SDL_Surface * create_masked_surface(const sprite_t *sprite, const sprite_t *mask) { size_t m_width = le16toh(mask->w); size_t m_height = le16toh(mask->h); size_t s_width = le16toh(sprite->w); size_t s_height = le16toh(sprite->h); void *s_data = (uint8_t *)sprite + sizeof(sprite_t); uint8_t *s_copy = new uint8_t[m_width * m_height]; if (s_copy == NULL) abort(); size_t to_copy = m_width * m_height; uint8_t *copy_dest = s_copy; while (to_copy) { size_t s = min(to_copy, s_width * s_height); memcpy(copy_dest, s_data, s * sizeof(uint8_t)); to_copy -= s; copy_dest += s; } /* Mask */ void *m_data = (uint8_t *)mask + sizeof(sprite_t); /* Unpack mask */ size_t unpack_size = m_width * m_height; uint8_t *m_unpack = new uint8_t[unpack_size]; if (m_unpack == NULL) abort(); data_unpack_mask_sprite(m_unpack, m_data, unpack_size); /* Fill alpha value from mask data */ for (int y = 0; y < m_height; y++) { for (int x = 0; x < m_width; x++) { if (!m_unpack[y*m_width+x]) { *(s_copy + y * m_width + x) = 0; } } } free(m_unpack); SDL_Surface *surf = create_surface_from_data(s_copy, (int)m_width, (int)m_height, 1); free(s_copy); return surf; }