示例#1
0
void
destination (void)
{
	double now;
	struct unit *up;
	
	now = get_secs ();

	if (mousebutton[3]) {
		for (up = first_unit; up; up = up->next) {
			if (up->selected) {
				now = get_secs ();
				
				up->moveto_x = mouse_x;
				up->moveto_y = mouse_y;
				up->lasttime = now;
				up->moving = 1;
			}
		}
		destimg.drawing = 1;
		destimg.x = mouse_x - 20;
		destimg.y = mouse_y - 20;
		destimg.lasttime = now;
	}
}
示例#2
0
void draw(void)
{
    GLenum err;
    GLdouble secs;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    draw_room();
    draw_cone();

    /* draw the front and then the back faces (essentially sorts the
       polygons). */
    secs = get_secs();
    glEnable(GL_BLEND);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    draw_sphere(secs * 360. / 10.);
    glCullFace(GL_BACK);
    draw_sphere(secs * 360. / 10.);
    glDisable(GL_CULL_FACE);
    glDisable(GL_BLEND);

    err = glGetError();
    if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));

    glutSwapBuffers();
}
示例#3
0
void draw(void)
{
    GLenum err;
    GLdouble secs, degrees;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* one revolution every 10 seconds... */
    secs = get_secs();
    secs = secs - 10.*trunc(secs / 10.);
    degrees = (secs/10.) * (360.);

    draw_room();

    glEnable(GL_BLEND);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    draw_cone();
    draw_sphere(degrees);
    glCullFace(GL_BACK);
    draw_cone();
    draw_sphere(degrees);
    glDisable(GL_CULL_FACE);
    glDisable(GL_BLEND);

    err = glGetError();
    if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));

    glutSwapBuffers();
}
示例#4
0
void draw(void)
{
  GLenum err;
  GLfloat secs = get_secs();
  
  glDisable(GL_STENCIL_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  
  if (!headsUp) glEnable(GL_STENCIL_TEST);
  draw_scene(secs, draw_passes, GL_BACK, 0, (unsigned)-1);
  glDisable(GL_STENCIL_TEST);

  if (headsUp) {
    /* draw a red floor on the original scene */
    glDisable(GL_LIGHTING);
    glBegin(GL_QUADS);
    glColor3f(1, 0, 0);
    glVertex3f(-1, -.95, 1);
    glVertex3f(1, -.95, 1);
    glVertex3f(1, -.95, -1);
    glVertex3f(-1, -.95, -1);    
    glEnd();
    glEnable(GL_LIGHTING);
  }

  err = glGetError();
  if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));
  
  glutSwapBuffers(); 
}
示例#5
0
void
init_units (void)
{
	double units, unit_x, unit_y;

	unit_x = 200;
	unit_y = 240;

	for (units = 0; units <= 1; units++) {
		struct unit *up;
		up = xcalloc (1, sizeof *up);

		if (first_unit == NULL) {
			first_unit = up;
		} else {
			last_unit->next = up;
		}

		last_unit = up;

		up->x = unit_x;
		up->y = unit_y;
		up->h = 20;
		up->w = 40;
		up->color = 0x00ff00ff;
		unit_def (up, NULL);
		up->moveto_x = up->center_x;
		up->moveto_y = up->center_y;
		up->lasttime = get_secs();
		up->moving = 0;
		unit_x += 200;
	}
}
示例#6
0
void
movement (void)
{
	double now, dt;
	
	now = get_secs ();

	dt = now - player.lasttime;

	if (arrowkey[LEFT] != arrowkey[RIGHT] && mousebutton[3] == 0
	    && mousebutton[2] == 0) {
		if (arrowkey[LEFT]) {
			player.theta = player.theta + player.turnspeed * dt;
		}
		if (arrowkey[RIGHT]) {
			player.theta = player.theta - player.turnspeed * dt;
		}
		if (player.theta < 0) {
			player.theta = player.theta + DTOR (360);
		}
	}
	player.theta = fmod (player.theta, DTOR (360));
	
	player.moving = NO;
	
	if (arrowkey[UP] == 1)
		player.moving = FORW;
	if (arrowkey[DOWN] == 1)
		player.moving = BACK;
	if (arrowkey[Q] == 1 || arrowkey[FAKE_Q] == 1)
		player.moving = SIDE_Q;
	if (arrowkey[E] == 1 || arrowkey[FAKE_E] == 1)
		player.moving = SIDE_E;
	if ((arrowkey[Q] == 1 && arrowkey[UP] == 1)
	    || (arrowkey[FAKE_Q] == 1 && arrowkey[UP] == 1)
	    || (arrowkey[Q] == 1 && mousebutton[2] == 1)
	    || (arrowkey[FAKE_Q] == 1 && mousebutton[2] == 1))
		player.moving = FORW_Q;
	if ((arrowkey[E] == 1 && arrowkey[UP] == 1)
	    || (arrowkey[FAKE_E] == 1 && arrowkey[UP]  == 1)
	    || (arrowkey[E] == 1 && mousebutton[2] == 1)
	    || (arrowkey[FAKE_E] == 1 && mousebutton[2] == 1))
		player.moving = FORW_E;
	if ((arrowkey[Q] == 1 && arrowkey[DOWN] == 1)
	    || (arrowkey[FAKE_Q] == 1 && arrowkey[DOWN] == 1))
		player.moving = BACK_Q;
	if ((arrowkey[E] == 1 && arrowkey[DOWN] == 1)
	    || (arrowkey[FAKE_E] == 1 && arrowkey[DOWN] == 1))
		player.moving = BACK_E;
}
示例#7
0
static void calc_time (APP_TIMER_T * ptmr)
{
	unsigned int tick = ptmr->timer->rmt;
	unsigned int secs = tick / tm_get_ticks_per_second ();
	unsigned int mins = secs / 60;
	unsigned int hrs =  mins / 60;
	unsigned int tick_offset = 0;
	unsigned int secs_offset = 0;
	unsigned int mins_offset = 0;

	SET_TICK (ptmr->ctime,  tick % SYS_MAX_TICKS_IN_SEC);

	if (secs) {
		tick_offset = get_ticks () % SYS_MAX_TICKS_IN_SEC;
		if (tick_offset) {
			tick += tick_offset;
			SET_TICK (ptmr->ctime,  tick % SYS_MAX_TICKS_IN_SEC);
			secs = tick / SYS_MAX_TICKS_IN_SEC;
		}
		SET_SECS (ptmr->ctime,  secs);
	}

	if (mins) {
		secs_offset = get_secs () % 60;
		if (secs_offset) {
			secs += secs_offset;
			SET_SECS (ptmr->ctime,  secs);
			mins = secs / 60;
		}
		SET_MINS (ptmr->ctime,  mins);
	}

	if (hrs) {
		mins_offset = get_mins () % 60;
		if (mins_offset) {
			mins += mins_offset;
			SET_SECS (ptmr->ctime,  mins);
			hrs = mins / 60;
		}
		SET_HRS  (ptmr->ctime,  hrs);
	}

	ptmr->timer->exp = tick + clk[TICK] - tick_offset;

#ifdef TIMER_DEBUG
	printf ("secs : %d ticks %d tick offset : %d curr ticks : %d expiry : %d act : %d\n", 
		 secs, tick % SYS_MAX_TICKS_IN_SEC, tick_offset, get_ticks (), ptmr->timer->exp, 
		 ptmr->timer->rmt);
#endif
}
示例#8
0
static void xid_expire(void)
{
	struct xid_item *item = dhcprelay_xid_list.next;
	struct xid_item *last = &dhcprelay_xid_list;
	unsigned current_time = get_secs();

	while (item != NULL) {
		if ((current_time - item->timestamp) > MAX_LIFETIME) {
			last->next = item->next;
			free(item);
			item = last->next;
		} else {
			last = item;
			item = item->next;
		}
	}
}
示例#9
0
void
init_destimg (void)
{
	double now;

	now = get_secs ();

	load_blit (&destimg.frames[0], "destination/frame00.png");
	load_blit (&destimg.frames[1], "destination/frame01.png");
	load_blit (&destimg.frames[2], "destination/frame02.png");
	load_blit (&destimg.frames[3], "destination/frame03.png");
	load_blit (&destimg.frames[4], "destination/frame04.png");
	load_blit (&destimg.frames[5], "destination/frame05.png");
	load_blit (&destimg.frames[6], "destination/frame06.png");
	load_blit (&destimg.frames[7], "destination/frame07.png");
	destimg.lasttime = now;
}
示例#10
0
static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
{
	struct xid_item *item;

	/* create new xid entry */
	item = xmalloc(sizeof(struct xid_item));

	/* add xid entry */
	item->ip = *ip;
	item->xid = xid;
	item->client = client;
	item->timestamp = get_secs ();
	item->next = dhcprelay_xid_list.next;
	dhcprelay_xid_list.next = item;

	return item;
}
示例#11
0
文件: main.c 项目: Ferritt1975/booth
int main(int argc, char *argv[], char *envp[])
{
	int rv;

	init_set_proc_title(argc, argv, envp);
	get_secs(&start_time);

	memset(&cl, 0, sizeof(cl));
	strncpy(cl.configfile,
			BOOTH_DEFAULT_CONF, BOOTH_PATH_LEN - 1);
	cl.lockfile[0] = 0;
	debug_level = 0;
	cl_log_set_entity("booth");
	cl_log_enable_stderr(TRUE);
	cl_log_set_facility(0);


	rv = read_arguments(argc, argv);
	if (rv < 0)
		goto out;


	switch (cl.type) {
	case STATUS:
		rv = do_status(cl.type);
		break;

	case ARBITRATOR:
	case DAEMON:
	case SITE:
		rv = do_server(cl.type);
		break;

	case CLIENT:
		rv = do_client();
		break;
	}

out:
	/* Normalize values. 0x100 would be seen as "OK" by waitpid(). */
	return (rv >= 0 && rv < 0x70) ? rv : 1;
}
示例#12
0
void draw(void)
{
    GLenum err;
    GLdouble secs;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    draw_room();
    draw_cone();

    glEnable(GL_BLEND);
    secs = get_secs();
    draw_sphere(secs * 360. / 10.);
    glDisable(GL_BLEND);

    err = glGetError();
    if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));

    glutSwapBuffers();
}
示例#13
0
void draw(void)
{
    GLenum err;
    GLdouble secs;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    draw_room();
    draw_cone();
    secs = get_secs();

    /* draw the transparent object... */
    glEnable(GL_POLYGON_STIPPLE);
    draw_sphere(secs * 360. / 10.);
    glDisable(GL_POLYGON_STIPPLE);

    err = glGetError();
    if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));

    glutSwapBuffers();
}
示例#14
0
void
moving (void)
{
	double now, dt, dx, dy, theta;
	struct unit *up;

	for (up = first_unit; up; up = up->next) {
		now = get_secs ();

		if (up->moving) {
			dx = up->moveto_x - up->center_x;
			dy = up->moveto_y - up->center_y;
			theta = atan2 (dy, dx);
			
			dt = now - up->lasttime;
			
			up->mov_x = SPEED * dt * cos (theta);
			up->mov_y = SPEED * dt * sin (theta);
			
			if (fabs (up->mov_x) > fabs (dx)
			    && fabs (up->mov_y) > fabs (dy)) {
				up->x = up->moveto_x - up->w / 2;
				up->y = up->moveto_y - up->h / 2;
				up->moving = 0;
			} else {
				if (collision_x (up)) {
					up->mov_x = 0;
				}
				up->x += up->mov_x;
				if (collision_y (up)) {
					up->mov_y = 0;
				}
				up->y += up->mov_y;
			}
			
			up->lasttime = now;
			unit_def (up, NULL);
		}
	}
}
示例#15
0
void draw(void)
{
    GLenum err;
    GLdouble secs, degrees;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* one revolution every 10 seconds... */
    secs = get_secs();
    secs = secs - 10.*trunc(secs / 10.);
    degrees = (secs/10.) * (360.);

#if 0
    draw_room();
#endif
    draw_torus(degrees);

    err = glGetError();
    if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));

    glutSwapBuffers();
}
示例#16
0
文件: spheremap.c 项目: xtmacbook/SGI
void 
draw(void)
{
    static int frame = 0;
    GLenum err;
    GLdouble secs;
    static double degrees = 0;
    GLfloat sphereX, sphereY, sphereZ;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* one revolution every 10 seconds... */
    if (animate) {
	secs = get_secs();
	secs = secs - 10. * floor(secs / 10.);
	degrees = (secs / 10.) * (360.);
    }
/* if (frame == 0) { */
    if (0) {
	/* switch the viewport and draw the faces of the cube from the 
	 * point of view of the square... */

	glViewport(w + 0 * faceW, 0, faceW, faceW);
	make_projection(LEFT, 0, 0, 0);
	draw_scene(degrees, -1 & ~TORUS_BIT);
	glReadPixels(w + 0 * faceW, 0, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[LEFT]);
	eliminate_alpha(faceW, faceW, faceMap[LEFT]);

	glViewport(w + 1 * faceW, 0, faceW, faceW);
	make_projection(RIGHT, 0, 0, 0);
	draw_scene(degrees, -1 & ~TORUS_BIT);
	glReadPixels(w + 1 * faceW, 0, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[RIGHT]);
	eliminate_alpha(faceW, faceW, faceMap[RIGHT]);

	glViewport(w + 2 * faceW, 0, faceW, faceW);
	make_projection(BOTTOM, 0, 0, 0);
	draw_scene(degrees, -1 & ~TORUS_BIT);
	glReadPixels(w + 2 * faceW, 0, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[BOTTOM]);
	eliminate_alpha(faceW, faceW, faceMap[BOTTOM]);

	glViewport(w + 0 * faceW, faceW, faceW, faceW);
	make_projection(TOP, 0, 0, 0);
	draw_scene(degrees, -1 & ~TORUS_BIT);
	glReadPixels(w + 0 * faceW, faceW, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[TOP]);
	eliminate_alpha(faceW, faceW, faceMap[TOP]);

	glViewport(w + 1 * faceW, faceW, faceW, faceW);
	make_projection(FRONT, 0, 0, 0);
	draw_scene(degrees, -1 & ~TORUS_BIT);
	glReadPixels(w + 1 * faceW, faceW, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[FRONT]);
	eliminate_alpha(faceW, faceW, faceMap[FRONT]);

	glViewport(w + 2 * faceW, faceW, faceW, faceW);
	make_projection(BACK, 0, 0, 0);
	draw_scene(degrees, -1 & ~TORUS_BIT);
	glReadPixels(w + 2 * faceW, faceW, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[BACK]);
	eliminate_alpha(faceW, faceW, faceMap[BACK]);

	/* create the sphere map for the cube... */
	glViewport(w, 2 * faceW, sphereW, sphereW);
	render_spheremap();
	glReadPixels(w, 2 * faceW, sphereW, sphereW, GL_RGBA, GL_UNSIGNED_BYTE,
		     sphereMap[0]);
    } else {
	sphereX =
	    sphereY = cos((degrees / 360.) * 2. * M_PI);
	sphereZ = -sin((degrees / 360.) * 2. * M_PI);

	glViewport(w + 0 * faceW, 0, faceW, faceW);
	make_projection(LEFT, sphereX, sphereY, sphereZ);
	draw_scene(degrees, 0 & ~SPHERE_BIT);
	glReadPixels(w + 0 * faceW, 0, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[LEFT]);
	eliminate_alpha(faceW, faceW, faceMap[LEFT]);

	glViewport(w + 1 * faceW, 0, faceW, faceW);
	make_projection(RIGHT, sphereX, sphereY, sphereZ);
	draw_scene(degrees, 0 & ~SPHERE_BIT);
	glReadPixels(w + 1 * faceW, 0, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[RIGHT]);
	eliminate_alpha(faceW, faceW, faceMap[RIGHT]);

	glViewport(w + 2 * faceW, 0, faceW, faceW);
	make_projection(BOTTOM, sphereX, sphereY, sphereZ);
	draw_scene(degrees, 0 & ~SPHERE_BIT);
	glReadPixels(w + 2 * faceW, 0, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[BOTTOM]);
	eliminate_alpha(faceW, faceW, faceMap[BOTTOM]);

	glViewport(w + 0 * faceW, faceW, faceW, faceW);
	make_projection(TOP, sphereX, sphereY, sphereZ);
	draw_scene(degrees, 0 & ~SPHERE_BIT);
	glReadPixels(w + 0 * faceW, faceW, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[TOP]);
	eliminate_alpha(faceW, faceW, faceMap[TOP]);

	glViewport(w + 1 * faceW, faceW, faceW, faceW);
	make_projection(FRONT, sphereX, sphereY, sphereZ);
	draw_scene(degrees, 0 & ~SPHERE_BIT);
	glReadPixels(w + 1 * faceW, faceW, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[FRONT]);
	eliminate_alpha(faceW, faceW, faceMap[FRONT]);

	glViewport(w + 2 * faceW, faceW, faceW, faceW);
	make_projection(BACK, sphereX, sphereY, sphereZ);
	draw_scene(degrees, 0 & ~SPHERE_BIT);
	glReadPixels(w + 2 * faceW, faceW, faceW, faceW,
		     GL_RGBA, GL_UNSIGNED_BYTE, faceMap[BACK]);
	eliminate_alpha(faceW, faceW, faceMap[BACK]);

	/* create the sphere map for the cube... */
	glViewport(w + sphereW, 2 * faceW, sphereW, sphereW);
	render_spheremap();
	glReadPixels(w + sphereW, 2 * faceW, sphereW, sphereW,
		     GL_RGBA, GL_UNSIGNED_BYTE, sphereMap[1]);
    }
    frame = (frame == 0);

    /* draw both spheremaps */
    glViewport(w, 2 * faceW, 2 * sphereW, sphereW);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 2 * sphereW, 0, sphereW, 0, 1);
    glRasterPos2i(0, 0);
    glDrawPixels(sphereW, sphereW, GL_RGBA, GL_UNSIGNED_BYTE, sphereMap[0]);
    glRasterPos2i(sphereW, 0);
    glDrawPixels(sphereW, sphereW, GL_RGBA, GL_UNSIGNED_BYTE, sphereMap[1]);


    /* draw the scene for the viewer's visual gratification... */
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, 1, .01, 10);
    gluLookAt(0, 0, 0,
	      0, 0, -1,
	      0, 1, 0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.f, 0.f, -2.577f);
    draw_scene(degrees, 2);	/* just draw green sphere */
    glLoadIdentity();

    err = glGetError();
    if (err != GL_NO_ERROR)
	printf("Error:  %s\n", gluErrorString(err));

    if (dblbuf)
	glutSwapBuffers();
    else
	glFlush();
}
示例#17
0
void
draw (void)
{
	double now, dt;
	struct unit *up;
	struct pathblock *pp;

	now = get_secs ();

	if (destimg.drawing) {
		dt = floor (10 * (now - destimg.lasttime));
		if (dt < 8) {
			switch ((int) dt) {
			case 0:
				draw_blit (destimg.frames[0],
					   destimg.x, destimg.y);
				break;
			case 1:
				draw_blit (destimg.frames[1],
					   destimg.x, destimg.y);
				break;
			case 2:
				draw_blit (destimg.frames[2],
					   destimg.x, destimg.y);
				break;
			case 3:
				draw_blit (destimg.frames[3],
					   destimg.x, destimg.y);
				break;
			case 4:
				draw_blit (destimg.frames[4],
					   destimg.x, destimg.y);
				break;
			case 5:
				draw_blit (destimg.frames[5],
					   destimg.x, destimg.y);
				break;
			case 6:
				draw_blit (destimg.frames[6],
					   destimg.x, destimg.y);
				break;
			case 7:
				draw_blit (destimg.frames[7],
					   destimg.x, destimg.y);
				break;
			}

		} else {
			destimg.drawing = 0;
		}
	}

	for (pp = first_pathblock; pp; pp = pp->next) {
		boxColor (screen, pp->left, pp->top, pp->right, pp->bottom,
			  pp->color);
	}

	for (up = first_unit; up; up = up->next) {
		boxColor (screen, up->left, up->top, up->right, up->bottom,
			  up->color);
		if (up->selected == 1) {
			circleColor (screen, up->center_x, up->center_y,
				     hypot (up->h / 2, up->w / 2) + 3,
				     0x00ff00ff);
			aacircleColor (screen, up->center_x, up->center_y,
				       hypot (up->h / 2, up->w / 2) + 3,
				       0x00ff00ff);
		}
	}

	if (selectbox.drawing) {
		rectangleColor (screen, selectbox.x1, selectbox.y1,
				selectbox.x2, selectbox.y2, selectbox.color);
	}
}
示例#18
0
int
main (int argc, char **argv)
{
	int c;
	int ac;
	char *av[100];
	int fd;
	struct sockaddr_in addr;
	socklen_t addrlen;
	fd_set rset;
	int maxfd;
	int n;
	char buf[1000];
	struct timeval tv;
	double now;
	char *outp;
	int i;

	seteuid (getuid ());

	outp = argbuf;
	for (i = 0; i < argc; i++) {
		sprintf (outp, "%s ", argv[i]);
		outp += strlen (outp);
	}

	setbuf (stdout, NULL);
	setbuf (stderr, NULL);

	fd = open ("/dev/null", O_RDONLY);
	dup2 (fd, 0);
	close (fd);

	for (fd = 3; fd < 100; fd++)
		close (fd);

	while ((c = getopt (argc, argv, "k:Ld:n")) != EOF) {
		switch (c) {
		case 'n':
			no_fork = 1;
			break;
		case 'd':
			game_dir = optarg;
			break;
		case 'L':
			use_logfile = 1;
			break;
		case 'k':
			conf_key = optarg;
			break;
		default:
			usage ();
		}
	}

	if (optind >= argc)
		usage ();
	
	zfile_name = argv[optind++];

	if (optind != argc)
		usage ();

	if (strlen (zfile_name) > 30) {
		fprintf (stderr, "invalid zfile name\n");
		exit (1);
	}

	if (conf_key == NULL) {
		struct passwd *pw;

		if ((pw = getpwuid (getuid ())) == NULL) {
			fprintf (stderr, "can't get my name\n");
			exit (1);
		}

		conf_key = malloc (100);
		sprintf (conf_key, "%s-dev", pw->pw_name);
	}
			
	if (strlen (conf_key) > 50) {
		fprintf (stderr, "invalid conf_key\n");
		exit (1);
	}

	sprintf (aux_dir, "/var/zorky-%s", conf_key);

	orig_stdout = dup (1);

	if (use_logfile) {
		sprintf (log_name, "%s/tmp/dfrotz.log", aux_dir);
		log_fd = open (log_name,
			   O_WRONLY | O_CREAT | O_APPEND, 0666);
		if (log_fd < 0) {
			fprintf (stderr, "can't create log file\n");
			exit (1);
		}
		fchmod (log_fd, 0666);
		dup2 (log_fd, 1);
		dup2 (log_fd, 2);
	}

	printf ("\n");
	printf ("running: %s\n", argbuf);

	listen_sock = socket (AF_INET, SOCK_STREAM, 0);
	listen (listen_sock, 1);

	addrlen = sizeof addr;
	if (getsockname (listen_sock, (struct sockaddr *)&addr, &addrlen) < 0) {
		perror ("getsockname");
		exit (1);
	}

	port = ntohs (addr.sin_port);

	printf ("startp id %d\n", getpid ());
	printf ("port = %d\n", port);

	if (game_dir == NULL) {
		printf ("game_dir must be specified\n");
		exit (1);
	}

	setup_chroot ();

	if (no_fork) {
		pid = 0;
	} else {
		if ((pid = fork ()) < 0) {
			fprintf (stderr, "fork error\n");
			exit (1);
		}
	}

	if (pid > 0) {
		printf ("child1 %d\n", pid);
		sprintf (buf, "%d %d\n", port, pid);
		write (orig_stdout, buf, strlen (buf));
		exit (0);
	}

	close (orig_stdout);

	ac = 0;
	av[ac++] = cmd_name;
	av[ac++] = z5_filename;
	av[ac] = NULL;

	if (pipe (fds_to_frotz) < 0
	    || pipe (fds_from_frotz) < 0) {
		fprintf (stderr, "error making pipes\n");
		exit (1);
	}

	printf ("ready to fork frotz\n");

	if (no_fork == 0) {
		if ((pid = fork ()) < 0) {
			fprintf (stderr, "fork error\n");
			exit (1);
		}
	} else {
		pid = 0;
	}

	if (pid == 0) {
		dup2 (fds_to_frotz[0], 0);
		dup2 (fds_from_frotz[1], 1);
		dup2 (1, 2);

		close (fds_to_frotz[0]);
		close (fds_to_frotz[1]);
		close (fds_from_frotz[0]);
		close (fds_from_frotz[1]);

		for (fd = 3; fd < 100; fd++) {
			if (fd != log_fd)
				close (fd);
		}

		sprintf (buf, "execing  %s %s %s %s %d %d\n",
			 cmd_name, av[0], av[1], av[2],
			 open (av[0],O_RDONLY),
			 open (av[1],O_RDONLY));
		write (log_fd, buf, strlen (buf));


		execv (cmd_name, av);
		
		sprintf (buf, "exec failed %s %s\n",
			 cmd_name, strerror (errno));
		write (log_fd, buf, strlen (buf));
		exit (1);
	}

	printf ("child2 %d\n", pid);

	to_frotz = fds_to_frotz[1];
	close (fds_to_frotz[0]);

	from_frotz = fds_from_frotz[0];
	close (fds_from_frotz[1]);

	fcntl (from_frotz, F_SETFL, O_NONBLOCK);

	base_secs = get_secs ();
	while (1) {
		FD_ZERO (&rset);

		maxfd = 0;

		FD_SET (listen_sock, &rset);
		if (listen_sock > maxfd)
			maxfd = listen_sock;

		tv.tv_sec = 1000;
		tv.tv_usec = 0;

		if (client_sock) {
			now = get_secs ();
			if (now - base_secs > .5) {
				fprintf (stderr, "frotz timeout\n");
				close (client_sock);
				client_sock = 0;
			}


			FD_SET (from_frotz, &rset);
			if (from_frotz > maxfd)
				maxfd = from_frotz;

			FD_SET (client_sock, &rset);
			if (client_sock > maxfd)
				maxfd = client_sock;

			tv.tv_sec = 0;
			tv.tv_usec = 50 * 1000;
		}

		if (select (maxfd + 1, &rset, NULL, NULL, &tv) < 0) {
			perror ("select");
			exit (1);
		}

		if (FD_ISSET (listen_sock, &rset)) {
			if ((fd = accept (listen_sock, NULL, NULL)) >= 0) {
				if (client_sock) {
					printf ("rejected second connection\n");
					close (fd);
				} else {
					printf ("connected...\n");
					client_sock = fd;
					fcntl (client_sock, F_SETFL,
					       O_NONBLOCK);
					base_secs = get_secs ();
				}
			}
		}

		if (client_sock) {
			if (FD_ISSET (from_frotz, &rset)) {
				errno = 0;
				n = read (from_frotz, buf, sizeof buf);
				if (n == 0
				    || (n < 0 && errno != EWOULDBLOCK)) {
					fprintf (stderr,
						 "frotz died %d %d\n",
						 n, errno);
					exit (0);
				} if (n > 0) {
					base_secs = get_secs ();
					write (client_sock, buf, n);
				}
			}

			if (FD_ISSET (client_sock, &rset)) {
				n = read (client_sock, buf, sizeof buf);
				if (n == 0
				    || (n < 0 && errno != EWOULDBLOCK)) {
					fprintf (stderr,
						 "client died\n");
					close (client_sock);
					client_sock = 0;
				} else {
					write (to_frotz, buf, n);
				}
			}
		}
	}

	return (0);
}
示例#19
0
void
falling_moving (void)
{
	double now, dt;
	struct vect grav, grav_acc;
	struct groundtri *gt;
	struct pt newpos, p1;

	now = get_secs ();
	dt = now - player.lasttime;

	if ((gt = detect_plane (&player.p)) == NULL) {
		printf ("Can't find ground, moving player to start position "
			"to avoid seg fault\n");
		player.p.x = 0;
		player.p.y = 0;
		player.p.z = ground_height (&player.p);
		player.movtype = GROUNDED;
		player.loc = detect_plane (&player.p);
		return;
	}

	if (player.p.z <= ground_height (&player.p) && gt->theta < DTOR (50)) {
		player.movtype = GROUNDED;
		player.p.z = ground_height (&player.p);
		return;
	}

	grav.x = 0;
	grav.y = 0;
	grav.z = GRAVITY;

	vscal (&grav_acc, &grav, 1 / player.mass);

	player.vel.x += grav_acc.x;
	player.vel.y += grav_acc.y;
	player.vel.z += grav_acc.z;

	newpos.x = player.p.x + player.vel.x * dt;
	newpos.y = player.p.y + player.vel.y * dt;
	newpos.z = player.p.z + player.vel.z * dt;

	if (newpos.z <= ground_height (&newpos)) {
		if ((gt = detect_plane (&newpos)) == NULL) {
			printf ("Can't find ground at newpos, moving player to "
				"start position to avoid seg fault\n");
			player.p.x = 0;
			player.p.y = 0;
			player.p.z = ground_height (&player.p);
			player.movtype = GROUNDED;
			player.loc = detect_plane (&player.p);
			return;
		}

		if (gt->theta < DTOR (50)) {
			player.p = newpos;
			player.p.z = ground_height (&player.p);
			player.movtype = GROUNDED;
			player.loc = gt;
			return;
		} else {
			p1.x = vdot (&player.vel, &gt->normv) * gt->normv.x;
			p1.y = vdot (&player.vel, &gt->normv) * gt->normv.y;
			p1.z = vdot (&player.vel, &gt->normv) * gt->normv.z;

			player.vel.x -= p1.x;
			player.vel.y -= p1.y;
			player.vel.z -= p1.z;
			
			player.p.x += player.vel.x * dt;
			player.p.y += player.vel.y * dt;
			player.p.z += player.vel.z * dt;
		}
	} else {
		player.p.x = newpos.x;
		player.p.y = newpos.y;
		player.p.z = newpos.z;
		
		player.loc = gt;
	}
}
示例#20
0
static void
alex_window_proc (gpointer spec, gpointer user_data)
{
  MetaWindow *window = spec;
  MetaRectangle rect;
  MetaFrameGeometry geomp;
  double now, dt, dx, dy, inst_vel, time_const, factor;
  int left, top, right, bottom, newx, newy, screenx, screeny, bounce_flag;
  static double velx, vely;

  now = get_secs ();
  dt = now - window->lasttime;

  window->lasttime = now;

  meta_window_get_client_root_coords (window, &rect);

  alex_window_get_position (window, &left, &top);

  if (window->frame)
    meta_frame_calc_geometry (window->frame, &geomp);
  else
    return;

  right = left + geomp.left_width + rect.width + geomp.right_width;
  bottom = top + geomp.top_height + rect.height + geomp.bottom_height;

  switch (window->phys_state) {
  case 1:
    time_const = dt;
    
    dx = rect.x - window->lastx;
    inst_vel = (dx / dt);
    factor = dt / time_const;
    if (factor < 0)
      factor = 0;
    if (factor > 1)
      factor = 1;
    velx = velx * (1 - factor) + inst_vel * factor;

    dy = rect.y - window->lasty;
    inst_vel = (dy / dt);
    factor = dt / time_const;
    if (factor < 0)
      factor = 0;
    if (factor > 1)
      factor = 1;
    vely = vely * (1 - factor) + inst_vel * factor;

    window->theta = atan2 (vely, velx);
    /* window->speed = hypot (vely, velx); */
    window->speed = sqrt (vely * vely + velx * velx);
    break;
  case 2:
    if (now - window->lasttimemouse > .1) {
      window->speed = 0;
      window->velx = 0;
      window->vely = 0;
      window->phys_state = 0;
      break;
    }

    window->phys_state = 3;
  case 3:
    bounce_flag = 0;

    velx = window->speed * cos (window->theta);
    vely = window->speed * sin (window->theta);

    newx = rect.x + velx * dt;
    newy = rect.y + vely * dt;

    if (left + velx * dt <= 0) {
      meta_window_move (window, TRUE, 0, newy);

      window->theta = atan2 (vely, -velx);
      window->speed -= 200;

      bounce_flag = 1;
    }

    if (top + vely * dt <= 25) {
      meta_window_move (window, TRUE, newx, 25 + geomp.top_height);

      window->theta = atan2 (-vely, velx);
      window->speed -= 200;

      bounce_flag = 1;
    }

   screenx = window->screen->xscreen->width;
   screeny = window->screen->xscreen->height;

    if (right + velx * dt >= screenx) {
      meta_window_move (window, TRUE, screenx - geomp.right_width - rect.width, newy);

      window->theta = atan2 (vely, -velx);
      window->speed -= 200;

      bounce_flag = 1;
    }

    if (bottom + vely * dt >= screeny - 25) {
      meta_window_move (window, TRUE, newx, screeny - 25 - geomp.bottom_height - rect.height);

      window->theta = atan2 (-vely, velx);
      window->speed -= 200;

      bounce_flag = 1;
    }

    if (bounce_flag == 0) {
      meta_window_move (window, TRUE, newx, newy);
    }

    if (window->speed < 20) {
      window->speed = 0;
      window->phys_state = 0;
    }
    
    window->speed -= 1000 * dt;
    break;
  }
}
示例#21
0
int
main (int argc, char **argv)
{
	int i;
	double now;

	feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);

	init_sdl_gl_flags (WIDTH, HEIGHT, 0);

	srandom (time (NULL));

	init_gl (&argc, argv);

	glEnable (GL_LIGHTING);
	glEnable (GL_DEPTH_TEST);
	glEnable (GL_AUTO_NORMAL);
	glEnable (GL_NORMALIZE);

	glClearDepth (1);
	
	glViewport (0, 0, WIDTH, HEIGHT);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluPerspective (60, (GLfloat) WIDTH/(GLfloat) HEIGHT, .1, 1000);
	glClearColor (0, 0, 0, 0);
	glMatrixMode (GL_MODELVIEW);

	SDL_ShowCursor (1);

	makeImages ();

	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
	glGenTextures (1, texName);
	
	makeTexture (texName[0], groundtexture.texturesize,
		     (GLubyte ***) groundtexture.tex);

	glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

	glBlendFunc (GL_SRC_ALPHA, GL_ONE);

	gndcounter = 1;
	read_terrain ();

	player.p.x = 0;
	player.p.y = 0;
	player.p.z = ground_height (&player.p);

	player.loc = detect_plane (&player.p);

	player.movtype = GROUNDED;

	player.speed = 100;
	player.mass = 1;

	vset (&player.vel, 0, 0, 0);

	player.turnspeed = DTOR (180);
	player.theta = DTOR (0);
	player.camdist = 15;

	player.lasttime = get_secs ();
	player.moving = NO;

	playercamera.phi = DTOR (0);
	playercamera.theta_difference = 0;
	
	while (1) {
		process_input ();
		
		if (mousebutton[1] == 0
		    && mousebutton[2] == 0
		    && mousebutton[3] == 0) {
			SDL_ShowCursor (1);
		} else {
			SDL_ShowCursor (0);
		}

		movement ();
		if (paused == NO) {
			for (i = 0; i < 1; i++) {
				moving ();
				now = get_secs ();
				player.lasttime = now;
			}
		}

		process_mouse ();

		draw ();
		
		now = get_secs ();
		
		player.lasttime = now;

		SDL_Delay (10);
	}

	return (0);
}
示例#22
0
void
grounded_moving (void)
{
	double now, dt;
	struct pt newpos;
	struct groundtri *gt;
	struct vect ctrl_vel;

	now = get_secs ();
	dt = now - player.lasttime;

	ctrl_vel.x = 0;
	ctrl_vel.y = 0;
	ctrl_vel.z = 0;

	if (fabs (player.p.z - ground_height (&player.p)) <= 1e6) {
		switch (player.moving) {
		case BACK:
			ctrl_vel.x = -.3 * player.speed * cos (player.theta);
			ctrl_vel.y = -.3 * player.speed * sin (player.theta);
			break;
		case FORW:
			ctrl_vel.x = player.speed * cos (player.theta);
			ctrl_vel.y = player.speed * sin (player.theta);
			break;
		case SIDE_Q:
			ctrl_vel.x = player.speed
				* cos (player.theta + DTOR (90));
			ctrl_vel.y = player.speed
				* sin (player.theta + DTOR (90));
			break;
		case SIDE_E:
			ctrl_vel.x = player.speed
				* cos (player.theta - DTOR (90));
			ctrl_vel.y = player.speed
				* sin (player.theta - DTOR (90));
			break;
		case FORW_Q:
			ctrl_vel.x = player.speed
				* cos (player.theta + DTOR (45));
			ctrl_vel.y = player.speed
				* sin (player.theta + DTOR (45));
			break;
		case FORW_E:
			ctrl_vel.x = player.speed
				* cos (player.theta - DTOR (45));
			ctrl_vel.y = player.speed
				* sin (player.theta - DTOR (45));
			break;
		case BACK_Q:
			ctrl_vel.x = -.3 * player.speed
				* cos (player.theta - DTOR (45));
			ctrl_vel.y = -.3 * player.speed
				* sin (player.theta - DTOR (45));
			break;
		case BACK_E:
			ctrl_vel.x = -.3 * player.speed
				* cos (player.theta + DTOR (45));
			ctrl_vel.y = -.3 * player.speed
				* sin (player.theta + DTOR (45));
			break;
		}
	}

	newpos.x = player.p.x + ctrl_vel.x * dt;
	newpos.y = player.p.y + ctrl_vel.y * dt;
	newpos.z = player.p.z + ctrl_vel.z * dt;

	psub (&player.vel, &newpos, &player.p);
	vscal (&player.vel, &player.vel, 1 / dt);

	if ((gt = detect_plane (&newpos)) == NULL) {
		printf ("Can't find ground, moving player to start position "
			"to avoid seg fault\n");
		player.p.x = 0;
		player.p.y = 0;
		player.p.z = ground_height (&player.p);
		player.movtype = GROUNDED;
		player.loc = detect_plane (&player.p);
		return;
	}

	if (gt->theta < DTOR (50)) {
		newpos.z = ground_height (&newpos);
		psub (&player.vel, &newpos, &player.p);
		vscal (&player.vel, &player.vel, 1 / dt);
		player.p = newpos;
	} else if (newpos.z > gt->pl.middle.z) {
		//imperfect detection of cliffs but tolerable for now
		player.p = newpos;
		player.movtype = FALLING;
		printf ("switching to falling\n");
	} else {
		if (gt == player.loc) {
			printf ("line %d: HORRIBLE MESSINESS\n", __LINE__);
			paused = YES;
			printf ("paused game\n");
			return;
		}

		struct vect v1, v2;

		vnorm (&v2, &ctrl_vel);
		vcross (&v1, &player.loc->normv, &gt->normv);
		vnorm (&v1, &v1);
		if (vdot (&v1, &v2) < 0)
			vscal (&v1, &v1, -1);
		vscal (&ctrl_vel, &v1, vdot (&ctrl_vel, &v1));
			
		newpos.x = player.p.x + ctrl_vel.x * dt;
		newpos.y = player.p.y + ctrl_vel.y * dt;
		newpos.z = player.p.z + ctrl_vel.z * dt;

		newpos.z = ground_height (&newpos);
		psub (&player.vel, &newpos, &player.p);
		vscal (&player.vel, &player.vel, 1 / dt);
				
		player.p = newpos;
	}
}
示例#23
0
int main(void) {
#if SDL
    SDL_Event event;
    SDL_Rect rect;
    SDL_Renderer *renderer;
    SDL_Window *window;
# if STREAMING
    SDL_Texture *texture = NULL;
    void *pixels;
    Uint8 *base;
    /* TODO: is it mandatory to use this? Maybe SDL_LockTexture
     * will make the array non continuous to improve alignment? */
    int pitch;
# endif
#else
    double sum;
#endif
    const unsigned int WINDOW_WIDTH = 600;
    const unsigned int WINDOW_HEIGHT = WINDOW_WIDTH;
    const double SPEED = (WINDOW_WIDTH / 10.0);
    const double CENTER_X = WINDOW_WIDTH / 2.0;
    const double CENTER_Y = WINDOW_HEIGHT / 2.0;
    const double PERIOD = (WINDOW_WIDTH / 10.0);
    const double PI2 = 2.0 * acos(-1.0);
    double dt;
    double fps_dt;
    double fps_last_time;
    double initial_time;
    double t;
    int nframes;
    float z;
    unsigned int x;
    unsigned int xc;
    unsigned int y;
    unsigned int yc;

#if SDL
    SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
# if STREAMING
    texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WINDOW_WIDTH, WINDOW_HEIGHT);
