void sdl_draw_sprite(const sprite_t *sprite, int x, int y, frame_t *dest) { int r; x += le16toh(sprite->x) + dest->clip.x; y += le16toh(sprite->y) + dest->clip.y; SDL_Surface *surf = create_sprite_surface(sprite); /* Not cached */ SDL_Rect dest_rect = { x, y, 0, 0 }; SDL_SetClipRect(dest->surf, &dest->clip); /* Blit sprite */ r = SDL_BlitSurface(surf, NULL, dest->surf, &dest_rect); if (r < 0) { LOGE("sdl-video", "BlitSurface error: %s.", SDL_GetError()); } /* Clean up */ SDL_FreeSurface(surf); #if 0 /* Bounding box */ sdl_draw_rect(x, y, surf->w, surf->h, 68, dest); #endif }
void sdl_draw_sprite(const sprite_t *sprite, int x, int y, int y_offset, frame_t *dest) { x += dest->clip.x; y += dest->clip.y; SDL_Surface *surf = create_surface_from_sprite(sprite); SDL_Rect src_rect = { 0, y_offset, surf->w, surf->h - y_offset }; SDL_Rect dest_rect = { x, y + y_offset, 0, 0 }; SDL_SetClipRect(dest->surf, &dest->clip); /* Blit sprite */ int r = SDL_BlitSurface(surf, &src_rect, (SDL_Surface*)dest->surf, &dest_rect); if (r < 0) { LOGE("sdl-video", "BlitSurface error: %s.", SDL_GetError()); } /* Clean up */ SDL_FreeSurface(surf); #if 0 /* Bounding box */ sdl_draw_rect(x, y + y_off, surf->w, surf->h - y_off, 72, dest); #endif }
/* rectangle-drawing function for collision-detection debug */ void DrawRec(ObjData *my, GrpData *grp) { #ifdef HAVE_LIBSDL /* use double-standard here to keep the 10 pixel boundary */ sdl_draw_rect(my->X-my->HarfW + 10, my->Y-my->HarfH + 10, my->Width, my->Height); #else /* not HAVE_LIBSDL */ /* XFillRectangle(dpy,WorkPixmap,FillGC,my->X-my->HarfW,my->Y-my->HarfH,my->Width,my->Height); */ XDrawRectangle(dpy,WorkPixmap,FillGC,my->X-my->HarfW,my->Y-my->HarfH,my->Width,my->Height); #endif /* not HAVE_LIBSDL */ return; }
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); }
void sdl_draw_transp_sprite(const sprite_t *sprite, int x, int y, int use_off, int y_off, int color_off, frame_t *dest) { int r; x += dest->clip.x; y += dest->clip.y; if (use_off) { x += le16toh(sprite->x); y += le16toh(sprite->y); } const surface_id_t id = { .sprite = sprite, .mask = NULL, .offset = color_off }; surface_t **surface = surface_ht_store(&transp_sprite_cache, &id); if (*surface == NULL) { *surface = new surface_t; if (*surface == NULL) abort(); (*surface)->surf = create_transp_surface(sprite, color_off); } 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, 72, dest); #endif }
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; }