Beispiel #1
0
static char *
GetAddrNumerical(AG_NetAddr *na)
{
    const char *s;

    switch (na->family) {
    case AG_NET_INET4:
        Free(na->sNum);
        if ((na->sNum = TryMalloc(INET_ADDRSTRLEN)) == NULL) {
            return (NULL);
        }
        s = inet_ntop(AF_INET, &na->na_inet4.addr, na->sNum,
                      INET_ADDRSTRLEN);
        break;
#ifdef AF_INET6
    case AG_NET_INET6:
        Free(na->sNum);
        if ((na->sNum = TryMalloc(INET6_ADDRSTRLEN)) == NULL) {
            return (NULL);
        }
        s = inet_ntop(AF_INET6, &na->na_inet4.addr, na->sNum,
                      INET6_ADDRSTRLEN);
        break;
#endif
    default:
        AG_SetError("Bad address format");
        return (NULL);
    }
    if (s == NULL) {
        AG_SetError("inet_ntop: %s", strerror(errno));
        return (NULL);
    }
    return (na->sNum);
}
Beispiel #2
0
static int
InitSocket(AG_NetSocket *ns)
{
	int sockDomain, sockType;

	if (ns->family == 0) {
		/* Defer actual socket creation until connect() or bind() */
		return (0);
	}
	switch (ns->family) {
	case AG_NET_INET4:	sockDomain = PF_INET;	break;
	case AG_NET_INET6:	sockDomain = PF_INET6;	break;
	default:
		AG_SetError("Bad address family: %d", ns->family);
		return (-1);
	}
	switch (ns->type) {
	case AG_NET_STREAM:	sockType = SOCK_STREAM;	break;
	case AG_NET_DGRAM:	sockType = SOCK_DGRAM;	break;
	default:
		AG_SetError("Bad socket type: %d", ns->type);
		return (-1);
	}
	if ((ns->fd = socket(sockDomain, sockType, ns->proto)) == INVALID_SOCKET) {
		AG_SetError("socket: %s", strerror(errno));
		return (-1);
	}
	return (0);
}
Beispiel #3
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);
}
Beispiel #4
0
static int
GetOption(AG_NetSocket *ns, enum ag_net_socket_option so, void *p)
{
	socklen_t optLen;
	int rv = 0;
	
	AG_MutexLock(&agNetWin32Lock);

	switch (so) {
	case AG_NET_BACKLOG:
		*(int *)p = ns->listenBacklog;
		break;
	case AG_NET_DEBUG:
	case AG_NET_REUSEADDR:
	case AG_NET_KEEPALIVE:
	case AG_NET_DONTROUTE:
	case AG_NET_BROADCAST:
	case AG_NET_SNDBUF:
	case AG_NET_RCVBUF:
		optLen = sizeof(int);
		rv = getsockopt(ns->fd, SOL_SOCKET, agSockOptionMap[so],
		    p, &optLen);
		break;
	case AG_NET_OOBINLINE:
		optLen = sizeof(int);
		rv = getsockopt(ns->fd, SOL_SOCKET, SO_OOBINLINE, p, &optLen);
		break;
	case AG_NET_LINGER:
		{
			struct linger ling;

			optLen = sizeof(struct linger);
			rv = getsockopt(ns->fd, SOL_SOCKET, SO_LINGER,
			    (char *)&ling, &optLen);
			if (rv == 0) {
				*(int *)p = ling.l_onoff ? ling.l_linger : 0;
			}
		}
		break;
	default:
		AG_SetError("Bad socket option");
		goto fail;
	}
	if (rv == SOCKET_ERROR) {
		AG_SetError("getsockopt(%u): %s", (Uint)so, strerror(errno));
		goto fail;
	}
	
	AG_MutexUnlock(&agNetWin32Lock);
	return (0);
fail:
	AG_MutexUnlock(&agNetWin32Lock);
	return (-1);
}
Beispiel #5
0
static int
SetOption(AG_NetSocket *ns, enum ag_net_socket_option so, const void *p)
{
	int rv = 0;
	
	AG_MutexLock(&agNetWin32Lock);

	switch (so) {
	case AG_NET_BACKLOG:
		ns->listenBacklog = *(const int *)p;
		break;
	case AG_NET_DEBUG:
	case AG_NET_REUSEADDR:
	case AG_NET_KEEPALIVE:
	case AG_NET_DONTROUTE:
	case AG_NET_BROADCAST:
	case AG_NET_SNDBUF:
	case AG_NET_RCVBUF:
		rv = setsockopt(ns->fd, SOL_SOCKET, agSockOptionMap[so],
		    p, sizeof(int));
		break;
	case AG_NET_OOBINLINE:
		rv = setsockopt(ns->fd, SOL_SOCKET, SO_OOBINLINE, p, sizeof(int));
		break;
	case AG_NET_LINGER:
		{
			int val = *(const int *)p;
			struct linger ling;

			ling.l_onoff = (val > 0);
			ling.l_linger = val;
			rv = setsockopt(ns->fd, SOL_SOCKET, SO_LINGER,
			    (const char *)&ling, sizeof(struct linger));
		}
		break;
	default:
		AG_SetError("Bad socket option");
		goto fail;
	}
	if (rv != SOCKET_ERROR) {
		AG_SetError("setsockopt(%u): %s", (Uint)so, strerror(errno));
		goto fail;
	}

	AG_MutexUnlock(&agNetWin32Lock);
	return (0);
fail:
	AG_MutexUnlock(&agNetWin32Lock);
	return (-1);
}
Beispiel #6
0
static int
GetIfConfig(AG_NetAddrList *nal)
{
#ifdef HAVE_SIOCGIFCONF
    enum ag_net_addr_family af;
    AG_NetAddr *na;
    char buf[4096];
    struct ifconf conf;
    struct ifreq *ifr;
    int sock;

    AG_NetAddrListClear(nal);

    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        AG_SetError("socket: %s", strerror(errno));
        return (-1);
    }
    conf.ifc_len = sizeof(buf);
    conf.ifc_buf = (caddr_t)buf;
    if (ioctl(sock, SIOCGIFCONF, &conf) < 0) {
        AG_SetError("SIOCGIFCONF: %s", strerror(errno));
        goto fail;
    }