# endif
#endif
    initial_time = get_secs();
    fps_last_time = initial_time;
    nframes = 0;
    while (1) {
        t = get_secs();
        dt = t - initial_time;
#if STREAMING
        SDL_LockTexture(texture, NULL, &pixels, &pitch);
#endif
        for (x = 0; x < WINDOW_WIDTH; x++) {
            for (y = 0; y < WINDOW_HEIGHT; y++) {
                xc = CENTER_X - x;
                yc = CENTER_Y - y;
                z = COLOR_MAX * 0.5 * (1.0 + (sin(PI2 * (sqrt(xc*xc + yc*yc) - SPEED * dt) / PERIOD)));
#if SDL
# if STREAMING
                base = ((Uint8 *)pixels) + (4 * (x * WINDOW_WIDTH + y));
                base[0] = 0;
                base[1] = 0;
                base[2] = z;
                base[3] = COLOR_MAX;
# else
                SDL_SetRenderDrawColor(renderer, z, 0, 0, COLOR_MAX);
                SDL_RenderDrawPoint(renderer, x, y);
# endif
#else
                sum += z;
#endif
            }
        }
#if SDL
# if STREAMING
        SDL_UnlockTexture(texture);
        SDL_RenderCopy(renderer, texture, NULL, NULL);
# endif
        SDL_RenderPresent(renderer);
#endif
        nframes++;
        fps_dt = t - fps_last_time;
        if (fps_dt > 0.25) {
            /* Produce a side-effect for the non-SDL version so that
             * it does not get optimized away. */
            printf("FPS = %f\n", nframes / fps_dt);
            fps_last_time = t;
            nframes = 0;
#if !SDL
            printf("%f\n", sum);
#endif
        }
#if SDL
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
            break;
#endif
    }
