Exemple #1
0
SGAtlas* SG_CALL sgAtlasCreateTexture(SGTexture* texture, SGbool owner)
{
    SGAtlas* atlas = sgAtlasCreate(sgTextureGetWidth(texture), sgTextureGetHeight(texture), sgTextureGetBPP(texture));
    if(!atlas) return NULL;

    _sgAtlasAddTexture(atlas, texture, owner);

    return atlas;
}
Exemple #2
0
SGMask* SG_EXPORT sgMaskCreateTexture2i(SGTexture* texture, SGint xoffset, SGint yoffset)
{
	SGMask* mask = malloc(sizeof(SGMask));
	if(mask == NULL)
		return NULL;

	SGuint i, j;

	mask->width = sgTextureGetWidth(texture);
	mask->height = sgTextureGetHeight(texture);

	mask->xoffset = xoffset;
	mask->yoffset = yoffset;

	mask->precise = SG_TRUE;

	mask->field = malloc(mask->width * sizeof(SGbool*));
	for(i = 0; i < mask->width; i++)
		mask->field[i] = calloc(mask->height, sizeof(SGbool));

	SGuint awidth = 0;
	SGuint aheight = 0;
	SGuint bpp = 0;
	char* data = NULL;
	if(psgmGraphicsTextureGetData != NULL)
		psgmGraphicsTextureGetData(texture->handle, &awidth, &aheight, &bpp, (void**)&data);

	/*SGubyte bypp = 0;*/
	SGubyte r, g, b, a;
	SGubyte rbits = 0;
	SGubyte gbits = 0;
	SGubyte bbits = 0;
	SGubyte abits = 0;

	switch(bpp)
	{
		case 32:
			rbits = 8;
			gbits = 8;
			bbits = 8;
			abits = 8;
			/*bypp = 4;*/
			break;
		case 24:
			rbits = 8;
			gbits = 8;
			bbits = 8;
			/*bypp = 3;*/
			break;
		case 16:
			rbits = 5;
			gbits = 6;
			bbits = 5;
			/*bypp = 2;*/
			break;
		case 15:
			rbits = 5;
			gbits = 5;
			bbits = 5;
			abits = 1; // ?
			/*bypp = 2;*/
			break;
		case 8:
			abits = 8;
			/*bypp = 1;*/
			break;

		default:
			fprintf(stderr, "Unsupported BPP '%d'\n", bpp);
	}

    float col[4];
    sgDrawGetColor4fv(col);

    r = (SGubyte)(col[0] * ((1 << rbits) - 1));
    g = (SGubyte)(col[1] * ((1 << gbits) - 1));
    b = (SGubyte)(col[2] * ((1 << bbits) - 1));
    a = (SGubyte)(col[3] * ((1 << abits) - 1));

	SGuint ui = a
			|	(b << abits)
			|	(g << (abits + bbits))
			|	(r << (abits + bbits + gbits));

	for(i = 0; i < mask->width; i++)
	{
		for(j = 0; j < mask->height; j++)
		{
			switch(bpp)
			{
				case 32:
					mask->field[i][j] = data[(j * awidth + i) * 4 + 3] ? SG_TRUE : SG_FALSE;
					break;
				case 24:
					mask->field[i][j] = (data[(j * awidth + i) * 3 + 0] == r)
									&&	(data[(j * awidth + i) * 3 + 1] == g)
									&&	(data[(j * awidth + i) * 3 + 2] == b);
					break;
				case 16:
				case 15:
					mask->field[i][j] = *(SGushort*)&data[(j * awidth + i) * 2] == ui;
					break;
				case 8:
					mask->field[i][j] = data[(j * awidth + i)] == a;
					break;
			}
		}
	}

	psgmGraphicsTextureFreeData(data);

	return mask;
}