#if !defined(_SIZEOF_ADDR_IFREQ)
#define _SIZEOF_ADDR_IFREQ sizeof
#endif
    for (ifr = (struct ifreq *)buf;
            (char *)ifr < &buf[conf.ifc_len];
            ifr = (struct ifreq *)((char *)ifr + _SIZEOF_ADDR_IFREQ(*ifr))) {
        if ((af = GetAddrFamily(ifr->ifr_addr.sa_family)) == 0) {
            continue;
        }
        if ((na = SockAddrToNetAddr(af, &ifr->ifr_addr)) == NULL) {
            goto fail;
        }
        na->port = 0;
        TAILQ_INSERT_TAIL(nal, na, addrs);
    }
    close(sock);
    return (0);
fail:
    close(sock);
    return (-1);
#else
    AG_SetError("GetIfConfig: SIOCGIFCONF unavailable");
    return (-1);
#endif /* !HAVE_SIOCGIFCONF */
}
Beispiel #7
0
/* Load the given object's data from the database. */
int
AG_DbObjectLoad(void *obj, AG_Db *db, const char *key)
{
	AG_DbObject *dbo = obj;
	AG_DataSource *ds;
	AG_DbEntry dbe;

#ifdef AG_DEBUG
	if (!AG_OfClass(dbo, "AG_DbObject:*")) {
		AG_SetError("Object is not an AG_DbObject");
		return (-1);
	}
#endif
	if (AG_DbLookup(db, &dbe, key) == -1) {
		return (-1);
	}
	if ((ds = AG_OpenCore(dbe.data, dbe.dataSize)) == NULL) {
		return (-1);
	}
	if (AG_ObjectUnserialize(dbo, ds) == -1) {
		AG_CloseCore(ds);
		return (-1);
	}
	AG_CloseCore(ds);
	return (0);
}
Beispiel #8
0
static AG_NetSocket *
Accept(AG_NetSocket *ns)
{
    struct sockaddr_storage sa;
    AG_NetSocket *nsNew;
    socklen_t saLen = sizeof(struct sockaddr_storage);
    int sock;

    memset(&sa, 0, saLen);
    if ((sock = accept(ns->fd, (struct sockaddr *)&sa, &saLen)) == -1) {
        AG_SetError("accept: %s", strerror(errno));
        return (NULL);
    }
    if ((nsNew = AG_NetSocketNew(0, ns->type, ns->proto)) == NULL) {
        close(sock);
        return (NULL);
    }
    if ((nsNew->addrRemote = SockAddrToNetAddr(ns->family, &sa)) == NULL) {
        close(sock);
        AG_NetSocketFree(nsNew);
        return (NULL);
    }
    nsNew->family = ns->family;
    nsNew->fd = sock;
    nsNew->flags |= AG_NET_SOCKET_CONNECTED;
    return (nsNew);
}
Beispiel #9
0
AG_Surface *
AG_SDL_ShadowSurface(SDL_Surface *ss)
{
	AG_PixelFormat *pf;
	AG_Surface *ds;

	if ((pf = AG_SDL_GetPixelFormat(ss)) == NULL) {
		return (NULL);
	}
	if (pf->palette != NULL) {
		AG_SetError("Indexed formats not supported");
		AG_PixelFormatFree(pf);
		return (NULL);
	}

	if ((ds = AG_SurfaceNew(AG_SURFACE_PACKED, 0, 0, pf, 0))
	    == NULL) {
		goto out;
	}
	if (ss->flags & SDL_SRCCOLORKEY) { ds->flags |= AG_SRCCOLORKEY; }
	if (ss->flags & SDL_SRCALPHA) { ds->flags |= AG_SRCALPHA; }

	ds->pixels = ss->pixels;
	ds->w = ss->w;
	ds->h = ss->h;
	ds->pitch = ds->w * pf->BytesPerPixel;
	ds->clipRect = AG_RECT(0,0,ds->w,ds->h);

out:
	AG_PixelFormatFree(pf);
	return (ds);
}
Beispiel #10
0
static int
Bind(AG_NetSocket *ns, const AG_NetAddr *na)
{
	struct sockaddr_storage sa;
	socklen_t saLen = 0;

	AG_MutexLock(&agNetWin32Lock);
	if (ns->family == 0) {			/* Inherit from address */
		ns->family = na->family;
		if (InitSocket(ns) == -1)
			goto fail;
	}
	NetAddrToSockAddr(na, &sa, &saLen);
	if (bind(ns->fd, (struct sockaddr *)&sa, saLen) == SOCKET_ERROR) {
		AG_SetError("bind: %s", strerror(errno));
		goto fail;
	}
	if (ns->type == AG_NET_STREAM ||
	    ns->type == AG_NET_SEQPACKET) {
		if (listen(ns->fd, ns->listenBacklog) == -1)
			goto fail;
	}
	AG_MutexUnlock(&agNetWin32Lock);
	return (0);
fail:
	AG_MutexUnlock(&agNetWin32Lock);
	return (-1);
}
Beispiel #11
0
/* Import the contents of a file in a den archive. */
int
AG_DenImportFile(AG_Den *den, int ind, const char *name, const char *lang,
                 const char *infile)
{
    char buf[8192];
    AG_DenMember *memb;
    FILE *f;
    size_t size, rrv;
    off_t offs;

    offs = AG_Tell(den->buf);
    size = 0;

    if ((f = fopen(infile, "rb")) == NULL) {
        AG_SetError("Unable to open %s", infile);
        return (-1);
    }
    for (;;) {
        rrv = fread(buf, 1, sizeof(buf), f);
        size += rrv;

        if (AG_Write(den->buf, buf, rrv, 1) != 0) {
            return (-1);
        }
    }
    fclose(f);

    memb = &den->members[ind];
    Strlcpy(memb->name, name, sizeof(memb->name));
    Strlcpy(memb->lang, lang, sizeof(memb->lang));
    memb->offs = offs;
    memb->size = size;
    return (0);
}
Beispiel #12
0
static AG_NetSocket *
Accept(AG_NetSocket *ns)
{
	struct sockaddr_storage sa;
	AG_NetSocket *nsNew;
	socklen_t saLen = sizeof(struct sockaddr_storage);
	SOCKET sock;

	AG_MutexLock(&agNetWin32Lock);

	memset(&sa, 0, saLen);
	if ((sock = accept(ns->fd, (struct sockaddr *)&sa, &saLen)) == INVALID_SOCKET) {
		AG_SetError("accept: %s", strerror(errno));
		goto fail;
	}
	if ((nsNew = AG_NetSocketNew(0, ns->type, ns->proto)) == NULL) {
		goto fail;
	}
	if ((nsNew->addrRemote = SockAddrToNetAddr(ns->family, &sa)) == NULL) {
		AG_NetSocketFree(nsNew);
		goto fail;
	}
	nsNew->family = ns->family;
	nsNew->fd = (int)sock;
	nsNew->flags |= AG_NET_SOCKET_CONNECTED;

	AG_MutexUnlock(&agNetWin32Lock);
	return (nsNew);
fail:
	if (sock != -1) { closesocket(sock); }
	AG_MutexUnlock(&agNetWin32Lock);
	return (NULL);
}
Beispiel #13
0
/*
 * Test whether the polygon is convex. Polygon must be non self-intersecting.
 * Return 1 if the polygon is convex or 0 if the polygon is concave.
 */
