Why does SDL2 have some extra delay? - c

SDL2 seems to have a strange behaviour that I don't think SDL1.2 had, concerning event loops. When executing a program made with the following code, there is an extra delay that makes it really slow.
#include "SDL.h"
int main(int argc, char *argv[])
{
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *wind = SDL_CreateWindow("test", 700, 100, 300, 300, 0);
while (1)
{
SDL_PollEvent(&event);
if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
break;
SDL_Delay(1000);
}
SDL_Quit();
return 0;
}
For example, when clicking on the button "escape", it takes up to 4-5 seconds before the program closes, whereas it should take less than 1 second, since the delay is fixed at 1000ms.
When using a printf in the loop, I noticed that even though the loop is working, my input is not considered before a certain amount of time.
Why is there such a delay?

Related

C - How to use halfdelay(i) in the ncurses library

I am trying to create a snake game in C using the ncurses library. I would like my program to detect user input at a constant tick speed. If there is no input after a certain amount of time I would like my program to continue along (ie. update game).
This is what I wrote to test out the halfdelay(i) function:
#include <ncurses.h>
int main(void)
{
initscr();
halfdelay(5);
int user_input;
do
{
user_input = getch();
if (user_input != -1)
{
clear();
printw("Key pressed: %d\n", user_input);
}
else
{
//printf("Timeout.\n");
printw("Timeout.\n");
}
} while (user_input != ESC);
endwin();
return 0;
}
EDIT #1:
I would like to see
Timeout.
Timeout.
Timeout.
Code used:
#include <ncurses.h>
int main(void)
{
initscr();
halfdelay(5);
int in;
do
{
timeout(1);
in = getch();
if (in != -1)
{
clear();
printw("Key pressed: %d\n", in);
}
else
{
//printf("Timeout.\n");
printw("Timeout.\n");
}
refresh();
} while (in != 27);
endwin();
return 0;
}
Some history first.
Traditionally, the ESC key value has been used as the prefix for escape sequences, which denote special sequences of characters that can be interpreted as a non-graphic character (e.g., an arrow key, a function key, etc.)
Due to (n)curses focus on portability (and telecommunication), out of the box it supports this notion of escape sequences, and as such pressing the ESC key can have some side effects. Notably, when keypad is enabled, there is an inbuilt delay as the program waits to decide if the user simply pressed ESC or if it needs to wait for some more information to complete the escape sequence. This timing can be adjusted via the ESCDELAY environment variable, or the set_escdelay function.
All this is important as you work forward, as given its a game you may want to enable the functionality of the keypad eventually, which will create some extra steps when using the ESC key.
And because of all this, there is no ESC or KEY_ESC macro for for the escape key. Instead, its raw code is 27 (or the octal 033).
Your use of the halfdelay function seems perfectly fine to me, just know that the argument is in tenths of a second, so 5 is half a second. Tenths of a second may not achieve the desired effect in a game, so consider using the timeout function instead, which allows for higher precision.
A simple, working example:
#include <ncurses.h>
int main(void) {
initscr();
halfdelay(5);
int user_input;
while ((user_input = getch()) != 27) {
clear();
if (user_input == ERR)
printw("Timeout.");
else
printw("Key pressed: %d\n", user_input);
refresh();
}
endwin();
}
Your updated program "works", it just doesn't clear the screen properly. Note that you probably don't want to mix calls to printw and printf as it creates a strange mess of the screen.
Also, you should use halfdelay or timeout, but not both. Remember that timeout takes its argument in milliseconds, which is 1/1000 of a second, and it sets the blocking delay for a window (timeout for stdscr, wtimeout for specific windows). It's not a "sleep" style function.
Use one delay function at a time, move clear to outside the if statement, and use printw. This is functionally the same program to the one posted above, just with a do ... while loop.
#include <ncurses.h>
int main(void) {
initscr();
/*
noecho();
scrollok(stdscr, TRUE);
*/
timeout(500);
int in;
do {
in = getch();
clear();
if (in != ERR)
printw("Key pressed: %d\n", in);
else
printw("Timeout.\n");
refresh();
} while (in != 27);
endwin();
}
If you are expecting the display of this program to be something along the lines of
Timeout.
Timeout.
Key pressed: 65
Timeout.
...
like a more traditional terminal, then you simply want to remove clear all together, and un-comment noecho and scrollok.

ncurses only reporting mouse movement when bstate is non-zero

