Main Page   Data Structures   File List   Globals  

process_input.c File Reference

Converts mouse_drag messages into game moves or button presses. More...

#include "white_interface.h"
#include "e:\AndrewWorkspace\Stratego\input.h"

Go to the source code of this file.

Functions

void process_move (void)
 Determines the starting and finishing board squares of a move. More...

void process_placing (void)
 Determines the starting square in the placing panel and the finishing square on the main game board. More...

void process_button (void)
 Works out which button the user clicked. More...

int calc_x_square (int x, int square_width)
 Calculates the x value of the board square at the supplied screen coordinates. More...

int calc_y_square (int y, int square_height, int zero_pixel)
 Calculates the y value of the board square at the supplied screen coordinates. More...

void poin_entry_Init ()
 VCC Init function. More...

void poin_entry_Run ()
 VCC Run function. More...


Variables

int down_x
int down_y
int up_x
int up_y


Detailed Description

Converts mouse_drag messages into game moves or button presses.

This block receives mouse_drag messages from the receive_input block, which tell it where the user has dragged the mouse on the screen. It reads these messages and decides what the user was inteding to do. For example, they may have been placing a piece on the game board, moving an existing piece from one square to another or clicking one of the buttons at the bottom of the screen. Messages are then send to the output_move or output_button blocks, which will send move or button messages to the game core.

Definition in file process_input.c.


Function Documentation

int calc_x_square int    x,
int    square_width
 

Calculates the x value of the board square at the supplied screen coordinates.

Parameters:
x  The x screen coordinate for which you need the corresponding grid square number.
square_width  The width of one board square (in screen pixels).
Returns:
an integer specifying the x value of the board square.

Definition at line 235 of file process_input.c.

Referenced by process_button, process_move, and process_placing.

00236 {
00237   return (x / square_width);
00238 }

int calc_y_square int    y,
int    square_height,
int    zero_pixel
 

Calculates the y value of the board square at the supplied screen coordinates.

Parameters:
y  The y screen coordinate for which you need the corresponding grid square number.
square_height  The height of one board square (in screen pixels).
zero_pixel  The y screen coordinate of the bottom edge of the row of squares with y value 0.
Returns:
an integer specifying the y value of the board square.

Definition at line 241 of file process_input.c.

Referenced by process_move, and process_placing.

00242 {
00243   int new_y;
00244   int square;
00245 
00246   /* first work out number of pixels above or below y=0 line
00247      (the bottom edge of the bottom row of main board squares). */
00248   new_y = y - zero_pixel;
00249 
00250   /* now calculate the y value of the board square. */
00251   square = new_y / square_height;
00252   
00253   /* this copes with calculating the correct negative y value
00254      for a square in the placing panel. */
00255 
00256   if (new_y < 0 && new_y % square_height != 0)
00257     square = square - 1;
00258 
00259   return (square);
00260 }

void poin_entry_Init  
 

VCC Init function.

This function runs when the VCC simulation is first started.

Definition at line 95 of file process_input.c.

00095                        {
00096 }

void poin_entry_Run  
 

VCC Run function.

This runs whenever a message arrives at the process_input block's in port.

Definition at line 103 of file process_input.c.

References BOARD_PLACE_BOUNDARY, down_x, down_y, PLACE_BUTTONS_BOUNDARY, process_button, process_move, process_placing, SCREEN_HEIGHT, SCREEN_WIDTH, up_x, and up_y.

00103                       {
00104 
00105   mouse_drag input;
00106   
00107   if (in_Enabled())
00108   {
00109     input = *in_Value();
00110 
00111     down_x = input.down_x;
00112     down_y = input.down_y;
00113     up_x = input.up_x;
00114     up_y = input.up_y;
00115 
00116 
00117     /* first check if click is on the screen horizontally */
00118 
00119     if (down_x >= 0 && up_x >=0 && down_x < SCREEN_WIDTH && up_x < SCREEN_WIDTH)
00120     {
00121       /* If y coordinate is in main game board area, user is trying to make a move. */
00122       if (down_y < SCREEN_HEIGHT && up_y < SCREEN_HEIGHT && 
00123           down_y >= BOARD_PLACE_BOUNDARY && up_y >= BOARD_PLACE_BOUNDARY)
00124       {      
00125         process_move();
00126       }
00127       
00128       /* if y coordinate is in the placing panel, user is trying to place a piece. */
00129       if (down_y < BOARD_PLACE_BOUNDARY && down_y >= PLACE_BUTTONS_BOUNDARY && 
00130           up_y < SCREEN_HEIGHT && up_y >= BOARD_PLACE_BOUNDARY)
00131       {
00132         process_placing();
00133       }
00134 
00135       /* if y coordinate is in the button area, user is trying to click a button. */
00136       if (down_y < PLACE_BUTTONS_BOUNDARY && down_y >= 0 && 
00137           up_y < PLACE_BUTTONS_BOUNDARY && up_y >= 0)
00138       { 
00139         process_button();
00140       }
00141     }
00142   }
00143 }

void process_button void   
 

Works out which button the user clicked.

This takes the start and end coordinates of where the user clicked the mouse and works out which button they clicked. An integer specifying the number of the button is send to the output_button block. The mouse button must be pressed and released over the same button, otherwise the button click will be ignored.

Definition at line 212 of file process_input.c.

References BUTTONS_WIDTH, calc_x_square, down_x, NO_BUTTONS_X, and up_x.

Referenced by poin_entry_Run.