int
M_PolygonIsConvex(const M_Polygon *P)
{
	int i, flag = 0;

	if (P->n < 3) {
		AG_SetError("<3 vertices");
		return (-1);
	}
	for (i = 0; i < P->n; i++) {
		M_Vector2 pi = P->v[(i)   % P->n];
		M_Vector2 pj = P->v[(i+1) % P->n];
		M_Vector2 pk = P->v[(i+2) % P->n];
		M_Vector2 pji = M_VecSub2p(&pj, &pi);
		M_Vector2 pkj = M_VecSub2p(&pk, &pj);
		M_Real dot;

		dot = M_VecPerpDot2p(&pji, &pkj);
		if (dot < 0.0) {
			flag |= 1;
		} else if (dot >= 0.0) {
			flag |= 2;
		}
		if (flag == 3) {
			return (0);
		}
	}
	if (flag != 0) {
		return (1);
	}
	return (0);
}
Beispiel #14
0
/* Return the corresponding Agar PixelFormat structure for a SDL_Surface. */
AG_PixelFormat *
AG_SDL_GetPixelFormat(SDL_Surface *su)
{
	switch (su->format->BytesPerPixel) {
	case 1:
		return AG_PixelFormatIndexed(su->format->BitsPerPixel);
	case 2:
	case 3:
	case 4:
		if (su->format->Amask != 0) {
			return AG_PixelFormatRGBA(su->format->BitsPerPixel,
			                          su->format->Rmask,
			                          su->format->Gmask,
			                          su->format->Bmask,
						  su->format->Amask);
		} else {
			return AG_PixelFormatRGB(su->format->BitsPerPixel,
			                         su->format->Rmask,
			                         su->format->Gmask,
			                         su->format->Bmask);
		}
	default:
		AG_SetError("Unsupported pixel depth (%d bpp)",
		    (int)su->format->BitsPerPixel);
		return (NULL);
	}
}
Beispiel #15
0
Datei: anim.c Projekt: adsr/agar
/* Return a new surface from a given frame#. */
AG_Surface *
AG_AnimFrameToSurface(AG_Anim *a, int f)
{
	AG_Surface *su;
	AG_AnimFrame *af;

	AG_MutexLock(&a->lock);
	if (f < 0 || f >= a->n) {
		AG_SetError("No such frame#");
		AG_MutexUnlock(&a->lock);
		return (NULL);
	}
	af = &a->f[f];
	if (a->format->Amask != 0) {
		su = AG_SurfaceFromPixelsRGBA(af->pixels, a->w, a->h,
		    a->format->BitsPerPixel,
		    a->format->Rmask,
		    a->format->Gmask,
		    a->format->Bmask,
		    a->format->Amask);
	} else {
		su = AG_SurfaceFromPixelsRGB(af->pixels, a->w, a->h,
		    a->format->BitsPerPixel,
		    a->format->Rmask,
		    a->format->Gmask,
		    a->format->Bmask);
	}
	AG_MutexUnlock(&a->lock);
	return (su);
}
Beispiel #16
0
/* Save a surface to a BMP file. */
int
AG_SurfaceExportBMP(const AG_Surface *su, const char *path)
{
	/* TODO */
	AG_SetError("Unimplemented");
	return (-1);
}
Beispiel #17
0
static int
Resolve(AG_NetAddrList *nal, const char *host, const char *port, Uint flags)
{
	struct addrinfo hints, *res, *res0;
	enum ag_net_addr_family af;
	AG_NetAddr *na;
	int rv;
	
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	if (flags & AG_NET_NUMERIC_HOST) { hints.ai_flags |= AI_NUMERICHOST; }

	if ((rv = getaddrinfo(host, port, &hints, &res0)) != 0) {
		AG_SetError("%s:%s: %s", host, port, gai_strerror(rv));
		return (-1);
	}
	for (res = res0; res != NULL; res = res->ai_next) {
		if ((af = GetAddrFamily(res->ai_family)) == 0) {
			continue;
		}
		if ((na = SockAddrToNetAddr(af, res->ai_addr)) != NULL)
			TAILQ_INSERT_TAIL(nal, na, addrs);
	}
	freeaddrinfo(res0);
	return (0);
}
Beispiel #18
0
/* Convert a SDL_Surface to an AG_Surface. */
AG_Surface *
AG_SDL_ImportSurface(SDL_Surface *ss)
{
	AG_PixelFormat *pf;
	AG_Surface *ds;
	Uint8 *pSrc, *pDst;
	int y;
#if 0
	Uint32 px;
	AG_Color C;
	int x;
#endif

	if ((pf = AG_SDL_GetPixelFormat(ss)) == NULL) {
		return (NULL);
	}
	if (pf->palette != NULL) {
		AG_SetError("Indexed formats not supported");
		AG_PixelFormatFree(pf);
		return (NULL);
	}

	if ((ds = AG_SurfaceNew(AG_SURFACE_PACKED, ss->w, ss->h, pf, 0))
	    == NULL) {
		goto out;
	}
	if (ss->flags & SDL_SRCCOLORKEY) { ds->flags |= AG_SRCCOLORKEY; }
	if (ss->flags & SDL_SRCALPHA) { ds->flags |= AG_SRCALPHA; }
	
	if (SDL_MUSTLOCK(ss)) {
		SDL_LockSurface(ss);
	}
	pSrc = (Uint8 *)ss->pixels;
	pDst = (Uint8 *)ds->pixels;
	for (y = 0; y < ss->h; y++) {
		memcpy(pDst, pSrc, ss->pitch);
		pSrc += ss->pitch;
		pDst += ds->pitch;
	}
#if 0
	for (y = 0; y < ss->h; y++) {
		for (x = 0; x < ss->w; x++) {
			AG_PACKEDPIXEL_GET(ss->format->BytesPerPixel, px, pSrc);
			C = AG_SDL_GetColorRGBA(px, ss->format);
			AG_PUT_PIXEL(ds,pDst,
			    AG_MapColorRGBA(ds->format, C));
			pSrc += ss->format->BytesPerPixel;
			pDst += ds->format->BytesPerPixel;
		}
	}
#endif
	if (SDL_MUSTLOCK(ss))
		SDL_UnlockSurface(ss);

out:
	AG_PixelFormatFree(pf);
	return (ds);
}
Beispiel #19
0
static int
GetIfConfig(AG_NetAddrList *nal)
{
	PIP_ADAPTER_INFO pAdapterInfo;
	PIP_ADAPTER_INFO pAdapter;
	PIP_ADDR_STRING pAddress;
	DWORD dwRetVal = 0;
	ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
	
	AG_NetAddrListClear(nal);

	if ((pAdapterInfo = TryMalloc(sizeof(IP_ADAPTER_INFO))) == NULL)
		return (-1);

	AG_MutexLock(&agNetWin32Lock);

	dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
	if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
		PIP_ADAPTER_INFO pAdapterInfoNew;
		if ((pAdapterInfoNew = TryRealloc(pAdapterInfo, ulOutBufLen))
		    == NULL) {
			free(pAdapterInfo);
			goto fail;
		}
		pAdapterInfo = pAdapterInfoNew;
		dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
	}
	if (dwRetVal != NO_ERROR) {
		AG_SetError("GetAdaptersInfo() failed");
		goto fail;
	}
	for (pAdapter = pAdapterInfo;
	     pAdapter != NULL;
	     pAdapter = pAdapter->Next) {
		for (pAddress = &pAdapterInfo->IpAddressList;
		     pAddress != NULL;
		     pAddress = pAddress->Next) {
			AG_NetAddr *na;

			if ((na = AG_NetAddrNew()) == NULL) {
				AG_NetAddrListClear(nal);
				goto fail;
			}
			na->family = AG_NET_INET4;
			na->port = 0;
			na->na_inet4.addr = inet_addr(pAddress->IpAddress.String);
			TAILQ_INSERT_TAIL(nal, na, addrs);
		}
	}

	AG_MutexUnlock(&agNetWin32Lock);
	free(pAdapterInfo);
	return (0);
