Non instantaneous/jerky movement using SDL2 & OpenGL - c++

I've been working on a little 3D engine trying to fix up the camera and make it less jerky. I've been using SDL for the input and while it works it's doing this thing where if I press and hold a button it will instantly move once then pause and then start moving properly making the movement feel unresponsive.
I recorded a GIF of it and while it may be slightly hard to see what's happening hopefully it'll give some idea:
Moving forward and then right would be like:
w wwwwwwwwwwwwwwww a aaaaaaaaaaaaaaaaaaaaa
The important code is here but feel free to ask for more if necessary:
//Poll events
SDL_Event event;
while (m_EngineState != EngineState::EXIT)
{
m_last = m_current;
m_current = SDL_GetPerformanceCounter();
deltaTime = (double)((m_current - m_last) * 1000 / SDL_GetPerformanceFrequency());
while (SDL_PollEvent(&event))
{
switch (event.type) {
case SDL_QUIT:
m_EngineState = EngineState::EXIT;
break;
case SDL_MOUSEMOTION:
break;
case SDL_KEYDOWN:
m_Keys[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
m_Keys[event.key.keysym.sym] = false;
break;
}
ProcessEvents();
}
void Engine::ProcessEvents()
{
if (m_Keys[SDLK_w])
{
m_Camera->MoveForward(5.0f*(deltaTime*0.001));
}
if (m_Keys[SDLK_s])
{
m_Camera->MoveForward(-5.0f*(deltaTime*0.001));
}
if (m_Keys[SDLK_d])
{
m_Camera->MoveRight(5.0f*(deltaTime*0.001));
}
if (m_Keys[SDLK_a])
{
m_Camera->MoveRight(-5.0f*(deltaTime*0.001));
}
}
void Camera::MoveForward(float amount)
{
m_pos += m_forward * amount;
}
void Camera::MoveRight(float amount)
{
m_pos += glm::cross(m_forward, m_up) * amount;
}

Do not use SDL_PollEvent with the SDL_KEYDOWN and SDL_KEYUP events, it is subject to OS keyboard repeat rates. Which is great for typing, but not for camera/player controls. Use SDL_GetKeyboardState(NULL) in stead to query the current state of the keys.
For example:
Uint8* keystate = SDL_GetKeyboardState(NULL);
if (keystate[SDL_SCANCODE_W])
{
m_Camera->MoveForward(5.0f*(deltaTime*0.001));
}
if (keystate[SDL_SCANCODE_S])
{
m_Camera->MoveForward(-5.0f*(deltaTime*0.001));
}

Related

Disable mouse / cursor detection

I'm making a simple Pong game.
When I ran the program, all the moves are perfect (the moving are not too fast nor too slow).
However, when I move my cursor around, the movement are faster which making the game much harder.
while (enable_loop)
{
Ticks = SDL_GetTicks();
while (SDL_PollEvent(&any_event))
{
if (any_event.type == SDL_QUIT)
{
enable_loop = false;
}
// Process keyboard event
keyPressed = SDL_GetKeyboardState(NULL);
if (keyPressed[SDL_SCANCODE_ESCAPE])
{
enable_loop = false;
}
if (keyPressed[SDL_SCANCODE_UP] )
{
player2.Update(25);
}
if (keyPressed[SDL_SCANCODE_DOWN])
{
player2.Update(-25);
}
if (keyPressed[SDL_SCANCODE_W])
{
player1.Update(20);
}
if (keyPressed[SDL_SCANCODE_S])
{
player1.Update(-20);
}
}
Grouping.update(surface, background);
SDL_UpdateWindowSurface(window);
limitFPS(Ticks);
}
}
Note : I've tried SDL_Delay(5) but the movement are too junky and jumping around, not usable :/
This game does not require mouse and I just may plug out my mouse, but I'm asking this for my experience and knowledge purposes.
I've used SDL_ShowCursor(SDL_DISABLE), SDL_CaptureMouse(SDL_FALSE) and limit my FPS to 60.
The effect is lessen but still noticeable.
How do I disable mouse detection or any code that will stop the effect?
Move the keyPressed checks outside the SDL_PollEvent() loop:
while (enable_loop)
{
Ticks = SDL_GetTicks();
while (SDL_PollEvent(&any_event))
{
if (any_event.type == SDL_QUIT)
{
enable_loop = false;
}
}
// Process keyboard event
keyPressed = SDL_GetKeyboardState(NULL);
if (keyPressed[SDL_SCANCODE_ESCAPE])
{
enable_loop = false;
}
if (keyPressed[SDL_SCANCODE_UP] )
{
player2.Update(25);
}
if (keyPressed[SDL_SCANCODE_DOWN])
{
player2.Update(-25);
}
if (keyPressed[SDL_SCANCODE_W])
{
player1.Update(20);
}
if (keyPressed[SDL_SCANCODE_S])
{
player1.Update(-20);
}
Grouping.update(surface, background);
SDL_UpdateWindowSurface(window);
limitFPS(Ticks);
}
That way you're only processing keyboard input once per frame instead of once per event.

Some problem with touch events with cef and SDL2

I'm experiencing some problem to getting working touch events with CEF in offscreen rendering using SDL2 in some situation:
Im writing an SDL-CEF application that need to execute some online game into a videolottery. so I start the application from tty using startx and not in DE. Morever, I can't use the mouse but is only possible use touchscreen to interact with the machine.The touch event work pretty well in the most os cases, but with some game the click event is not recognized by the web application.
For pass the correct event to the CEF browser I have written some specialized handler functions that catchs the SDL_Event and converts their to CEF_Event. Fo example for processing tauch events I have a function called handleFingerEvent(SDL_Event, CEF_Browser*). This specialized function is called from a generic handleEvents(SDL_Event, VEF_Browser*) handler function of highter level .
// The Touch Event Hendler function
void handleFingerEvent(SDL_Event &e, CefBrowser* browser) {
CefMouseEvent event;
switch (e.type) {
case SDL_FINGERDOWN:
// W_SIZE and H_SIZE are the dimension of the screen in px
if (e.tfinger.pressure > 0.9) {
event.x = static_cast<int>(e.tfinger.x*W_SIZE);
event.y = static_cast<int>(e.tfinger.y*H_SIZE);
browser->GetHost()->SendMouseClickEvent(event,
MBT_LEFT,
false, 1);
}
break;
case SDL_FINGERUP:
event.x = static_cast<int>(e.tfinger.x*W_SIZE);
event.y = static_cast<int>(e.tfinger.y*H_SIZE);
browser->GetHost()->SendMouseClickEvent(event, MBT_LEFT, true, 1);
break;
case SDL_FINGERMOTION: {
int scrollX = static_cast<int>(e.tfinger.dx);
int scrollY = static_cast<int>(e.tfinger.dy);
event.x = static_cast<int>(e.tfinger.x*W_SIZE);
event.y = static_cast<int>(e.tfinger.y*H_SIZE);
browser->GetHost()->SendMouseWheelEvent(event, scrollX, scrollY);
break;
}
default:
break;
}
}
void handleEvents(SDL_Event &e, CefBrowser *browser) {
switch (e.type) {
// Key events
case SDL_KEYDOWN:
case SDL_KEYUP:
handleKeyEvent(e, browser);
break;
// Window events
case SDL_WINDOWEVENT:
handleWindowEvent(e, browser);
break;
//Mouse events
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEWHEEL:
handleMouseEvent(e, browser);
break;
// Touch events
case SDL_FINGERDOWN:
case SDL_FINGERMOTION:
case SDL_FINGERUP:
handleFingerEvent(e, browser);
break;
default:
break;
}
}
I call it from a SDL_PollEvent() while loop like this:
browser = CefBrowserHost::CreateBrowserSync(window_info,
browserClient, //My BrowserClient implementation
"https://www.something.sometother",
browserSettings, // The cef setting
nullptr, nullptr);
while(!browserClient->closeAllowed()) {
//Send events to the browser
SDL_Event e;
while(SDL_PollEvent(&e) != 0){
//Quit and window resize
if(e.type == SDL_QUIT) {
browser->GetHost()->CloseBrowser(false);
}
else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
renderHandler->resize(e.window.data1, e.window.data2);
browser->GetHost()->WasResized();
}
else {
handleEvents(e, browser.get());
}
}
// Cef main work loop
cefApp->doCefWork();
//Set background color
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
//Clear window/renderer
SDL_RenderClear(renderer);
renderHandler->render();
//Update screen contents
SDL_RenderPresent(renderer);
}
Now I really don't know why in some game the event isn't caught by the browser. Morever if I open the game in a machine with mouse connected and a DM, and so I use the mouse pointer for interact with the game, it works without any problem. Maybe is there a better approach to send a click event to CefBrowser?