#if SDL
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
#endif
    return EXIT_SUCCESS;
}
示例#24
0
文件: sparsevec.c 项目: 8l/insieme
int main(int argc, const char** argv)
{
	size_t x = 512, y = 250000; //y has to be a multiple of ciDeviceCount!
	struct svm_node* px = (struct svm_node*)malloc((x+1)*sizeof(struct svm_node));
	gen_data(px, x, 1, 3);
	struct svm_node* py = (struct svm_node*)malloc((x+1)*y*sizeof(struct svm_node));
	for(size_t i = 0; i < y; ++i) {
		struct svm_node* tmp = py+i*(x+1);
		gen_data(tmp, x, 3,2);
	}
	dtype* result = (dtype*)malloc(y*sizeof(dtype));
	int* pyLength = (int*)malloc(y*sizeof(int));
	
	for(size_t i = 0; i < y; ++i)
	{
		for(size_t j = 0; py[i*(x+1)+j].index >= 0; ++j)
			pyLength[i] = py[i*(x+1)+j].index;
		++pyLength[i];
	}
	
	cl_int err = CL_SUCCESS;
//	cl_platform_id platform = NULL;
//	cl_uint ciDeviceCount = 0;
//	cl_device_id *device = NULL;

	// retrieve devices
	cl_platform_id platform;
	err = clGetPlatformIDs(1, &platform, NULL);
	cl_device_id device;
	err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);

	size_t localDim  = 256l;
	size_t globalDim = localDim*y;