fail:
	AG_MutexUnlock(&agNetWin32Lock);
	return (-1);
}
Beispiel #20
0
/* Insert a new object to the database, fail if key is taken. */
int
AG_DbObjectInsert(AG_Db *db, void *pDbo)
{
	AG_DbObject *dbo = pDbo;

	if (AG_DbExists(db, AGOBJECT(dbo)->name)) {
		AG_SetError("Existing db object: %s", AGOBJECT(dbo)->name);
		return (-1);
	}
	return AG_DbObjectSave(dbo, db);
}
Beispiel #21
0
Datei: anim.c Projekt: adsr/agar
int
AG_AnimPlay(AG_AnimState *ast)
{
	int rv = 0;

	AG_MutexLock(&ast->lock);
	ast->play = 1;
#ifdef AG_THREADS
	if (AG_ThreadCreate(&ast->th, AnimProc, ast) != 0) {
		AG_SetError("Failed to create playback thread");
		rv = -1;
		ast->play = 0;
	}
#else
	AG_SetError("AG_AnimPlay() requires threads support");
	rv = -1;
#endif
	AG_MutexUnlock(&ast->lock);
	return (rv);
}
Beispiel #22
0
/* Remove a vertex from a polygon. */
int
M_PolygonDelVertex(M_Polygon *P, int v)
{
	if (v < 0 || v >= P->n) {
		AG_SetError("Bad vertex");
		return (-1);
	}
	if (v < P->n-1) {
		memmove(&P->v[v], &P->v[v+1], (P->n - 1)*sizeof(M_Vector2));
	}
	P->n--;
	return (0);
}
Beispiel #23
0
/* Copy the full pathname of a data file to a sized buffer. */
int
AG_ConfigFile(const char *path_key, const char *name, const char *ext,
    char *path, size_t path_len)
{
	char file[AG_PATHNAME_MAX];
	char *dir, *pathp = path;
	int rv;

	AG_GetString(agConfig, path_key, path, path_len);

	for (dir = Strsep(&pathp, AG_PATHSEPMULTI);
	     dir != NULL;
	     dir = Strsep(&pathp, AG_PATHSEPMULTI)) {
		Strlcpy(file, dir, sizeof(file));

		if (name[0] != AG_PATHSEPCHAR) {
			Strlcat(file, AG_PATHSEP, sizeof(file));
		}
		Strlcat(file, name, sizeof(file));
		if (ext != NULL) {
			Strlcat(file, ".", sizeof(file));
			Strlcat(file, ext, sizeof(file));
		}
		if ((rv = AG_FileExists(file)) == 1) {
			if (Strlcpy(path, file, path_len) >= path_len) {
				AG_SetError(_("The search path is too big."));
				return (-1);
			}
			return (0);
		} else if (rv == -1) {
			AG_SetError("%s: %s", file, AG_GetError());
			return (-1);
		}
	}
	AG_GetString(agConfig, path_key, path, path_len);
	AG_SetError(_("Cannot find %s.%s (in <%s>:%s)."), name,
	    (ext != NULL) ? ext : "", path_key, path);
	return (-1);
}
Beispiel #24
0
/* Configure refresh rate. */
int
AG_SDL_SetRefreshRate(void *obj, int fps)
{
	AG_DriverSw *dsw = obj;

	if (fps < 1) {
		AG_SetError("Invalid refresh rate");
		return (-1);
	}
	dsw->rNom = 1000/fps;
	dsw->rCur = 0;
	return (0);
}
Beispiel #25
0
/* Read Agar archive version information. */
int
AG_ReadVersion(AG_DataSource *ds, const char *name, const AG_Version *ver,
    AG_Version *rver)
{
	char nbuf[AG_VERSION_NAME_MAX];
	size_t nlen;
	Uint32 major, minor;

	nlen = strlen(name);

	if (AG_Read(ds, nbuf, sizeof(nbuf), 1) != 0 ||
	    strncmp(nbuf, name, nlen) != 0) {
		AG_SetError("%s: Bad magic (\"%s\" != %s)", name, nbuf,
		    name);
		return (-1);
	}
	if (AG_ReadUint32v(ds, &major) == -1 ||
	    AG_ReadUint32v(ds, &minor) == -1) {
		AG_SetError("Reading version numbers: %s", AG_GetError());
		return (-1);
	}

	if (rver != NULL) {
		rver->major = major;
		rver->minor = minor;
	}
	if (major != ver->major) {
		AG_SetError(_("%s: Major differs: v%u.%u != %u.%u"),
		    name, (Uint)major, (Uint)minor, 
		    (Uint)ver->major, (Uint)ver->minor);
		return (-1);
	}
	if (minor != ver->minor) {
		Verbose(_("Warning: %s: Minor differs: v%u.%u != %u.%u\n"),
		    name, (Uint)major, (Uint)minor, 
		    (Uint)ver->major, (Uint)ver->minor);
	}
	return (0);
}
Beispiel #26
0
int
AG_Kill(AG_ProcessID pid)
{
#if defined(_WIN32) && !defined(_XBOX)
	HANDLE psHandle;
#endif
	if(pid <= 0) {
		AG_SetError("Invalid process id");
		return (-1);
	}

#if defined(_WIN32) && !defined(_XBOX)
	if((psHandle = OpenProcess(SYNCHRONIZE |
	                           PROCESS_TERMINATE |
	                           PROCESS_QUERY_INFORMATION,
	                           FALSE, pid)) == NULL) {
		AG_SetError(_("Unable to obtain process handle (%s)"), AG_Strerror(GetLastError()));
		return -1;
	}

	if(TerminateProcess(psHandle, -1) == 0) {
		AG_SetError(_("Unable to kill process (%s)"), AG_Strerror(GetLastError()));
		return -1;
	}

	CloseHandle(psHandle);

	return (0);
#elif defined(_MK_HAVE_SIGNAL)
	if (kill(pid, SIGKILL) == -1) {
		AG_SetError(_("Failed to kill process (%s)"), AG_Strerror(errno));
		return (-1);
	}
	return (0);
#endif
	AG_SetError("AG_Kill() is not supported on this platform");
	return (-1);
}
Beispiel #27
0
static int
SDLGL_OpenVideoContext(void *obj, void *ctx, Uint flags)
{
	AG_DriverSDLGL *sgl = obj;
	AG_DriverSw *dsw = obj;
	AG_Driver *drv = obj;
	SDL_Surface *ctxSu = (SDL_Surface *)ctx;

	if (!(ctxSu->flags & SDL_OPENGL)) {
		AG_SetError("Given display surface is not SDL_OPENGL");
		return (-1);
	}

	/* Set the requested display options. */
	if (flags & AG_VIDEO_OVERLAY)
		dsw->flags |= AG_DRIVER_SW_OVERLAY;
	if (flags & AG_VIDEO_BGPOPUPMENU)
		dsw->flags |= AG_DRIVER_SW_BGPOPUP;

	/* Use the given display surface. */
	sgl->s = (SDL_Surface *)ctx;
	if ((drv->videoFmt = AG_SDL_GetPixelFormat(sgl->s)) == NULL) {
		goto fail;
	}
	dsw->w = sgl->s->w;
	dsw->h = sgl->s->h;
	dsw->depth = (Uint)drv->videoFmt->BitsPerPixel;

	Verbose(_("SDLGL: Using existing display (%ux%ux%d bpp)\n"),
	    dsw->w, dsw->h, (int)drv->videoFmt->BitsPerPixel);
	
	/* Initialize our OpenGL context and viewport. */
	if (AG_GL_InitContext(sgl, &sgl->gl) == -1) {
		goto fail;
	}
	AG_GL_SetViewport(&sgl->gl, AG_RECT(0, 0, dsw->w, dsw->h));

	/* Create the cursors. */
	if (AG_SDL_InitDefaultCursor(sgl) == -1 ||
	    AG_InitStockCursors(drv) == -1)
		goto fail;
	
	return (0);
fail:
	if (drv->videoFmt) {
		AG_PixelFormatFree(drv->videoFmt);
		drv->videoFmt = NULL;
	}
	return (-1);
}
Beispiel #28
0
/* Create a cursor. */
int
AG_SDL_CreateCursor(void *obj, AG_Cursor *ac)
{
	SDL_Cursor *sc;

	sc = SDL_CreateCursor(ac->data, ac->mask,
	    ac->w, ac->h,
	    ac->xHot, ac->yHot);
	if (sc == NULL) {
		AG_SetError("SDL_CreateCursor failed");
		return (-1);
	}
	ac->p = (void *)sc;
	return (0);
}
Beispiel #29
0
static int
Write(AG_NetSocket *ns, const void *p, size_t size, size_t *nWrote)
{
    ssize_t rv;

    rv = write(ns->fd, p, size);
    if (rv < 0) {
        AG_SetError("write: %s", strerror(errno));
        return (-1);
    }
    if (nWrote != NULL) {
        *nWrote = (size_t)rv;
    }
    return (0);
}
Beispiel #30
0
static int
Init(void)
{
	WORD verPref = MAKEWORD(2,2);
	WSADATA wsaData;

	if (AG_MutexTryInit(&agNetWin32Lock) == -1) {
		return (-1);
	}
	if (WSAStartup(verPref, &wsaData) != 0) {
		AG_SetError("Winsock 2.2 initialization failed");
		return (-1);
	}
	return (0);
}