Example #1
0
static void ReadMouse( void )
{
	int i;
	mouse_state_t * state = read_mouse();
	mouse_states[ new_input ].xrel = state->xrel;
	mouse_states[ new_input ].yrel = state->yrel;
	mouse_states[ new_input ].wheel = mouse_state.wheel;
	for ( i = 0; i < MAX_MOUSE_BUTTONS; i++ )
		mouse_states[ new_input ].buttons[ i ] = mouse_state.buttons[ i ];
}
Example #2
0
File: test.c Project: mdurrer/cgd
int main(int argc, char **argv)
{
	int mbn, prev_mx = -1, prev_my = -1, prev_mbn = 0;

	if(parse_args(argc, argv) == -1) {
		return 1;
	}

	if(init() == -1) {
		return 1;
	}

	reset_timer();

	for(;;) {
        if(kbhit()) {
			if(keyb(getch()) == 0) {
				break;
            }
        }

		mbn = read_mouse(&mx, &my);
		if(mbn != prev_mbn) {
			mouse_button(mbn, mx, my);
			prev_mbn = mbn;
		}
		if(mx != prev_mx || my != prev_my) {
			if(mbn) {
				mouse_motion(mx, my);
			}
			prev_mx = mx;
			prev_my = my;
		}

		redraw();
	}

	shutdown();
	print_perf();
	return 0;
}
void newxy(int *mx,int *my,int *mbutt)
{
 int ch,xx0=*mx,yy0=*my;
 int xm,ym;
 ch=read_mouse(&xm,&ym,mbutt);
 cursor(xx0,yy0);
 cursor(xm,ym);
 switch(ch)
   {
    case 0:
	   break;
    case 1:
	   circle(xm,ym,6);
	   break;
    case 2:
	   rectangle(xm,ym,xm+12,ym+12);
	   break;
    default:
	   putpixel(xm,ym,7);
	   break;
   }
 *mx=xm;
 *my=ym;
}
Example #4
0
void showlogo()
{
	struct bitfileheader
	{
		int  bfType;
		long bfSize;
		int  bfReserved1;
		int  bfReserved2;
		long bfOffBits;
		long biSize;
		long biWidth;
		long biHeight;
		int  biPlanes;
		int  biBitCount;
		long biCompression;
		long biSizeImage;
		long biXPelsPerMeter;
		long biYPelsPerMeter;
		long biClrUsed;
		long byClrImportant;
	};

	int xmax = getmaxx();
	int ymax = getmaxy();
	bitfileheader testing;
	palettetype pal;
	getpalette(&pal);
	int i, j;
	char blue, green, red, pix1, pix2, pixel, alpha;
	mouse_data mouse;

	cleardevice();

	if(!init_mouse())
	{
		closegraph();
		cout << "Mouse couldn't be initialised";
	}

	hide_mouse();

	fstream bitmap("logo.saq", ios::in);

	bitmap.read((char*)&testing, sizeof testing);

	for (i = 0; i< pal.size; i++)
	{
		bitmap.read(&blue,1);
		bitmap.read(&green,1);
		bitmap.read(&red,1);
		bitmap.read(&alpha,1);
//		setrgbpalette(pal.colors[i],(int)red,(int)green,(int)blue);
	}

	for (i = 1; i<= testing.biHeight; i++)
		for (j = 1; j <= testing.biWidth; j+=2)
		{
			bitmap.read(&pixel, 1);
			pix1 = pixel & 0xF0;
			pix1 >>= 4;
			pix2 = pixel & 0x0F;
			putpixel(j,   testing.biHeight - i,pix1);
			putpixel(j+1, testing.biHeight - i,pix2);
		}

	show_mouse();
	do
	{
		read_mouse(&mouse);
	}while(!kbhit() || !button_press(LEFT_BUTTON, &mouse));
		
}
Example #5
0
/* Function Definitions */
int mainoptions()
{

	//Will draw five buttons each having the following fucntions
	//1. New Game
	//2. Load Game
	//3. Rules
	//4. About
	//5. Quit
	button button[5];

	int xmax = getmaxx();
	int ymax = getmaxy();
	char *text;
	int i;
	unsigned size;
	mouse_data mouse;
	
	cleardevice();
	hide_mouse();
	setfillstyle(SOLID_FILL, LIGHTGRAY);
	bar(0,0,xmax,ymax);

	//Draw the five buttons and store their info
	for(i = 0; i <= 4; i++)
	{
		//Mention the coordinates of the buttons
		button[i].coords[0][0] = xmax / 2 - 100;       // Left
		button[i].coords[0][1] = (i * 30) + 100;      // Top
		button[i].coords[1][0] = xmax / 2 + 100;       // Right
		button[i].coords[1][1] = ((i + 1) * 30) + 99; // Bottom
		
		//Dynamically allocate memory for images of up and down buttons
		size = imagesize(button[i].coords[0][0], button[i].coords[0][1], button[i].coords[1][0], button[i].coords[1][1]);

		button[i].up = (int *)malloc(size);
		if(!button[i].up){
			cout << "Error..";
			getch();
			exit(0);
		}

		button[i].down = (int *)malloc(size);
		if(!button[i].down){
			cout << "Error..";
			getch();
			exit(0);
		}

		//Put the text on button's face
		switch (i)
		{
			case 0:
				text = "Start New Game";
				break;
			case 1:
				text = "Load Game";
				break;
			case 2:
				text = "The Rules";
				break;
			case 3:
				text = "About Ludo Master 2002";
				break;
			case 4:
				text = "Quit";
				break;
		}
		//Draw button and store info
		draw_button(button[i].coords[0][0], button[i].coords[0][1], button[i].coords[1][0], button[i].coords[1][1], LIGHTBLUE, LIGHTCYAN, BLUE, WHITE, text, button[i].up,button[i].down);
	}
	show_mouse();

	//Wait for user's response through mouse
	do
	{
		//When mouse button is pressed see if it is pressed on a button, if yes then suprress that button
		read_mouse(&mouse);

		if (button_press(LEFT_BUTTON, &mouse))
		{
			// See which button user has pressed
			for(int i = 0; i <= 4; i++)
			{

				if ( mouse.x >= button[i].coords[0][0] && mouse.x <= button[i].coords[1][0] && mouse.y >= button[i].coords[0][1] && mouse.y <= button[i].coords[1][1])
				{
					hide_mouse();
					putimage(button[i].coords[0][0], button[i].coords[0][1], button[i].down, 0);
					show_mouse();
					delay(100);
                    hide_mouse();
					putimage(button[i].coords[0][0], button[i].coords[0][1], button[i].up  , 0);
					show_mouse();

					//Delete the memory allocated to buttons' images
                    for(int j = 0; j <= 4; j++)
					{
						free( (void*) button[j].up);
						free( (void*) button[j].down);
					}
 
					//Actions for each button user may click on
					switch (i)
					{
						case 0:
							// Start new game
							new_game_vars();
							return gameoptions();
							break;
						case 1:
							// Load game
							new_game_vars();
							return loadgame();
							break;
						case 2:
							// Rules of Ludo
							return 0;
							break;
						case 3:
							// About the author
							showlogo();
							return 0;
							break;
						case 4:                   	
							// Quit
                            exit(1);
							break;
					}
			   }

			}
		}
	}	while(1); //Infinite loop
}
Example #6
0
int gameoptions()
{
	int i, j, size;
	int xmax = getmaxx();
	int ymax = getmaxy();
	mouse_data mouse;
	
	button blue[3];
	button green[3];
	button red[3];
	button yellow[3];
	button ok;
	button cancel;

	// Clear previous elements and draw background
	cleardevice();
	hide_mouse();
	setfillstyle(SOLID_FILL, LIGHTGRAY);

	bar(0,0,xmax,ymax);

	settextjustify(CENTER_TEXT, BOTTOM_TEXT);
	setcolor(BLACK);
	outtextxy(xmax/2,20, "Please specify number and types of players");
	

	settextjustify(LEFT_TEXT, TOP_TEXT);
	setcolor(BLACK);
	//Draw block and buttons of green player
	setfillstyle(SOLID_FILL, GREEN);
	bar(xmax/2, 50, xmax/2 + 150, 200);
	outtextxy(xmax/2 + 5, 55, "Green Player:");

	//Draw block and buttons of red player
	setfillstyle(SOLID_FILL, RED);
	bar(xmax/2, 200, xmax/2 + 150, 350);
	outtextxy(xmax/2 + 5, 205, "Red Player");
	
	//Draw block and buttons of blue player
	setfillstyle(SOLID_FILL, BLUE);
	bar(xmax/2 - 150, 50, xmax/2, 200);
	outtextxy(xmax/2 - 145, 55, "Blue Player");
	
	//Draw block and buttons of yellow player
	setfillstyle(SOLID_FILL, YELLOW);
	bar(xmax/2 - 150, 200, xmax/2, 350);
	outtextxy(xmax/2 - 145, 205, "Yellow Player");

	//Assign coordinates and stores images of option buttons
	size = imagesize(0,0,100,30);
	for( i = 0; i <= 2; i++)
	{
		char *text;
		switch (i)
		{
			case 0:
				text = "None";
				break;
			case 1:
				text = "Human";
				break;
			case 2:
				text = "Computer";
				break;
		}
		
		//Set coordinaates for blue player's option buttons and store it as image
		blue[i].coords[0][0] = xmax/2 - 125;
		blue[i].coords[0][1] = 80 + i * 30;
		blue[i].coords[1][0] = xmax/2 - 25;
		blue[i].coords[1][1] = 80 + (i+1)*30;
		
		blue[i].up = (int *)malloc(size) ;
		blue[i].down = (int *)malloc(size) ;
		if(!blue[i].up || !blue[i].down)
		{
			cleardevice();
			closegraph();
			cout << endl << "Either u've run out of memory or memory can't be allocated. Ludo Master 2002 will quit now";
			getch();
			exit(0);
		}

		draw_button(blue[i].coords[0][0], blue[i].coords[0][1], blue[i].coords[1][0], blue[i].coords[1][1], DARKGRAY, LIGHTGRAY, BLACK, WHITE, text, blue[i].up,blue[i].down);
						
		//Set coordinaates for green player's option buttons and store it as image
		green[i].coords[0][0] = xmax/2 + 25;
		green[i].coords[0][1] = 80 + i * 30;
		green[i].coords[1][0] = xmax/2 + 125;
		green[i].coords[1][1] = 80 + (i+1)*30;
		
		green[i].up = blue[i].up;      // Images of buttons are same
		green[i].down = blue[i].down;  // Images of buttons are same
		putimage(green[i].coords[0][0], green[i].coords[0][1], green[i].up,0);
				
		//Set coordinaates for red player's option buttons and store it as image
		red[i].coords[0][0] = xmax/2 + 25;
		red[i].coords[0][1] = 230 + i * 30;
		red[i].coords[1][0] = xmax/2 + 125;
		red[i].coords[1][1] = 230 + (i+1)*30;
		
		red[i].up = blue[i].up;      // Images of buttons are same
		red[i].down = blue[i].down;  // Images of buttons are same
		putimage(red[i].coords[0][0], red[i].coords[0][1], red[i].up, 0);
		
		//Set coordinaates for yellow player's option buttons and store it as image
		yellow[i].coords[0][0] = xmax/2 - 125;
		yellow[i].coords[0][1] = 230 + i * 30;
		yellow[i].coords[1][0] = xmax/2 - 25;
		yellow[i].coords[1][1] = 230 + (i+1)*30;
		
		yellow[i].up = blue[i].up;      // Images of buttons are same
		yellow[i].down = blue[i].down;  // Images of buttons are same
		putimage(yellow[i].coords[0][0], yellow[i].coords[0][1], yellow[i].up,0);
	}
	
	//Draw Ok button
	size = imagesize(0,0,80,20);

	ok.coords[0][0] = xmax/2 - 105;
	ok.coords[0][1] = ymax - 70;
	ok.coords[1][0] = xmax/2 - 25;
	ok.coords[1][1] = ymax - 50;
	ok.up = (int *)malloc(size);
	ok.down = (int *)malloc(size);
	
	if(!ok.up || !ok.down) // Check if memory has been allocated
	{
		cleardevice();
		closegraph();
		cout << endl << "Either u've run out of memory or memory can't be allocated. Ludo Master 2002 will quit now";
		getch();
		exit(0);
	}
	
	draw_button(ok.coords[0][0], ok.coords[0][1], ok.coords[1][0], ok.coords[1][1], LIGHTBLUE, LIGHTCYAN, BLUE, WHITE, "Start", ok.up,ok.down);

	//Draw cancel button
	cancel.coords[0][0] = xmax/2 + 25;
	cancel.coords[0][1] = ymax - 70;
	cancel.coords[1][0] = xmax/2 + 105;
	cancel.coords[1][1] = ymax - 50;
	cancel.up = (int *)malloc(size);
	cancel.down = (int *)malloc(size);
	
	if(!cancel.up || !cancel.down)
	{
		cleardevice();
		closegraph();
		cout << endl << "Either u've run out of memory or memory can't be allocated. Ludo Master 2002 will quit now";
		getch();
		exit(0);
	}
	draw_button(cancel.coords[0][0], cancel.coords[0][1], cancel.coords[1][0], cancel.coords[1][1], LIGHTBLUE, LIGHTCYAN, BLUE, WHITE, "Cancel", cancel.up, cancel.down);

	/* Set default as None option to all players */
	putimage(blue[0].coords[0][0], blue[0].coords[0][1], blue[0].down,0);
	putimage(green[0].coords[0][0], green[0].coords[0][1], green[0].down, 0);
	putimage(red[0].coords[0][0], red[0].coords[0][1], red[0].down,0);
	putimage(yellow[0].coords[0][0], yellow[0].coords[0][1], yellow[0].down,0);
	player[PLR_BLUE].type = TYPE_NONE;
	player[PLR_GREEN].type = TYPE_NONE;
	player[PLR_RED].type = TYPE_NONE;
	player[PLR_YELLOW].type = TYPE_NONE;
	
	show_mouse();

	/* Get user's input through mouse */
	do
	{
		read_mouse(&mouse);
		
		if (button_press(LEFT_BUTTON, &mouse))
		{
			// If blue player's option is pressed:
			if (mouse.x >= blue[0].coords[0][0] && mouse.x <= blue[2].coords[1][0] && mouse.y >= blue[0].coords[0][1] && mouse.y <= blue[2].coords[1][1])
			{
				hide_mouse();
				for (i = 0; i <= 2; i++)
				{
					putimage(blue[i].coords[0][0], blue[i].coords[0][1], blue[i].up, 0);
					if (mouse.x >= blue[i].coords[0][0] && mouse.x <= blue[i].coords[1][0] && mouse.y >= blue[i].coords[0][1] && mouse.y <= blue[i].coords[1][1])
					{
						putimage(blue[i].coords[0][0], blue[i].coords[0][1], blue[i].down, 0);
						player[PLR_BLUE].type = i;						
					}
				}
				show_mouse();
			}

			// If green player's option is pressed:
			if (mouse.x >= green[0].coords[0][0] && mouse.x <= green[2].coords[1][0] && mouse.y >= green[0].coords[0][1] && mouse.y <= green[2].coords[1][1])
			{
				hide_mouse();
				for (i = 0; i <= 2; i++)
				{
					putimage(green[i].coords[0][0], green[i].coords[0][1], green[i].up, 0);
					if (mouse.x >= green[i].coords[0][0] && mouse.x <= green[i].coords[1][0] && mouse.y >= green[i].coords[0][1] && mouse.y <= green[i].coords[1][1])
					{
						putimage(green[i].coords[0][0], green[i].coords[0][1], green[i].down, 0);
						player[PLR_GREEN].type = i;						
					}
				}
				show_mouse();
			}

			// If red player's option is pressed:
			if (mouse.x >= red[0].coords[0][0] && mouse.x <= red[2].coords[1][0] && mouse.y >= red[0].coords[0][1] && mouse.y <= red[2].coords[1][1])
			{
				hide_mouse();
				for (i = 0; i <= 2; i++)
				{
					putimage(red[i].coords[0][0], red[i].coords[0][1], red[i].up, 0);
					if (mouse.x >= red[i].coords[0][0] && mouse.x <= red[i].coords[1][0] && mouse.y >= red[i].coords[0][1] && mouse.y <= red[i].coords[1][1])
					{
						putimage(red[i].coords[0][0], red[i].coords[0][1], red[i].down, 0);
						player[PLR_RED].type = i;						
					}
				}
				show_mouse();
			}

			// If yellow player's option is pressed:
			if (mouse.x >= yellow[0].coords[0][0] && mouse.x <= yellow[2].coords[1][0] && mouse.y >= yellow[0].coords[0][1] && mouse.y <= yellow[2].coords[1][1])
			{
				hide_mouse();
				for (i = 0; i <= 2; i++)
				{
					putimage(yellow[i].coords[0][0], yellow[i].coords[0][1], yellow[i].up, 0);
					if (mouse.x >= yellow[i].coords[0][0] && mouse.x <= yellow[i].coords[1][0] && mouse.y >= yellow[i].coords[0][1] && mouse.y <= yellow[i].coords[1][1])
					{
						putimage(yellow[i].coords[0][0], yellow[i].coords[0][1], yellow[i].down, 0);
						player[PLR_YELLOW].type = i;						
					}
				}
				show_mouse();
			}

			// If Start/OK button is pressed:
			if (mouse.x >= ok.coords[0][0] && mouse.x <= ok.coords[1][0] && mouse.y >= ok.coords[0][1] && mouse.y <= ok.coords[1][1])
			{
				hide_mouse();
				putimage(ok.coords[0][0], ok.coords[0][1], ok.down, 0);
				show_mouse();
				delay(100);
				hide_mouse();
				putimage(ok.coords[0][0], ok.coords[0][1], ok.up, 0);
				show_mouse();
				delay(100);
				
				//Check how many players are playing
				int count = 0;
				for (i = 0; i <= 3; i++)
				{
					if (player[i].type != TYPE_NONE)
						count++;
				}
					
				//If number of players playing is less than 2 then give msg and do nothing
				
				if (count >= 2)
				{		
					// Free the memory allocated to images
					free ((void*) ok.up);
					free ((void*) ok.down);
					free ((void*) cancel.up);
					free ((void*) cancel.down);
					for(i = 0; i <= 3; i++)
					{
						free ((void*) blue[i].up);
						free ((void*) blue[i].down);
						free ((void*) green[i].up);
						free ((void*) green[i].down);
						free ((void*) red[i].up);
						free ((void*) red[i].down);
						free ((void*) yellow[i].up);
						free ((void*) yellow[i].down);
						return 1;
					}
				}
				else
				{
					settextjustify(CENTER_TEXT, CENTER_TEXT);
					setcolor(DARKGRAY);
					outtextxy(xmax/2, ymax - 90, "There should be atleast two players playing to start the game");
				}
			}
			
			// If Cancel button is pressed:	
			if (mouse.x >= cancel.coords[0][0] && mouse.x <= cancel.coords[1][0] && mouse.y >= cancel.coords[0][1] && mouse.y <= cancel.coords[1][1])
			{
				hide_mouse();
				putimage(cancel.coords[0][0], cancel.coords[0][1], cancel.down, 0);
				show_mouse();
				delay(100);
				hide_mouse();
				putimage(cancel.coords[0][0], cancel.coords[0][1], cancel.up, 0);
				show_mouse();
				delay(100);
				
				// Free the memory allocated to images
				free ((void*) ok.up);
				free ((void*) ok.down);
				free ((void*) cancel.up);
				free ((void*) cancel.down);
				for(i = 0; i <= 3; i++)
				{
					free ((void*) blue[i].up);
					free ((void*) blue[i].down);
					free ((void*) green[i].up);
					free ((void*) green[i].down);
					free ((void*) red[i].up);
					free ((void*) red[i].down);
					free ((void*) yellow[i].up);
					free ((void*) yellow[i].down);
				}
				
				return 0;
			}
		}
	}while(1);  //Infinite loop
}