/*	
	device = (cl_device_id *)malloc(ciDeviceCount * sizeof(cl_device_id) );
	err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, ciDeviceCount, device, NULL);
	if (err != CL_SUCCESS)
	{
		printf("Failed to get devices:\n%s\n", oclErrorString(err));
		return -1;
	}
	*/
	//Create the context
	cl_context context1 = clCreateContext(0, 1, &device, NULL, NULL, &err);
	if(err != CL_SUCCESS)
	{
		printf("Context creation failed:\n%d\n", err);
		return -1;
	}										 

	// create a command queue for first device the context reported
	cl_command_queue queue = clCreateCommandQueue(context1, device, 0, 0);
	
	// load program from disk
	char *tmp = strdup(argv[0]);
	char* my_dir = dirname(tmp);

//	size_t program_length;
	char path[256];
  	snprintf(path, PATH_MAX - 1, "%s/vecops.cl", my_dir);
 
	cl_program vecops = load_kernel(path, context1);

	if(err != CL_SUCCESS)
	{
		printf("Program creation failed:\n%d\n", (err));
		return -1;
	}
 
	err = clBuildProgram(vecops, 0, NULL, "-I.", NULL, NULL);
	if(err != CL_SUCCESS)
	{
			err = clGetProgramBuildInfo(vecops, device, CL_PROGRAM_BUILD_LOG, 8192, buffer, NULL);
			if(err != CL_SUCCESS)
				printf("Cannot get build info: %d\n", (err));

			printf("Build log:\n%s\n", buffer);
	}
	
	// create kernel
	cl_kernel sparsedot_kernel;
	
