Exemplo n.º 1
0
void
skia_draw_overlay_sprite(const sprite_t *sprite, int x, int y, int y_off, frame_t *dest)
{
//    if(1) return;
//  LOGI("skia_draw_overlay_sprite x=%d y=%d, y_off=%d", x, y, y_off);

    size_t width = le16toh(sprite->w);
    size_t height = le16toh(sprite->h);

    x += le16toh(sprite->x);
    y += le16toh(sprite->y);

    std::shared_ptr<SkBitmap> bitmap;
    bitmap_cache_t::iterator it = g_bitmap_cache.find(sprite);

    if( it != g_bitmap_cache.end() ) {
        bitmap = it->second;
    }
    else {
        LOGI("create overlay sprite = %d", sprite);

        // Create bitmap and add to cache
        bitmap.reset(new SkBitmap());
        g_bitmap_cache[sprite] = bitmap;

        bitmap->setConfig(SkBitmap::kA8_Config, width, height);
        bitmap->allocPixels();

        {
            // Fill bitmap with sprite data
            SkAutoLockPixels alp(*bitmap);
            void *data = (uint8_t *)sprite + sizeof(sprite_t);
            gfx_unpack_overlay_sprite(bitmap->getPixels(), data, width * height);
        }

        bitmap->notifyPixelsChanged();
    }

    SkRect src_rect;
    src_rect.fLeft = 0;
    src_rect.fTop = y_off;
    src_rect.fRight = SkIntToScalar(width);
    src_rect.fBottom = - y_off + SkIntToScalar(height);

    SkRect dst_rect;
    dst_rect.fLeft = x;
    dst_rect.fTop = y + y_off;
    dst_rect.fRight = x + SkIntToScalar(width);
    dst_rect.fBottom = y - y_off + SkIntToScalar(height);

    ((SkCanvas*)dest->skCanvas)->drawBitmapRectToRect(*bitmap, &src_rect, dst_rect, NULL);

#if 0
    /* Bounding box */
    skia_draw_rect(x, y + y_off, width, height - y_off, 1, dest);
#endif
}
Exemplo n.º 2
0
static SDL_Surface *
create_overlay_surface(const sprite_t *sprite)
{
	int r;

	void *data = (uint8_t *)sprite + sizeof(sprite_t);

	size_t width = le16toh(sprite->w);
	size_t height = le16toh(sprite->h);

	/* Unpack */
	size_t unpack_size = width * height;
	uint8_t *unpack = calloc(unpack_size, sizeof(uint8_t));
	if (unpack == NULL) abort();

	gfx_unpack_overlay_sprite(unpack, data, unpack_size);

	/* Create sprite surface */
	SDL_Surface *surf = sdl_create_surface((int)width, (int)height);
	r = SDL_LockSurface(surf);
	if (r < 0) {
		LOGE("sdl-video", "Unable to lock sprite.");
		exit(EXIT_FAILURE);
	}

	/* Fill alpha value from overlay data */
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			uint32_t *p = (uint32_t *)((uint8_t *)surf->pixels + y * surf->pitch);
			p[x] = SDL_MapRGBA(surf->format, 0, 0, 0, unpack[y*width+x]);
		}
	}

	SDL_UnlockSurface(surf);
	free(unpack);

	return surf;
}