# SDL2 event mouse wheel trouble

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.

• 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. – Tyyppi_77 Oct 4 '15 at 9:04
• 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. – Renato Kuurstra Oct 4 '15 at 9:15
• BTW, you should replace your if sequence inside of handleKeys() with switch statement. – HolyBlackCat Mar 28 '16 at 13:45