#if version == 1
	sparsedot_kernel = clCreateKernel(vecops, "sparsedot1_kernel", &err);
#endif
#if version == 2
	sparsedot_kernel = clCreateKernel(vecops, "sparsedot4_kernel", &err);
#endif
#if version == 3
	sparsedot_kernel = clCreateKernel(vecops, "sparsedot3_kernel", &err);
#endif
	if (err != CL_SUCCESS)
	{
		printf("Kernel creation failed:\n%d\n", (err));
		return -1;
	}
	
	 
	// allocate memory on the devices
	cl_mem px_d, py_d, result_d, pyLength_d;
	
#if version == 1
	px_d = clCreateBuffer(context1,
							 CL_MEM_READ_ONLY,
							 (x+1) * sizeof(struct svm_node),
							 0, &err);
#endif
#if version == 2 || version == 3
	//unpack px
	int size = px[x-1].index+1;

	for(size_t i = 0; i < y; ++i)
		size = size > pyLength[i] ? size : pyLength[i];

	dtype* px_u = (dtype*)calloc(size, sizeof(dtype));

	unpack(px, px_u);
	printf("px size: %d\n", size);
#endif
#if version == 3
	size_t height, width;
	clGetDeviceInfo(device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof(size_t), &height, 0);
	clGetDeviceInfo(Device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t), &width, 0);

	size_t region[3];
	region[2] = 1;

	region[0] = min(4, size);
	region[1] = (size+2-1) / 4;
		

	cl_image_format px_format;
	px_format.image_channel_order = CL_R;
	px_format.image_channel_data_type = CL_FLOAT;