00213 {
00214   int down_square, up_square;
00215   int button_width;
00216 
00217   /* calculate size of each square in pixels */
00218   button_width = BUTTONS_WIDTH / NO_BUTTONS_X;
00219 
00220   /* work out which button the user clicked. */
00221   
00222   down_square = calc_x_square(down_x, button_width);
00223   up_square = calc_x_square(up_x, button_width);
00224 
00225   /* If the user clicked 1 button rather than
00226      dragging the mouse between 2 buttons,
00227      output the button press */
00228 
00229   if (down_square == up_square)
00230         button_out_Post(down_square + 1);
00231 
00232 }

void process_move void   
 

Determines the starting and finishing board squares of a move.

process_move examines the start and end coordinates of where the user dragged the mouse and calculates the starting and finishing board squares. If the move is legal (i.e. between 2 different squares that fit on the screen) a temp_move message is sent to the output_move block. Otherwise the move is ignored.

Definition at line 146 of file process_input.c.

References BOARD_HEIGHT, BOARD_PLACE_BOUNDARY, BOARD_WIDTH, BOARD_X_SQUARES, BOARD_Y_SQUARES, calc_x_square, calc_y_square, down_x, down_y, up_x, and up_y.

Referenced by poin_entry_Run.

00147 {
00148   int start_x_square, start_y_square, end_x_square, end_y_square;
00149   int square_width, square_height;
00150 
00151   temp_move move;
00152 
00153   /* calculate size of each square in pixels */
00154   square_width = BOARD_WIDTH / BOARD_X_SQUARES;
00155   square_height = BOARD_HEIGHT / BOARD_Y_SQUARES;
00156 
00157   /* work out which squares the user clicked. Squares are numbered 0 to 9,
00158      left to right and bottom to top of the screen.  */
00159   
00160   start_x_square = calc_x_square(down_x, square_width);
00161   end_x_square = calc_x_square(up_x, square_width);
00162 
00163   start_y_square = calc_y_square(down_y, square_height, BOARD_PLACE_BOUNDARY);
00164   end_y_square = calc_y_square(up_y, square_height, BOARD_PLACE_BOUNDARY);
00165 
00166   /* if start and end square are different, send the move
00167      to the output_move block. */
00168 
00169   if ((start_x_square != end_x_square) || (start_y_square != end_y_square))
00170   {
00171     move.start_x = start_x_square;
00172     move.start_y = start_y_square;
00173     move.end_x = end_x_square;
00174     move.end_y = end_y_square;
00175     move_out_Post(&move);
00176   }
00177 }

void process_placing void   
 

Determines the starting square in the placing panel and the finishing square on the main game board.

process_placing examines the start and end coordinates of where the user dragged the mouse and calculates the starting square on the placing panel and the finishing square on the main game board. A piece must be dragged from the placing panel to the main board. If it is dropped somewhere else on the placing panel or on a button the placing will be ignored. If the placing is legal, a temp_move message will be send to the output_move block. Note that the starting square y value is negative to indicate that it refers to a square on the placing panel.

Definition at line 180 of file process_input.c.

References BOARD_HEIGHT, BOARD_PLACE_BOUNDARY, BOARD_WIDTH, BOARD_X_SQUARES, BOARD_Y_SQUARES, calc_x_square, calc_y_square, down_x, down_y, PLACE_HEIGHT, PLACE_WIDTH, PLACE_X_SQUARES, PLACE_Y_SQUARES, up_x, and up_y.

Referenced by poin_entry_Run.

00181 {
00182   int start_x_square, start_y_square, end_x_square, end_y_square;
00183   int board_square_width, board_square_height;
00184   int place_square_width, place_square_height;
00185 
00186   temp_move placing;
00187 
00188   /* calculate size of each square in pixels */
00189   board_square_width = BOARD_WIDTH / BOARD_X_SQUARES;
00190   board_square_height = BOARD_HEIGHT / BOARD_Y_SQUARES;
00191   place_square_width = PLACE_WIDTH / PLACE_X_SQUARES;
00192   place_square_height = PLACE_HEIGHT / PLACE_Y_SQUARES;
00193 
00194   /* work out which squares the user clicked. */
00195   
00196   start_x_square = calc_x_square(down_x, place_square_width);
00197   end_x_square = calc_x_square(up_x, board_square_width);
00198 
00199   start_y_square = calc_y_square(down_y, place_square_height, BOARD_PLACE_BOUNDARY);
00200   end_y_square = calc_y_square(up_y, board_square_height, BOARD_PLACE_BOUNDARY);
00201 
00202   /* output the move */
00203 
00204   placing.start_x = start_x_square;
00205   placing.start_y = start_y_square;
00206   placing.end_x = end_x_square;
00207   placing.end_y = end_y_square;
00208   move_out_Post(&placing);
00209 }


Variable Documentation

int down_x
 

variables to store the x and y coordinates of where the mouse is clicked and released.

Definition at line 86 of file process_input.c.

Referenced by poin_entry_Run, process_button, process_move, and process_placing.

int down_y
 

variables to store the x and y coordinates of where the mouse is clicked and released.

Definition at line 86 of file process_input.c.

Referenced by poin_entry_Run, process_move, and process_placing.

int up_x
 

variables to store the x and y coordinates of where the mouse is clicked and released.

Definition at line 86 of file process_input.c.

Referenced by poin_entry_Run, process_button, process_move, and process_placing.

int up_y
 

variables to store the x and y coordinates of where the mouse is clicked and released.

Definition at line 86 of file process_input.c.

Referenced by poin_entry_Run, process_move, and process_placing.


Copyright © 2002 Andrew Bates
Last Updated 03/04/02