How to correctly handle control-key combinations in SDL2 - keyboard-events

In my SDL 2.0 based application, I would like to handle both Control + and Control =.
I understand that I could handle the SDL_KEYDOWN event and look for the SDLK_EQUALS keycode in combination with KEYMODE_CTRL. And even check for KEYMOD_SHIFT' to distinguish between+and=`. However, this is not portable and breaks on keyboards where those symbols are mapped to different keys.
Another thing I have tried is to enable SDL_StartTextInput() and then listen to SDL_TEXTINPUT events. However that only works for printable characters. It ignores control sequences completely.
What is the correct the way to do this? I see SDL 1.2 actually had a unicode field in the SDL_Keysym structure. That would definitely make this a lot easier for me. Does anyone know why that was removed and what the equivalent in SDL 2.0 would be?

Here is an example how you can get unicode input as SDL_TEXTINPUT but the rest as SDL_KEYDOWN:
#include "SDL.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
int done = 0;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *w = SDL_CreateWindow("foo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, 0);
int lctrl = 0, rctrl = 0;
SDL_StartTextInput();
while (!done) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
done = 1;
break;
case SDL_TEXTINPUT: {
int ctrl_state = lctrl || rctrl;
printf("%s, ctrl %s\n", event.text.text, (ctrl_state) ? "pressed" : "released");
} break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_RCTRL) { rctrl = 1; }
else if(event.key.keysym.sym == SDLK_LCTRL) { lctrl = 1; }
break;
case SDL_KEYUP:
if(event.key.keysym.sym == SDLK_RCTRL) { rctrl = 0; }
else if(event.key.keysym.sym == SDLK_LCTRL) { lctrl = 0; }
break;
}
}
SDL_UpdateWindowSurface(w);
}
SDL_Quit();
return 0;
}
To simplify things, it ignores SDL_TEXTEDITING, which may (or not) be what you want. Also SDL_GetKeyboardState can be used instead of manually processing events and accumulating modifier keys flags, with the same result.

Related

How to move two objects simultaneously (Ncurses)