#endif
#if version == 2
	px_d = clCreateBuffer(context1,
				 CL_MEM_READ_ONLY,
				 size * sizeof(dtype),
				 0, &err);
#endif
#if version == 3
	 px_d = clCreateImage2D(context1, CL_MEM_READ_ONLY, &px_format,
				  region[0], region[1], 0, 0, &err);

#endif
	if(err != CL_SUCCESS)
	{
		printf("Failed to allocate px:\n%d\n", (err));
		return -1;
	}
	py_d = clCreateBuffer(context1,
		 CL_MEM_READ_ONLY,
		 (x+1) * y * sizeof(struct svm_node),
		 0, &err);
	if(err != CL_SUCCESS)
	{
		printf("Failed to allocate px:\n%d\n", (err));
		return -1;
	}
	result_d = clCreateBuffer(context1,
		CL_MEM_WRITE_ONLY,
		y * sizeof(dtype),
		0, 0);
	pyLength_d = clCreateBuffer(context1,
		CL_MEM_READ_ONLY,
		y * sizeof(int),
		0, 0);
	

#if bench
	//start time measurement
	start_timer(0);
#endif

	// copy host vectors to device
	err = CL_SUCCESS;
   
    err |= clEnqueueWriteBuffer(queue, py_d, CL_FALSE, 0, 
								(x+1) * y * sizeof(struct svm_node), py, 0, NULL, NULL);
									

	err |= clEnqueueWriteBuffer(queue, pyLength_d, CL_FALSE, 0, 
								y * sizeof(int), pyLength, 0, NULL, NULL);