SFML/C++ Simulating user input

I'm trying to write some unit test methods. I want to test method which checks if key is pressed or released. And here is my question:
Is it possible in SFML with C++ to simulate random key press on keyboard?
Or I will just have to trust myself that this works?
The internals of sf::Keyboard::isKeyPressed would make simulation difficult, but if you are looking to simulate KeyPressed or KeyReleased events I'm pretty sure the following should work:
const sf::Event simulateKeypress(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system)
{
sf::Event::KeyEvent data;
data.code = key;
data.alt = alt;
data.control = control;
data.shift = shift;
data.system = system;
sf::Event event;
event.type = sf::Event::KeyPressed;
event.key = data;
return event;
}
//your handler here
handleEvent(simulateKeypress(sf::Keyboard::Key::A, false, false, false, false));
I'm unable to test this at the moment... If it works, then you should be able to make similar functions for other events.
Sure you can if you use a key manager of your own.
An example in C with SDL (but its exactly the same in C++ with SFML, just few name to change) :
typedef struct
{
char key[SDLK_LAST];
int mousex,mousey;
int mousexrel,mouseyrel;
char mousebuttons[8];
char quit;
} Input;
void UpdateEvents(Input* in)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
in->key[event.key.keysym.sym]=1;
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym]=0;
break;
case SDL_MOUSEMOTION:
in->mousex=event.motion.x;
in->mousey=event.motion.y;
in->mousexrel=event.motion.xrel;
in->mouseyrel=event.motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
in->mousebuttons[event.button.button]=1;
break;
case SDL_MOUSEBUTTONUP:
in->mousebuttons[event.button.button]=0;
break;
case SDL_QUIT:
in->quit = 1;
break;
default:
break;
}
}
}
int main()
{
Input in;
memset(&in,0,sizeof(in));
while(!in.key[SDLK_ESCAPE] && !in.quit)
{
UpdateEvents(&in);
if (in.mousebuttons[SDL_BUTTON_LEFT])
{
in.mousebuttons[SDL_BUTTON_LEFT] = 0;
}
if (in.key[SDLK_UP] && in.key[SDLK_LEFT])
{
}
}
return 0;
}
Edit : Found a key manager for C++ SFML : https://github.com/dabbertorres/SwiftInputManager
Can't give you my own code cause the code is not in english

