void ioDestroy(void) {
	if (eventThreadID != -1) {
		threadkill(eventThreadID);
	}
	closemouse(mousectl);
	closekeyboard(keyboardctl);
}
Example #2
0
int main(void)
{
    int     count = 0;
    int     G_pressed = 0, R_pressed = 1;
    char    vstring[10], wstring[10];
    double  x1_old, y1_old;
    double  x2_old, y2_old;
    double  x1_new, y1_new;
    double  x2_new, y2_new;
    double  vx, vy, wx, wy;
    double  v, w;
    posvel  pv;

    x1_old = 100.0;
    y1_old = 100.0;
    vx = 10.0;
    vy = 5.0;


    x2_old = 250.0;
    y2_old = 250.0;
    wx = -8.0;
    wy = 4.0;

    v = sqrt(pow(wx,2)+pow(wy,2));
    w = sqrt(pow(vx,2)+pow(vy,2));


    /* open the graphics window */
    initwindow(XWINDOW, YWINDOW);

    /* allow mouse operations */
	initmouse();

    /* allow keyboard operations */
    initkeyboard();

    /* create an event queue */
    create_event_queue();

    /* register display, mouse and keyboard as event sources */
    reg_display_events();
    reg_mouse_events();
    reg_keyboard_events();

    /* initialize the font */
    initfont();

    outtextxy(4,5,"To quit press left mouse button or close graphics window");
    outtextxy(4,15,"To pause press right mouse button");
    outtextxy(4,25,"To change ball speed use arrow keys (up/down = red, left/right = blue)");
    outtextxy(4,35,"To make the red ball green press G or g ");

	do
	{

        if (check_if_event())
        {

            /* wait for event  */
            wait_for_event();

            if (event_close_display())
                break;
            else if (event_mouse_button_down())
            {
                if (event_mouse_left_button_down())
                    break;
                else if (event_mouse_right_button_down())
                    wait_for_event();
            }
            else if (event_key_down())
            { /* change speed of first ball */
                if(event_key_up_arrow())
                {
                    vx = 1.25*vx;
                    vy = 1.25*vy;
                }
                else if(event_key_down_arrow())
                {
                    vx = 0.75*vx;
                    vy = 0.75*vy;
                }
                else if(event_key_left_arrow())
                {
                    wx = 0.75*wx;
                    wy = 0.75*wy;
                }
                else if(event_key_right_arrow())
                {
                    wx = 1.25*wx;
                    wy = 1.25*wy;
                }
                if (event_key('G'))
                {
                    G_pressed = 1;
                    R_pressed = 0;
                }
                else if (event_key('R'))
                {
                    R_pressed = 1;
                    G_pressed = 0;
                }
            }
        }

        v = sqrt(pow(wx,2)+pow(wy,2));
        w = sqrt(pow(vx,2)+pow(vy,2));

	    /* calculate new ball positions */
        x1_new = x1_old + vx*TICK;
        y1_new = y1_old + vy*TICK;
        x2_new = x2_old + wx*TICK;
        y2_new = y2_old + wy*TICK;

        /* handle what to do if balls hit boundaries */
        pv = hit_boundary(x1_new, vx, 1);
        x1_new = pv.pos;
        vx = pv.vel;

        pv = hit_boundary(x2_new, wx, 1);
        x2_new = pv.pos;
        wx = pv.vel;

        pv = hit_boundary(y1_new, vy, 0);
        y1_new = pv.pos;
        vy = pv.vel;

        pv = hit_boundary(y2_new, wy, 0);
        y2_new = pv.pos;
        wy = pv.vel;

        /* draw balls on screen buffer in new positions */
        filled_circle(x1_new, y1_new, RADIUS , BLUE);
        if (G_pressed)
            filled_circle(x2_new, y2_new, RADIUS , GREEN);
        else if (R_pressed)
            filled_circle(x2_new, y2_new, RADIUS , RED);

        /* make the balls visible on the screen display
           and remove the balls in the previous positions */
	    sprintf(vstring, "%4.2lf", v);
	    sprintf(wstring, "%4.2lf", w);
        setcolor(RED);
        outtextxy(VEL_TEXT_X,VEL_TEXT_Y,"v =  ");
        /* clear area for numeric output */
        filled_rectangle(VEL_TEXT_X+40, VEL_TEXT_Y-10, VEL_TEXT_X+100, VEL_TEXT_Y+20, BLACK);
        outtextxy(VEL_TEXT_X+60,VEL_TEXT_Y,vstring);
        setcolor(BLUE);
        outtextxy(VEL_TEXT_X+120,VEL_TEXT_Y,"w =  ");
        /* clear area for numeric output */
        filled_rectangle(VEL_TEXT_X+160, VEL_TEXT_Y-10, VEL_TEXT_X+220, VEL_TEXT_Y+20, BLACK);
        outtextxy(VEL_TEXT_X+180,VEL_TEXT_Y,wstring);

        update_display();

        /* remove the balls in the previous positions on
           the screen buffer */
        filled_circle(x1_old, y1_old, RADIUS , BLACK);
        filled_circle(x2_old, y2_old, RADIUS , BLACK);

        /* update the old positions */
        x1_old = x1_new;
        y1_old = y1_new;
        x2_old = x2_new;
        y2_old = y2_new;

        count++;
        pausefor(8); /* wait 8 miliseconds */


	}
    while (count < MAXCOUNT);

    /* close the mouse */
    closemouse();


    /* remove the display */
    closegraph();


    return 0;
}