SDL  2.0
keyboard.c File Reference
#include "SDL.h"
#include "common.h"
+ Include dependency graph for keyboard.c:

Go to the source code of this file.

Data Structures

struct  fontMapping
 

Macros

#define TEST_INPUT_RECT
 
#define GLYPH_SIZE_IMAGE   16 /* size of glyphs (characters) in the bitmap font file */
 
#define GLYPH_SIZE_SCREEN   32 /* size of glyphs (characters) as shown on the screen */
 
#define MAX_CHARS   1024
 
#define TABLE_SIZE   51 /* size of our table which maps keys and modifiers to font indices */
 

Functions

int keyToGlyphIndex (SDL_Keysym key)
 
void getPositionForCharNumber (int n, int *x, int *y)
 
void drawGlyph (int glyph, int positionIndex)
 
SDL_TextureloadFont (void)
 
void draw ()
 
int main (int argc, char *argv[])
 

Variables

static SDL_Texturetexture
 
static SDL_Rendererrenderer
 
static int numChars = 0
 
static SDL_Color bg_color = { 50, 50, 100, 255 }
 
static int glyphs [MAX_CHARS]
 
fontMapping map [TABLE_SIZE]
 

Macro Definition Documentation

◆ GLYPH_SIZE_IMAGE

#define GLYPH_SIZE_IMAGE   16 /* size of glyphs (characters) in the bitmap font file */

Definition at line 12 of file keyboard.c.

Referenced by drawGlyph(), and main().

◆ GLYPH_SIZE_SCREEN

#define GLYPH_SIZE_SCREEN   32 /* size of glyphs (characters) as shown on the screen */

Definition at line 13 of file keyboard.c.

Referenced by drawGlyph(), and getPositionForCharNumber().

◆ MAX_CHARS

#define MAX_CHARS   1024

Definition at line 15 of file keyboard.c.

Referenced by main().

◆ TABLE_SIZE

#define TABLE_SIZE   51 /* size of our table which maps keys and modifiers to font indices */

Definition at line 38 of file keyboard.c.

Referenced by keyToGlyphIndex().

◆ TEST_INPUT_RECT

#define TEST_INPUT_RECT

Definition at line 10 of file keyboard.c.

Function Documentation

◆ draw()

void draw ( )

Definition at line 199 of file keyboard.c.

References SDL_Color::a, SDL_Color::b, drawGlyph(), SDL_Color::g, glyphs, i, numChars, SDL_Color::r, SDL_RenderClear, SDL_RenderPresent, and SDL_SetRenderDrawColor.

Referenced by main().

200 {
203 
204  for (int i = 0; i < numChars; i++) {
205  drawGlyph(glyphs[i], i);
206  }
207 
208  drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
209 
211 }
Uint8 g
Definition: SDL_pixels.h:298
static SDL_Renderer * renderer
Definition: keyboard.c:19
Uint8 b
Definition: SDL_pixels.h:299
void drawGlyph(int glyph, int positionIndex)
Definition: keyboard.c:153
static int glyphs[MAX_CHARS]
Definition: keyboard.c:23
Uint8 r
Definition: SDL_pixels.h:297
Uint8 a
Definition: SDL_pixels.h:300
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_RenderClear
static SDL_Color bg_color
Definition: keyboard.c:21
#define SDL_SetRenderDrawColor
#define SDL_RenderPresent
static int numChars
Definition: keyboard.c:20

◆ drawGlyph()

void drawGlyph ( int  glyph,
int  positionIndex 
)

Definition at line 153 of file keyboard.c.

References getPositionForCharNumber(), GLYPH_SIZE_IMAGE, GLYPH_SIZE_SCREEN, and SDL_RenderCopy.

Referenced by draw().

