Mercurial > sdl-ios-xcode
annotate XCodeiPhoneOS/Demos/src/keyboard.c @ 2417:ac26bd83db1f gsoc2008_iphone
Cleaned up code, added comments, added randomized colors, added check for extension GL_POINT_SIZE_ARRAY_OES, which not all OpenGL ES systems have.
author | Holmes Futrell <hfutrell@umail.ucsb.edu> |
---|---|
date | Wed, 13 Aug 2008 23:10:51 +0000 |
parents | a01958cd513d |
children | 74d25e48d54d |
rev | line source |
---|---|
2412
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
1 /* |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
2 * keyboard.c |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
3 * written by Holmes Futrell |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
4 * use however you want |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
5 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
6 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
7 #import "SDL.h" |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
8 #import "common.h" |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
9 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
10 #define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
11 #define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
12 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
13 static SDL_TextureID textureID; /* texture where we'll hold our font */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
14 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
15 /* iPhone SDL addition keyboard related function definitions */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
16 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_WindowID windowID); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
17 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_WindowID windowID); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
18 extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
19 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_WindowID windowID); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
20 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
21 /* function declarations */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
22 void cleanup(void); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
23 void drawBlank(int x, int y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
24 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
25 static int numChars = 0; /* number of characters we've typed so far */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
26 static SDL_bool lastCharWasColon = 0; /* we use this to detect sequences such as :) */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
27 static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
28 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
29 /* this structure maps a scancode to an index in our bitmap font. |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
30 it also contains data about under which modifiers the mapping is valid |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
31 (for example, we don't want shift + 1 to produce the character '1', |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
32 but rather the character '!') |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
33 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
34 typedef struct { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
35 SDL_scancode scancode; /* scancode of the key we want to map */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
36 int allow_no_mod; /* is the map valid if the key has no modifiers? */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
37 SDLMod mod; /* what modifiers are allowed for the mapping */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
38 int index; /* what index in the font does the scancode map to */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
39 } fontMapping; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
40 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
41 #define TABLE_SIZE 51 /* size of our table which maps keys and modifiers to font indices */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
42 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
43 /* Below is the table that defines the mapping between scancodes and modifiers to indices in the |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
44 bitmap font. As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
45 the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A), |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
46 The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
47 mapping is also valid if the user is holding shift. |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
48 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
49 fontMapping map[TABLE_SIZE] = { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
50 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
51 { SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }, /* A */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
52 { SDL_SCANCODE_B, 1, KMOD_SHIFT, 34 }, /* B */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
53 { SDL_SCANCODE_C, 1, KMOD_SHIFT, 35 }, /* C */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
54 { SDL_SCANCODE_D, 1, KMOD_SHIFT, 36 }, /* D */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
55 { SDL_SCANCODE_E, 1, KMOD_SHIFT, 37 }, /* E */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
56 { SDL_SCANCODE_F, 1, KMOD_SHIFT, 38 }, /* F */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
57 { SDL_SCANCODE_G, 1, KMOD_SHIFT, 39 }, /* G */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
58 { SDL_SCANCODE_H, 1, KMOD_SHIFT, 40 }, /* H */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
59 { SDL_SCANCODE_I, 1, KMOD_SHIFT, 41 }, /* I */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
60 { SDL_SCANCODE_J, 1, KMOD_SHIFT, 42 }, /* J */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
61 { SDL_SCANCODE_K, 1, KMOD_SHIFT, 43 }, /* K */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
62 { SDL_SCANCODE_L, 1, KMOD_SHIFT, 44 }, /* L */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
63 { SDL_SCANCODE_M, 1, KMOD_SHIFT, 45 }, /* M */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
64 { SDL_SCANCODE_N, 1, KMOD_SHIFT, 46 }, /* N */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
65 { SDL_SCANCODE_O, 1, KMOD_SHIFT, 47 }, /* O */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
66 { SDL_SCANCODE_P, 1, KMOD_SHIFT, 48 }, /* P */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
67 { SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49 }, /* Q */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
68 { SDL_SCANCODE_R, 1, KMOD_SHIFT, 50 }, /* R */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
69 { SDL_SCANCODE_S, 1, KMOD_SHIFT, 51 }, /* S */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
70 { SDL_SCANCODE_T, 1, KMOD_SHIFT, 52 }, /* T */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
71 { SDL_SCANCODE_U, 1, KMOD_SHIFT, 53 }, /* U */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
72 { SDL_SCANCODE_V, 1, KMOD_SHIFT, 54 }, /* V */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
73 { SDL_SCANCODE_W, 1, KMOD_SHIFT, 55 }, /* W */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
74 { SDL_SCANCODE_X, 1, KMOD_SHIFT, 56 }, /* X */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
75 { SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57 }, /* Y */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
76 { SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58 }, /* Z */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
77 { SDL_SCANCODE_0, 1, 0, 16 }, /* 0 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
78 { SDL_SCANCODE_1, 1, 0, 17 }, /* 1 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
79 { SDL_SCANCODE_2, 1, 0, 18 }, /* 2 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
80 { SDL_SCANCODE_3, 1, 0, 19 }, /* 3 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
81 { SDL_SCANCODE_4, 1, 0, 20 }, /* 4 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
82 { SDL_SCANCODE_5, 1, 0, 21 }, /* 5 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
83 { SDL_SCANCODE_6, 1, 0, 22 }, /* 6 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
84 { SDL_SCANCODE_7, 1, 0, 23 }, /* 7 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
85 { SDL_SCANCODE_8, 1, 0, 24 }, /* 8 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
86 { SDL_SCANCODE_9, 1, 0, 25 }, /* 9 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
87 { SDL_SCANCODE_SPACE, 1, 0, 0 }, /*' '*/ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
88 { SDL_SCANCODE_1, 0, KMOD_SHIFT, 1 }, /* ! */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
89 { SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31}, /* ? */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
90 { SDL_SCANCODE_SLASH, 1, 0, 15}, /* / */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
91 { SDL_SCANCODE_COMMA, 1, 0, 12}, /* , */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
92 { SDL_SCANCODE_SEMICOLON, 1, 0, 27}, /* ; */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
93 { SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26}, /* : */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
94 { SDL_SCANCODE_PERIOD, 1, 0, 14}, /* . */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
95 { SDL_SCANCODE_MINUS, 1, 0, 13}, /* - */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
96 { SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11}, /* = */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
97 { SDL_SCANCODE_APOSTROPHE, 1, 0, 7}, /* ' */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
98 { SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2}, /* " */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
99 { SDL_SCANCODE_5, 0, KMOD_SHIFT, 5}, /* % */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
100 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
101 }; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
102 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
103 /* |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
104 This function maps an SDL_keysym to an index in the bitmap font. |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
105 It does so by scanning through the font mapping table one entry |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
106 at a time. |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
107 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
108 If a match is found (scancode and allowed modifiers), the proper |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
109 index is returned. |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
110 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
111 If there is no entry for the key, -1 is returned |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
112 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
113 int keyToIndex(SDL_keysym key) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
114 int i, index = -1; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
115 for (i=0; i<TABLE_SIZE; i++) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
116 fontMapping compare = map[i]; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
117 if (key.scancode == compare.scancode) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
118 /* if this entry is valid with no key mod and we have no keymod, or if |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
119 the key's modifiers are allowed modifiers for that mapping */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
120 if ((compare.allow_no_mod && key.mod == 0) || ( key.mod & compare.mod ) ) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
121 index = compare.index; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
122 break; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
123 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
124 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
125 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
126 return index; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
127 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
128 /* |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
129 This function returns and x,y position for a given character number. |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
130 It is used for positioning each character of text |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
131 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
132 void getPositionForCharNumber(int n, int *x, int *y) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
133 int x_padding = 16; /* padding space on left and right side of screen */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
134 int y_padding = 32; /* padding space at top of screen */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
135 /* figure out the number of characters that can fit horizontally across the screen */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
136 int max_x_chars = (SCREEN_WIDTH - 2 * x_padding) / GLYPH_SIZE_SCREEN; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
137 int line_separation = 5; /* pixels between each line */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
138 *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
139 *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
140 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
141 void drawIndex(int index) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
142 int x, y; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
143 getPositionForCharNumber(numChars, &x, &y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
144 SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE }; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
145 SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN }; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
146 drawBlank(x, y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
147 SDL_RenderCopy(textureID, &srcRect, &dstRect); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
148 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
149 /* draws the cursor icon at the current end position of the text */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
150 void drawCursor(void) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
151 drawIndex(29); /* cursor is at index 29 in the bitmap font */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
152 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
153 /* paints over a glyph sized region with the background color |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
154 in effect it erases the area |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
155 */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
156 void drawBlank(int x, int y) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
157 SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN }; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
158 SDL_RenderFill(bg_color.r, bg_color.g, bg_color.b, bg_color.unused, &rect); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
159 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
160 /* moves backwards one character, erasing the last one put down */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
161 void backspace(void) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
162 int x, y; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
163 if (numChars > 0) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
164 getPositionForCharNumber(numChars, &x, &y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
165 drawBlank(x, y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
166 numChars--; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
167 getPositionForCharNumber(numChars, &x, &y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
168 drawBlank(x, y); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
169 drawCursor(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
170 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
171 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
172 /* this function loads our font into an SDL_Texture and returns the SDL_TextureID */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
173 SDL_TextureID loadFont(void) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
174 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
175 SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp"); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
176 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
177 if (!surface) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
178 printf("Error loading bitmap: %s\n", SDL_GetError()); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
179 return 0; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
180 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
181 else { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
182 /* set the transparent color for the bitmap font (hot pink) */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
183 SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252 )); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
184 /* now we convert the surface to our desired pixel format */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
185 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
186 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
187 int bpp; /* bits per pixel for desired format */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
188 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
189 SDL_Surface *converted = SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask, Bmask, Amask); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
190 SDL_BlitSurface(surface, NULL, converted, NULL); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
191 /* create our texture */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
192 textureID = SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, converted); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
193 if (textureID == 0) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
194 printf("texture creation failed: %s\n", SDL_GetError()); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
195 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
196 else { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
197 /* set blend mode for our texture */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
198 SDL_SetTextureBlendMode(textureID, SDL_TEXTUREBLENDMODE_BLEND); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
199 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
200 SDL_FreeSurface(surface); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
201 SDL_FreeSurface(converted); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
202 return textureID; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
203 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
204 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
205 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
206 int main(int argc, char *argv[]) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
207 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
208 int index; /* index of last key we pushed in the bitmap font */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
209 SDL_Event event; /* last event received */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
210 SDLMod mod; /* key modifiers of last key we pushed */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
211 SDL_scancode scancode; /* scancode of last key we pushed */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
212 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
213 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
214 printf("Error initializing SDL: %s", SDL_GetError()); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
215 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
216 /* create window */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
217 SDL_WindowID windowID = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
218 /* create renderer */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
219 SDL_CreateRenderer(windowID, 0, 0); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
220 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
221 /* load up our font */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
222 loadFont(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
223 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
224 /* draw the background, we'll just paint over it */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
225 SDL_RenderFill(bg_color.r, bg_color.g, bg_color.b, bg_color.unused, NULL); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
226 SDL_RenderPresent(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
227 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
228 int done = 0; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
229 /* loop till we get SDL_Quit */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
230 while (SDL_WaitEvent(&event)) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
231 switch (event.type) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
232 case SDL_QUIT: |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
233 done = 1; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
234 break; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
235 case SDL_KEYDOWN: |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
236 index = keyToIndex(event.key.keysym); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
237 scancode = event.key.keysym.scancode; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
238 mod = event.key.keysym.mod; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
239 if (scancode == SDL_SCANCODE_DELETE) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
240 /* if user hit delete, delete the last character */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
241 backspace(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
242 lastCharWasColon = 0; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
243 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
244 else if (lastCharWasColon && scancode == SDL_SCANCODE_0 && (mod & KMOD_SHIFT)) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
245 /* if our last key was a colon and this one is a close paren, the make a hoppy face */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
246 backspace(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
247 drawIndex(32); /* index for happy face */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
248 numChars++; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
249 drawCursor(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
250 lastCharWasColon = 0; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
251 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
252 else if (index != -1) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
253 /* if we aren't doing a happy face, then just draw the normal character */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
254 drawIndex(index); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
255 numChars++; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
256 drawCursor(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
257 lastCharWasColon = (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON\ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
258 && (event.key.keysym.mod & KMOD_SHIFT)); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
259 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
260 /* check if the key was a colon */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
261 /* draw our updates to the screen */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
262 SDL_RenderPresent(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
263 break; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
264 case SDL_MOUSEBUTTONUP: |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
265 /* mouse up toggles keyboard */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
266 SDL_iPhoneKeyboardToggle(windowID); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
267 break; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
268 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
269 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
270 cleanup(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
271 return 0; |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
272 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
273 |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
274 /* clean up after ourselves like a good kiddy */ |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
275 void cleanup(void) { |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
276 SDL_DestroyTexture(textureID); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
277 SDL_Quit(); |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
278 } |
a01958cd513d
keyboard.c contains the source code for the bitmap font keyboard example.
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
279 |