SDL  2.0
keyboard.c
Go to the documentation of this file.
1 /*
2  * keyboard.c
3  * written by Holmes Futrell
4  * use however you want
5  */
6 
7 #include "SDL.h"
8 #include "common.h"
9 
10 #define TEST_INPUT_RECT
11 
12 #define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */
13 #define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */
14 
15 #define MAX_CHARS 1024
16 
17 static SDL_Texture *texture; /* texture where we'll hold our font */
18 
20 static int numChars = 0; /* number of characters we've typed so far */
21 static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */
22 
23 static int glyphs[MAX_CHARS];
24 
25 /* this structure maps a scancode to an index in our bitmap font.
26  it also contains data about under which modifiers the mapping is valid
27  (for example, we don't want shift + 1 to produce the character '1',
28  but rather the character '!')
29 */
30 typedef struct
31 {
32  SDL_Scancode scancode; /* scancode of the key we want to map */
33  int allow_no_mod; /* is the map valid if the key has no modifiers? */
34  SDL_Keymod mod; /* what modifiers are allowed for the mapping */
35  int index; /* what index in the font does the scancode map to */
36 } fontMapping;
37 
38 #define TABLE_SIZE 51 /* size of our table which maps keys and modifiers to font indices */
39 
40 /* Below is the table that defines the mapping between scancodes and modifiers to indices in the
41  bitmap font. As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map
42  the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A),
43  The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the
44  mapping is also valid if the user is holding shift.
45 */
47 
48  {SDL_SCANCODE_A, 1, KMOD_SHIFT, 33}, /* A */
49  {SDL_SCANCODE_B, 1, KMOD_SHIFT, 34}, /* B */
50  {SDL_SCANCODE_C, 1, KMOD_SHIFT, 35}, /* C */
51  {SDL_SCANCODE_D, 1, KMOD_SHIFT, 36}, /* D */
52  {SDL_SCANCODE_E, 1, KMOD_SHIFT, 37}, /* E */
53  {SDL_SCANCODE_F, 1, KMOD_SHIFT, 38}, /* F */
54  {SDL_SCANCODE_G, 1, KMOD_SHIFT, 39}, /* G */
55  {SDL_SCANCODE_H, 1, KMOD_SHIFT, 40}, /* H */
56  {SDL_SCANCODE_I, 1, KMOD_SHIFT, 41}, /* I */
57  {SDL_SCANCODE_J, 1, KMOD_SHIFT, 42}, /* J */
58  {SDL_SCANCODE_K, 1, KMOD_SHIFT, 43}, /* K */
59  {SDL_SCANCODE_L, 1, KMOD_SHIFT, 44}, /* L */
60  {SDL_SCANCODE_M, 1, KMOD_SHIFT, 45}, /* M */
61  {SDL_SCANCODE_N, 1, KMOD_SHIFT, 46}, /* N */
62  {SDL_SCANCODE_O, 1, KMOD_SHIFT, 47}, /* O */
63  {SDL_SCANCODE_P, 1, KMOD_SHIFT, 48}, /* P */
64  {SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49}, /* Q */
65  {SDL_SCANCODE_R, 1, KMOD_SHIFT, 50}, /* R */
66  {SDL_SCANCODE_S, 1, KMOD_SHIFT, 51}, /* S */
67  {SDL_SCANCODE_T, 1, KMOD_SHIFT, 52}, /* T */
68  {SDL_SCANCODE_U, 1, KMOD_SHIFT, 53}, /* U */
69  {SDL_SCANCODE_V, 1, KMOD_SHIFT, 54}, /* V */
70  {SDL_SCANCODE_W, 1, KMOD_SHIFT, 55}, /* W */
71  {SDL_SCANCODE_X, 1, KMOD_SHIFT, 56}, /* X */
72  {SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57}, /* Y */
73  {SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58}, /* Z */
74  {SDL_SCANCODE_0, 1, 0, 16}, /* 0 */
75  {SDL_SCANCODE_1, 1, 0, 17}, /* 1 */
76  {SDL_SCANCODE_2, 1, 0, 18}, /* 2 */
77  {SDL_SCANCODE_3, 1, 0, 19}, /* 3 */
78  {SDL_SCANCODE_4, 1, 0, 20}, /* 4 */
79  {SDL_SCANCODE_5, 1, 0, 21}, /* 5 */
80  {SDL_SCANCODE_6, 1, 0, 22}, /* 6 */
81  {SDL_SCANCODE_7, 1, 0, 23}, /* 7 */
82  {SDL_SCANCODE_8, 1, 0, 24}, /* 8 */
83  {SDL_SCANCODE_9, 1, 0, 25}, /* 9 */
84  {SDL_SCANCODE_SPACE, 1, 0, 0}, /* ' ' */
85  {SDL_SCANCODE_1, 0, KMOD_SHIFT, 1}, /* ! */
86  {SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31}, /* ? */
87  {SDL_SCANCODE_SLASH, 1, 0, 15}, /* / */
88  {SDL_SCANCODE_COMMA, 1, 0, 12}, /* , */
89  {SDL_SCANCODE_SEMICOLON, 1, 0, 27}, /* ; */
90  {SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26}, /* : */
91  {SDL_SCANCODE_PERIOD, 1, 0, 14}, /* . */
92  {SDL_SCANCODE_MINUS, 1, 0, 13}, /* - */
93  {SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11}, /* = */
94  {SDL_SCANCODE_APOSTROPHE, 1, 0, 7}, /* ' */
95  {SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2}, /* " */
96  {SDL_SCANCODE_5, 0, KMOD_SHIFT, 5}, /* % */
97 
98 };
99 
100 /*
101  This function maps an SDL_KeySym to an index in the bitmap font.
102  It does so by scanning through the font mapping table one entry
103  at a time.
104 
105  If a match is found (scancode and allowed modifiers), the proper
106  index is returned.
107 
108  If there is no entry for the key, -1 is returned
109 */
110 int
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 }
128 
129 /*
130  This function returns and x,y position for a given character number.
131  It is used for positioning each character of text
132 */
133 void
134 getPositionForCharNumber(int n, int *x, int *y)
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 }
151 
152 void
153 drawGlyph(int glyph, int positionIndex)
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 }
161 
162 /* this function loads our font into an SDL_Texture and returns the SDL_Texture */
164 loadFont(void)
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 */
185  texture = SDL_CreateTextureFromSurface(renderer, converted);
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 }
197 
198 void
200 {
201  SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
202  SDL_RenderClear(renderer);
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 
210  SDL_RenderPresent(renderer);
211 }
212 
213 int
214 main(int argc, char *argv[])
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 */
230  renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
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 
296  SDL_DestroyTexture(texture);
297  SDL_DestroyRenderer(renderer);
298  SDL_DestroyWindow(window);
299  SDL_Quit();
300  return 0;
301 }
SDL_Keymod mod
Definition: keyboard.c:34
#define MAX_CHARS
Definition: keyboard.c:15
#define SDL_PollEvent
#define SDL_GetError
Uint8 g
Definition: SDL_pixels.h:298
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
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
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
SDL_Scancode scancode
Definition: SDL_keyboard.h:49
The SDL keysym structure, used in key events.
Definition: SDL_keyboard.h:47
Uint8 b
Definition: SDL_pixels.h:299
#define SDL_CreateWindow
void drawGlyph(int glyph, int positionIndex)
Definition: keyboard.c:153
fontMapping map[TABLE_SIZE]
Definition: keyboard.c:46
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
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
#define SDL_RenderCopy
#define SDL_GetWindowSize
#define SDL_StopTextInput
void draw()
Definition: keyboard.c:199
#define SDL_CreateTextureFromSurface
GLuint64 key
Definition: gl2ext.h:2192
Uint8 r
Definition: SDL_pixels.h:297
void getPositionForCharNumber(int n, int *x, int *y)
Definition: keyboard.c:134
#define KMOD_SHIFT
Definition: SDL_keycode.h:343
#define TABLE_SIZE
Definition: keyboard.c:38
#define SDL_FreeSurface
Uint8 a
Definition: SDL_pixels.h:300
#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 main(int argc, char *argv[])
Definition: keyboard.c:214
#define SDL_SetColorKey
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
#define SDL_PixelFormatEnumToMasks
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:220
SDL_Scancode scancode
Definition: keyboard.c:32
int w
Definition: SDL_rect.h:67
#define SDL_StartTextInput
SDL_Keymod
Enumeration of valid key mods (possibly OR&#39;d together).
Definition: SDL_keycode.h:325
GLuint index
int allow_no_mod
Definition: keyboard.c:33
#define SDL_Delay
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 NULL
Definition: begin_code.h:164
Uint16 mod
Definition: SDL_keyboard.h:51
SDL_PixelFormat * format
Definition: SDL_surface.h:72
static SDL_Texture * texture
Definition: keyboard.c:17
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_RenderClear
#define SDL_CreateRGBSurface
#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
static SDL_Color bg_color
Definition: keyboard.c:21
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
uint32_t Uint32
Definition: SDL_stdinc.h:203
GLdouble n
#define SDL_MapRGB
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
int index
Definition: keyboard.c:35
#define SDL_SetRenderDrawColor
#define SDL_DestroyRenderer
#define SDL_RenderGetLogicalSize
#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
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:559
static int numChars
Definition: keyboard.c:20