1
\$\begingroup\$

it's been all day I have a problem getting mousewheel events properly in one of my program. I can make a program from scratch and it work properly, however as soon as i do the same things inside this one, nothing seem to work as i want:

#include <iostream>

#include <SDL2/SDL.h>

int    SCREEN_WIDTH = 640;
int    SCREEN_HEIGHT = 480;

SDL_Event e;
SDL_Window* gWindow;

bool initSDL();

bool handleKeys();

void close();

bool initSDL()
{

    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
    }

    gWindow = SDL_CreateWindow( "Opengl Test",
                                SDL_WINDOWPOS_CENTERED,
                                SDL_WINDOWPOS_CENTERED,
                                SCREEN_WIDTH,
                                SCREEN_HEIGHT,
                                SDL_WINDOW_SHOWN );

    return true;
}

bool handleKeys()
{
    while( SDL_PollEvent( &e ) )
    {
        if ( e.type == SDL_MOUSEWHEEL )
        {
            std::cout << "WhhheeeeeelllllllLLLLLLLLlllllll" << std::endl;
        }

        if  ( e.type           == SDL_QUIT )
        {
            return true;
        } 

    }
}

void close()
{
    //Destroy Window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;

    //Quit SDL subsystem
    SDL_Quit();
}


int main()
{
    if(!initSDL())
    {
    }

    while( !handleKeys() )
    {
    }

    close();
}

I compile with this makefile:

#comment
#Compiler to use:
CC=g++
#Compiler flags:
APPNAME=TutOpengl
OBJ=main.o 
#LIBS=-lSDL2 
LIBS=-lSDL2 
CFLAGS=-c -g -Wall -std=c++11 -I ./src $(LIBS)

all: $(APPNAME) 

$(APPNAME): $(OBJ)
    $(CC) $(OBJ) -o $(APPNAME) $(LIBS) 

main.o: main.cpp
    $(CC) $(CFLAGS) main.cpp

clean:
    rm -rf *.o $(APPNAME) 

Under Ubuntu(14.04), SDL2. I can't get MouseWheel events properly and as of today i'm unable to find out. In the larger program set i get the MOUSEWHEELS events only every now and then like at random. Compiling should give you one warning, thanks in advance if anyone can help.

\$\endgroup\$
3
  • \$\begingroup\$ Does adding return false; to the end of your handleKeys() function solve the problem? For me your code (in VS2015) worked fine once I resolved all the warnings. \$\endgroup\$ – Tyyppi_77 Oct 4 '15 at 9:04
  • \$\begingroup\$ I'm starting to think it's something to deal with the compiler and the way maybe ignore certain parts of the code when the loops are fast, but my ignorance on the matter is abysmal so it's only speculation. Edit: Yes I even added the return false, also i even got all the handlekeys() instructions and inserted directly in the main, I get the same problem. Adding SDL_Delay(1) it's a workaround. \$\endgroup\$ – Renato Kuurstra Oct 4 '15 at 9:15
  • \$\begingroup\$ BTW, you should replace your if sequence inside of handleKeys() with switch statement. \$\endgroup\$ – HolyBlackCat Mar 28 '16 at 13:45
1
\$\begingroup\$

Adding a delay of 1 millisecond at the end of handlekeys() function seem to solve the issue entirely. I'm clueless why, will try to find a answer but so far... no luck.

Edit

According to a friend of mine it's common in event-built programs to suffer from "too fast loops". If the loops are too fast, in some ways, sometimes, you don't give time to update the states. I don't know the exact details, I'm not that good. But if I have to speculate it's something to deal with the automatic "sleep" that the compilers use nowday use when they identify loops void of significant instruction. But this is just speculation by me.

\$\endgroup\$
1
  • \$\begingroup\$ Yes, the "sleep" lets other thready-bits run. Many SDL2 programs have a V-sync delay around there. \$\endgroup\$ – david van brink Feb 26 '16 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.