Пример #1
0
int main()
{
	Model* m = new Model();
	View* v = new CSLView();
	//View* v = new SFMLView();

	v->setModel(m);

	while(v->run())
	{
		v->update();

		v->draw();
	}

	delete v;
	delete m;
	
	return EXIT_SUCCESS;
}
Пример #2
0
int main(int argc, char **argv) {

    // initialize the world with particle2 file?
    if (argc>1) {
	char *file = argv[1];
	if (!strcmp(file, "."))
	    particle2_read_stream(stdin, "stdin");
	else {
	    FILE *fp;
	    fp = fopen(file, "r");
	    if (fp==NULL) {
		fprintf(stderr, "Can't open particle file %s\n", file);
		exit(1);
	    }
	    particle2_read_stream(fp, file);
	    fclose(fp);
	}
    }

    /* initialize Xforms */
    fl_initialize(&argc, argv, "n-body", 0, 0);
    ui = create_form_nbody();

    // draw the UI window
    fl_show_form(ui->nbody,  	/* form */
	FL_PLACE_SIZE,   /* pos & size flags */
	FL_FULLBORDER,   /* border flags */
	argv[0]          /* window name */
    );

    // set up so we can draw in the big window of "ui" using OpenGL
    // set bits-per-pixel and other attributes of window

    // attributes for RGBA, without z-buffer, single-buffered
    // int single_attrs[] =
    //	{GLX_RGBA, GLX_DEPTH_SIZE, 0, GLX_RED_SIZE, 4, None};

    // attributes for RGBA, without z-buffer, double-buffered
    int double_attrs[] =
	{GLX_RGBA, GLX_DEPTH_SIZE, 0, GLX_RED_SIZE, 4, GLX_DOUBLEBUFFER, None};

    glxf_bind_pane(&pane,
	ui->pane,		// window
	double_attrs,		// requested attributes for window
	handle_event		// event_handler for window
    );

    // set viewport for OpenGL
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0, 1, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    // leave matrix mode at MODELVIEW so that later transformations
    // (glScale, glTranslate) go into that matrix

    // save info about current transformations
    view.update();

    // disable z-buffer and lighting
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);

    dt_proc(ui->dt_slider, 0);	// initialize world.timestep

    world.draw();		// draw the world (clear the screen)

    // Xforms handles events until exit button is pushed

    // the following loop repeats continuously when the world is "running",
    // otherwise it sleeps in fl_do_forms() waiting for mouse motion or other
    // Xwindows events, so as to not eat up unneeded CPU time

    // We have callback procedures set up for the sliders, but not for
    // the run and exit buttons.
    // fl_check_forms() and fl_do_forms() return only when an event occurs
    // (button, slider, mouse motion...) for which no callback procedure has
    // been set.
    // Therefore, control returns here when the run or exit buttons is hit.

    FL_OBJECT *obj;
    do {
	if (world.running) {		// if running:
	    world.step(world.timestep);	// take a time step
	    world.draw();		// redraw
	    obj = fl_check_forms();	// do a (non-blocking) check for events
	}
	else				// if not running:
	    obj = fl_do_forms();	// sleep until next event comes

	if (obj == ui->run_button) {
	    // user just hit the run button; toggle it
	    world.running = !world.running;
	}
    } while (obj != ui->exit_button);	// keep going until exit button is hit

    return 0;
}
Пример #3
0
void handle_event(XEvent *event) {
    /* see /usr/include/X11/X.h for definitions of X Windows event types */

    // you won't want to leave these printf's in here for your finished program,
    // but they should help you get started and debug.

    static int xprev, yprev, xstart, ystart;

    switch (event->type) {
	case ButtonPress:    /* button press */
	    printf("  ButtonPress.  button %d at (%d, %d)\n",
		event->xbutton.button, event->xbutton.x, event->xbutton.y);
	    switch (event->xbutton.button) {
		case 1:
		    world.curp = world.pick_particle(event->xbutton.x,
			event->xbutton.y, view);
		    if (!world.curp) break;
		    if (is_key_pressed(pane.display, XK_Shift_L) ||
		        is_key_pressed(pane.display, XK_Shift_R)) {
			    world.delete_particle(world.curp);
			    world.curp = NULL;
			}
		    else
			fl_set_slider_value(ui->mass_slider, world.curp->mass);
			    // set slider to show the mass of this particle
		    break;
		case 2:
		    {
			Vec2 p = view.screen_to_world(event->xbutton.x,
			    event->xbutton.y);
			world.curp = world.insert_particle(p[0], p[1], 0, 0,
			    fl_get_slider_value(ui->mass_slider));
		    }
		    break;
		case 3:
		    // save these for later use when MotionNotify event comes
		    xstart = xprev = event->xbutton.x;
		    ystart = yprev = event->xbutton.y;
		    break;
	    }
	    world.draw();
	    break;

	case ButtonRelease:  /* button release */
	    printf("  ButtonRelease. button %d\n", event->xbutton.button);
	    break;

	case MotionNotify:   /* mouse pointer moved */
	    printf("  MotionNotify at (%d, %d) state=%d=(%s %s %s)\n",
		event->xmotion.x, event->xmotion.y,
		event->xmotion.state,
		event->xmotion.state&Button1Mask ? "button1" : ".",
		event->xmotion.state&Button2Mask ? "button2" : ".",
		event->xmotion.state&Button3Mask ? "button3" : ".");
	    if (world.curp && (
		event->xmotion.state & (Button1Mask|Button2Mask))) {
		    /* printf("moving particle %d\n", world.curp-world.pt); */
		    world.curp->pos = view.screen_to_world(event->xmotion.x,
			event->xmotion.y);
	    }
	    else if (event->xmotion.state & Button3Mask) {
		if (is_key_pressed(pane.display, XK_Shift_L) ||
		    is_key_pressed(pane.display, XK_Shift_R)) {
			float s = 1. + (event->xmotion.x - xprev)/100.;
			/* printf("s=%g\n", s); */
			Vec2 pos = view.screen_to_world(xstart, ystart);
			// scale view around the point where user moused down
			glTranslatef(pos[0], pos[1], 0.);
			glScalef(s, s, 1.);
			glTranslatef(-pos[0], -pos[1], 0.);
		}
		else {
		    float t = view.viewport.width*view.relscale();
		    glTranslatef((event->xmotion.x - xprev)/t,
			-(event->xmotion.y - yprev)/t, 0.);
		}
		view.update();
		xprev = event->xmotion.x;
		yprev = event->xmotion.y;
	    }
	    world.draw();
	    break;

	case EnterNotify:       /* mouse pointer entered window */
	    printf("  EnterNotify.\n");
	    break;

	case LeaveNotify:       /* mouse pointer left window */
	    printf("  LeaveNotify.\n");
	    break;

	case KeyPress:          /* key pressed */
	case KeyRelease:        /* key released */
	    {
		char buf[10];
		int n;
		KeySym keysym;

		printf(event->type==KeyPress ? "  KeyPress." : "  KeyRelease.");
		n = XLookupString(&event->xkey, buf, sizeof buf - 1,
		    &keysym, 0);
		buf[n] = 0;
		/* print key as ascii and in hexadecimal */
		/* see /usr/include/X11/keysymdef.h for key codes */
		printf(" key=(%c)=0x%x\n", keysym, keysym);
	    }
	    break;

	case Expose:            /* window just uncovered */
	    printf("  Expose.\n");
	    world.draw();
	    break;

	case VisibilityNotify:  /* window occluded or exposed */
	    printf("  VisibilityNotify.\n");
	    break;

	default:
	    printf("  unknown event type.\n");
	    break;
    }
}