Cannot restore mouse coordinates correctly after resizing window

The following is simplified code showing the problem I'm facing.

The purpose of the code is to create a 512x512 window and resize Y of its top-level surface to 512x (512 + 25) when a left click is detected. When another left-click is detected, we return sizes up to 512x512.

When a left-click event is detected or upon detection, mouseMotionEventwe display the coordinate of the mouse (s printf()).

Strange behavior is observed:

When I run the code, I clicked once, the Y size of the window changes, but when I move the mouse inside the newly created area, the displayed Y coordinates are anchored to 511.

Sometimes I donโ€™t get this strange behavior, then the Y-coordinate can be more than 511. To get strange behavior, click several times, quickly moving the mouse.

To compile (linux):

$ gcc -o test test.c `sdl-config --cflags --libs`

Source: (test.c)

#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>

/* Prototypes */
void event_handler(void);

/* global variables declaration */
SDL_Surface *screen=NULL;


/***** MAIN FUNCTION *****/
int main(int argc, char** argv)
{   
    /* init SDL */
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "Erreur ร  l'initialisation de la SDL : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    if ((screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE )) == NULL) {
        fprintf(stderr, "Graphic mode could not be correctly initialized : %s\n",     SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_WM_SetCaption("my window", NULL);

    /* handle event & wait until quit_event is raised */
    event_handler();    

    /* quit & return */
    SDL_Quit();
    return EXIT_SUCCESS;
}


/*** Event handler ***/
void event_handler(void)
{
    SDL_Event event;
    int quit=0;
    char message_is_displayed=0;
    SDL_Rect mess_coord = {0,512,512,512+25}; //{x_start,y_start,width,height}

    while(!quit)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                quit = 1;
                break;

            case SDL_MOUSEBUTTONDOWN:
                if (event.button.button == SDL_BUTTON_LEFT)
                {
                    if(!message_is_displayed)
                    {
                        screen = SDL_SetVideoMode(512,512+25, 32, SDL_SWSURFACE); //change the screen size
                        SDL_FillRect(screen, &mess_coord, SDL_MapRGB(screen->format, 255, 255, 255));  // fill in white the bottom area 
                    }
                    else
                    {
                        screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
                    }
                    message_is_displayed = !message_is_displayed;
                    SDL_Flip(screen);
                }
                printf("mouse position: (%d,%d)\n",event.button.x, event.button.y);
                break;

            case SDL_MOUSEMOTION:
                printf("mouse position: (%d,%d)\n",event.motion.x, event.motion.y);
                break;
        }
    }
}
+5
source share
3 answers

You can try the following functions to get x and y coordinates in oldx and oldy variables as

int oldx,oldy;    
oldx=wherex();
oldy=wherey();

works in windows.

0
source

If you are using SDL1, I think you will add it to your code:

int x, y;
SDL_GetMouseState(&x, &y);

Now the mouse position is in xand y.

0
source

, , . , .

SDL_MOUSEBUTTONUP. , (SDL_MOUSEBUTTONDOWN), .

0

All Articles