What is e in
SDL_Event e;
How to pass e from one function to another function then to another function.
is it possible?
Basically is e bool or int
bool x_out_window()
{
bool quit = false;
SDL_Event e;
while( !quit )
{
while( SDL_PollEvent( &e ) != 0 )
{
if( e.type == SDL_QUIT )
quit = true;
else
detect_key_press(e.type);
}
}
}
bool detect_key_press(e.type)
{
//SDL_Event e;
if( e.type == SDL_KEYDOWN )
{
//Select surfaces based on key press
switch( e.key.keysym.sym )
{
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ];
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ];
break;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ];
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
break;
default:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
break;
}
SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );
SDL_UpdateWindowSurface( gWindow );
}
}
I am sorry if this is a silly question but i am unable to understand the concept, i am novice in coding.
Just declare your function 2 as
void funtion2(SDL_Event e){
//...
}
in C++ you need to provide a type for both the function (probably void here) and all the parameters (a SDL_Event named e here)
Related
I have tried to write a code that creates a window which shows a particular image when pressed a particular arrow key and closes the window when the x button(on the screen) is clicked:
#include <SDL.h>
#include <iostream>
#include <string>
using namespace std;
enum Gamestate
{
Play, Exit
};
enum Keypress
{
KEY_PRESS_DEFAULT,
KEY_PRESS_UP,
KEY_PRESS_DOWN,
KEY_PRESS_RIGHT,
KEY_PRESS_LEFT,
KEY_PRESS_TOTAL,
};
class Game
{
public:
Game();
~Game();
void run();
private:
//data members
int WINDOW_WIDTH;
int WINDOW_HEIGHT;
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
SDL_Surface* loadSurface[ KEY_PRESS_TOTAL ];
SDL_Surface* defaultSurface = NULL;
Gamestate _gamestate;
private:
//methods
void init();
void gameLoop();
void processInput();
void updateWindowSurface();
};
void error( string error1 )
{
int itq;
cout << error1 << endl;
SDL_GetError();
cout << endl << "Enter any key to quit" << endl;
cin >> itq;
SDL_Quit();
}
Game::Game()
{
WINDOW_WIDTH = 640;
WINDOW_HEIGHT = 480;
_gamestate = Play;
}
void Game::run()
{
init();
gameLoop();
}
void Game::init()
{
SDL_Init( SDL_INIT_EVERYTHING );
window = SDL_CreateWindow( "GAME", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
error( "SDL could not create a window!" );
}
screenSurface = SDL_GetWindowSurface( window );
if( window == NULL )
{
error( "SDL could not create a screen!" );
}
loadSurface[ KEY_PRESS_DEFAULT ] = SDL_LoadBMP( "press.bmp" );
if( loadSurface[ KEY_PRESS_DEFAULT ] == NULL )
{
error( "Unable to load the Surface!" );
}
loadSurface[ KEY_PRESS_UP ] = SDL_LoadBMP( "up.bmp" );
if( loadSurface[ KEY_PRESS_UP ] == NULL )
{
error( "Unable to load Image!" );
}
loadSurface[ KEY_PRESS_DOWN ] = SDL_LoadBMP( "down.bmp" );
if( loadSurface[ KEY_PRESS_DOWN ] == NULL )
{
error( "Unable to load Image!" );
}
loadSurface[ KEY_PRESS_RIGHT ] = SDL_LoadBMP( "right.bmp" );
if( loadSurface[ KEY_PRESS_RIGHT ] == NULL )
{
error( "Unable to load Image!" );
}
loadSurface[ KEY_PRESS_LEFT ] = SDL_LoadBMP( "left.bmp" );
if( loadSurface[ KEY_PRESS_LEFT ] == NULL )
{
error( "Unable to load Image!" );
}
defaultSurface = loadSurface[ KEY_PRESS_DEFAULT ];
}
void Game::gameLoop()
{
while( _gamestate != Exit )
{
processInput();
updateWindowSurface();
}
}
void Game::processInput()
{
SDL_Event e;
while( SDL_PollEvent( &e ) != 0 )
{
if( e.type == SDL_QUIT )
{
_gamestate = Exit;
}
else if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
case SDLK_UP:
defaultSurface = loadSurface[ KEY_PRESS_UP ];
break;
case SDLK_DOWN:
defaultSurface = loadSurface[ KEY_PRESS_DOWN ];
break;
case SDLK_RIGHT:
defaultSurface = loadSurface[ KEY_PRESS_RIGHT ];
break;
case SDLK_LEFT:
defaultSurface = loadSurface[ KEY_PRESS_LEFT ];
break;
default:
defaultSurface = loadSurface[ KEY_PRESS_DEFAULT ];
}
}
}
}
void Game::updateWindowSurface()
{
SDL_BlitSurface( defaultSurface, NULL, screenSurface, NULL );
SDL_UpdateWindowSurface( window );
}
Game::~Game()
{
for( int i = 0; i <= KEY_PRESS_TOTAL; i++ )
{
SDL_FreeSurface( loadSurface[ i ] );
loadSurface[ i ] = NULL;
}
SDL_DestroyWindow( window );
window = NULL;
SDL_Quit();
}
int main( int argc, char* args[] )
{
Game game;
game.run();
return 0;
}
When I press the arrow keys the images which display is correct but when I try to close it, the program crashes. But when I try closing the window before pressing any arrow keys it works just fine.
Can any one help me with it?
I want to create my first game.
Nothing special, just blue rectangle moving when WSAD keys are pressed.
The problem is that when I run my game, there are bugs with rectangle(See image below). Bugs appears only during horizontal movement, and not vertical.
Which is interesting, when I changed line:
renderer = SDL_CreateRenderer(display, -1, SDL_RENDERER_ACCELERATED)
to:
renderer = SDL_CreateRenderer(display, -1, SDL_RENDERER_SOFTWARE)
everything is OK
I am using Windows 10, MinGw with CMake(C++14), and SDL 2.0.8, Intel core i5 7th gen, Radeon M7 R465
Im my code OnRender function is responsible for rendering, maybe I made something wrong in it?(Function in my code posted at end of question)
I am also using SDL_WINDOW_OPENGL flag to create my window, but changing it to SDL_WINDOW_SHOWN doesn't change anything.
#include <SDL2/SDL.h>
class Game
{
private:
SDL_Surface *display_surf = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Window *display = nullptr;
private:
bool running, prW = false, prS = false, prD = false, prA = false;
int x, y;
int spd_y, spd_x;
int scr_w, scr_h;
public:
Game();
int OnExecute();
public:
bool OnInit();
void OnEvent( SDL_Event *event );
void OnLoop();
void OnRender();
void OnCleanup();
};
Game::Game()
{
running = false;
}
int Game::OnExecute()
{
if( !OnInit() )
{
return -1;
}
running = true;
SDL_Event event;
while( running )
{
while( SDL_PollEvent( &event ) )
{
OnEvent( &event );
}
OnLoop();
OnRender();
SDL_Delay( 1 );
}
OnCleanup();
return 0;
}
bool Game::OnInit()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}
SDL_DisplayMode dspm;
if( SDL_GetDesktopDisplayMode( 0, &dspm ) < 0 )
{
return false;
}
scr_h = dspm.h;
scr_w = dspm.w;
if( ( display = SDL_CreateWindow( "Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080,
SDL_WINDOW_OPENGL ) ) == nullptr )
{
return false;
}
display_surf = SDL_GetWindowSurface( display );
if( ( renderer = SDL_CreateRenderer( display, -1, SDL_RENDERER_ACCELERATED ) ) == nullptr )
{
return false;
}
x = 0;
y = 0;
spd_x = 0;
spd_y = 0;
SDL_SetWindowFullscreen( display, SDL_WINDOW_FULLSCREEN );
return true;
}
void Game::OnEvent( SDL_Event *event )
{
if( event->type == SDL_QUIT )
{
running = false;
return;
}
switch( event->type )
{
case SDL_KEYDOWN:
switch( event->key.keysym.sym )
{
case SDLK_w:
if( prS )
{
spd_y = 0;
}
else
{
spd_y = -5;
}
prW = true;
break;
case SDLK_s:
if( prW )
{
spd_y = 0;
}
else
{
spd_y = 5;
}
prS = true;
break;
case SDLK_d:
if( prA )
{
spd_x = 0;
}
else
{
spd_x = 5;
}
prD = true;
break;
case SDLK_a:
if( prD )
{
spd_x = 0;
}
else
{
spd_x = -5;
}
prA = true;
break;
default:
return;
}
break;
case SDL_KEYUP:
switch( event->key.keysym.sym )
{
case SDLK_w:
if( !prS )
{
spd_y = 0;
}
else
{
spd_y = 5;
}
prW = false;
break;
case SDLK_s:
if( !prW )
{
spd_y = 0;
}
else
{
spd_y = -5;
}
prS = false;
break;
case SDLK_a:
if( !prD )
{
spd_x = 0;
}
else
{
spd_x = 5;
}
prA = false;
break;
case SDLK_d:
if( !prA )
{
spd_x = 0;
}
else
{
spd_x = -5;
}
prD = false;
break;
default:
return;
}
default:
return;
}
}
void Game::OnLoop()
{
x += spd_x;
y += spd_y;
if( x < 0 )
{
x = 0;
}
else if( x > scr_w - 100 )
{
x = scr_w - 100;
}
if( y < 0 )
{
y = 0;
}
else if( y > scr_h - 100 )
{
y = scr_h - 100;
}
}
void Game::OnRender()
{
SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0x00 );
SDL_RenderClear( renderer );
SDL_Rect charc;
charc.x = x;
charc.y = y;
charc.w = 100;
charc.h = 100;
SDL_SetRenderDrawColor( renderer, 0, 0, 0xff, 0 );
SDL_RenderFillRect( renderer, &charc );
SDL_RenderPresent( renderer );
}
void Game::OnCleanup()
{
SDL_DestroyWindow( display );
SDL_Quit();
}
int main( int argc, char** argv )
{
Game game;
return game.OnExecute();
}
Looks a lot like tearing caused by a high frame-rate & lack of vsync.
I can get tear-less drawing by passing SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC to flags on SDL_CreateRenderer():
#include <SDL2/SDL.h>
#include <iostream>
class Game
{
private:
SDL_Renderer *renderer = nullptr;
SDL_Window *display = nullptr;
private:
bool running, prW = false, prS = false, prD = false, prA = false;
int x, y;
int spd_y, spd_x;
int scr_w, scr_h;
public:
Game();
int OnExecute();
public:
bool OnInit();
void OnEvent( SDL_Event *event );
void OnLoop();
void OnRender();
void OnCleanup();
};
Game::Game()
{
running = false;
}
int Game::OnExecute()
{
if( !OnInit() )
{
return -1;
}
running = true;
SDL_Event event;
Uint32 beg = SDL_GetTicks();
size_t frames = 0;
while( running )
{
while( SDL_PollEvent( &event ) )
{
OnEvent( &event );
}
OnLoop();
OnRender();
frames++;
Uint32 end = SDL_GetTicks();
if( end - beg > 1000 )
{
std::cout << "Frame time: " << ( end - beg ) / frames << " ms" << std::endl;
beg = end;
frames = 0;
}
}
OnCleanup();
return 0;
}
bool Game::OnInit()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}
if( ( display = SDL_CreateWindow( "Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0 ) ) == nullptr )
{
return false;
}
scr_w = 1280;
scr_h = 720;
Uint32 rflags = SDL_RENDERER_ACCELERATED;
rflags |= SDL_RENDERER_PRESENTVSYNC;
if( ( renderer = SDL_CreateRenderer( display, -1, rflags ) ) == nullptr )
{
return false;
}
x = 0;
y = 0;
spd_x = 0;
spd_y = 0;
return true;
}
void Game::OnEvent( SDL_Event *event )
{
if( event->type == SDL_QUIT )
{
running = false;
return;
}
switch( event->type )
{
case SDL_KEYDOWN:
switch( event->key.keysym.sym )
{
case SDLK_w:
if( prS )
{
spd_y = 0;
}
else
{
spd_y = -5;
}
prW = true;
break;
case SDLK_s:
if( prW )
{
spd_y = 0;
}
else
{
spd_y = 5;
}
prS = true;
break;
case SDLK_d:
if( prA )
{
spd_x = 0;
}
else
{
spd_x = 5;
}
prD = true;
break;
case SDLK_a:
if( prD )
{
spd_x = 0;
}
else
{
spd_x = -5;
}
prA = true;
break;
default:
return;
}
break;
case SDL_KEYUP:
switch( event->key.keysym.sym )
{
case SDLK_w:
if( !prS )
{
spd_y = 0;
}
else
{
spd_y = 5;
}
prW = false;
break;
case SDLK_s:
if( !prW )
{
spd_y = 0;
}
else
{
spd_y = -5;
}
prS = false;
break;
case SDLK_a:
if( !prD )
{
spd_x = 0;
}
else
{
spd_x = 5;
}
prA = false;
break;
case SDLK_d:
if( !prA )
{
spd_x = 0;
}
else
{
spd_x = -5;
}
prD = false;
break;
default:
return;
}
default:
return;
}
}
void Game::OnLoop()
{
x += spd_x;
y += spd_y;
if( x < 0 )
{
x = 0;
}
else if( x > scr_w - 100 )
{
x = scr_w - 100;
}
if( y < 0 )
{
y = 0;
}
else if( y > scr_h - 100 )
{
y = scr_h - 100;
}
}
void Game::OnRender()
{
SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0x00 );
SDL_RenderClear( renderer );
SDL_Rect charc;
charc.x = x;
charc.y = y;
charc.w = 100;
charc.h = 100;
SDL_SetRenderDrawColor( renderer, 0, 0, 0xff, 0 );
SDL_RenderFillRect( renderer, &charc );
SDL_Delay( 1 );
SDL_RenderPresent( renderer );
}
void Game::OnCleanup()
{
SDL_DestroyWindow( display );
SDL_Quit();
}
int main( int argc, char** argv )
{
Game game;
return game.OnExecute();
}
If I just pass SDL_RENDERER_ACCELERATED I get tearing and a vastly higher frame-rate.
Make sure your OS isn't configured to disable vsync by default.
Many developers seem to get some unwanted behaviours when enabling SDL_RENDERER_ACCELERATED flag on SDL 2.0.8.
A ticket has been opened in libsdl's bugzilla (https://bugzilla.libsdl.org/show_bug.cgi?id=4110). It's about another problem but issue with hardware rendering is mentioned in description.
For now, I use software rendering (SDL_RENDERER_SOFTWARE) as a fallback. Not really what I wanted to do but now I get the expected result.
I'll try PREVENTSYNC...
I'm making a simple top down 2D shooter, where you move with WASD and shoot in the direction you're looking at with space. I can move and shoot just fine, but I can't do them both at the same time. For example if I'm shooting and start moving, my character will stop shooting until I release and press space again, and if I then start moving in another direction I have to release and press space again.
Here's my main method:
int main(int argc, char* args[])
{
if (!init())
{
log("Failed to initialize!\n");
}
else
{
log("Initialized SDL and SDL subsystems. \nLoading assets:\n");
if (!loadAssets())
{
printf("Failed to load assets!\n");
}
else
{
log("All assets loaded successfully.\n");
bool running = true;
SDL_Event e;
std::vector<Shot> shots;
LTimer shotTimer;
float cooldown = 250.0f;
float previousCooldown = 0.0f;
Player player;
log("Game running.\n");
shotTimer.start();
while (running)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
log("SDL_QUIT event triggered.\n");
running = false;
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
{
running = false;
break;
}
}
}
player.handleEvent(e);
if (SDL_GetTicks() - previousCooldown > cooldown)
{
previousCooldown = SDL_GetTicks();
shots = shoot(e, player, shots);
}
}
player.move();
for (int i = 0; i < shots.size(); i++)
{
shots[i].move();
}
/*if (shotTimer.getTicks() >= cooldown)
shotTimer.restart();*/
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderClear(renderer);
background.render(0, 0);
for (int i = 0; i < shots.size(); i++)
{
shots[i].render();
}
player.render();
SDL_RenderPresent(renderer);
}
}
}
close();
return 0;
}
Here's my shoot() function, which is probably what's causing this:
std::vector<Shot> shoot(SDL_Event& e, Player player, std::vector<Shot> shots)
{
bool shoot = false;
if (e.type = SDL_KEYDOWN && e.key.repeat == 1)
{
switch (e.key.keysym.sym)
{
case SDLK_SPACE:
{
shoot = true;
break;
}
}
}
else if (e.type = SDL_KEYUP && e.key.repeat == 1)
{
switch (e.key.keysym.sym)
{
case SDLK_SPACE:
{
shoot = false;
break;
}
}
}
if (shoot)
{
Shot newShot(player.getDir(), player);
shots.push_back(newShot);
}
return shots;
}
And this is how I move the player
void Player::handleEvent(SDL_Event& e)
{
if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_w: mVelY -= PLAYER_VEL; mDir = 0; break;
case SDLK_s: mVelY += PLAYER_VEL; mDir = 1; break;
case SDLK_d: mVelX += PLAYER_VEL; mDir = 2; break;
case SDLK_a: mVelX -= PLAYER_VEL; mDir = 3; break;
}
}
else if (e.type == SDL_KEYUP && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_w: mVelY += PLAYER_VEL; break;
case SDLK_s: mVelY -= PLAYER_VEL; break;
case SDLK_d: mVelX -= PLAYER_VEL; break;
case SDLK_a: mVelX += PLAYER_VEL; break;
}
}
}
Separate your event polling from the rest of the game logic.
Poll all events once per frame in a separate function and store the state of the keystates into an array. Then, when you need the state, simply check the array.
You can also use SDL_GetKeyboardState to sample the surrent state of the keys, without having to handle the key events yourself.
I am trying to catch the event for F1 key in SDL in C++.
But,somehow i cannot see any changes after pressing the F1 key.But,when i wnat to toggle my animation objects on the screen i make use SDL_t case to do so,and it is executing fine.
Just not geting why is it not taking the F1 key.
Here is the switch case for handling key press events:
SDL_Event event;
bool done = false;
bool keyPress = false;
while ( not done ) {
draw();
SDL_Flip(screen);
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) { break; }
if(event.type == SDL_KEYUP) { keyCatch = false; }
if(event.type == SDL_KEYDOWN) {
switch ( event.key.keysym.sym ) {
case SDLK_ESCAPE : done = true; break;
case SDLK_q : done = true; break;
case SDLK_F1 : {
if(!keyCatch){
keyPress=true;
//this method is from another class.
io.printMessageAt("F1 is selected:",50,10);
currentOrb = (currentOrb+1) % orbs.size();
}
break;
}
case SDLK_t : case SDLK_SPACE :
if ( !keyPress ) {
//something
}
break;
case SDLK_p : {
if (!keyPress) {
keyPress = true;
//something
}
break;
}
default : break;
}
}
}
NOTE: i cannot give the entire code here.The above is my part what i am trying to do.
Double-check that your window manager or a hotkey daemon isn't consuming your F1's before they hit your process.
This works fine (F1 closes the program) on my system:
#include <SDL.h>
int main( int, char** )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_ANYFORMAT);
SDL_Event event;
bool done = false;
bool keyPress = false;
while ( !done )
{
SDL_Flip(screen);
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
{
break;
}
if(event.type == SDL_KEYUP)
{
keyPress = false;
}
if(event.type == SDL_KEYDOWN)
{
switch ( event.key.keysym.sym )
{
case SDLK_ESCAPE:
case SDLK_q:
done = true;
break;
case SDLK_F1:
if(!keyPress)
{
keyPress=true;
done = true;
}
break;
case SDLK_t:
case SDLK_SPACE:
if ( !keyPress )
{
//something
}
break;
case SDLK_p:
if (!keyPress)
{
keyPress = true;
//something
}
break;
default:
break;
}
}
}
SDL_Quit();
return 0;
}
I did notice that keyCatch was undefined. I replaced it with keyPress.
I am completely stumped as to why this code does not get any SDL keypress events. The other SDL events (removed for clarity) work fine. It does not work on my XP or Vista machines. No compile/link errors, just never recieve a keydown event.
#include "SDL/SDL.h"
// Yes SDL.lib and SDLmain.lib are linked
Uint32 TimeLeft(void)
{
static Uint32 next_time = 0;
Uint32 now;
now = SDL_GetTicks();
if ( next_time <= now ) {
next_time = now + tickInterval;
return 0;
}
return(next_time-now);
}
int main( int argc, char **argv )
{
if( -1 == SDL_Init( SDL_INIT_EVERYTHING ) )
{
cerr << "Error: SDL_Init failed" << endl;
return -1;
}
SDL_Event event;
bool quit = false;
while( !quit )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
case SDLK_q:
quit = true;
break;
default:
break;
}
break;
case SDL_JOYAXISMOTION:
// stuff removed
break;
case SDL_QUIT:
quit = true;
break;
default:
break;
}
}
SDL_Delay( TimeLeft() );
}
SDL_Quit();
return 0;
}
You'll need to create a window with SDL_SetVideoMode to get mouse and keyboard events.
I don't think you'll have luck trying to SDL_WM_GrabInput the mouse and keyboard without a window. It may also raise security alerts the first time on moderm Windows machines.