154 {
155  int x, y;
156  getPositionForCharNumber(positionIndex, &x, &y);
157  SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * glyph, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
158  SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
159  SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
160 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
#define GLYPH_SIZE_SCREEN
Definition: keyboard.c:13
static SDL_Renderer * renderer
Definition: keyboard.c:19
#define SDL_RenderCopy
GLenum GLenum GLuint texture
void getPositionForCharNumber(int n, int *x, int *y)
Definition: keyboard.c:134
#define GLYPH_SIZE_IMAGE
Definition: keyboard.c:12
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ getPositionForCharNumber()

void getPositionForCharNumber ( int  n,
int *  x,
int *  y 
)

Definition at line 134 of file keyboard.c.

References GLYPH_SIZE_SCREEN, and SDL_RenderGetLogicalSize.

Referenced by drawGlyph().

135 {
136  int renderW, renderH;
137  SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
138 
139  int x_padding = 16; /* padding space on left and right side of screen */
140  int y_padding = 32; /* padding space at top of screen */
141  /* figure out the number of characters that can fit horizontally across the screen */
142  int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
143  int line_separation = 5; /* pixels between each line */
144  *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
145 #ifdef TEST_INPUT_RECT
146  *y = renderH - GLYPH_SIZE_SCREEN;
147 #else
148  *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
149 #endif
150 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
#define GLYPH_SIZE_SCREEN
Definition: keyboard.c:13
static SDL_Renderer * renderer
Definition: keyboard.c:19
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLdouble n
#define SDL_RenderGetLogicalSize

◆ keyToGlyphIndex()

int keyToGlyphIndex ( SDL_Keysym  key)

Definition at line 111 of file keyboard.c.

References fontMapping::allow_no_mod, i, fontMapping::index, fontMapping::mod, SDL_Keysym::mod, fontMapping::scancode, SDL_Keysym::scancode, and TABLE_SIZE.

Referenced by main().

112 {
113  int i, index = -1;
114  for (i = 0; i < TABLE_SIZE; i++) {
115  fontMapping compare = map[i];
116  if (key.scancode == compare.scancode) {
117  /* if this entry is valid with no key mod and we have no keymod, or if
118  the key's modifiers are allowed modifiers for that mapping */
119  if ((compare.allow_no_mod && key.mod == 0)
120  || (key.mod & compare.mod)) {
121  index = compare.index;
122  break;
123  }
124  }
125  }
126  return index;
127 }
SDL_Keymod mod
Definition: keyboard.c:34
SDL_Scancode scancode
Definition: SDL_keyboard.h:49
fontMapping map[TABLE_SIZE]
Definition: keyboard.c:46
#define TABLE_SIZE
Definition: keyboard.c:38
SDL_Scancode scancode
Definition: keyboard.c:32
GLuint index
int allow_no_mod
Definition: keyboard.c:33
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
Uint16 mod
Definition: SDL_keyboard.h:51
int index
Definition: keyboard.c:35

◆ loadFont()

SDL_Texture* loadFont ( void  )

Definition at line 164 of file keyboard.c.

References SDL_Surface::format, SDL_Surface::h, NULL, SDL_BLENDMODE_BLEND, SDL_BlitSurface, SDL_CreateRGBSurface, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_MapRGB, SDL_PIXELFORMAT_ABGR8888, SDL_PixelFormatEnumToMasks, SDL_SetColorKey, SDL_SetTextureBlendMode, texture, and SDL_Surface::w.

Referenced by main().

165 {
166  SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
167 
168  if (!surface) {
169  printf("Error loading bitmap: %s\n", SDL_GetError());
170  return 0;
171  } else {
172  /* set the transparent color for the bitmap font (hot pink) */
173  SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252));
174  /* now we convert the surface to our desired pixel format */
175  int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
176  Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
177  int bpp; /* bits per pixel for desired format */
178  SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
179  &Amask);
180  SDL_Surface *converted =
181  SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
182  Bmask, Amask);
183  SDL_BlitSurface(surface, NULL, converted, NULL);
184  /* create our texture */
186  if (texture == 0) {
187  printf("texture creation failed: %s\n", SDL_GetError());
188  } else {
189  /* set blend mode for our texture */
191  }
192  SDL_FreeSurface(surface);
193  SDL_FreeSurface(converted);
194  return texture;
195  }
196 }
#define SDL_GetError
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
static SDL_Renderer * renderer
Definition: keyboard.c:19
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
#define SDL_BlitSurface
Definition: SDL_surface.h:483
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
#define SDL_FreeSurface
#define SDL_SetColorKey
#define SDL_PixelFormatEnumToMasks
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
static SDL_Texture * texture
Definition: keyboard.c:17
#define SDL_CreateRGBSurface
uint32_t Uint32
Definition: SDL_stdinc.h:203
#define SDL_MapRGB

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 214 of file keyboard.c.

References done, draw(), SDL_WindowEvent::event, GLYPH_SIZE_IMAGE, glyphs, SDL_Rect::h, SDL_Event::key, SDL_KeyboardEvent::keysym, keyToGlyphIndex(), loadFont(), MAX_CHARS, numChars, SDL_Keysym::scancode, SDL_CreateRenderer, SDL_CreateWindow, SDL_Delay, SDL_DestroyRenderer, SDL_DestroyTexture, SDL_DestroyWindow, SDL_GetError, SDL_GetWindowSize, SDL_Init, SDL_INIT_VIDEO, SDL_IsTextInputActive, SDL_KEYDOWN, SDL_MOUSEBUTTONUP, SDL_PollEvent, SDL_Quit, SDL_QUIT, SDL_RENDERER_PRESENTVSYNC, SDL_RenderSetLogicalSize, SDL_SCANCODE_BACKSPACE, SDL_SetTextInputRect, SDL_StartTextInput, SDL_StopTextInput, SDL_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_FULLSCREEN_DESKTOP, SDL_WINDOW_RESIZABLE, SDL_WINDOWEVENT, SDL_WINDOWEVENT_RESIZED, SDL_Event::type, SDL_Rect::w, SDL_Event::window, SDL_Rect::x, and SDL_Rect::y.

