Beispiel #1
0
/*
 * performActions()
 *  Evaluate all keyboard and mouse events
 * Parameters:
 *  char* - data from device
 * Returns:
 *  -none-
 */
void performActions(char *data)
{

	/* Mouse joystick */
	mouse_move(IS_JOYSTICK_UP(data), IS_JOYSTICK_DOWN(data),
	IS_JOYSTICK_LEFT(data), IS_JOYSTICK_RIGHT(data));

	/* Mouse clicks */
	mouse_click(IS_BUTTON_3(data), &click_mode_left, 1);
	mouse_click(IS_BUTTON_4(data), &click_mode_right, 3);

	/* Keys events */
	keyboard_press(IS_CURSOR_UP(data), &key_up, "Up");
	keyboard_press(IS_CURSOR_DOWN(data), &key_down, "Down");
	keyboard_press(IS_CURSOR_LEFT(data), &key_left, "Left");
	keyboard_press(IS_CURSOR_RIGHT(data), &key_right, "Right");

	keyboard_press(IS_BUTTON_6(data), &key_pprior, "Prior");
	keyboard_press(IS_BUTTON_8(data), &key_pnext, "Next");

	keyboard_press(IS_BUTTON_1(data), &key_return, "Return");
	keyboard_press(IS_BUTTON_2(data), &key_escape, "Escape");

	/* Keys speed config */
	mouse_speed(IS_BUTTON_5(data), &speed_decrement, 1, "Increment");
	mouse_speed(IS_BUTTON_7(data), &speed_increment, -1, "Decrement");
}
Beispiel #2
0
void check_mouse(XEvent *e)
{
    static int savex = 0;
    static int savey = 0;
    //Log("check_mouse()...\n");
    int x,y;
    int i;
    lbutton=0;
    rbutton=0;
    if (e->type == ButtonRelease) {
        mouse_click(2);
        return;
    }
    if (e->type == ButtonPress) {
        if (e->xbutton.button==1) {
            //Left button is down
            lbutton=1;
        }
        if (e->xbutton.button==3) {
            //Right button is down
            rbutton=1;
        }
    }
    //Log("e->xbutton.x: %i e->xbutton.y: %i\n",e->xbutton.x,e->xbutton.y);
    x = e->xbutton.x;
    y = e->xbutton.y;
    //reverse the y position
    y = yres - y;
    if (savex != e->xbutton.x || savey != e->xbutton.y) {
        //Mouse moved
        savex = e->xbutton.x;
        savey = e->xbutton.y;
    }
    //Log("xy: %i %i\n",x,y);
    if (x == savex && y == savey) {
        return;
    }
    savex=x;
    savey=y;

    for (i=0; i<nbuttons; i++) {
        button[i].over=0;
        button[i].down=0;
        //Log("xy: %i %i\n",x,y);
        if (x >= button[i].r.left &&
                x <= button[i].r.right &&
                y >= button[i].r.bot &&
                y <= button[i].r.top) {
            button[i].over=1;
            //Log("over button: %i\n", i);
            break;
        }
    }
    if (lbutton) {
        mouse_click(1);
    }
    if (rbutton) {
        mouse_click(1);
    }
}
Beispiel #3
0
int l_mouse_button_click(lua_State* L)
{
	int button = luaL_checkint(L, 1);
	int state = lua_toboolean(L, 2);
	mouse_click(button, state);
	return 0;
}
Beispiel #4
0
/** Process gui events and deliver them as required. 
 \todo This should probably limit how frequently it calls draw 
 render thread
 */
void sdl_master::process_events()
{
	bool done = false;
	
	SDL_MouseMotionEvent mouse_position;
	mouse_position.state = 0;
	mouse_position.x = 0;
	mouse_position.y = 0;
	
	add_draw_timer(50);	//20 fps
	
	SDL_Event event;
	while(!done)
	{
		if (SDL_WaitEvent(&event))
		{ //wait on an event
			switch(event.type)
			{ //Check event type
				case SDL_USEREVENT:
					switch (event.user.code)
					{
						case 0:
							draw();
							break;
						default:
							break;
					}
					break;
				case SDL_MOUSEMOTION:
					mouse_move(&mouse_position, &event.motion);
					mouse_position = event.motion;
					break;
				case SDL_MOUSEBUTTONDOWN:
				case SDL_MOUSEBUTTONUP:
					mouse_click(&event.button);
					break;
				case SDL_KEYDOWN:
				case SDL_KEYUP:
					key_press(&event.key);
					break;
				case SDL_QUIT:
					quit_request();
					break;
				case SDL_VIDEORESIZE: //User resized window
					display = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,
						SDL_HWSURFACE | SDL_DOUBLEBUF); // Create new window
					break;
			}
		}
		done = check_users(done);
	}
