Example #1
0
//message handler (window procedure)
long DXGame::messageHandler(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){	
	switch(message){
	case WM_ACTIVATEAPP:
		ActiveApp = (wParam ? true : false); // is app active or not		
		break;
    case WM_CREATE:
		dprintf(("window created\n"));
		break;
    case WM_KEYDOWN: case WM_KEYUP:// case WM_SYSKEYDOWN: case WM_SYSKEYUP:
		keyboardHandler(message, wParam); break;	// key hit		
	case WM_LBUTTONUP: case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:  // mouse button
	case WM_RBUTTONUP: case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
		mouseButtonHandler(message, wParam, lParam); break;
	case WM_MOUSEMOVE:		// mouse move
		mouseMotionHandler(message, wParam, lParam); break;		
    case WM_DESTROY:		//end of game, cleanup
		dprintf(("destroying window\n"));		
		ShowCursor(TRUE);	//show the mouse cursor
		PostQuitMessage(0); //and exit
		break;	
    default: //default window procedure		
		return DefWindowProc(hwnd, message, wParam, lParam);
	}// end switch(message)
	return 0L;
} // messageHandler
Example #2
0
Keyboard::Keyboard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Keyboard)
{
    ui->setupUi(this);

    connect ( ui->Buttonq, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonw, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttone, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonr, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttont, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttony, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonu, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttoni, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttono, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonp, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );


    connect ( ui->Buttona, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttons, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttond, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonf, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttong, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonh, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonj, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonk, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonl, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );

    connect ( ui->Buttonz, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonx, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonc, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonv, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonb, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonn, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonm, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );

    connect ( ui->Button0, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button1, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button2, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button3, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button4, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button5, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button6, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button7, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button8, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button9, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );

    connect ( ui->space, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );

    outputText = "";
    shift = false;
    ui->lineEdit->setFocus();

}
Example #3
0
int game(WINDOW *left, WINDOW *bottom, WINDOW *textBox, WINDOW *middle,
    int sock, unsigned char id, unsigned char players,
    unsigned char bombs, unsigned char mines, struct data *pData,
    unsigned char field[16][16]){
    
    unsigned char i, toReceive, toDiscard, chatLen, y, x;
    unsigned char msgLen, len, isChatting, turn, bombing;
    //255-64
    char buffer[191];
    //65 to fit a 0 at the end before printing
    char chatBuffer[65];

    fd_set set;

    //Initialize what we have to
    isChatting = 0;
    toReceive = 0;
    toDiscard = 0;
    chatLen = 2;
    bombing = 0;
    msgLen = 0;
    turn = players + 1;
    chatBuffer[1] = 't';
    y = 7;
    x = 7;

    //Windows' timeval
#ifdef WIN32
    struct timeval timeVal;
    timeVal.tv_sec = 0;
    timeVal.tv_usec = 250;
#endif

    //Main loop
    while(mines){
        //Build the set
        FD_ZERO(&set);
#ifndef WIN32
        FD_SET(fileno(stdin), &set);
#endif
        FD_SET(sock, &set);

        //Wait for something to happen
#ifdef WIN32
        if(select(FD_SETSIZE, &set, NULL, NULL, &timeVal) < 0){
#else
        if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0){
#endif
            return 1;
        }

        //Read the keyboard first
#ifndef WIN32
        if(FD_ISSET(fileno(stdin), &set)){
#endif
            //Send the message if the handler says it's OK
            switch(keyboardHandler(middle, textBox, &chatLen, &isChatting,
                bombs, &bombing, &y, &x, id, turn, field, chatBuffer)){

                //Chat
                case 1:
                    //Set the size of the message and send it
                    chatBuffer[0] = chatLen - 1;
                    send(sock, chatBuffer, chatLen, 0);
                    //Cleanup
                    isChatting = 0;
                    mvwhline(textBox, 0, 0, ' ', COLS-18);
                    chatLen = 2;
                    wrefresh(textBox);
                    break;

                //Sweep
                case 2:
                    buffer[0] = 2;
                    buffer[1] = 's';
                    buffer[2] = (y << 4) | (x & 15);
                    send(sock, buffer, 3, 0);
                    break;

                //Bomb
                case 3:
                    buffer[0] = 2;
                    buffer[1] = 'b';
                    buffer[2] = (y << 4) | (x & 15);
                    send(sock, buffer, 3, 0);
                    bombs--;
                    updateBombs(left, id, bombs);
                    break;
            }
#ifndef WIN32
        }
#endif

        //Receive data
        //Read the keyboard first
        if(FD_ISSET(sock, &set)){
            //Receive if we can
            if(toReceive){
                len = recv(sock, buffer + (msgLen - toReceive) * sizeof(char),
                    toReceive, 0);

                if(len > 0){
                    toReceive -= len;
                    if(toReceive){
                        continue;
                    }
                }
            }
            //Discard if we have to
            else if(toDiscard){
                len = recv(sock, buffer, toDiscard, 0);
                if(len > 0){
                    toDiscard -= len;
                    continue;
                }
            }
            //Get the length of the new message
            else {
                len = recv(sock, buffer, 1, 0);
                //Get the length if we didn't lose connection
                if(len > 0){
                    //Adjust the values
                    if((unsigned char) buffer[0] > 64){
                        msgLen = 64;
                        toReceive = 64;
                        toDiscard = (unsigned char) buffer[0] - 64;
                    }
                    else {
                        msgLen = (unsigned char) buffer[0];
                        toReceive = (unsigned char) buffer[0];
                    }
                    continue;
                }
            }
            //Lost connection
            if(len <= 0){
                return 1;
            }

            //If we got down here, we're ready to parse

            //Chat
            if(buffer[0] == 't'){
                addLine(bottom, msgLen, buffer, pData);
            }
            //New player
            else if(buffer[0] == 'c'){
                //Copy name
                for(i = 2; i < msgLen; i++){
                    //Make it 7-bit
                    if(buffer[i] < 32){
                        buffer[i] = '?';
                    }
                    pData[(unsigned char) buffer[1]].name[i - 2] = buffer[i];
                }

                //Null terminate
                pData[(unsigned char) buffer[1]].name[i-2] = 0;

                //Update the stats
                pData[(unsigned char) buffer[1]].stats = 1;

                //Add player to the left panel
                addPlayer(left, bottom, (unsigned char) buffer[1], pData);
            }
            //Player left
            else if(buffer[0] == 'l'){
                //Update the stats
                pData[(unsigned char) buffer[1]].stats = 0;

                //Remove from the window
                removePlayer(left, bottom, (unsigned char) buffer[1], pData);
            }
            //New turn
            else if(buffer[0] == 'v'){
                //Update the stats
                turn = buffer[1];

                //Remove from the window
                tellTurn(bottom, turn, pData);

                //Draw the cursor if we're playing now
                if(turn == id){
                    bombing = 0;
                    startCursor(middle, &y, &x, bombing, field);
                }
                //Hide cursor
                else {
                    setValue(middle, y, x, field[y][x] & 15,
                        (field[y][x] & 240) >> 4);
                }
            }
            //Player won
            else if(buffer[0] == 'w'){
                return 0;
            }
            //Reveal
            else if(buffer[0] == 'r'){
                for(i = 1; i < msgLen; i += 2){
                    //Update the matrix
                    field[(buffer[i] & 240) >> 4][buffer[i] & 15] =
                        buffer[i+1];

                    //Update the screen
                    setValue(middle, (buffer[i] & 240) >> 4, buffer[i] & 15,
                        buffer[i+1], turn);

                    //Set the player and update the score
                    if(buffer[i+1] > 8){
                        field[(buffer[i] & 240) >> 4][buffer[i] & 15] |=
                            turn << 4;

                        pData[turn].score++;

                        updateMines(left, --mines);
                    }
                }
                //Update the score on the screen
                updateScore(left, turn, pData);
            }
        }
Example #4
0
int main(int argc, char **argv)
{
    cvNamedWindow("HandMade");

    // Setup capture object
    cv::VideoCapture cap(0);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

    int frameWidth = 640;
    int frameHeight = 480;

    // Setup preprocessor and detector
    Preprocess p(getBackground(cap));
    Detect d;

    Mat frame;

    // variables to draw
    Point prev;
    prev.x = -1;
    int count = 0;
    Mat whiteboard = Mat::zeros(frameHeight, frameWidth, CV_32F);

    // the main input loop

    while (true) {
        cap >> frame;

        Mat raw = frame.clone();

        frame = p(frame);
        imshow("processed", frame);

        // make the processed movie if necessary
        if(makeMovies) { 
	        char buffer[20];
	        sprintf(buffer, "processed/processed_%03d.jpg", count);
	        imwrite(buffer, frame);
	    }
            
        // call the detection module
        std::pair<vector<Point>, std::pair<Point, double>> handInfo = d(frame, raw, count);

        // get where the finger tips are from the detection module
        vector<Point>& tips = handInfo.first;
        std::pair<Point, double>& maxCircle = handInfo.second;

        // all five fingers are present, so erase
        if(tips.size() >= 10) {
            maxCircle.first.x = frameWidth - maxCircle.first.x;
            circle(whiteboard, maxCircle.first, 3.5*maxCircle.second, Scalar(0, 0, 0), -1);
        }

        // else, draw the first finger
        else if(tips.size() > 0) {
            if(prev.x != -1) {
                tips[0].x = frameWidth - tips[0].x;
                if(d.euclideanDist(prev, tips[0]) < 200)
                    line(whiteboard, prev, tips[0], Scalar(255, 255, 255), 3);
            }
            prev = tips[0];
        }

        
        imshow("HandMade", whiteboard);

        // output the board movie (if the flag is set)
        if(makeMovies) {
	        char buffer2[20];
	        sprintf(buffer2, "board/board_%03d.jpg", count);
			imwrite(buffer2, whiteboard);
    	}

        keyboardHandler();

        ++count;
    }
}