#if version == 1
	err |= clEnqueueWriteBuffer(queue, px_d, CL_FALSE, 0, 
								(x+1) * sizeof(struct svm_node), px, 0, NULL, NULL);
#endif
#if version == 2
	err |= clEnqueueWriteBuffer(queue, px_d, CL_FALSE, 0, 
								size * sizeof(dtype), px_u, 0, NULL, NULL);
#endif
#if version == 3
	size_t offset[] = {0,0,0};
	err |= clEnqueueWriteImage(queue, px_d, CL_TRUE, offset, region, sizeof(dtype), 0, 
							   px_u, 0, 0, NULL);
#endif
	clFinish(queue);

	 
	if(err != CL_SUCCESS)
	{
		printf("Data transfer to GPU failed:\n%d\n", (err));
		return -1;
	}

#if bench
	stop_timer(0);
	start_timer(1);
#endif
	// set kernel arguments

	clSetKernelArg(sparsedot_kernel, 0, sizeof(cl_mem), (void *) &px_d);
	clSetKernelArg(sparsedot_kernel, 1, sizeof(cl_mem), (void *) &py_d);
	clSetKernelArg(sparsedot_kernel, 2, sizeof(cl_mem), (void *) &result_d);
	clSetKernelArg(sparsedot_kernel, 3, sizeof(cl_mem), (void *) &pyLength_d);
	clSetKernelArg(sparsedot_kernel, 4, sizeof(cl_ulong), (void *) &x);
	clSetKernelArg(sparsedot_kernel, 5, sizeof(cl_ulong), (void *) &y);
//	clSetKernelArg(sparsedot_kernel, 6, sizeof(cl_float8)*localDim, 0);
#if version == 3
		clSetKernelArg(sparsedot_kernel, 7, sizeof(cl_long), (void *) &region[1]) ;		
		clSetKernelArg(sparsedot_kernel, 8, sizeof(cl_long), (void *) &region[0]) ;		
#endif
	clFlush(queue);

	// start kernel
	err = clEnqueueNDRangeKernel(queue, sparsedot_kernel, 1, 0, &globalDim, &localDim,
					   0, NULL, 0);

	if(err != CL_SUCCESS)
	{
		printf("Kernel launch failed:\n%d\n", (err));
		return -1;
	}

	clFinish(queue);
	
#if bench	
	stop_timer(1);
	start_timer(2);
#endif

	cl_event result_gather;
	 
	// Non-blocking copy of result from device to host
	err = clEnqueueReadBuffer(queue, result_d, CL_FALSE, 0, y * sizeof(dtype), 
						result, 0, NULL, &result_gather);
	
	if(err != CL_SUCCESS)
	{
		printf("Reading result failed:\n%d\n", (err));
		return -1;
	}

	// CPU sync with GPU
	clWaitForEvents(1, &result_gather);

#if bench	
	// stop GPU time measurement
	stop_timer(2);
#endif
	//check result
/*	for(size_t i = 0; i < y; ++i)
	{
		printf("%f ", result[i]);
	}
	printf("\n");
  */  

#if bench
	start_timer(3);
#endif
	bool correct = validate(px, py, result, x, y);
#if bench
	stop_timer(3);
	printf("v%i; x: %lu, y: %lu\n", version, x, y);
	printf("CPU: %f, upcpy: %f DeviceCalc: %f, downcpy: %f\n", 
		   get_secs(3), get_secs(0), get_secs(1), get_secs(2));
#endif
	
	if(correct)
		printf("SUCCESS!\n");
		
	//cleenup

	clReleaseKernel(sparsedot_kernel);
	clReleaseCommandQueue(queue);
	clReleaseEvent(result_gather);
	clReleaseMemObject(px_d);
	clReleaseMemObject(py_d);
	clReleaseMemObject(result_d);
	clReleaseMemObject(pyLength_d);
//	clReleaseDevice(device);

	free(px);
#if version == 2 || version == 3
	free(px_u);
