Example #1
0
static int
Load(void *p, AG_DataSource *ds, const AG_Version *ver)
{
	VG_Text *vt = p;

	if ((vt->p1 = VG_ReadRef(ds, vt, "Point")) == NULL ||
	    (vt->p2 = VG_ReadRef(ds, vt, "Point")) == NULL)
		return (-1);

	vt->align = (enum vg_alignment)AG_ReadUint8(ds);
	AG_CopyString(vt->fontFace, ds, sizeof(vt->fontFace));
	vt->fontSize = (int)AG_ReadUint8(ds);
	vt->fontFlags = (Uint)AG_ReadUint16(ds);
	AG_CopyString(vt->text, ds, sizeof(vt->text));
	return (0);
}
Example #2
0
int
M_PolyhedronRead(AG_DataSource *ds, M_Polyhedron *P)
{
	Uint i, j;

	P->nv = (Uint)AG_ReadUint32(ds);
	P->ne = (Uint)AG_ReadUint32(ds);
	P->nf = (Uint)AG_ReadUint32(ds);
	
	if ((P->v = TryMalloc(P->nv*sizeof(M_Vector3))) == NULL ||
	    (P->e = TryMalloc(P->ne*sizeof(M_Halfedge))) == NULL ||
	    (P->f = TryMalloc(P->nf*sizeof(M_Facet))) == NULL)
		goto fail;

	/* Read vertices */
	for (i = 0; i < P->nv; i++)
		P->v[i] = M_ReadVector3(ds);

	/* Read edges */
	for (i = 0; i < P->ne; i+=2) {
		M_Halfedge *eHead = &P->e[i];
		M_Halfedge *eTail = &P->e[i+1];

		eHead->v = (Uint)AG_ReadUint32(ds);
		eTail->v = (Uint)AG_ReadUint32(ds);
		if (eHead->v >= P->nv || eTail->v >= P->nv) {
			AG_SetError("Edge%d: Bad vertex %d", i, eHead->v);
			goto fail;
		}
		eHead->f = (Uint)AG_ReadUint32(ds);
		eTail->f = (Uint)AG_ReadUint32(ds);
		if (eHead->f >= P->nf || eTail->f >= P->nf) {
			AG_SetError("Edge%d: Bad facet %d", i, eHead->f);
			goto fail;
		}
		eHead->oe = i+1;
		eTail->oe = i;
	}

	/* Read facets */
	for (i = 0; i < P->nf; i++) {
		M_Facet *f = &P->f[i];
		
		f->n = (Uint)AG_ReadUint8(ds);
		if ((f->e = TryMalloc(f->n*sizeof(Uint))) == NULL) {
			goto fail;
		}
		for (j = 0; j < f->n; j++) {
			f->e[j] = (Uint)AG_ReadUint32(ds);
			if (f->e[j] >= P->ne) {
				AG_SetError("Facet%d[%d]: Bad incident edge %d",
				    i, j, f->e[j]);
				goto fail;
			}
		}
	}
	return (0);
fail:
	return (-1);
}
Example #3
0
File: fill.c Project: adsr/agar
int
RG_FillLoad(void *p, AG_DataSource *buf)
{
	struct rg_fill_feature *f = p;

	if (AG_ReadVersion(buf, "RG_Feature:RG_Fill", &rgFillVer, NULL) == -1)
		return (-1);

	f->type = (enum rg_fill_type)AG_ReadUint8(buf);
	switch (f->type) {
	case FILL_SOLID:
		f->f_solid.c = AG_ReadColor(buf);
		break;
	case FILL_HGRADIENT:
	case FILL_VGRADIENT:
	case FILL_CGRADIENT:
		f->f_gradient.c1 = AG_ReadColor(buf);
		f->f_gradient.c2 = AG_ReadColor(buf);
		break;
	case FILL_PATTERN:
		f->f_pattern.texid = (int)AG_ReadUint32(buf);
		f->f_pattern.tex_xoffs = (int)AG_ReadUint32(buf);
		f->f_pattern.tex_yoffs = (int)AG_ReadUint32(buf);
		break;
	}
	return (0);
}
Example #4
0
static int
Load(void *p, AG_DataSource *ds, const AG_Version *ver)
{
#ifdef AG_DEBUG
	agDebugLvl = AG_ReadUint8(ds);
#else
	(void)AG_ReadUint8(ds);
#endif
	/* For backward compatibility with <9.5 (pre-1.4.2) saves. */
	if (ver->minor < 2) { AG_ReadUint8(ds); }
	(void)AG_ReadUint8(ds);
	if (ver->minor >= 3) { AG_ReadUint8(ds); }
	if (ver->minor >= 4) { AG_ReadUint32(ds); }
	AG_Seek(ds, 22, AG_SEEK_CUR);
	if (ver->minor >= 1) { AG_ReadUint8(ds); }
	(void)AG_ReadUint8(ds);				/* agRcsMode */
	AG_SkipString(ds);				/* agRcsHostname */
	(void)AG_ReadUint16(ds);			/* agRcsPort */
	AG_SkipString(ds);				/* agRcsUsername */
	AG_SkipString(ds);				/* agRcsPassword */
	return (0);
}
Example #5
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);
}