site  contact  subhomenews

Learning SDL v1.2 Lesson 08

January 17, 2023 — BarryK

I posted that came to a stop at Lesson 07:

And jumped that hurdle:

Lesson 08 is here, introducing key presses:

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:

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;
SDL_Event 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:

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

Tags: easy