Problem with SDL events

hello everyone,
i’m working with sdl on an audio game, and i’m writing a menu for it
but, when i move using up and down arrow keys, it doesn’t work
this is the menu code:
*menu.hpp
#ifndef MENU_HPP
#define MENU_HPP

#include <sdl2/sdl.h>
#include
#include
#include <soloud.h>
#include <tolk.h>

/*
*the menu class
*/
class menu
{
private:
std::vectorstd::wstring menu_items;
int pos;
bool running;
public:
//this function add’s an item to the menu
void add(std::wstring item);
//this function reset’s the menu to it’s default state which has no item in it
void reset();
//this function return’s the count in menu
int get_count();
//this function return our position in menu
int get_position();
//this function run’s the menu and wait’s for the user to select
int run(std::wstring text);
//this function determines whether the menu is running or not
bool is_running();
};

#endif

*menu.cpp
#include “menu.hpp”
#include “vars.hpp”
#include <sdl2/sdl.h>

//uses
using namespace std;

bool menu::is_running()
{
return running;
}

int menu::run(wstring text)
{
Tolk_Speak(text.c_str(), true);
running=true;
SDL_Event e;
while(SDL_PollEvent(&e))
{
if(e.type==SDL_KEYDOWN)
{
if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_DOWN)
{
pos++;
Tolk_Speak(menu_items[pos].c_str(), true);
}
else if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_UP)
{
pos–;
Tolk_Speak(menu_items[pos].c_str(), true);
}
else if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_RETURN)
{
return get_position();
}
if(pos>get_count())
{
pos=get_count();
}
else if(pos<0)
{
pos=0;
}
}
}
}

int menu::get_count()
{
return menu_items.size();
}

int menu::get_position()
{
return pos;
}

void menu::add(wstring item)
{
menu_items.push_back(item);
}

void menu::reset()
{
running=false;
menu_items.clear();
}

*main.cpp
#include <sdl2/sdl.h>
#include <stdio.h>
#include <stdlib.h>
#include <tolk.h>
#include <windows.h>
#include <soloud.h>
#include <soloud_wav.h>
#include <soloud_wavstream.h>
#include <physfs.h>
#include “updater.hpp”
#include “menu.hpp”
#include “vars.hpp”
#include “functions.hpp”
#include “keyhook.hpp”
#include “gamedata.hpp”
#include “logger.hpp”

int SDL_main(int argc, char** argv)
{
quit=false; //set our boolean quit function to false
if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
{
exit(1);
return 1;
} //if SDL was not initialized, exit
s.init(); //initialize SoLoud
PHYSFS_init(argv[0]); //our filesystem library: physicsFS
CoInitialize(nullptr); //initialize com
Tolk_Load(); //initialize tolk: our screen reading library
atexit(exitfunc); //exitfunc() should be called when we wannt to exit
//setup our menu
menu m;
m.add(L"start game");
m.add(L"game options");
m.add(L"exit");
int r=m.run(L"select an item from the list then press enter to
activate that item");
//create and show our window
w=SDL_CreateWindow(“missiontime”, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_FULLSCREEN|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED|SDL_WINDOW_SHOWN);
if(!w)
{
MessageBox(nullptr, “error”, SDL_GetError(), MB_ICONERROR|MB_OK);
quit=true;
exit(1);
return 1;
} //if not created (nullptr), give an error and exit
while(!quit)
{
SDL_ShowWindow(w);
SDL_Event e;
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
quit=true;
break;
}
}
}

return 0;
}

the window show’s, but i can’t move in menu
no crash occurs, (at least that i’ve debugged it)
thanks in advance

SDL_PollEvent “eats” events away from the event queue. Let’s say you move
your mouse. One SDL_MouseMove event will be added to your queue. Then
SDL_PollEvent in your main while loop is called. It sees that it’s not a
quit event, so it continues. Then your menu’s update function is called, in
which in turn SDL_PollEvent is called. Since in the while loop the Mouse
move event was polled and discarded, this time the pollevent will give you
nothing. So the menu doesn’t ever change because all the events are taken
before he sees them.

You can solve this in two ways: one, have one “event loop” that polls for
events and then passes the event to all subsystems that need events
before continuing. So it should pass it to your menu class, but also to
some functiom that checks for exit conditions. Two, you could use
SDL_PeekEvent (might be called differently) to “peek” what the next event
is without removing it. Then, at the end of one frame, call PollEvent to
move on to the next event. This is quite bad practice I believe, but it’s
an option.

