Esempio n. 1
0
File: main.c Progetto: Aiur/Airtab
bool execCommand(Command *cmd) {
    double x, y;
    int key;
    if (strcmp(cmd->cmd, "mm") == 0) {
        x = strtod(cmd->arg1, NULL);
        y = strtod(cmd->arg2, NULL);
        mousemove(x, y);
        return true;
    } else if (strcmp(cmd->cmd, "kd") == 0) {
        key = (int) strtol(cmd->arg1, NULL, 10);
        keydown(key);
    } else if (strcmp(cmd->cmd, "ku") == 0) {
        key = (int)strtol(cmd->arg1, NULL, 10);
        keyup(key);
    } else if (strcmp(cmd->cmd, "md") == 0) {
        mousedown(cmd->arg1[0]);
    } else if (strcmp(cmd->cmd, "mu") == 0) {
        mouseup(cmd->arg1[0]);
    } else if (strcmp(cmd->cmd, "sy") == 0) {
        scrollY(strtod(cmd->arg1, NULL));
    } else if (strcmp(cmd->cmd, "sx") == 0) {
        scrollX(strtod(cmd->arg1, NULL));
    }
    
    return false;
}
Esempio n. 2
0
// The main Allegro loop, all input handling, animation and drawing is done here
_Bool main_loop(void)
{
    // Flag for drawing
    _Bool needredraw = true;
    // Declare primitive data types
    float old_time = 0.0, current_time = 0, dt = 0;

    while(!data->exit)
    {
        if(needredraw && al_event_queue_is_empty(data->queue) && al_event_queue_is_empty(data->queue2))
        {
            // Clear, draw, flip
            al_clear_to_color(data->background_color);
            render();
            if(get_fps_status())
                al_draw_textf(data->font, data->text_color, 0, res_height-30, 0, "fps: %.2f", 1/dt);
            al_flip_display();
            needredraw = false;
        }

        // Block until an event enters the queue
        al_wait_for_event(data->queue, &(data->event));

        while(!al_event_queue_is_empty(data->queue2))
        {
            al_get_next_event(data->queue2, &(data->event2));
            switch(data->event2.type)
            {
                case ALLEGRO_EVENT_DISPLAY_CLOSE:
                {
                    // If x button is pressed on window
                    data->exit = true;
                    data->gamestarted = false;
                }
                break;
                case ALLEGRO_EVENT_DISPLAY_RESIZE:
                {
                    al_acknowledge_resize(data->event2.display.source);
                    scale(res_width, res_height);
                }
                break;
                case ALLEGRO_EVENT_MOUSE_AXES:
                {
                    // Stores the mouse's new position and change in position
                    mouseaxes(&(data->event2.mouse));
                }
                case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
                {
                    // Stores the mouse button pressed
                    mousedown(&(data->event2.mouse));
                }
                break;
                case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
                {
                    // Stores the mouse button released
                    mouseup(&(data->event2.mouse));
                }
                break;
                case ALLEGRO_EVENT_KEY_DOWN:
                {
                    // Stores keydown keycode into keycode array for processing
                    keydown(&(data->event2.keyboard));
                }
                break;
                case ALLEGRO_EVENT_KEY_UP:
                {
                    // Stores keycode into keycode array for processing
                    keyup(&(data->event2.keyboard));
                }
                break;
                default:
                break;
            }
        }

        switch (data->event.type)
        {
            case ALLEGRO_EVENT_TIMER:
            {
                // Determine the change in time between frames, in seconds
				current_time = al_current_time();
				dt = current_time-old_time;

				// If the computer lags for some reason, don't penalize the player
				// Cap dt at 0.5 seconds
				if(dt > 0.25)
				{
                    dt = 0.25;
				}

				// Handle Mouse and Keyboard events and Button Events
				buttoncheck(&buttonhandler, data);
				keycheck(&keyhandler, data);
				mousecheck(&mousehandler, data);
				keyupdate();
                mouseupdate();

                // Check if data->quit has been set before updating and drawing
                if(data->exit) break;

                // Update the game, always
                update();

                // Skip drawing frames if computer lags
                if(current_time - data->event.timer.timestamp <= 1.0/al_get_display_refresh_rate(data->display))
                {
                    needredraw = true;
                }

				// Make the time at this frame the old time, for the next frame
				old_time = current_time;
            }
            break;
            default:
            break;
        }
    }
    return true;
}
Esempio n. 3
0
void window::main_loop()
{
  SDL_Event event;

  while(true)
  {
    while(SDL_PollEvent(&event))
    {
      switch(event.type) {
        case SDL_KEYDOWN:
          switch(event.key.keysym.sym)
	  {
            case SDLK_ESCAPE:
              exit(0);
              break;

            default:
	      keydown(event.key.keysym.sym);
              break;
          }
          break;
	case SDL_KEYUP:
	  keyup(event.key.keysym.sym);
          break;
	case SDL_MOUSEBUTTONDOWN:
	  switch(event.button.button)
	  {
	    case SDL_BUTTON_LEFT:
	      {
		const float x_screen = static_cast<float>(event.button.x);
		const float y_screen = static_cast<float>(event.button.y);

		// scaling from <0,width> to <-1,1>
		const float x_scaled =
		  2.0 * (x_screen - static_cast<float>(width) / 2.0)
		  / static_cast<float>(width);
		const float y_scaled = 
		  2.0 * (y_screen - static_cast<float>(height) / 2.0)
		  / static_cast<float>(height);
		
		mousedown(x_scaled, y_scaled);
	      }
	      break;
	    case SDL_BUTTON_WHEELUP:
	      mousewheelup();
	      break;
	    case SDL_BUTTON_WHEELDOWN:
	      mousewheeldown();
	      break;
	  }
	  break;
	case SDL_MOUSEBUTTONUP:
	  if(event.button.button == SDL_BUTTON_LEFT)
	    mouseup();
	  break;
	case SDL_MOUSEMOTION:
	  {
	    const float x_screen = static_cast<float>(event.motion.x);
	    const float y_screen = static_cast<float>(event.motion.y);

	    // scaling from <0,width> to <-1,1>
	    const float x_scaled =
	      2.0 * (x_screen - static_cast<float>(width) / 2.0)
	      / static_cast<float>(width);
	    const float y_scaled = 
	      2.0 * (y_screen - static_cast<float>(height) / 2.0)
	      / static_cast<float>(height);

	    mousemotion(x_scaled, y_scaled);
	  }
	  break;
	case SDL_VIDEORESIZE:
	  handle_resize(event.resize.w, event.resize.h);
	  break;
        case SDL_QUIT:
          exit(0);
          break;
      }
    }

    frame();
  }
}
Esempio n. 4
0
static boolean minimousedown (Point pt, tyclickflags flags) {
	
	register hdlminirecord hm = minidata;
	short item;	
	Rect r;	
	
	minicheckselectall (); /*if a selectall is pending, do it now*/
	
	item = minifindtextobject (pt);
	
	if (item >= 0) {
		
		miniactivatemessage (false);
		
		(**hm).activetextitem = item;
		
		if (!minisetwpglobals (item))
			return (false);
		
		wpclick (pt, flags);
		
		(*(**hm).texthitroutine) (pt);
		
		shellevent.when = gettickcount ();
		
		mouseup (shellevent.when, shellevent.where.h, shellevent.where.v, shellevent.what); /*textedit may swallow the mouseup event when tracking click*/
		
		return (true);
		}
	
	if (pointinrect (pt, (**hm).msgrect)) {
		
		if (minisetactivewpglobals ())
			wpactivate (false);
		
		(**hm).activetextitem = -1;

		miniactivatemessage (true);
		}

	r = (**hm).iconrect;
	
	if (pointinrect (pt, r)) {
		
		if ((**hm).fliconenabled)
			if (trackicon (r, minidrawicon))
				return (miniiconhit (false));
		
		return (true);
		}
	
	bundle { /*check for mousedown in a popup*/
		
		register short x;
		
		x = minifindpopup (pt);
		
		if (x >= 0) { /*mousedown was in a popup*/
			
			return (minipopuphit (x));
			}
		}
	
	/*
	ouch ();
	*/
	
	return (true);
	} /*minimousedown*/