I am trying to get a basic simultaneous movement of both a player and an enemy working. Is there any way I can accomplish this correctly?
Below is a crude way of accomplishing. I would like to move while the x is also moving but I can only get one of them at a time to work. I tried using while loops but perhaps something else is needed...
Any tips?
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <windows.h>
WINDOW* createwindow();
char theplayer();
char theenemy();
int main()
{
initscr();
WINDOW* border=createwindow();
while(1)
{
theplayer();
theenemy();
}
wgetch(border);
endwin();
return 0;
}
WINDOW* createwindow()
{
WINDOW* temp=newwin(15,40,10,10);
box(temp,0,0);
return temp;
}
char theplayer(WINDOW* border)
{
int playerlocationy=3;
int playerlocationx=3;
int input;
char player='#';
keypad(border,true);
mvwprintw(border,3,3,"%c",player);
while(1)
{
input=wgetch(border);
switch (input)
{
case KEY_LEFT:
mvwprintw(border,playerlocationy,playerlocationx,"%c",' ');
playerlocationx--;
mvwprintw(border,playerlocationy,playerlocationx,"%c", player);
break;
case KEY_RIGHT:
mvwprintw(border,playerlocationy,playerlocationx,"%c",' ');
playerlocationx++;
mvwprintw(border,playerlocationy,playerlocationx,"%c", player);
break;
case KEY_UP:
mvwprintw(border,playerlocationy,playerlocationx,"%c",' ');
playerlocationy--;
mvwprintw(border,playerlocationy,playerlocationx,"%c", player);
break;
case KEY_DOWN:
mvwprintw(border,playerlocationy,playerlocationx,"%c",' ');
playerlocationy++;
mvwprintw(border,playerlocationy,playerlocationx,"%c", player);
break;
default:
break;
}
break;
}
return player;
}
char theenemy(WINDOW* border)
{
char enemy='X';
int enemylocationy=9;
int enemylocationx=9;
while(1)
{
mvwprintw(border,enemylocationy,enemylocationx,"%c", enemy);
mvwprintw(border,enemylocationy,enemylocationx,"%c",' ');
enemylocationx++;
mvwprintw(border,enemylocationy,enemylocationx,"%c", enemy);
wrefresh(border);
Sleep(1000);
}
return 0;
}
To start, these function declarations
char theplayer();
char theenemy();
are an obsolescent feature of C, that say these functions will take an unspecified but fixed number of arguments. With this, the compiler cannot reason about what an invocation of these functions should look like.
This is hiding the fact that your program has undefined behaviour. Both function definitions require a WINDOW * argument, but you call them with no arguments.
while(1)
{
theplayer();
theenemy();
}
To any extent that this program functions as is, is pure chance. Always use proper function prototypes in your declarations
WINDOW *createwindow(void);
char theplayer(WINDOW *);
char theenemy(WINDOW *);
which will help to find errors.
Using <windows.h> for Sleep reduces the portability of your program. You are already using curses, which provides the very similar napms function.
As for "simultaneous movement", the general idea is to have only one main event loop. Every iteration, this loop
handles input
updates entities
redraws the screen
Nothing else should block execution of the program (i.e., loop forever).
To update your enemy, you will need some way of tracking how much time has passed. This can be as advanced as delta timing or as simple as a frame counter, as shown below.
Here is a cursory example to get you started:
#include <curses.h>
struct entity {
int y;
int x;
unsigned char repr;
};
void update_player(struct entity *, int);
void update_enemy(struct entity *, unsigned);
void draw_entity(struct entity *, WINDOW *);
int main(void)
{
initscr();
noecho();
curs_set(0);
WINDOW *field = newwin(15, 40, 10, 10);
keypad(field, TRUE);
wtimeout(field, 0);
struct entity player = { 3, 3, '#' };
struct entity enemy = { 9, 9, 'X' };
unsigned tick = 1;
while (1) {
/* handle input */
int c = wgetch(field);
if ((c & A_CHARTEXT) == 'q')
break;
/* update stuff */
update_player(&player, c);
update_enemy(&enemy, tick);
/* draw things */
werase(field);
box(field, 0, 0);
draw_entity(&player, field);
draw_entity(&enemy, field);
wrefresh(field);
tick = (tick > 60) ? 0 : tick + 1;
napms(16);
}
delwin(field);
endwin();
}
void update_player(struct entity *p, int ch)
{
switch (ch) {
case KEY_LEFT:
p->x--;
break;
case KEY_RIGHT:
p->x++;
break;
case KEY_UP:
p->y--;
break;
case KEY_DOWN:
p->y++;
break;
}
}
void update_enemy(struct entity *e, unsigned t)
{
if (t == 60)
e->x++;
}
void draw_entity(struct entity *et, WINDOW *f)
{
mvwaddch(f, et->y, et->x, et->repr);
}

C and SDL quit from function