Also, not unimportant: you don’t call the run function in your main while
loop (the one with !quit). So the menu’s “run” function is only called
once! To make it work, make sure it’s called every frame.
Op 1 mrt. 2016 8:06 a.m. schreef “Amir Ramezani” <
amir.ramezani1370 at gmail.com>:> hello everyone,

i’m working with sdl on an audio game, and i’m writing a menu for it
but, when i move using up and down arrow keys, it doesn’t work
this is the menu code:
*menu.hpp
#ifndef MENU_HPP
#define MENU_HPP

#include <sdl2/sdl.h>
#include
#include
#include <soloud.h>
#include <tolk.h>

/*
*the menu class
*/
class menu
{
private:
std::vectorstd::wstring menu_items;
int pos;
bool running;
public:
//this function add’s an item to the menu
void add(std::wstring item);
//this function reset’s the menu to it’s default state which has no item
in it
void reset();
//this function return’s the count in menu
int get_count();
//this function return our position in menu
int get_position();
//this function run’s the menu and wait’s for the user to select
int run(std::wstring text);
//this function determines whether the menu is running or not
bool is_running();
};

#endif

*menu.cpp
#include “menu.hpp”
#include “vars.hpp”
#include <sdl2/sdl.h>

//uses
using namespace std;

bool menu::is_running()
{
return running;
}

int menu::run(wstring text)
{
Tolk_Speak(text.c_str(), true);
running=true;
SDL_Event e;
while(SDL_PollEvent(&e))
{
if(e.type==SDL_KEYDOWN)
{
if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_DOWN)
{
pos++;
Tolk_Speak(menu_items[pos].c_str(), true);
}
else if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_UP)
{
pos–;
Tolk_Speak(menu_items[pos].c_str(), true);
}
else if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_RETURN)
{
return get_position();
}
if(pos>get_count())
{
pos=get_count();
}
else if(pos<0)
{
pos=0;
}
}
}
}

int menu::get_count()
{
return menu_items.size();
}

int menu::get_position()
{
return pos;
}

void menu::add(wstring item)
{
menu_items.push_back(item);
}

void menu::reset()
{
running=false;
menu_items.clear();
}

*main.cpp
#include <sdl2/sdl.h>
#include <stdio.h>
#include <stdlib.h>
#include <tolk.h>
#include <windows.h>
#include <soloud.h>
#include <soloud_wav.h>
#include <soloud_wavstream.h>
#include <physfs.h>
#include “updater.hpp”
#include “menu.hpp”
#include “vars.hpp”
#include “functions.hpp”
#include “keyhook.hpp”
#include “gamedata.hpp”
#include “logger.hpp”

int SDL_main(int argc, char** argv)
{
quit=false; //set our boolean quit function to false
if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
{
exit(1);
return 1;
} //if SDL was not initialized, exit
s.init(); //initialize SoLoud
PHYSFS_init(argv[0]); //our filesystem library: physicsFS
CoInitialize(nullptr); //initialize com
Tolk_Load(); //initialize tolk: our screen reading library
atexit(exitfunc); //exitfunc() should be called when we wannt to exit
//setup our menu
menu m;
m.add(L"start game");
m.add(L"game options");
m.add(L"exit");
int r=m.run(L"select an item from the list then press enter to
activate that item");
//create and show our window
w=SDL_CreateWindow(“missiontime”, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480,

SDL_WINDOW_FULLSCREEN|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED|SDL_WINDOW_SHOWN);
if(!w)
{
MessageBox(nullptr, “error”, SDL_GetError(), MB_ICONERROR|MB_OK);
quit=true;
exit(1);
return 1;
} //if not created (nullptr), give an error and exit
while(!quit)
{
SDL_ShowWindow(w);
SDL_Event e;
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
quit=true;
break;
}
}
}

return 0;
}

the window show’s, but i can’t move in menu
no crash occurs, (at least that i’ve debugged it)
thanks in advance


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

hi again,
if i call it in while(!quit), my screen reader read’s select an item
from the menu and press enter to activate and i can’t make it silent
i can’t do anything then
so, how to pass all events to all the sub-systems?
if i press the keys, it doesn’t read anything!
thanks

???-??-?? ??:?? +??:?? ???, Bob Rubbens :> SDL_PollEvent “eats” events away from the event queue. Let’s say you move

your mouse. One SDL_MouseMove event will be added to your queue. Then
SDL_PollEvent in your main while loop is called. It sees that it’s not a
quit event, so it continues. Then your menu’s update function is called, in
which in turn SDL_PollEvent is called. Since in the while loop the Mouse
move event was polled and discarded, this time the pollevent will give you
nothing. So the menu doesn’t ever change because all the events are taken
before he sees them.