215 {
217  SDL_Event event; /* last event received */
218  SDL_Scancode scancode; /* scancode of last key we pushed */
219  int width;
220  int height;
221  int done;
222  SDL_Rect textrect;
223 
224  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
225  printf("Error initializing SDL: %s", SDL_GetError());
226  }
227  /* create window */
229  /* create renderer */
231 
232  SDL_GetWindowSize(window, &width, &height);
233  SDL_RenderSetLogicalSize(renderer, width, height);
234 
235  /* load up our font */
236  loadFont();
237 
238  /* Show onscreen keyboard */
239 #ifdef TEST_INPUT_RECT
240  textrect.x = 0;
241  textrect.y = height - GLYPH_SIZE_IMAGE;
242  textrect.w = width;
243  textrect.h = GLYPH_SIZE_IMAGE;
244  SDL_SetTextInputRect(&textrect);
245 #endif
247 
248  done = 0;
249  while (!done) {
250  while (SDL_PollEvent(&event)) {
251  switch (event.type) {
252  case SDL_QUIT:
253  done = 1;
254  break;
255  case SDL_WINDOWEVENT:
256  if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
257  width = event.window.data1;
258  height = event.window.data2;
259  SDL_RenderSetLogicalSize(renderer, width, height);
260 #ifdef TEST_INPUT_RECT
261  textrect.x = 0;
262  textrect.y = height - GLYPH_SIZE_IMAGE;
263  textrect.w = width;
264  textrect.h = GLYPH_SIZE_IMAGE;
265  SDL_SetTextInputRect(&textrect);
266 #endif
267  }
268  break;
269  case SDL_KEYDOWN:
270  if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
271  if (numChars > 0) {
272  numChars--;
273  }
274  } else if (numChars + 1 < MAX_CHARS) {
275  int index = keyToGlyphIndex(event.key.keysym);
276  if (index >= 0) {
277  glyphs[numChars++] = index;
278  }
279  }
280  break;
281  case SDL_MOUSEBUTTONUP:
282  /* mouse up toggles onscreen keyboard visibility */
283  if (SDL_IsTextInputActive()) {
285  } else {
287  }
288  break;
289  }
290  }
291 
292  draw();
293  SDL_Delay(15);
294  }
295 
298  SDL_DestroyWindow(window);
299  SDL_Quit();
300  return 0;
301 }
#define MAX_CHARS
Definition: keyboard.c:15
#define SDL_PollEvent
#define SDL_GetError
static SDL_Renderer * renderer
Definition: keyboard.c:19
SDL_Scancode scancode
Definition: SDL_keyboard.h:49
#define SDL_CreateWindow
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
static int glyphs[MAX_CHARS]
Definition: keyboard.c:23
SDL_WindowEvent window
Definition: SDL_events.h:562
#define SDL_GetWindowSize
GLenum GLenum GLuint texture
#define SDL_StopTextInput
void draw()
Definition: keyboard.c:199
#define GLYPH_SIZE_IMAGE
Definition: keyboard.c:12
struct _cl_event * event
#define SDL_RenderSetLogicalSize
#define SDL_Quit
int done
Definition: checkkeys.c:28
SDL_Texture * loadFont(void)
Definition: keyboard.c:164
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:220
int w
Definition: SDL_rect.h:67
#define SDL_StartTextInput
GLuint index
#define SDL_Delay
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_SetTextInputRect
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
int keyToGlyphIndex(SDL_Keysym key)
Definition: keyboard.c:111
SDL_KeyboardEvent key
Definition: SDL_events.h:563
#define SDL_DestroyTexture
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
#define SDL_DestroyRenderer
#define SDL_DestroyWindow
int y
Definition: SDL_rect.h:66
#define SDL_IsTextInputActive
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_CreateRenderer
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
static int numChars
Definition: keyboard.c:20
Uint32 type
Definition: SDL_events.h:559

Variable Documentation

◆ bg_color

SDL_Color bg_color = { 50, 50, 100, 255 }
static

Definition at line 21 of file keyboard.c.

◆ glyphs

int glyphs[MAX_CHARS]
static

Definition at line 23 of file keyboard.c.

Referenced by draw(), and main().

◆ map

Definition at line 46 of file keyboard.c.

◆ numChars

int numChars = 0
static

Definition at line 20 of file keyboard.c.

Referenced by draw(), and main().

◆ renderer

SDL_Renderer* renderer
static

Definition at line 19 of file keyboard.c.

◆ texture

Definition at line 17 of file keyboard.c.

Referenced by loadFont().