I'm following along with the code from
https://gist.github.com/sylt/93d3f7b77e7f3a881603
Here it is, in case of 404
#include <curses.h>
#include <stdio.h>
int main()
{
initscr();
cbreak();
noecho();
// Enables keypad mode. This makes (at least for me) mouse events getting
// reported as KEY_MOUSE, instead as of random letters.
keypad(stdscr, TRUE);
// Don't mask any mouse events
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
printf("\033[?1003h\n"); // Makes the terminal report mouse movement events
fflush(stdout);
for (;;) {
int c = wgetch(stdscr);
// Exit the program on new line fed
if (c == '\n')
break;
char buffer[512];
size_t max_size = sizeof(buffer);
if (c == ERR) {
snprintf(buffer, max_size, "Nothing happened.");
}
else if (c == KEY_MOUSE) {
MEVENT event;
if (getmouse(&event) == OK) {
snprintf(buffer, max_size, "Mouse at row=%d, column=%d bstate=0x%08lx",
event.y, event.x, event.bstate);
}
else {
snprintf(buffer, max_size, "Got bad mouse event.");
}
}
else {
snprintf(buffer, max_size, "Pressed key %d (%s)", c, keyname(c));
}
move(0, 0);
insertln();
addstr(buffer);
clrtoeol();
move(0, 0);
}
printf("\033[?1003l\n"); // Disable mouse movement events, as l = low
endwin();
return 0;
}
When I put that into a file and compile it (gcc -o test test.c -lncurses), in xterm, mouse movements are reported without a button press. In gnome-terminal and Konsole, the movement events are only reported if a button on my mouse is pressed!
I would usually say it's just a compatibility/ standards thing BUT when I execute:
printf '\033[?1003h' on gnome-terminal or konsole, I can see all the mouse movements reported without any buttons pressed!
So what's going on here? How do I persuade gnome-terminal and konsole to behave like xterm? Or even to behave like themselves when they are in bash mode?
EDIT: Additional clues:
It appears this code works on all terminals if I scroll my mouse wheel. It's more than likely that this is what happened when I originally tested in xterm, as even xterm requires either a button pressed or the mouse to be scrolled for this to work.
Therefore: my question becomes why does the terminal only report mouse movement after the bstate has been modified in some way, even when the terminal reports escape sequences in bash mode?

Recognizing special keys without taking over the screen

In a program that uses the curses or ncurses library, the getch() function can recognize either an ordinary character (like 'x') or the escape sequence sent by the arrow and function keys. For example, typing the up arrow key might send the sequence (Escape, '[', 'A'), but getch() will return the int value KEY_UP. It even uses timing information to distinguish between an Escape character sent by itself and an Escape character that's part of a sequence.
To use getch() successfully, you first have to call initscr(), which clears the screen so that curses can control what's displayed.
Is there a convenient way to recognize special keys without taking over the screen? Ideally I'd like to call getch() without first calling initscr(), and have it return the same value it would have returned if I had called initscr(), but experiment indicates that doesn't work.
Here's a non-working example of what I'm trying to do. The program prompts the user to enter Up, Down, Escape, Left, Right, and conforms that the correct keys were entered.
Running the program with -c invokes initscr() and allows it to work correctly. Running it without -c calls getch() without first calling initscr(); all the getch() calls fail without waiting for input, returning ERR (-1).
// gcc arrows.c -o arrows -lncurses
#include <term.h>
#include <stdio.h>
#include <curses.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int use_curses = 0;
if (argc == 2 && strcmp(argv[1], "-c") == 0) {
use_curses = 1;
}
else if (argc != 1) {
fprintf(stderr, "Usage: %s [-c]\n", argv[0]);
exit(EXIT_FAILURE);
}
WINDOW *win;
if (use_curses) {
win = initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
mvaddstr(0, 0, "Press UP, DOWN, Escape, LEFT, RIGHT");
}
else {
puts("Press UP, DOWN, Escape, LEFT, RIGHT");
}
int keys[5];
for (int i = 0; i < 5; i ++) {
keys[i] = getch();
}
int ok;
if (keys[0] == KEY_UP &&
keys[1] == KEY_DOWN &&
keys[2] == '\x1b' &&
keys[3] == KEY_LEFT &&
keys[4] == KEY_RIGHT)
{
ok = 1;
if (use_curses) {
mvaddstr(2, 0, "OK");
}
else {
puts("OK");
}
}
else {
ok = 0;
char message[100];
sprintf(message,
"Incorrect input: (%d, %d, %d, %d, %d)",
keys[0], keys[1], keys[2], keys[3], keys[4]);
if (use_curses) {
mvaddstr(2, 0, message);
}
else {
puts(message);
}
}
if (use_curses) {
mvaddstr(4, 0, "Press any key to quit ");
(void)getch();
endwin();
}
exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
}
The output without the -c option (on Ubuntu 17.04 x86_64) is:
Press UP, DOWN, Escape, LEFT, RIGHT
Incorrect input: (-1, -1, -1, -1, -1)
UPDATE: A solution using something other than curses would be fine. It would have to use terminfo, directly or indirectly, to know what input sequences to look for. (It seems to me that recognizing special key inputs should be straightforward and not intimately tied to taking over the entire screen.)
The filter function, called before initscr, tells curses to use a single line (rather than the whole screen). There is an example of its use (named "filter") in ncurses-examples, which consists of a command-prompt.
getch updates at least part of the screen, e.g., when it echoes the input. That update can affect either just a line (using filter), or the whole screen. If you're clever, you can turn off echo, and (since filter suppresses the screen erasure), pretend that it updates nothing at all. For example, you could redirect the output to /dev/null by initializing curses with newterm.
Echoing input is only part of the story: curses does initialize the screen, and the associated refresh done by getch would make that hard to avoid in a portable way. There's an ncurses-specific option in the sample program which works around unwanted switching between normal/alternate screens, which could occur during screen-initialization.
Here's a screenshot of "filter", demonstrating that it does not take over the whole screen. The inverse video is due to ncurses initializing colors (white-on-black), but as the help-message indicates, that's configurable. Without making a movie, it's hard to demonstrate that it handles special keys, but reading the source code should be enough.

