site  contact  subhomenews

Learning SDL v1.2 Lesson 08

January 17, 2023 — BarryK

I posted that came to a stop at Lesson 07:

https://bkhome.org/news/202301/started-learning-sdl-v12-came-to-a-stop.html

And jumped that hurdle:

https://bkhome.org/news/202301/back-in-business-with-sdlttf.html

Lesson 08 is here, introducing key presses:

https://lazyfoo.net/SDL_tutorials/lesson08/index.php

Compiled and tested:

# g++ -o lesson08 lesson08.cpp -lSDL -lSDL_image -lSDL_ttf -lfreetype
# ./lesson08

Very good. Now back on gui_engine, the simple widget toolkit. I mentioned that there is no way to exit:

https://bkhome.org/news/202301/guiengine-runs-in-initrd.html

If compiled for the desktop, with SDL using X11, then the example app runs in a window, and can be closed via the window close-box. On the framebuffer though, there is no close-box.

So, I was keen to learn how to handle key presses. Easy peasy to add detection of pressing the ESC key and exit the program. You would have to download gui_engine to see the entire 'example.c', just showing what I inserted, shown in bold text:

  /* Main loop. */
bool running = true;
while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE) running = false;
}
}

The key symbols are tabulated here:

https://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlkey.html

Very good. Next up, want to investigate incorporating a TTF font.  

Tags: easy