You can solve this in two ways: one, have one “event loop” that polls for
events and then passes the event to all subsystems that need events
before continuing. So it should pass it to your menu class, but also to
some functiom that checks for exit conditions. Two, you could use
SDL_PeekEvent (might be called differently) to “peek” what the next event
is without removing it. Then, at the end of one frame, call PollEvent to
move on to the next event. This is quite bad practice I believe, but it’s
an option.

Also, not unimportant: you don’t call the run function in your main while
loop (the one with !quit). So the menu’s “run” function is only called
once! To make it work, make sure it’s called every frame.
Op 1 mrt. 2016 8:06 a.m. schreef “Amir Ramezani” <
@Amir_Ramezani>:

hello everyone,
i’m working with sdl on an audio game, and i’m writing a menu for it
but, when i move using up and down arrow keys, it doesn’t work
this is the menu code:
*menu.hpp
#ifndef MENU_HPP
#define MENU_HPP

#include <sdl2/sdl.h>
#include
#include
#include <soloud.h>
#include <tolk.h>

/*
*the menu class
*/
class menu
{
private:
std::vectorstd::wstring menu_items;
int pos;
bool running;
public:
//this function add’s an item to the menu
void add(std::wstring item);
//this function reset’s the menu to it’s default state which has no item
in it
void reset();
//this function return’s the count in menu
int get_count();
//this function return our position in menu
int get_position();
//this function run’s the menu and wait’s for the user to select
int run(std::wstring text);
//this function determines whether the menu is running or not
bool is_running();
};

#endif

*menu.cpp
#include “menu.hpp”
#include “vars.hpp”
#include <sdl2/sdl.h>

//uses
using namespace std;

bool menu::is_running()
{
return running;
}

int menu::run(wstring text)
{
Tolk_Speak(text.c_str(), true);
running=true;
SDL_Event e;
while(SDL_PollEvent(&e))
{
if(e.type==SDL_KEYDOWN)
{
if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_DOWN)
{
pos++;
Tolk_Speak(menu_items[pos].c_str(), true);
}
else if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_UP)
{
pos–;
Tolk_Speak(menu_items[pos].c_str(), true);
}
else if(e.key.state==SDL_PRESSED&&e.key.keysym.sym==SDLK_RETURN)
{
return get_position();
}
if(pos>get_count())
{
pos=get_count();
}
else if(pos<0)
{
pos=0;
}
}
}
}

int menu::get_count()
{
return menu_items.size();
}

int menu::get_position()
{
return pos;
}

void menu::add(wstring item)
{
menu_items.push_back(item);
}

void menu::reset()
{
running=false;
menu_items.clear();
}

*main.cpp
#include <sdl2/sdl.h>
#include <stdio.h>
#include <stdlib.h>
#include <tolk.h>
#include <windows.h>
#include <soloud.h>
#include <soloud_wav.h>
#include <soloud_wavstream.h>
#include <physfs.h>
#include “updater.hpp”
#include “menu.hpp”
#include “vars.hpp”
#include “functions.hpp”
#include “keyhook.hpp”
#include “gamedata.hpp”
#include “logger.hpp”

int SDL_main(int argc, char** argv)
{
quit=false; //set our boolean quit function to false
if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
{
exit(1);
return 1;
} //if SDL was not initialized, exit
s.init(); //initialize SoLoud
PHYSFS_init(argv[0]); //our filesystem library: physicsFS
CoInitialize(nullptr); //initialize com
Tolk_Load(); //initialize tolk: our screen reading library
atexit(exitfunc); //exitfunc() should be called when we wannt to exit
//setup our menu
menu m;
m.add(L"start game");
m.add(L"game options");
m.add(L"exit");
int r=m.run(L"select an item from the list then press enter to
activate that item");
//create and show our window
w=SDL_CreateWindow(“missiontime”, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480,

SDL_WINDOW_FULLSCREEN|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED|SDL_WINDOW_SHOWN);
if(!w)
{
MessageBox(nullptr, “error”, SDL_GetError(), MB_ICONERROR|MB_OK);
quit=true;
exit(1);
return 1;
} //if not created (nullptr), give an error and exit
while(!quit)
{
SDL_ShowWindow(w);
SDL_Event e;
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
quit=true;
break;
}
}
}

return 0;
}

the window show’s, but i can’t move in menu
no crash occurs, (at least that i’ve debugged it)
thanks in advance


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org