I use C and SDL 2.0, but i have a problem, this(when you click on the window's "x" it quits):
void function(SDL_Surface *screen) {
SDL_Event event;
bool quit=false;
while (!quit) {
SDL_WaitEvent(&event);
switch (event.type) {
case...
case SDL_QUIT:
quit = true;
break;
}
}
This is working, but not well. If this is in a function like this, it quits only to the main(), so I need to click on "x" again to quit from the whole program.
How can I solve it? (I want to quit from the whole program everytime, does not matter if it's in a function or not).
As already mentioned in comments you, most probably, have multiple event handling loops, which is usually incorrect design. You game general layout should be something like:
int main(int argc, char* argv[]) {
// do initialize stuff
bool run = true;
SDL_Event evt;
// game loop
while (run) {
// process OS events
while(SDL_PollEvent(&evt) != 0) {
switch (evt.type) {
case SDL_QUIT:
run = false;
break;
}
}
update();
render();
}
// clean up
SDL_Quit();
return 0;
}

SDL_Mixer not working on part of the code

I've just a strange problem. I'm looking to put sounds in my game. It's a game with two phases. One of rpg and one of fight. In rpg you can launch a fight when you walk on a monster. In my code i use 3 times SDL_mixer to play music (in the menu and in the rpg and in the fight). It works in the first two cases, but when i launch a fight there is no music. The music is loaded, and Mix_playingMusic returns true, but I can't hear any music when a fight is launched. I use the same music than in the rpg and menu. The code about SDL_Mixer is the same everywhere.
Part of the code of the fight :
{
SDL_Event events;
Mix_Music *fightMusic;
enum actual_player actual = PLAYER;
int go = 1;
int action;
int monsterId = 0;
Uint32 t = SDL_GetTicks(), nt;
fightMusic = Mix_LoadMUS("data/Music/rpg.mp3"); // we loading the music
if(fightMusic == NULL){
printf("error loading fightMusic\n");
}
Mix_VolumeMusic(MIX_MAX_VOLUME);
if(Mix_PlayMusic(fightMusic, -1)<0){
printf("error playing fightMusic \n");
}
if (Mix_PlayingMusic() ){
printf("Music it's playing \n");
}
while(go){
if(isFinished(fightSdl->fight))
go = 0;
nt = SDL_GetTicks();
while (SDL_PollEvent(&events)){
action = inputKeyboard(events);
if(action == QUIT){
return -1;
go = 0;
}
if(action == -2){
clearBufferGame(fightSdl->buffer, CHARACTER);
}
if(actual == PLAYER) {
switch(action){
case MOVE_DOWNWARD:
addActionToBufferGame(fightSdl->buffer, DOWN);
break;
case MOVE_UPWARD:
addActionToBufferGame(fightSdl->buffer, UP);
break;
case MOVE_LEFT:
addActionToBufferGame(fightSdl->buffer, LEFT);
break;
case MOVE_RIGHT:
addActionToBufferGame(fightSdl->buffer, RIGHT);
break;
case ATTACK_1:
actionAttackSdl(fightSdl, ATTACK_1);
break;
case ATTACK_2:
if(Mix_PlayChannel(-1,fireSound,0)<0){
printf("error playing fire sound");
}
actionAttackSdl(fightSdl, ATTACK_2);
break;
case ATTACK_3:
actionAttackSdl(fightSdl, ATTACK_3);
break;
case NOTHING:
actual = ENNEMY;
break;
case -1:
setIsIdleCharacSdl(fightSdl->characSdl,1);
}
}
}
if (nt-t>300){
if(actual == ENNEMY){
hitNRunSdl(fightSdl, monsterId);
if(getMpEnnemyFight(fightSdl->fight) == 3 && getMpEnnemyFight(fightSdl->fight) == 3)
monsterId++;
if(monsterId >= fightSdl->nbMstrSdl){
actual = PLAYER;
setApPlayerFight(fightSdl->fight, 3);
setMpPlayerFight(fightSdl->fight, 3);
monsterId = 0;
}
}
t = nt;
}
// updatePlayerDisplay(fightSdl);
// updateMonsterDisplay(fightSdl);
updatePlayerActionFight(fightSdl);
updateSpellDisplay(fightSdl);
updateDisplay(fightSdl);
drawGame(fightSdl);
// SDL_RenderPresent(fightSdl->renderer);
}
if(getLifePointsCharacter(getCharacterFight(fightSdl->fight)) >= 0)
return 1;
return 0;
}
EDIT : i've added a printf("%s", Mix_GetError) and there is no error in the menu and rpg but in the fight it was print : Invalid audio device ID
So,
I found a solution to solve my problem. Apparently the return value of SDL_OpenAudio is always success or failure, and not a device ID, which means you can only have one device open at a time with this function. I just closed the audio before re-opning it when launching fight, and it works.

Moving a square in SDL2 using C

I'm unable to move the rectangle I made in the program. There is no error message in the compiler when I run the program. Can you please tell me what I missed out in the keyboard event. Other event that I assigned to the window works fine. Thanks (an example will also be helpful).
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
SDL_Window *o;
SDL_Renderer *r;
SDL_Event e;
int i = 1;
SDL_Rect q;
SDL_Init(SDL_INIT_VIDEO);
o = SDL_CreateWindow("Game test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1024,
800,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(r,0,0,255,255);
SDL_RenderClear(r);
//Creating a box
q.x=475;
q.y=700;
q.h=50;
q.w=50;
SDL_SetRenderDrawColor(r,0,0,0,255);
SDL_RenderFillRect(r,&q);
//SDL_Delay(10);
SDL_RenderPresent(r);
while(i)
{
while(SDL_PollEvent(&e) !=0)
{
if(e.type == SDL_QUIT)
i=0;
else if(e.type == SDL_KEYDOWN)
{
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
i=0;
break;
case SDLK_UP:
q.y -=10;
SDL_Delay(11);
break;
case SDLK_DOWN:
q.y +=10;
SDL_Delay(11);
break;
case SDLK_RIGHT:
q.x +=10;
SDL_Delay(11);
break;
case SDLK_LEFT:
q.x -=10;
SDL_Delay(11);
break;
default:
break;
}
}
}
}
SDL_DestroyWindow(o);
SDL_DestroyRenderer(r);
SDL_Quit();
return 0;
}
You are only rendering the contents of the window before you enter your event loop. Since you never redraw the contents in the event loop, it's not very strange that no changes happen.
In SDL, you need to constantly redraw the window to see any changes you make. Since you only call the redraw function once, you only see what's happening in the very first moment of the window's creation. You simply need to add a redraw call inside of the loop, and it'll show you moving the rectangle as expected.

Smooth Camera Movement with OpenGL

I've been working with my own game engine for awhile now and I've been trying to get the input and controls to flow much more like a AAA FPS game, or at least a decent indie one. I've posted several topics in the past on this issue about making a smooth camera, and at the time of posting them, I had been satisfied with the results. Now however, I feel that it is not quite smooth enough, and I've switched the whole engine to SDL so I have control over the loop. (previously I was using GLUT). After changing everything to SDL, the mouse is smooth as butter, but the camera movement (walking) is still stutters and looks bad. I've implemented the last of Dwitter's game loop, the one with interpolation, and here is the relevant code:
int main (int argc, char** argv)
{
arg1 = argc;
arg2 = argv;
engineInit();
//the loop has to stay here
//kill all extra threads so they don't cause problems after we quit
//gameloop
SDL_Event event;
bool running = true;
const int TPS = 20;
const int SKIP_TICKS = 1000 / TPS;
const int MAX_FRAMESKIP = 5;
int loops;
long lastSec = 0;
long nextGameTick = SDL_GetTicks();
while (running)
{
while (SDL_PollEvent(&event)) {
//do crap with events
switch (event.type)
{
int x,y,button;
case SDL_QUIT:
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
cout << "The window has been closed.\n";
running = false;
break;
case SDL_MOUSEMOTION:
SDL_GetMouseState(&x, &y);
passiveMouse(x,y);
break;
case SDL_MOUSEBUTTONDOWN :
button = SDL_GetMouseState(&x, &y);
mouseFunc(button,1,x,y);
break;
case SDL_KEYDOWN:
keyboardDownFunc(event.key.keysym.sym);
break;
case SDL_KEYUP:
keyboardUpFunc(event.key.keysym.sym);
break;
default:
break;
}
}
loops = 0;
while (SDL_GetTicks()> nextGameTick && loops < MAX_FRAMESKIP) {
nextGameTick+=SKIP_TICKS;
loops++;
TickHandler.tps++;
TickHandler.onTick();
int tickTime = int(SDL_GetTicks()/1000);
if (tickTime > lastSec+1)
{
TickHandler.tps = 0;
lastSec = tickTime;
}
}
TickHandler.interpolation = double(SDL_GetTicks() + SKIP_TICKS - nextGameTick )
/ double( SKIP_TICKS );
TickHandler.onRender();
render();
}
Console.consoleActivated = false;
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
TickHandler.onRender() calls a few interpolated functions and here is the one that controls movement of the camera:
void renderTick(float intp)
{
if (cameraPlayer == true)
{
Physics.pos3 = -camy;
Physics.collisions();
Input.applyGravity();
if (Input.walking == true)
Input.moveCameraFirstPerson(1*intp);
else
{
roll = 0;
Input.change = false;
}
}
}
And here is the move camera first person:
void inputs::moveCameraFirstPerson(float speed)
{
speed = speed*walkspeed;
float radx = ((yaw+addedAngle)*MPI/180);
camx -= (sinf(radx)/10)*speed;
camz += (cosf(radx)/10)*speed;
Physics.pos1 = -camx;
Physics.pos2 = -camz;
if (Physics.collided == true)
{float radx = ((yaw+Input.addedAngle)*3.1415926535/180);
camx += (sinf(radx)/20)*speed;
camz -= (cosf(radx)/20)*speed;
Physics.collided = false;
}
Client.x = camx;
Client.z = camz;
Client.y = camy;
Projectile.x = camx;
Projectile.z = camz;
}
I'd love if I could get this all sorted out, any help or references?

Resources