Example #1
0
File: drv.c Project: varialus/agar
static void
Init(void *obj)
{
	AG_Driver *drv = obj;

	drv->id = 0;
	drv->flags = 0;
	drv->sRef = AG_SurfaceRGBA(1,1, 32, 0,
#if AG_BYTEORDER == AG_BIG_ENDIAN
 	    0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
#else
	    0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
#endif
	);
	if (drv->sRef == NULL) {
		AG_FatalError(NULL);
	}
	drv->videoFmt = NULL;
	drv->kbd = NULL;
	drv->mouse = NULL;
	drv->cursors = NULL;
	drv->nCursors = 0;
	drv->activeCursor = NULL;
	drv->gl = NULL;
	AG_TextInitGlyphCache(drv);
}
Example #2
0
File: iconmgr.c Project: adsr/agar
/* Compile surfaces/textures for the given static icon. */
void
AG_InitStaticIcon(AG_StaticIcon *icon)
{
	const Uint32 *src;
	Uint8 *dst;
	int x, y;

	if (!agGUI)
		return;

	icon->s = AG_SurfaceRGBA(icon->w, icon->h, agSurfaceFmt->BitsPerPixel,
	    0, icon->Rmask, icon->Gmask, icon->Bmask, icon->Amask);
	
	src = &icon->data[0];
	dst = icon->s->pixels;
	for (y = 0; y < icon->h; y++) {
		for (x = 0; x < icon->w; x++) {
			AG_SurfacePutPixel(icon->s, dst, *src);
			dst += icon->s->format->BytesPerPixel;
			src++;
		}
	}
}
Example #3
0
/*
 * Load surface contents from a Windows BMP file.
 *
 * Code for expanding 1bpp and 4bpp to 8bpp depth was shamefully
 * stolen from SDL.
 */