SDL: how to get the mouse motion while the left mouse button is down using case statement

I am currently working with SDL2 and am fairly new to it. I am trying to use case statments to get the mouse motion coordinates only while the left mouse button is pressed down.
In the end, I need to be able to click on a object and find how much the mouse is dragged from that selected object.
So far I have been able to get the mouse press and the mouse motion working separately, but not at the same time.
Here is my code for the mouse press event:
void SDL::OnEvent(SDL_Event *_event)
{
Mallet mallet;
switch (_event->type)
{
case SDL_QUIT:
m_running = false;
break;
default:
break;
case SDL_KEYUP:
switch (_event->key.keysym.sym)
{
case SDLK_SPACE:
if(m_playerTurn == 1)
m_playerTurn = 2;
else
m_playerTurn = 1;
std::cout<<"player turn = "<<m_playerTurn<<std::endl;
break;
}
case SDL_MOUSEBUTTONDOWN:
switch(_event->button.button)
{
case SDL_BUTTON_LEFT:
int x = _event->button.x;
int y = _event->button.y;
if(m_playerTurn == 1)
{
bool collision = checkCollision(x, y, m_player1->getTeamMallets(), mallet);
if(collision)
std::cout<<"collision with P1"<<std::endl;
}
if(m_playerTurn == 2)
{
bool collision = checkCollision(x, y, m_player2->getTeamMallets(), mallet);
if(collision)
std::cout<<"collision with P2"<<std::endl;
}
break;
}
}
}
Can anyone help.
Many thanks in advance.
Will
on SDL_MOUSEBUTTONDOWN set variable click = true ans save x,y coordinates,
on SDL_MOUSEMOTION check if click == true and update x,y coordinates,
on SDL_MOUSEBUTTONUP set click = false and calculate distance.
http://lazyfoo.net/tutorials/SDL/17_mouse_events/index.php

