SDL_EventState toggle - c++

I've been using SDL_EventState as to disable and enable certain events. However for some reason once I set any state to either on or off, no where else in the program can I change it. To explain further, here is my loop.
void game::startLoop()
{
while(QUIT == false)
{
getRoomUpdate();
applySurface(-15, 280, zombie_lefthand, buffer);
applySurface(455, 280, zombie_righthand, buffer);
SDL_Flip(buffer);
SDL_WaitEvent(&gameEvent);
{
switch(gameEvent.type)
{
case SDL_QUIT:
QUIT = true;
break;
case SDL_KEYUP:
switch(gameEvent.key.keysym.sym)
{
case SDLK_LEFT:
if(isTwoWay == true)
SDL_EventState(SDL_KEYUP, 1);
startLoop();
log("Left key pressed.");
SDL_EventState(SDL_KEYUP, 0);
case SDLK_RIGHT:
if(isTwoWay == true)
SDL_EventState(SDL_KEYUP, 1);
startLoop();
log("Right key pressed.");
SDL_EventState(SDL_KEYUP, 0);
case SDLK_UP:
if(isTwoWay == false)
SDL_EventState(SDL_KEYUP, 1);
startLoop();
log("Up key pressed.");
SDL_EventState(SDL_KEYUP, 0);
default:
SDL_EventState(SDL_KEYUP, 0);
}
}
}
}
}

Perhaps if you tried someone else's code.
If it worked you could try to transform their code to something like yours(functionally wise)
Good Luck!
p.s. I think you should use SDL_ENABLE and SDL_DISABLE.

Related

Texture drawn at wrong coordinates?

I tried drawing a '1' texture at mouse coordinates when I press the 1 key:
switch (e.type)
{
case SDL_QUIT:
{
quit = true;
break;
}
case SDL_KEYDOWN:
{
switch (e.key.keysym.sym)
{
case SDLK_1:
{
SDL_Rect rect = {e.motion.x - 8, e.motion.y - 8, 16, 16};
SDL_RenderCopy(gRenderer, gT[3], NULL, &rect);
printf("1\n");
break;
}
}
break;
}
}
I cannot comprehend why this doesn't work.
gT[3] is the texture of the '1'.
I thought maybe it is because its e.key.keysym.sym but I'm not sure.
SDL_Event::motion is only valid when SDL_Event::type is SDL_MOUSEMOTION.
Stop trying to use SDL_Event::motion when SDL_Event::type is SDL_KEYDOWN, perhaps by recording the X and Y coordinates of the most recent SDL_MOUSEMOTION event and using those instead.

SDL 2.0 Key repeat and delay

I'm having a problem with SDL 2.0 keyboard input in pong-like game. When I order to move to the left by pressing left arrow, it is processed by SDL_PollEvents() and responds correctly if the key was pressed once. However, if I keep the key pressed, I get a short delay (as long as Windows key repeat delay) before moving continuously.
Here is function processing events:
void Event::PlayerEvent (Player &player)
{
while (SDL_PollEvent (&mainEvent))
{
switch (mainEvent.type)
{
case SDL_KEYDOWN :
switch (mainEvent.key.keysym.sym)
{
case SDLK_ESCAPE :
gameRunning = false;
break;
case SDLK_LEFT :
player.moving = player.left;
break;
case SDLK_RIGHT :
player.moving = player.right;
}
break;
case SDL_QUIT :
gameRunning = false;
}
}
}
EDIT: After all, I managed to fix this issue by calling
SystemParametersInfo (SPI_SETKEYBOARDDELAY, 0, 0, 0) at the start of the program and SystemParametersInfo (SPI_SETKEYBOARDDELAY, 1, 0, 0) at the end, to return to standard key repeat delay.
For game movement, you would typically not use events, but rather use states.
Try using SDL_GetKeyboardState() outside of the event loop:
const Uint8* keystates = SDL_GetKeyboardState(NULL);
...
if(keystates[SDL_SCANCODE_LEFT])
player.moving = player.left;
else if(keystates[SDL_SCANCODE_RIGHT])
player.moving = player.right;
using SPI_SETKEYBOARDDELAY is not a good approach. This way your game will not be protable anymore since its only available on Windows.
Instead you should use like menthiond in an answer before SDL_GetKeyboardState.
Howeve be aware that you still have to collect the SDL_PollEvent. Otherwise SDL_GetKeyboardState will be always empty.
So it should be like this:
//...
SDL_Event sdlEvent;
while (SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) {
//..
}
}
const Uint8* keystates = SDL_GetKeyboardState(NULL);
if(keystates[SDL_SCANCODE_LEFT]) {
//...
}
if(keystates[SDL_SCANCODE_RIGHT]) {
/...
}
//...
simple as that
int vertical = 0;
int horizontal = 0;
float x = 500;
float y = 500;
float speed = 5.0;
in your sdl loop:
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT: horizontal=-1; break;
case SDLK_RIGHT: horizontal = 1; break;
case SDLK_UP: vertical=-1; break;
case SDLK_DOWN: vertical=+1; break;
}
break;
}
case SDL_KEYUP:
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT: horizontal = 0; break;
case SDLK_RIGHT: horizontal = 0; break;
case SDLK_UP: vertical = 0; break;
case SDLK_DOWN: vertical = 0; break;
}
break;
}
}
}
x += horizontal * speed;
y += vertical * speed;
Use the SDL_GetKeyboardState capturing outside of - while (SDL_PollEvent (&mainEvent)), that works fine.