//	int status;
//	if (game_client[0] != 0)
//		SDL_WaitThread(game_client[0], &status);
}
Beispiel #5
0
/*
 * Generate a regular click event (both up and down events)
 *
 * You can optionally specify a point to click; the mouse cursor will
 * instantly jump to the given point; otherwise the click event happens
 * at the current cursor position.
 *
 * @overload click()
 *   @return [CGPoint]
 * @overload click()
 *   @param point [CGPoint]
 *   @return [CGPoint]
 */
static
VALUE
rb_mouse_click(const int argc, VALUE* const argv, UNUSED const VALUE self)
{
    switch (argc) {
    case 0:
        mouse_click();
        break;
    case 1:
    default:
        mouse_click2(rb_mouse_unwrap_point(argv[0]));
    }

    return CURRENT_POSITION;
}
Beispiel #6
0
void main_page(int *choice)
{
	CAR cp1;
	MAP mp;
	mp.map_xspeed=0;mp.map_yspeed=0;
	cp1.angle=0;cp1.xspeed=0;cp1.yspeed=0;
	double  speed=0;
	double map_speed=0;
	int i=0;                        
	int flag_click_beg=0;    //the three flags are used to judge if the "Start","Finish","Begin" button is pressed
	double distance=0;                //此距离要根据选择路径的函数来实时反馈
	int start_point=-1, finish_point=-1;
	int  center_x;
	int  center_y;
	int flag_judge_finish=0;
	int flag_road_error=0;
	int map_first=0;  //to judge  car  whether it is out of bounds(150,100,400,250)  
	double p_distance[SPOTNUM][SPOTNUM];
	double d[SPOTNUM];
	int path[SPOTNUM];
	POINT point[SPOTNUM];
	STREET all_street[18];
   	LITTLE all_ninebuilding[3];
	LITTLE all_small_door[5];
	LITTLE all_tree[10];
	LITTLE all_mpeople[5];
	LITTLE all_wpeople[5];
	LITTLE all_lake[3];
	LITTLE all_playground[3];
	LITTLE all_bigbuilding[5];
	LITTLE all_house[10];
	LITTLE bicycle[3];
	init_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
	init_point_info(point,p_distance);
	while(1)
	{
		flag_click_beg=0;
		flag_judge_finish=0;
		InitGra(350);
		setbkcolor(LIGHTGREEN);
		draw_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
		right_plus();
		 while(!(1==flag_click_beg && start_point>=0 && finish_point>=0))
		{
			switch(*choice)
			{
				case 0:
					search(point,&flag_click_beg,&start_point,&finish_point); 
				     break;
				case 1:
					mouse_ctrl(&flag_click_beg,&start_point,&finish_point);
					break;
				default:
					break;
			}
			cleardevice();
			draw_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
			mouse_click();
		}
		shortestpath(p_distance,SPOTNUM,d,path,start_point);
		distance=rest_distance(&cp1,point[finish_point]);
		center_x=340-point[start_point].x;
		center_y=170-point[start_point].y;
		second_init_map(&center_x, &center_y,point,all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
		cp1.x=point[start_point].x;
		cp1.y=point[start_point].y;
	     while(0==flag_judge_finish && 0==flag_road_error)
	    {
			while(0==flag_judge_finish && 0==flag_road_error)     //有没有完成一次导航的标志,如果完成就会跳出大循环
			{
				for(i=0;i<2;i++)
				{
					setactivepage(i);
				     cleardevice();
				     draw_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
				     drawroutine(start_point, finish_point,point,path); 					//画出最短的路径
					 car_control(&cp1,&speed);
				     map_ctrl(&mp,&cp1,&speed,&map_first,&map_speed,point,all_street,all_tree,all_mpeople,all_wpeople,
					              all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
					 distance=rest_distance(&cp1,point[finish_point]);
					 draw_car(&cp1);
				     mouse_click();
					 show_distance(&distance);
				/*======================判断小车所处位置=============================*/
				     judge_pre_car(&cp1,point);
				     change_route(&cp1, point, &start_point, &finish_point,path,&flag_road_error);
				     warning(&map_speed, &speed);
				     setvisualpage(i);
				     delay(15);
			     }
			    flag_judge_finish=judge_finish(&cp1,point[finish_point]);
		    }
	          if(1==flag_judge_finish)
		    {
			     distance=0;
				draw_bar1(494,57,636,78,LIGHTGRAY);
			     dis_hz(495,60,"到达目的地",0,20,YELLOW);
			     guidance_end(150,100);
			     cp1.angle=0;		
			     flag_click_beg=0;
			     speed=0;
			     map_speed=0; 			    //再次进入的时候,还是先判断车速
			     start_point=-1;
				finish_point=-1;
				map_first=0;
		     }
			if(1==flag_road_error)
		    {
				shortestpath(p_distance,SPOTNUM,d,path,start_point);
				draw_bar1(494,57,636,150,LIGHTGRAY);
			     dis_hz(495,60,"路线错误,正在",0,18,LIGHTRED);
				dis_hz(495,80,"为您重新查找……",0,18,LIGHTRED);
			     delay(1000);
			     draw_bar1(494,57,636,150,LIGHTGRAY);
			     flag_road_error=0;
		     }
	     }
	}
}
int cam() //calling main
{
    int hdims = 16;
    printf("I am main");
    CvCapture* capture = cvCreateCameraCapture(1); //determining usb camera
    CvHistogram *hist = 0;
    CvMemStorage* g_storage = NULL;
    Display *display=construct_display();
    int x,y, tmpx=0, tmpy=0, chk=0;
    IplImage* image=0;
    IplImage* lastimage1=0;
    IplImage* lastimage=0;
    IplImage* diffimage;
    IplImage* bitimage;
    IplImage* src=0,*hsv=0,*hue=0,*backproject=0;
    IplImage* hsv1=0,*hue1=0,*histimg=0,*frame=0,*edge=0;
    float* hranges;
    cvNamedWindow( "CA", CV_WINDOW_AUTOSIZE ); //display window 3
    //Calculation of Histogram//
    cvReleaseImage(&src);
    src= cvLoadImage("images/skin.jpg"); //taking patch
    while(1)
    {
        frame = cvQueryFrame( capture ); //taking frame by frame for image prcessing
        int j=0;
        float avgx=0;
        float avgy=0;
        if( !frame ) break;
        //#########################Background Substraction#########################//
        if(!image)
        {
            image=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
            bitimage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
            diffimage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
            lastimage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
        }
        cvCvtColor(frame,image,CV_BGR2GRAY);
        if(!lastimage1)
        {
            lastimage1=cvLoadImage("images/img.jpg");
        }
        cvCvtColor(lastimage1,lastimage,CV_BGR2GRAY);
        cvAbsDiff(image,lastimage,diffimage);
        cvThreshold(diffimage,bitimage,65,225,CV_THRESH_BINARY);
        cvInRangeS(bitimage,cvScalar(0),cvScalar(30),bitimage);
        cvSet(frame,cvScalar(0,0,0),bitimage);
        cvReleaseImage(&hsv);
        hsv= cvCreateImage( cvGetSize(src), 8, 3 );
        cvReleaseImage(&hue);
        hue= cvCreateImage( cvGetSize(src), 8, 1);
        cvCvtColor(src,hsv,CV_BGR2HSV);
        cvSplit(hsv,hue,0,0,0);
        float hranges_arr[] = {0,180};
        hranges = hranges_arr;
        hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
        cvCalcHist(&hue, hist, 0, 0 );
        cvThreshHist( hist, 100 );
        //#############################Display histogram##############################//
        cvReleaseImage(&histimg);
        histimg = cvCreateImage( cvSize(320,200), 8, 3 );
        cvZero( histimg );
        int bin_w = histimg->width / hdims;
        //#### Calculating the Probablity of Finding the skin with in-built method ###//
        if(0)
        {
            free (backproject);
            free (hsv1);
            free (hue1);
        }
        cvReleaseImage(&backproject);
        backproject= cvCreateImage( cvGetSize(frame), 8, 1 );
        cvReleaseImage(&hsv1);
        hsv1 = cvCreateImage( cvGetSize(frame), 8, 3);
        cvReleaseImage(&hue1);
        hue1 = cvCreateImage( cvGetSize(frame), 8, 1);
        cvCvtColor(frame,hsv1,CV_BGR2HSV);
        cvSplit(hsv1,hue1,0,0,0);
        cvCalcBackProject( &hue1, backproject, hist );
        cvSmooth(backproject,backproject,CV_GAUSSIAN);
        cvSmooth(backproject,backproject,CV_MEDIAN);
        if( g_storage == NULL )
        g_storage = cvCreateMemStorage(0);
        else
        cvClearMemStorage( g_storage );
        CvSeq* contours=0;
        CvSeq* result =0;
        cvFindContours(backproject, g_storage, &contours );
        if(contours)
        {
            result=cvApproxPoly(contours, sizeof(CvContour), g_storage,
            CV_POLY_APPROX_DP, 7, 1);
        }
        cvZero( backproject);
        for( ; result != 0; result = result->h_next )
        {
            double area = cvContourArea( result );
            cvDrawContours( backproject,result, CV_RGB(255,255, 255), CV_RGB(255,0, 255)
            , -1,CV_FILLED, 8 );
            for( int i=1; i<=result-> total; i++ )
            {
                if(i>=1 and abs(area)>300)
                {
                    CvPoint* p2 = CV_GET_SEQ_ELEM( CvPoint, result, i );
                    if(1)
                    {
                        avgx=avgx+p2->x;
                        avgy=avgy+p2->y;
                        j=j+1;
                        cvCircle(backproject,cvPoint(p2->x,p2->y ),10,
                        cvScalar(255,255,255));
                    }
                }
            }
        }
        cvCircle( backproject, cvPoint(avgx/j, avgy/j ), 40, cvScalar(255,255,255) );
        x = ( avgx/j );
        y = ( avgy/j );
        x=( (x*1240)/640 )-20;
        y=( (y*840)/480 )-20;
        if ( (abs(tmpx-x)>6 or abs(tmpy-y)>6 ) and j )
        {
            tmpx = x;
            tmpy = y;
            chk=0;
        }
        else chk++;
        mouse_move1( tmpx, tmpy, display );
        if ( chk==10 )
        {
            mouse_click( 5, 2, display );
            mouse_click( 5, 3, display );
        }
        cvSaveImage( "final.jpg", frame );
        cvSaveImage( "final1.jpg", backproject );
        cvShowImage( "CA", backproject );
        char c = cvWaitKey(33);
        if( c == 27 )
        break; //function break and destroying windows if press <escape> key
    }
    cvReleaseCapture( &capture );
    cvDestroyWindow( "CA" );
}
Beispiel #8
0
void Special_Label::mousePressEvent(QMouseEvent *ev)
{
    emit mouse_click();
}
Beispiel #9
0
int main(void)
{
   ALLEGRO_DISPLAY *display;
   ALLEGRO_TIMER *timer;
   ALLEGRO_EVENT_QUEUE *queue;
   int redraw = 0, i;
   bool quit = false;

   if (!al_init()) {
      abort_example("Could not initialise Allegro\n");
      return 1;
   }
   al_init_primitives_addon();
   al_install_mouse();
   al_init_image_addon();
   al_init_font_addon();
   al_init_ttf_addon();
   srand(time(NULL));

   white = al_map_rgba_f(1, 1, 1, 1);

   display = al_create_display(640, 480);
   if (!display) {
      abort_example("Could not create display\n");
      return 1;
   }
   al_set_window_title(display, "Allegro Logo Generator");
   al_install_keyboard();

   /* Read logo parameters from logo.ini (if it exists). */
   config = al_load_config_file("logo.ini");
   if (!config)
      config = al_create_config();
   for (i = 0; param_names[i]; i++) {
      char const *value = al_get_config_value(config, "logo", param_names[i]);
      if (value)
         strncpy(param_values[i], value, sizeof(param_values[i]));
   }

   font = al_load_font("data/DejaVuSans.ttf", 12, 0);
   if (!font) {
      abort_example("Could not load font\n");
      return 1;
   }

   timer = al_create_timer(1.0 / 60);

   queue = al_create_event_queue();
   al_register_event_source(queue, al_get_keyboard_event_source());
   al_register_event_source(queue, al_get_mouse_event_source());
   al_register_event_source(queue, al_get_display_event_source(display));
   al_register_event_source(queue, al_get_timer_event_source(timer));

   al_start_timer(timer);
   while (!quit) {
      ALLEGRO_EVENT event;
      al_wait_for_event(queue, &event);
      if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
         break;
      if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
         if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
            quit = true;
         }
         else if (event.keyboard.keycode == ALLEGRO_KEY_ENTER) {
            if (editing) {
               regenerate = true;
               editing = false;
            }
            else {
               cursor = 0;
               editing = true;
            }
         }
         else if (event.keyboard.keycode == ALLEGRO_KEY_UP) {
            if (selection > 0) {
               selection--;
               cursor = 0;
               editing = false;
            }
         }
         else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) {
            if (param_names[selection + 1]) {
               selection++;
               cursor = 0;
               editing = false;
            }
         }
         else {
            int c = event.keyboard.unichar;
            if (editing) {
               if (c >= 32) {
                  ALLEGRO_USTR *u = al_ustr_new(param_values[selection]);
                  al_ustr_set_chr(u, cursor, c);
                  cursor++;
                  al_ustr_set_chr(u, cursor, 0);
                  strncpy(param_values[selection], al_cstr(u),
                     sizeof param_values[selection]);
                  al_ustr_free(u);
               }
            }
            else {
               if (c == 'r')
                  randomize();
               if (c == 's')
                  save();
            }
         }
      }
      if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
         if (event.mouse.button == 1) {
            mouse_click(event.mouse.x, event.mouse.y);
         }
      }
      if (event.type == ALLEGRO_EVENT_TIMER)
         redraw++;

      if (redraw && al_is_event_queue_empty(queue)) {
         redraw = 0;

         render();

         al_flip_display();
      }
   }

   /* Write modified parameters back to logo.ini. */
   for (i = 0; param_names[i]; i++) {
      al_set_config_value(config, "logo", param_names[i],
         param_values[i]);
   }
   al_save_config_file("logo.ini", config);
   al_destroy_config(config);

   return 0;
}
Beispiel #10
0
void TEST_mouse_click() {
    char *routine = "TEST_mouse_click";
    printf(testing, routine);
    assert(mouse_click(1, 1));
    printf(done, routine);
}
Beispiel #11
0
int main(int argc, char *argv[])
{
    init();
    dbgoutf("Initialisation terminee! Chargement des planetes...");

    bool planets_loaded = false;
    if(argc > 1 && argv[1] && *argv[1] != '\0')
    {
        char *filename = argv[1];
        planets_file = newstring(argv[1]);
        dbgoutf("Lecture du fichier \"%s\"", filename);
        planets_loaded = load_planets(filename);
    }
    if(!planets_loaded)
    {
        planets_loaded = load_planets(DEFAULT_PLANETS_FILE);
        if(!planets_loaded)
        {
            dbgoutf("Impossible de charger les planetes. Fermeture du programme.");
            callback_quit();
            return EXIT_FAILURE;
        }
    }

    compute_thread = SDL_CreateThread(compute_thread_func, NULL);

    SDL_Event event;

    if(invecrange(planets, 0)) curplanet = planets[0];

    render_millis.maxfps = MAXFPS;
    compute_millis.maxfps = MAXCPS;

    // main loop

    for(;;)
    {
        render_millis.last = render_millis.current; 
        render_millis.current = SDL_GetTicks();
        render_millis.diff = render_millis.current-render_millis.last;

        SDL_PollEvent(&event);

        bool pressed = false;
        switch(event.type)
        {
            case SDL_KEYDOWN:
            {
                pressed = true; // a key has been pressed
                switch (event.key.keysym.sym)
                {
                    case SDLK_ESCAPE: callback_quit(); break;
                    case SDLK_s: start_pause(1); break;
                    case SDLK_p: start_pause(0); break;

                    case SDLK_7:
                    case SDLK_KP7: if(keyreleased) precision /= 1.1; break;

                    case SDLK_8:
                    case SDLK_KP8: if(keyreleased) precision *= 1.1; break;

                    case SDLK_F1: if(keyreleased) smoother(); break;

                    case SDLK_F2: if(keyreleased) harder(); break;

                    case SDLK_o: { if(keyreleased) save_planets(); } break;
                    case SDLK_F4: { if(keyreleased) save_datas(); } break;

                    //case SDLK_F11: { if((keyreleased || 1) && !print_frame) print_frame = true; } break;
                    case SDLK_F12: { char buf[64] = ""; sprintf(buf, "../images/screenshot_%d.bmp", render_millis.current); takeScreenshot(buf); } break;

                    case SDLK_KP_PLUS: if(keyreleased) zoom_in(); break;
                    case SDLK_KP_MINUS: if(keyreleased) zoom_out(); break;

                    case SDLK_KP0: if(keyreleased) skip_hists = !skip_hists; break;

                    case SDLK_F3: if(keyreleased) draw_area = !draw_area; break;
                    
                    case SDLK_q:
                    case SDLK_a: if(keyreleased) draw_accel = !draw_accel; break;
                    
                    case SDLK_f: SDL_WM_ToggleFullScreen(screen); break;

                    case SDLK_4:
                    case SDLK_KP4:
                        if(keyreleased) increase_speed();
                    break;

                    case SDLK_5:
                    case SDLK_KP5:
                        if(keyreleased) lower_speed(); break;
                    
                    case SDLK_RETURN: if(keyreleased) studied(); break;

                    case SDLK_t: if(keyreleased) curplanet->showhist = !curplanet->showhist; break;

                    case SDLK_1:
                    case SDLK_KP1:
                        if(keyreleased) prev_ref();
                    break;

                    case SDLK_2:
                    case SDLK_KP2:
                        if(keyreleased) next_ref();
                    break;
                    case SDLK_RIGHT:
                    {
                        angle.y -= 20.0*double(render_millis.diff)/1000.0;
                        angle.y = angle.y > 360 ? int(angle.y) % 360 : angle.y;
                    }
                    break;
                    case SDLK_LEFT:
                    {
                        angle.y += 20.0*double(render_millis.diff)/1000.0;
                        angle.y = angle.y > 360 ? int(angle.y) % 360 : angle.y;
                    }
                    case SDLK_UP:
                    {
                        angle.z += 20.0*double(render_millis.diff)/1000.0;
                        angle.z = angle.z > 360 ? int(angle.z) % 360 : angle.z;
                    }
                    break;
                    case SDLK_DOWN:
                    {
                        angle.z -= 20.0*double(render_millis.diff)/1000.0;
                        angle.z = angle.z > 360 ? int(angle.z) % 360 : angle.z;
                    }
                    default:
                    {
                        
                    }
                }
            }
            break;
            case SDL_QUIT: callback_quit(); break;
            case SDL_VIDEORESIZE:
            {
                w = event.resize.w;
                h = event.resize.h;
                glViewport(0, 0, w, h);
                start_pause(0);
            }
            case SDL_MOUSEBUTTONDOWN:
            {
                if(!mousedown && event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) mouse_click(event.button.button, event.button.x, h-event.button.y, true);
                if(event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) mousedown = true;
            }
            break;

            case SDL_MOUSEBUTTONUP:
            {
                if(event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT && mousedown)
                {
                    mousedown = false;
                    mouse_click(event.button.button, event.button.x, h-event.button.y, false);
                }
            }
            break;

            case SDL_MOUSEMOTION:
            {
                mouse_x = event.motion.x;
                mouse_y = h-event.motion.y;
            }
            break;
            
        }
        
        if(pressed && keyreleased) keyreleased = false;
        else if(!pressed && !keyreleased) keyreleased = true;
        limitfps(render_millis);
        
        render();
    }

    

    system("pause");
    callback_quit();
    return EXIT_SUCCESS;
}
Beispiel #12
0
int main(){

    //setup log file
    fpLog = fopen("log.txt", "w");
    if(fpLog == NULL){
        printf("Failed to open log file\n");
    }
    //seed random
    srand(time(NULL));

    //set up frame limiting
    clock_t s_cycle, e_cycle;
    s_cycle = clock();  //get amount of time to init game

    fprintf(fpLog, "Initializing...\n");    //log
    allegro_init_install(); //allegro inits

    Game game;
    game_init(&game);   //initialize game struct

    TMX_map map;
    load_map(&game, &map);  //load first map

    Sprite player, car;
    player_init(&game, &player, &map);   //initialize player struct
    car_init(&game, &car, &map, 32, 32, 5); //initialize car struct

    gen_npcs(&game, &map);  //generate and initialize npcs

    e_cycle = clock();
    //get length of initialization
    fprintf(fpLog, "Initialization done in %f seconds.\n",
            (double)(e_cycle - s_cycle) / 1000);

    //show instructions on how to play
    display_instructions(&game);

    //set active sprite
    game.active_sprite = &player;

    fprintf(fpLog, "Beginning main loop.\n");
    while(game.running){
        //to limit frames we have a set refresh rate
        //to maintain this refresh rate we subtract the
        //time elapsed over the entire cycle
        //to do this we get the time at the start of the cycle
        //and the end to get the difference
        s_cycle = clock();
        //exit sequence
        exit_sequence(&game);

        //check if we enter the car
        enter_car(&game, &player, &car, &map);

        //toggle debugging
        if(key[KEY_F1] && !game.debug_cd){
            game.debug = !game.debug;
            printf(game.debug ? "DEBUGGING ENABLED\n" : "DEBUGGING DISABLED\n");
            game.debug_cd = 10;
        }else{
            game.debug_cd -= game.debug_cd ? 1 : 0;
        }
        //resets game
        if(key[KEY_F5]){
            game_init(&game);
            load_map(&game, &map);
            player_init(&game, &player, &map);   //initialize player struct
            car_init(&game, &car, &map, 32, 32, 5);
            gen_npcs(&game, &map);
        }

        //toggle music
        if(key[KEY_M]){
            game.music ? stop_sample(game.bgMusic) :
                         play_sample(game.bgMusic, 255, 128, 1000, 1);
        }

        //clear buffer
        clear_to_color(game.buffer, makecol(0, 0, 0));

        //show map
        masked_blit(map.map, game.buffer, game.cam_x, game.cam_y,
                    0, 0, game.screen_w, game.screen_h);

        //pan camera
        pan_camera(&game);

        //do mouse click
        mouse_click(&game, game.active_sprite);
        npc_sequence(&game, &map);

        //display the player if we're not in the car
        if (!game.in_car){
            sprite_sequence(&game, &player, &map, !game.in_car);
            if (key[KEY_SPACE] && !player.bullet_cd){
                sprite_shoot(&game, &player);
                player.bullet_cd = 10;
            }else{
                player.bullet_cd -= player.bullet_cd ? 1 : 0;
            }
        }else{
            free(player.bullets);
            player.bullets = NULL;
            player.bullet_count = 0;
        }

        //show car
        sprite_sequence(&game, &car, &map, game.in_car);

        //display all overlays
        blit_overlays(&game);

        //show hud
        show_hud(&game, &player);

        //display buffer to screen
        stretch_blit(game.buffer, screen, 0, 0, game.buffer->w,
                     game.buffer->h, 0, 0, game.screen_w * STRETCH_FACTOR,
                     game.screen_h * STRETCH_FACTOR);

        //countdown timer
        game_tick(&game, &map);

        e_cycle = clock();

        /*maintain consistent framerate by calculating delay of
        processing, using max because we don't want negative rest*/
        printf("\rIteration took %f%20s", (double)
                (e_cycle - s_cycle), " ");
        rest(max(game.delay - (double) (e_cycle - s_cycle), 0));
    }

    fprintf(fpLog, "Running exit scripts...\n");
    //destroy all bitmaps
    destroy_bitmaps(&game, &player, &map, &car);
    fprintf(fpLog, "Exiting...\n");
    return 0;
}END_OF_MAIN();