예제 #1
0
파일: gui.c 프로젝트: ofrinevo/Chess
/*function the present the page- SET BOARD- and handle the events releated to him*/
int setBoard(SDL_Surface* screen){
	SDL_Surface* setBoard_Screen = NULL;
	board_t tmp_board;
	board_t tmp_board_to_add;
	location loc;
	SDL_Event event;
	pixle pix;
	int x, y;
	int check_Board = TRUE;
	int cnt_op_save_illegal = 0;
	int show_illegal_sign=FALSE;
	piece player_to_add;
	int move_next_page=FALSE;
	int remove = 0;
	copyStoS(tmp_board, state->board); // do the adding to the tmp board- in the end- if changed == TRUE- copy the tmp board to the state->board. else- do nothing
	setBoard_Screen = SDL_LoadBMP("Images/setboard.bmp"); 

	if (setBoard_Screen == NULL){
		return 1;
	}
	apply_surface(0, 0, setBoard_Screen, screen);
	fromStateToGui(screen,tmp_board,SET_BOARD); //converts the board(2nd paramter) to graphical form. the 3rd argumant is for internal use 
	//Update Screen
	SDL_Flip(screen);

	//we do all the actions on a tmp board for the case we cancel. only after saving the game(and verifing the board is legal)- we saving all the changes in the real board
	player_to_add.type = EMPTY;
	while (quit == FALSE && move_next_page == FALSE){

		if (SDL_PollEvent(&event)){
			if (event.type == SDL_MOUSEBUTTONUP){
				if (cnt_op_save_illegal == 1){
					show_illegal_sign = 0;
					cnt_op_save_illegal = 0;
					check_Board = TRUE;
				}
				//Get the mouse offsets
				x = event.button.x;
				y = event.button.y;
				pix.x = x;
				pix.y = y;
				if (x <= 600 && y <= 600){// the user clicked on the game Area:
					// adding a new piece to the board seleted by the user:
					if (player_to_add.type != EMPTY){
						loc = pixel_to_loc(pix);
						copyStoS(tmp_board_to_add, tmp_board);

						tmp_board_to_add[loc.column][loc.row].color = player_to_add.color;
						tmp_board_to_add[loc.column][loc.row].type = player_to_add.type;	
						copyStoS(tmp_board, tmp_board_to_add);
						player_to_add.type = EMPTY;
					}
					if (remove){ //removing a piece selected by user;
						loc = pixel_to_loc(pix);
						tmp_board[loc.column][loc.row].color = EMPTY;
						tmp_board[loc.column][loc.row].type = EMPTY;
						remove = 0;
					}
					//update the board:
					apply_surface(0, 0, setBoard_Screen, screen);
					fromStateToGui(screen, tmp_board, SET_BOARD);
					SDL_Flip(screen);
				}
				//add button:
				if ((x > 624) && (x < 624 + 155) && (y > 76) && (y < 76 + 70)){
					remove = 0;
					player_to_add = select_piece(screen);//selecting piece to add
					apply_surface(0, 0, setBoard_Screen, screen); // return to the game Area screen
					fromStateToGui(screen, tmp_board, SET_BOARD);
					SDL_Flip(screen);
					if (player_to_add.type == EMPTY){
						continue;
					}
					
				}
				//remove button:
				if ((x > 624) && (x < 624 + 155) && (y > 76 + 121) && (y < 76 + 121 + 70)){
					remove = 1; add_tiles_to_removing_spots(screen, tmp_board);  SDL_Flip(screen);
				}
				//save:
				if ((x > 624) && (x < 624 + 155) && (y > 76 + 121 * 2) && (y < 76 + 121 * 2 + 70)){
					remove = 0;
					check_Board = checkBoard(tmp_board);// checking if the board is legal
					if (check_Board == TRUE){ // if legal - do the changing on the real board
						copyStoS(state->board, tmp_board);
						break;
					}
					else{ cnt_op_save_illegal++; show_illegal_sign = 1; } // else- tell the user that the board is illegal and continue editing until the board is legal
				}
				//cancel:
				if ((x > 624) && (x < 624 + 155) && (y > 76 + 121 * 3) && (y < 76 + 121 * 3 + 70)){ break; } //discard changes and return to previous page
				if (DEBUG){
					if ((x > 790) && (y > 590)){ // clear the board button- only for checks and debug
						remove = 0;
						clear_board(tmp_board);
						//update the board:
						apply_surface(0, 0, setBoard_Screen, screen);
						fromStateToGui(screen, tmp_board, SET_BOARD);
						SDL_Flip(screen);
					}
				}
				if (check_Board == FALSE && show_illegal_sign){ add_illegal_sign(screen); SDL_Flip(screen); }
			}//end of handle key pressed 
			if (event.type == SDL_QUIT){ SDL_FreeSurface(setBoard_Screen); return 1; }
		}
	}
	//Free the loaded image
	SDL_FreeSurface(setBoard_Screen);
	return 0;
}
예제 #2
0
int get_move(void) {
	int retval = -1;
	int mousex, mousey;
	SDL_Event event;
	const Uint8 *keystate = SDL_GetKeyboardState(NULL);
	Uint8 mousestate = SDL_GetMouseState(&mousex, &mousey);

	if (mousestate & SDL_BUTTON_MIDDLE) {
		move_camera(-(mousey - last_mousey) * MOUSE_CAM_SPEED, -(mousex - last_mousex) * MOUSE_CAM_SPEED);
		last_mousex = mousex;
		last_mousey = mousey;
	} else {
		last_mousex = mousex;
		last_mousey = mousey;
	}

	if (keystate[SDL_SCANCODE_LCTRL]) {
		if (keystate[SDL_SCANCODE_DOWN])
			move_camera(-KEYBOARD_CAM_SPEED, 0.0f);
		if (keystate[SDL_SCANCODE_LEFT])
			move_camera(0.0f, -KEYBOARD_CAM_SPEED);
		if (keystate[SDL_SCANCODE_RIGHT])
			move_camera(0.0f, KEYBOARD_CAM_SPEED);
		if (keystate[SDL_SCANCODE_UP])
			move_camera(KEYBOARD_CAM_SPEED, 0.0f);

		while (SDL_PollEvent(&event))
			if (event.type == SDL_QUIT)
				/* FIXME */
				exit(0);
	}

	while (SDL_PollEvent(&event)) {
		gg_event_t gg_event;

		if (event.type == SDL_QUIT)
			/* FIXME */
			exit(0);

		if (event.type == SDL_MOUSEMOTION)
			set_mouse_pos(event.motion.x, event.motion.y);

		if (!gg_dialog_get_active() && event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
			retval = get_mouse_square();
			if (retval != -1)
				select_piece(retval);

			continue;
		}

		if (!gg_dialog_get_active() && event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT) {
			gg_dialog_open(dialog_system_create());

			continue;
		}

		if ((event.type == SDL_KEYDOWN && event.key.keysym.mod & KMOD_ALT && event.key.keysym.sym == SDLK_RETURN) ||
			(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F11)) {
			DBG_LOG("Toggled fullscreen");
			toggle_fullscreen();
			continue;
		}

		gg_event = convert_event(&event);

		if (gg_event.type == GG_EVENT_NONE)
			continue;

		if (gg_dialog_get_active())
			gg_dialog_input_current(gg_event);

		/* In the promote dialog */
		else if (gg_event.type == GG_EVENT_KEY)
			switch (gg_event.key) {
			case GG_KEY_LEFT:
				move_selector(SELECTOR_LEFT);
				break;
			case GG_KEY_RIGHT:
				move_selector(SELECTOR_RIGHT);
				break;
			case GG_KEY_UP:
				move_selector(SELECTOR_UP);
				break;
			case GG_KEY_DOWN:
				move_selector(SELECTOR_DOWN);
				break;
			case GG_KEY_ACTION:
				retval = get_selector();
				select_piece(retval);
				break;
			case GG_KEY_ESCAPE:
				gg_dialog_open(dialog_system_create());
				break;
			case 'g':
			case GG_KEY_EXTRA3:
				/* gg_dialog_open(dialog_ingame_create()); */
				break;
			case 'p':
				game_view_prev();
				break;
			case 'n':
				game_view_next();
				break;
			case 'r':
				game_retract_move();
				break;
			default:
				break;
			}
	}
	return retval;
}
예제 #3
0
int get_move()
{
    int retval = -1;
    int mousex, mousey;
    static Sint16 rotx, roty;
    SDL_Event event;
    Uint8 *keystate = SDL_GetKeyState(NULL);
    Uint8 mousestate = SDL_GetMouseState(&mousex, &mousey);

    if( mousestate & SDL_BUTTON_MIDDLE )
    {
        move_camera( -(mousey-last_mousey)*MOUSE_CAM_SPEED, -(mousex-last_mousex)*MOUSE_CAM_SPEED);
        last_mousex=mousex;
        last_mousey=mousey;
    }
    else
    {
        last_mousex=mousex;
        last_mousey=mousey;
    }

    if (keystate[SDLK_LCTRL])
    {
        if (keystate[SDLK_DOWN])
            move_camera(-KEYBOARD_CAM_SPEED, 0.0f);
        if (keystate[SDLK_LEFT])
            move_camera(0.0f, -KEYBOARD_CAM_SPEED);
        if (keystate[SDLK_RIGHT])
            move_camera(0.0f, KEYBOARD_CAM_SPEED);
        if (keystate[SDLK_UP])
            move_camera(KEYBOARD_CAM_SPEED, 0.0f);

        while (SDL_PollEvent( &event ))
            if (event.type == SDL_QUIT)
                /* FIXME */
                exit(0);
    }

    if ((roty < -3000) || (roty > 3000))
        move_camera(-roty / (float) 32768 * KEYBOARD_CAM_SPEED, 0.0f);

    if ((rotx < -3000) || (rotx > 3000))
        move_camera(0.0f, -rotx / (float) 32768 * KEYBOARD_CAM_SPEED);

    while ( SDL_PollEvent( &event ) )
    {
        gg_event_t gg_event;

        if (event.type == SDL_QUIT)
            /* FIXME */
            exit(0);

        if (event.type == SDL_MOUSEMOTION)
            set_mouse_pos( event.motion.x, event.motion.y );

        if (!gg_dialog_get_active() && event.type == SDL_MOUSEBUTTONDOWN &&
                event.button.button == SDL_BUTTON_LEFT)
        {
            retval = get_mouse_square();
            if (retval != -1)
                select_piece(retval);

            continue;
        }

        if (!gg_dialog_get_active() && event.type == SDL_MOUSEBUTTONDOWN &&
                event.button.button == SDL_BUTTON_RIGHT)
        {
            gg_dialog_open(dialog_system_create());

            continue;
        }

        if ((event.type == SDL_JOYAXISMOTION) && (event.jaxis.axis == AXIS_VIEW_X))
        {
            rotx = event.jaxis.value;
            continue;
        }

        if ((event.type == SDL_JOYAXISMOTION) && (event.jaxis.axis == AXIS_VIEW_Y))
        {
            roty = event.jaxis.value;
            continue;
        }

        if ( (event.type == SDL_KEYDOWN && event.key.keysym.mod & KMOD_ALT &&
            event.key.keysym.sym == SDLK_RETURN) || (event.type == SDL_KEYDOWN &&
            event.key.keysym.sym == SDLK_F11) )
        {
            DBG_LOG( "toggled fullscreen" );
            toggle_fullscreen();
            continue;
        }

        gg_event = convert_event(&event);

        if (gg_event.type == GG_EVENT_NONE)
            continue;

        if (gg_dialog_get_active())
            gg_dialog_input_current(gg_event);

        /* In the promote dialog */
        else if (gg_event.type == GG_EVENT_KEY)
            switch (gg_event.key)
            {
            case GG_KEY_LEFT:
                move_selector(SELECTOR_LEFT);
                break;
            case GG_KEY_RIGHT:
                move_selector(SELECTOR_RIGHT);
                break;
            case GG_KEY_UP:
                move_selector(SELECTOR_UP);
                break;
            case GG_KEY_DOWN:
                move_selector(SELECTOR_DOWN);
                break;
            case GG_KEY_ACTION:
                retval = get_selector();
                select_piece(retval);
                break;
            case GG_KEY_ESCAPE:
                gg_dialog_open(dialog_system_create());
                break;
            case 'g':
            case GG_KEY_EXTRA3:
                /* gg_dialog_open(dialog_ingame_create()); */
                break;
            case 'p':
                game_view_prev();
                break;
            case 'n':
                game_view_next();
                break;
            case 'u':
                game_undo();
                break;
            case 's':
                fen_encode(get_board());
                /* game_save(); */
                break;
            case 'l':
                /* load_game(); */
                break;
            default:
                break;
            }
    }
    return retval;
}