SDL_event list getting long and messy?

What is a good way of doing SDL event handling?
You usually have the:
while (SDL_PollEvent(&event)) {
//Handles all the events when in the menu screen...
eventsMenu(event);
}
Problem is, when you get going with a game, there's usually a mess of a list of controls that you can do, like up and down detetion for many different keys. And I'm wondering weither the method I'm using is efficient and clean. Or if I should approach it diffrently...
I have a function pointer in the mainLoop, that can be assigned to quickly change how the events will be handled. (Say I swap to another function, and it will use those events)
However, I think the list of events get messy anyways, so I tried adding regions to break it up. Is that a good idea? And yeah, just want some input if I'm on the right path to a readable code.
void Window::eventsMenu(SDL_Event event) {
switch (event.type) {
#pragma region "Button Down"
case SDL_MOUSEBUTTONDOWN: {
//printf("Mouse button down!\n");
glClearColor(0.1, 0.1, 0.1, 1);
if (event.button.button == SDL_BUTTON_LEFT) {
mouseButtonLeft = true;
}
break;
}
#pragma endregion;
#pragma region "Button Up"
case SDL_MOUSEBUTTONUP: {
//printf("Mouse button up!\n");
glClearColor(0, 0, 0, 1);
if (event.button.button == SDL_BUTTON_LEFT) {
mouseButtonLeft = false;
}
break;
}
#pragma endregion;
#pragma region "Mouse Motion"
case SDL_MOUSEMOTION: {
//printf("Mouse moved!\n");
if (mouseButtonLeft) {
rotX += event.motion.xrel;
rotY += event.motion.yrel;
}
break;
}
#pragma endregion;
#pragma region "Mouse Wheel"
case SDL_MOUSEWHEEL: {
if (event.wheel.y != 0) {
musicVolume += ((event.wheel.y > 0) ? 1 : -1);
if (musicVolume > 100) {
musicVolume = 100;
}
else if (musicVolume < 0) {
musicVolume = 0;
}
Mix_VolumeMusic(musicVolume);
printf("Volume: %i%c\n", musicVolume, '%');
}
if (event.wheel.y > 0) {
//printf("Scroll forward!\n");
}
else {
//printf("Scroll backward!\n");
}
break;
}
#pragma endregion;
#pragma region "Key Down"
case SDL_KEYDOWN: {
printf("Button [%s] pressed\n", SDL_GetKeyName(event.key.keysym.sym));
switch (event.key.keysym.sym) {
case SDLK_1: {
Mix_PlayChannel(-1, sound1, 0);
break;
}
case SDLK_2: {
Mix_PlayChannel(-1, sound2, 0);
break;
}
}
break;
}
#pragma endregion;
case SDL_QUIT: {
running = false;
}
}
}
Two suggestions :
Remove the braces ( { and } ) around the case labels. You don't need them unless you need a new stack.
My second suggestion is to split things into function. Even if it will only be called from within switch. Putting things into several functions helps makes the code easier to read and understand.
So for instance :
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
HandeMouseButton( event );
break;
void HandeMouseButton( const SDL_Event &event )
{
if ( event.type == MOUSEBUTTONDOWN )
{
glClearColor(0.1, 0.1, 0.1, 1);
if (event.button.button == SDL_BUTTON_LEFT) {
mouseButtonLeft = true;
}
else if ( event.type == MOUSEBUTTONUP )
glClearColor(0, 0, 0, 1);
if (event.button.button == SDL_BUTTON_LEFT) {
mouseButtonLeft = false;
}
}
}
And generally ( slightly opinion based ) ; if you need to use #pragma once to make the code readable, it can ( and should ) be split into more function