SDL 2 hangs when polling for events

I'm working on a simple GUI, and i'm trying to use the SDL library with little success. I'm developing in C language, and i can't switch to C++.
I'm using SDL 2.0.5 on a Linux workstation (Red Hat Enterprise Server 5 (Tikanga)).
The problem is that when i try to catch events, the window hangs. No response, 100% CPU usage, and i've to kill the process. Clicking on the "x" have no effect.
I wrote a simple test code to figure out the problem, but i really don't know what to do. It looks like the inner "printf" it's never reached.
Also, if i remove the event loop, SDL_Quit() is reached (no need to kill the process) and it gives me a "Segmentation fault".
#include <SDL.h>
#include <SDL_events.h>
SDL_Window *mainWin;
SDL_Event mainEv;
int quit = 0;
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
printf("Init error.\n");
return(1);
}
mainWin = SDL_CreateWindow("Title", 0, 0, 640, 480, 0);
while(!quit)
{
while(SDL_PollEvent(&mainEv))
{
printf("Event!\n");
switch(mainEv.type)
{
case SDL_KEYDOWN:
quit=1;
}
}
}
SDL_Quit();
Try:
#include <SDL2/SDL.h>
//#include <SDL_events.h>
int main(void)
{ SDL_Window *mainWin;
...
... // same up to:
...
case SDL_KEYDOWN:
quit = 1;
...
SDL_Quit();
}
I pasted your code into "sdl_t.c", so compiled it with:
gcc sdl_t.c `sdl2-config --libs --cflags` -o sdl_t
I get a window that pops up and goes away when you press a key. There's obviously more to do ...
Have you seen this good tutorial?
http://www.willusher.io/pages/sdl2/

Trying to get a process running running without an SDL (percieved) crash C

Okay so I'm pretty new to programming, so I thought as my first real programming challenge I would make a chess program to teach openings. Inside my game game loop I have everything I need, except at some point in the game loop I want to stop for input from the user, and wait to process that information before drawing the screen and continuing the loop. However when I do that, if the user waits to long to input(~8 seconds)(btw the input in console input for now, that will change later) then the game just crashes and I get the standard ubuntu "do you want to force quit or wait" message. I would like to keep this message from popping up, but I don’t know how.
Here's my code btw:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "SDL2/SDL.h"
void drawBoard(SDL_Renderer *renderer)
{}
void drawPieces(char fen[100])
{}
int processEvents(SDL_Window *window)
{}
I'm leaving these functions out because the are long and not super important(if you want me to I’ll put them up later)
void next_move(char firstmove[10], char response[10])
{
int cont = 0;
char move[6];
printf("%s\n>>", firstmove);
while(cont == 0)
{
scanf("%s", move);
if(strcmp(move, response) == 0)
cont = 1;
else
printf("Incorrect.\n%s\n>>", firstmove);
}
}
void caro_kann()
{
printf("YOU HAVE SELECTED The Caro Kann Opening ( main line )\n");
next_move("1. e4", "c6");
next_move("2. d4", "d5");
next_move("3. Nc6", "dxe4");
next_move("4. Nxe4", "Bf5");
next_move("5. Ng3", "Bg6");
next_move("6. h4", "h6");
next_move("7. Nf3", "Nd7");
printf("success\n");
}
int main()
{
int done = 0;
SDL_Event event;
SDL_Window *window;
SDL_Renderer *renderer;
//initialize everything
SDL_Init(SDL_INIT_VIDEO);
//setting up the window
window = SDL_CreateWindow("PlatoChess ALPHA 1.0",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800,
800,
0);
//Setting up the renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
//setting up the program loop
while(!done)
{
if(processEvents(window) == 1)
done = 1;
caro_kann();
drawBoard(renderer);
SDL_Delay(10); //to cap the FPS
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
Any help would be much appreciated!
First of all, "do you want to force quit or wait" is not a crash. All it means is that your program isn't processing events (which is true - you're in a blocking scanf and doing nothing). Even if you're waiting for something to happen, you still should process window events and update display if necessary, otherwise user can't close your program and if it gets overshadowed by another program (or just minimised) its display will be completely wrong.
Basically you shouldn't block in wait for input. Your options are (sorted by ascending difficulty, based entirely on my opinion):
change input scheme (e.g. use keyboard events that SDL gives you instead of stdin)
perform nonblocking read from stdin when input is available
use separate input thread (requires sync)

Resources