#endif
	free(py);
	free(result);

	return 0;
}
示例#25
0
文件: tsolve.c 项目: pacew/tennis
int
main (int argc, char **argv)
{
	struct experiment_params params;
	double dx, dy;
	double speed, angle;
	gsl_vector *solution;
	int c;
	double compute_start, delta;

	while ((c = getopt (argc, argv, "v")) != EOF) {
		switch (c) {
		case 'v':
			vflag = 1;
			break;
		default:
			usage ();
		}
	}


	memset (&params, 0, sizeof params);
	params.observed_hit[0] = 0;
	params.observed_hit[1] = 0;
	params.observed_hit[2] = 1;
	params.observed_bounce[0] = 25;
	params.observed_bounce[1] = 0;
	params.observed_bounce[2] = 0;
	params.observed_secs = 1.359;

	dx = params.observed_bounce[0] - params.observed_hit[0];
	dy = params.observed_bounce[1] - params.observed_hit[1];
	params.observed_dist = hypot (dy, dx);

	params.simulator_dimen = 4;

	params.odesys.function = sim_func;
	params.odesys.dimension = params.simulator_dimen;
	params.odesys.params = &params;

	params.stepper = gsl_odeiv_step_alloc (gsl_odeiv_step_rk8pd,
					       params.simulator_dimen);
	params.controller = gsl_odeiv_control_y_new (1e-6, 0.0);
	params.evolver = gsl_odeiv_evolve_alloc (params.simulator_dimen);
	
	params.minimizer_dimen = 2;
	params.starting_point = gsl_vector_alloc (params.minimizer_dimen);

	params.minimizer_step_sizes = gsl_vector_alloc (params.minimizer_dimen);
	params.minimizer = gsl_multimin_fminimizer_alloc
		(gsl_multimin_fminimizer_nmsimplex2, params.minimizer_dimen);

       gsl_vector_set_all (params.starting_point, 0);
       gsl_vector_set_all (params.minimizer_step_sizes, 1.0);
     
	if (0) {
		graph_error_func (&params);
	}

	compute_start = get_secs ();
	solve_by_simulation (&params);
	delta = get_secs () - compute_start;

	solution = gsl_multimin_fminimizer_x (params.minimizer);

	speed = gsl_vector_get (solution, 0);
	angle = gsl_vector_get (solution, 1);

	printf ("speed = %8.3f angle = %8.3f; compute time %.3fms\n",
		speed * METERS_PER_SEC_TO_MPH,
		RTOD (angle),
		delta * 1000);

	return (0);
}
示例#26
0
void
old_grounded_moving (void)
{
	double now, dt;
	struct pt newpos;
	struct groundtri *gt;
	struct vect v1, v2, ctrl_vel;

	now = get_secs ();
	dt = now - player.lasttime;

	ctrl_vel.x = 0;
	ctrl_vel.y = 0;
	ctrl_vel.z = 0;

	if (player.p.z <= ground_height (&player.p)) {
		switch (player.moving) {
		case BACK:
			ctrl_vel.x = -.3 * player.speed * cos (player.theta);
			ctrl_vel.y = -.3 * player.speed * sin (player.theta);
			break;
		case FORW:
			ctrl_vel.x = player.speed * cos (player.theta);
			ctrl_vel.y = player.speed * sin (player.theta);
			break;
		case SIDE_Q:
			ctrl_vel.x = player.speed
				* cos (player.theta + DTOR (90));
			ctrl_vel.y = player.speed
				* sin (player.theta + DTOR (90));
			break;
		case SIDE_E:
			ctrl_vel.x = player.speed
				* cos (player.theta - DTOR (90));
			ctrl_vel.y = player.speed
				* sin (player.theta - DTOR (90));
			break;
		case FORW_Q:
			ctrl_vel.x = player.speed
				* cos (player.theta + DTOR (45));
			ctrl_vel.y = player.speed
				* sin (player.theta + DTOR (45));
			break;
		case FORW_E:
			ctrl_vel.x = player.speed
				* cos (player.theta - DTOR (45));
			ctrl_vel.y = player.speed
				* sin (player.theta - DTOR (45));
			break;
		case BACK_Q:
			ctrl_vel.x = -.3 * player.speed
				* cos (player.theta - DTOR (45));
			ctrl_vel.y = -.3 * player.speed
				* sin (player.theta - DTOR (45));
			break;
		case BACK_E:
			ctrl_vel.x = -.3 * player.speed
				* cos (player.theta + DTOR (45));
			ctrl_vel.y = -.3 * player.speed
				* sin (player.theta + DTOR (45));
			break;
		}
	}

	newpos.x = player.p.x + ctrl_vel.x * dt;
	newpos.y = player.p.y + ctrl_vel.y * dt;
	newpos.z = player.p.z + ctrl_vel.z * dt;

	psub (&player.vel, &newpos, &player.p);
	vscal (&player.vel, &player.vel, 1 / dt);

	if ((gt = detect_plane (&newpos)) == NULL) {
		printf ("Can't find ground, moving player to start position "
			"to avoid seg fault\n");
		player.p.x = 0;
		player.p.y = 0;
		player.p.z = ground_height (&player.p);
		player.movtype = GROUNDED;
		player.loc = detect_plane (&player.p);
		return;
	}

	if (gt->theta < DTOR (50)) {
		newpos.z = ground_height (&newpos);
		psub (&player.vel, &newpos, &player.p);
		vscal (&player.vel, &player.vel, 1 / dt);
		player.p = newpos;
	} else if (newpos.z > gt->pl.middle.z) {
		player.p = newpos;
		player.movtype = FALLING;
		printf ("switching to falling\n");
	} else if (newpos.z <= gt->pl.middle.z) {
		if (gt == player.loc) {
			printf ("line %d: some sort of bug caused"
				" player to be in grounded mode while"
				" on steep plane\n", __LINE__);
			pt_on_z_plane (&newpos, &newpos, &player.loc->pl);
			player.p = newpos;
			printf ("shifting player to nearest point on"
				" plane with same z\n");
			vset (&player.vel, 0, 0, 0);
			printf ("killing player velocity\n");
			player.movtype = FALLING;
			printf ("switching to falling\n");
			paused = YES;
			printf ("paused game\n");
			return;
		}

		vnorm (&v2, &ctrl_vel);
		vcross (&v1, &player.loc->normv, &gt->normv);
		vnorm (&v1, &v1);
		if (vdot (&v1, &v2) < 0)
			vscal (&v1, &v1, -1);
		vscal (&ctrl_vel, &v1, vdot (&ctrl_vel, &v1));
			
		newpos.x = player.p.x + ctrl_vel.x * dt;
		newpos.y = player.p.y + ctrl_vel.y * dt;
		newpos.z = player.p.z + ctrl_vel.z * dt;
			
		if ((gt = detect_plane (&newpos)) == NULL) {
			printf ("can't detect plane, exiting\n");
			exit (1);
		}
			
		if (gt->theta < DTOR (50)) {
			newpos.z = ground_height (&newpos);
			psub (&player.vel, &newpos, &player.p);
			vscal (&player.vel, &player.vel, 1 / dt);
				
			printf ("bar\n");

			player.p = newpos;
		} else {
//			struct groundtri *first_col, *last_col, *gt2;
//			int i;

			//PLAYER STARTS MOVING INTO GROUND BEFORE THIS EVER
			//HAPPENS

			printf ("pausing\n");
			paused = YES;

			/* i = 0; */
			/* first_col = NULL; */
			/* last_col = NULL; */

			/* for (gt2 = first_groundtri; gt2; gt2 = gt2->next) { */
			/* 	if (detect_collision (&player.p, */
			/* 			      &newpos, gt2)) { */
			/* 		if (first_col == NULL) { */
			/* 			first_col = gt2; */
			/* 		} else { */
			/* 			last_col->next = gt2; */
			/* 		} */
			/* 		last_col = gt2; */

			/* 		i++; */
			/* 	} */
			/* } */

			/* if (first_col == NULL) { */
			/* 	printf ("%s: %s: %d: something weird\n", */
			/* 		__FILE__, __FUNCTION__, __LINE__); */
			/* } */
			
			/* printf ("number of collisions: %d\n", i); */



			//TRY PROJECTING PLAYER TO LINE OF INTERSECTION
			//BETWEEN OLD & NEW PLANES. IF PLAYER IS STILL
			//INSIDE ANY PLANES, GRAB THREE PLANES AND GO
			//TO INTERSECTION POINT OF THOSE. MIGHT WANT
			//TO CHECK FOR OVER THREE PLANES JUST IN CASE

/* 			printf ("player.z: %g\n", player.p.z); */
/* 			newpos = player.p; */
/* 			rnd_escape (&newpos, gt); */
/* 			printf ("ran rnd_escape\n"); */
/* 			gt = detect_plane (&newpos); */


/* 			double b2; */
/* 			b2 = newpos.y - (gt->pl.b / gt->pl.a) * newpos.x; */
/* 			newpos.x = -(b2 * gt->pl.a * gt->pl.b */
/* 				     + gt->pl.c * newpos.z * gt->pl.a */
/* 				     + gt->pl.d * gt->pl.a) */
/* 				/ (square (gt->pl.a) + square (gt->pl.b)); */
/* 			newpos.y = (gt->pl.b / gt->pl.a) * newpos.x + b2; */
/* 			player.p = newpos; */
/* 			printf ("shifting player to nearest point on" */
/* 				" plane with same z\n"); */


/* 			gt = detect_plane (&player.p); */
/* 			printf ("re detected plane\n"); */
/* 			if (gt->theta >= DTOR (50)) { */
/* 				printf ("rnd_escape put player on slope," */
/* 					" changing player to falling\n"); */
/* 				player.movtype = FALLING; */
/* 				printf ("killing velocity\n"); */
/* 				vset (&player.vel, 0, 0, 0); */
/* 			} */
/* 			printf ("player.z: %g\n", player.p.z); */
		}
	} else {
		printf ("this should never be printed\n");
	}
}