Managing multiple RenderWindow's in sfml

I seem to be having a bit of trouble when managing multiple windows in SFML C++. When I try to manage multiple windows, they both open correctly and I can interact in the larger one, however, the smaller one, which upon creation is overlapping the larger window, I can not interact with until I move the large window away. An image is below to help with the visual. Also below is the main loop for my code.
The main loop of the code is as follows:
while (MainWin.isOpen() || F1Menu.isOpen())
{
sf::Event Event;
if (MainWin.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
MainWin.close();
if (F1Menu.isOpen())
F1Menu.close();
break;
case sf::Event::Resized:
MainView.reset(sf::FloatRect(0.f, 0.f, (MainWin.getSize().x*0.9f), (MainWin.getSize().y*0.9)));
MainWin.setView(MainView);
break;
case sf::Event::KeyPressed:
if (Event.key.code == sf::Keyboard::F1)
F1Menu.create(sf::VideoMode(200, 500), "SFML 2D - F1 Menu");
else if (Event.key.code == sf::Keyboard::Escape)
{
MainWin.close();
if (F1Menu.isOpen())
F1Menu.close();
}
break;
}
}
if (F1Menu.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
F1Menu.close();
break;
case sf::Event::MouseButtonReleased:
if (Event.mouseButton.button == sf::Mouse::Left)
if (LMButton.mouseIn(F1Menu))
LoadMap("MapA.dat");
break;
case sf::Event::MouseMoved:
if (LMButton.mouseIn(F1Menu))
LMButton.setColor(sf::Color::Yellow);
else
LMButton.setColor(sf::Color::White);
break;
}
}
moveClock.restart();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
player.move(0, -4 * time);
player.setDirection(sfm::Direction::Up);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
player.move(0, 4 * time);
player.setDirection(sfm::Direction::Down);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
player.move(-4 * time, 0);
player.setDirection(sfm::Direction::Left);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
player.move(4 * time, 0);
player.setDirection(sfm::Direction::Right);
}
if (F1Menu.isOpen())
{
F1Menu.clear();
F1Menu.draw(LMButton);
F1Menu.display();
}
if (MainWin.isOpen())
{
MainWin.clear();
if (SplashScreen.didAnimate)
SplashScreen.Animate(MainWin, sfg::Animation::FadeIn);
if (inMenu)
{
}
if (isPlaying)
{
DrawMap(MainWin);
MainWin.draw(player);
}
MainWin.display();
}
}
In my knowledge, you aren't able to poll two separate windows with one sf::Event object. this is because when polling events, you are essentially popping off of the event stack and handling each one. The signature of pollEvent is
bool pollEvent(sf::Event &event);
note that there is no const qualifier here, because each processed event gets popped off the event stack. By the time you finish polling your main window there are no events left for your other window. This could be the cause of your window being unable to focus. Your second window should use it's own separate sf::Event
On a side note I would highly recommend encapsulating your data inside classes. You can find a great reference here on how to go about doing so. It's great coding practice and helps minimize confusion when bug hunting