Change game controls in game

I'm programming game in C++ and Allegro 5, and I wanted to make controls settings. So if player wants, he can change controls in settings menu (in game).
I have this code:
while(!exit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(e_queue, &ev);
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_DOWN:
//player goes down...
break;
case ALLEGRO_KEY_UP:
//player goes up...
break;
case ALLEGRO_KEY_LEFT:
//player goes left...
break;
case ALLEGRO_KEY_RIGHT:
//player goes right...
break;
case ALLEGRO_KEY_SPACE:
break;
default:
break;
}
}
...
}
I thought, that I can make variable of ALLEGRO KEY CODE (i dont know if type like this exists) type, which has key code (for example ALLEGRO_KEY_LEFT), and than replace this code with:
...
allegro key code keyUP;
allegro key code keyDOWN;
...
while(!exit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(e_queue, &ev);
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case keyDOWN:
break;
case keyUP:
break;
case keyLEFT:
break;
case keyRIGHT:
break;
case keySPACE:
break;
default:
break;
}
}
}
and add in settings something like this:
//set 'UP' key
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_A:
keyUP = ALLEGRO_KEY_A;
break;
case ALLEGRO_KEY_B:
keyUP = ALLEGRO_KEY_B;
break;
case ALLEGRO_KEY_UP:
keyUP = ALLEGRO_KEY_UP;
break;
...
}
}
The point is, I don't know what type store things like ALLEGRO_KEY_UP, DOWN ...
Thanks!
I would have two maps, one that maps a function name to a function, and one that maps a key to either the function name or the function. When the user want to remap keys, change the second map.
It could be something like
std::unordered_map<std::string, std::function<void()>> function_map;
function_map["up"] = std::bind(&functionForUp);
function_map["down"] = std::bind(&functionForDown);
function_map["jump"] = std::bind(&functionForJump);
// etc.
std::unordered_map<int, std::string> key_map;
key_map[ALLEGRO_KEY_UP] = "up";
key_map[ALLEGRO_KEY_DOWN] = "down";
key_map[ALLEGRO_KEY_SPACE] = "jump";
// etc.
To call the function for a specific key, use e.g.
function_map[key_map[ev.keyboard.keycode]]();
This allows you to change the key_map at will:
key_map[ALLEGRO_KEY_J] = "jump";
References:
std::unordered_map
std::function
std::bind
All these Key Codes should be defined numbers. So maybe try using an integer.
int key = ALLEGRO_KEY_DOWN;
This should work.

SDL Input gives "fake-Events" on Startup

I've got a little Problem with SDL:
When starting my Program (on Archlinux 64bit), SDL_event gives me some SDL_KEYDOWN events. The annoying thing with that is, that my Program Quits, when pressing ESCAPE.
Thats the Code:
void Input::update() {
[...]
while(SDL_PollEvent(event)) {
if (event != NULL) {
handleInput();
}
}
// SDL_PumpEvents(); Commented out, because of jrok's suggestion.
SDL_GetMouseState(&mousex, &mousey);
}
And:
void Input::handleInput() {
switch(event->type) {
case SDL_KEYDOWN:
setKey(event->key, true);
break;
case SDL_KEYUP:
setKey(event->key, false);
break;
case SDL_QUIT:
setQuit();
break;
default:
break;
}
}
setKey() sets an Element of an bool-Array to the given value (true/false).
Also, its not only the Escape-key. Often the other keys are pressed too.
Okey, sorry. SDL has not got any bugs, and the code, i have shown to you is right.
The problem was, that I didn't initialize my boolean Array. So there were Random values left...
That helped:
for (int i = 0; i < MAX_VAL; i++) {
keys[i] = false;
}