AG_Surface *
AG_ReadSurfaceFromBMP(AG_DataSource *ds)
{
	struct ag_bmp_header bh;
	struct ag_bmp_info_header bi;
	Uint32 Rmask = 0, Gmask = 0, Bmask = 0, Amask = 0;
	AG_Surface *s;
	off_t offs;
	Uint8 *pStart, *pEnd, *pDst;
	int i, bmpPitch, expandBpp, bmpPad, topDown;

	offs = AG_Tell(ds);
	bh.magic[0] = '?';
	bh.magic[1] = '?';
	if (AG_Read(ds, bh.magic, 2) != 0 ||
	    bh.magic[0] != 'B' || bh.magic[1] != 'M') {
		AG_SetError("Not a Windows BMP file (`%c%c')",
		bh.magic[0], bh.magic[1]);
		return (NULL);
	}

	/* Uses little-endian byte order */
	AG_SetByteOrder(ds, AG_BYTEORDER_LE);

	bh.size = AG_ReadUint32(ds);
	bh.resv[0] = AG_ReadUint16(ds);
	bh.resv[1] = AG_ReadUint16(ds);
	bh.offBits = AG_ReadUint32(ds);

	bi.size = AG_ReadUint32(ds);
	if (bi.size == 12) {
		bi.w = (Uint32)AG_ReadUint16(ds);
		bi.h = (Uint32)AG_ReadUint16(ds);
		bi.planes = AG_ReadUint16(ds);
		bi.bitCount = AG_ReadUint16(ds);
		bi.encoding = AG_BMP_RGB;
		bi.sizeImage = 0;
		bi.XPelsPerMeter = 0;
		bi.YPelsPerMeter = 0;
		bi.clrUsed = 0;
		bi.clrImportant = 0;
	} else {
		bi.w = AG_ReadSint32(ds);
		bi.h = AG_ReadSint32(ds);
		bi.planes = AG_ReadUint16(ds);
		bi.bitCount = AG_ReadUint16(ds);
		bi.encoding = AG_ReadUint32(ds);
		bi.sizeImage = AG_ReadUint32(ds);
		bi.XPelsPerMeter = AG_ReadUint32(ds);
		bi.YPelsPerMeter = AG_ReadUint32(ds);
		bi.clrUsed = AG_ReadUint32(ds);
		bi.clrImportant = AG_ReadUint32(ds);
	}
	if (bi.h < 0) {
		topDown = 1;
		bi.h = -bi.h;
	} else {
		topDown = 0;
	}
	
	/* Will convert 1bpp/4bpp to 8bpp */
	if (bi.bitCount == 1 || bi.bitCount == 4) {
		expandBpp = bi.bitCount;
		bi.bitCount = 8;
	} else {
		expandBpp = 0;
	}

	switch (bi.encoding) {
	case AG_BMP_RGB:
		if (bh.offBits == (14 + bi.size)) {
			switch (bi.bitCount) {
			case 15:
			case 16:
				Rmask = 0x7C00;
				Gmask = 0x03E0;
				Bmask = 0x001F;
				break;
			case 24:
#if AG_BYTEORDER == AG_BIG_ENDIAN
			        Rmask = 0x000000FF;
			        Gmask = 0x0000FF00;
			        Bmask = 0x00FF0000;
				break;
#endif
			case 32:
				Rmask = 0x00FF0000;
				Gmask = 0x0000FF00;
				Bmask = 0x000000FF;
				break;
			}
			break;
		}
		/* FALLTHROUGH */
	case AG_BMP_BITFIELDS:
		switch (bi.bitCount) {
		case 15:
		case 16:
			Rmask = AG_ReadUint32(ds);
			Gmask = AG_ReadUint32(ds);
			Bmask = AG_ReadUint32(ds);
			break;
		case 32:
			Rmask = AG_ReadUint32(ds);
			Gmask = AG_ReadUint32(ds);
			Bmask = AG_ReadUint32(ds);
			Amask = AG_ReadUint32(ds);
			break;
		}
		break;
	default:
		AG_SetError("BMP compression unimplemented");
		return (NULL);
	}
	if ((s = AG_SurfaceRGBA(bi.w, bi.h, bi.bitCount,
	    (Amask != 0) ? AG_SRCALPHA : 0,
	    Rmask, Gmask, Bmask, Amask)) == NULL) {
		return (NULL);
	}

	if (s->format->palette != NULL) {
		if (bi.clrUsed == 0) {
			bi.clrUsed = (1 << bi.bitCount);
		}
		if (bi.size == 12) {
			for (i = 0; i < bi.clrUsed; i++) {
				s->format->palette->colors[i].b = AG_ReadUint8(ds);
				s->format->palette->colors[i].g = AG_ReadUint8(ds);
				s->format->palette->colors[i].r = AG_ReadUint8(ds);
			}	
		} else {
			for (i = 0; i < bi.clrUsed; i++) {
				s->format->palette->colors[i].b = AG_ReadUint8(ds);
				s->format->palette->colors[i].g = AG_ReadUint8(ds);
				s->format->palette->colors[i].r = AG_ReadUint8(ds);
				(void)AG_ReadUint8(ds); /* unused */
			}	
		}
		s->format->palette->nColors = bi.clrUsed;
	}

	if (AG_Seek(ds, offs+bh.offBits, AG_SEEK_SET) == -1)
		goto fail;
	
	pStart = (Uint8 *)s->pixels;
	pEnd = (Uint8 *)s->pixels + (s->h*s->pitch);
	switch (expandBpp) {
	case 1:
		bmpPitch = (bi.w + 7) >> 3;
		bmpPad = ((bmpPitch % 4) ? (4 - (bmpPitch % 4)) : 0);
		break;
	case 4:
		bmpPitch = (bi.w + 1) >> 1;
		bmpPad = ((bmpPitch % 4) ? (4 - (bmpPitch % 4)) : 0);
		break;
	default:
		bmpPad = ((s->pitch % 4) ? (4 - (s->pitch % 4)) : 0);
		break;
	}
	
	pDst = topDown ? pStart : (pEnd - s->pitch);
	while (pDst >= pStart && pDst < pEnd) {
		switch (expandBpp) {
		case 1:
		case 4:
			{
				Uint8 px = 0;
                		int shift = (8 - expandBpp);

				for (i = 0; i < s->w; i++) {
					if (i % (8/expandBpp) == 0) {
						px = AG_ReadUint8(ds);
					}
					*(pDst + i) = (px >> shift);
					px <<= expandBpp;
				}
			}
			break;
		default:
			if (AG_Read(ds, pDst, s->pitch) != 0) {
				goto fail;
			}
#if AG_BYTEORDER == AG_BIG_ENDIAN
			switch (bi.bitCount) {
			case 15:
			case 16:
				{
				        Uint16 *px = (Uint16 *)pDst;
					for (i = 0; i < s->w; i++) {
					        px[i] = AG_Swap16(px[i]);
					}
					break;
				}
			case 32:
				{
					Uint32 *px = (Uint32 *)pDst;
					for (i = 0; i < s->w; i++) {
					        px[i] = AG_Swap32(px[i]);
					}
					break;
				}
			}
#endif /* AG_BYTEORDER == AG_BIG_ENDIAN */
			break;
		}
		if (bmpPad != 0) {
			if (AG_Seek(ds, bmpPad, AG_SEEK_CUR) == -1)
				goto fail;
		}
		if (topDown) {
			pDst += s->pitch;
		} else {
			pDst -= s->pitch;
		}
	}
	return (s);
fail:
	AG_SurfaceFree(s);
	return (NULL);
}