detection of mouse hovering using pdcurses

  Kiến thức lập trình

I am making an implemintation of conway’s game of life using c and wanted to make the game highlight the cell the mouse in hovering over but i can’t seem to be able to catch mouse movements even though i used ALL_MOUSE_EVENTS and MOUSE_MOVED flags

here is the input handling code :

void handle_mouse_input(){
    MEVENT event;

    if(nc_getmouse(&event) != OK) return ;
    
    if(event.x >= Play_Start_X && event.x <= Play_End_X && event.y == Play_Y){

        Pause = !Pause;

    }else if(event.x >= Exit_Start_X && event.x <= Exit_End_X && event.y == Exit_Y){
        quit();
    }else if(event.x >= State_Start_X && event.y >= State_Start_Y && event.x <= State_End_X && event.y <= State_End_Y){
        lock_state();

        cell_state new_state = alive;
        unsigned x = ((event.x - State_Start_X) / 2) + X_Indent , y = event.y - State_Start_Y + Y_Indent;
        if(lookup_cell_state(Board , x , y , false) == alive){
            new_state = dead;
        }

        set_cell(Board , x , y , new_state);

        unlock_state();
    }else if(event.x >= Reset_Start_X && event.x <= Reset_End_X && event.y == Reset_Y){
        reset_board(Board);
    }
}

//the purpose of making the function of this type is to be used with pthreads
void *handle_input(void *arg){
    if(stdscr == NULL) pthread_exit(NULL);

    keypad(stdscr , true);
    mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION , NULL);
    mouseinterval(0);
    noecho();
    int ch = 0;

    while(1){
        pthread_mutex_lock(&IO_Mutex);

        ch = getch();

        pthread_mutex_unlock(&IO_Mutex);

        if(ch == ERR){
            continue;
        }

        if(ch == KEY_MOUSE){
            handle_mouse_input();
        }else{
            handle_keyboard_input(ch);
        }

        flushinp();
    };

    return NULL;
}

I am running this on windows terminal

note : i am testing if i catch the mouse movement using a debugger thats why i didn’t write an if statement to check for bstate yet

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT