Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 168 lines (156 sloc) 3.616 kb
a234a3e Paul Brook SDL renderer.
authored
1 /* SDL input handling
2 Copyright (C) 2011 Paul Brook
3 This code is licenced under the GNU GPL v3 */
4
5 #include "chuckie.h"
6
7 #include <SDL.h>
8
9 SDLKey keys[5][6] = {
10 /* right */ {SDLK_PERIOD, SDLK_RIGHT, SDLK_ESCAPE},
11 /* left */ {SDLK_COMMA, SDLK_LEFT, SDLK_ESCAPE},
12 /* down */ {SDLK_z, SDLK_DOWN, SDLK_ESCAPE},
13 /* up */ {SDLK_a, SDLK_UP, SDLK_ESCAPE},
14 /* jump */ {SDLK_SPACE, SDLK_HOME, SDLK_END, SDLK_PAGEUP,
15 SDLK_PAGEDOWN, SDLK_ESCAPE}
16 };
17
c7e656b Paul Brook Add frameskip.
authored
18 static uint32_t next_time;
19
e434ad0 Paul Brook Add spinny demo mode.
authored
20 float rotate_x;
21 float rotate_y;
e2530ac Paul Brook Improve rotation controls.
authored
22 int projection_mode;
e434ad0 Paul Brook Add spinny demo mode.
authored
23
a234a3e Paul Brook SDL renderer.
authored
24 /* Read inputs, and wait until the start of the next frame. */
25 void PollKeys(void)
26 {
27 SDL_Event event;
28 int i;
29 int sym;
30 SDLKey *keylist;
c7e656b Paul Brook Add frameskip.
authored
31 uint32_t now;
32 int32_t delta;
8a9b475 Paul Brook Optimize shaders. Animate models.
authored
33 static int skipcount;
34 static int framecount;
a234a3e Paul Brook SDL renderer.
authored
35
c7e656b Paul Brook Add frameskip.
authored
36 now = SDL_GetTicks();
37 if (next_time == 0)
38 next_time = now;
39 delta = next_time - now;
40 skip_frame = (delta < 0);
41 next_time += 30;
8a9b475 Paul Brook Optimize shaders. Animate models.
authored
42 framecount++;
43 skipcount += skip_frame;
44 if (framecount == 30) {
45 //printf("%d\n", skipcount);
46 framecount = 0;
47 skipcount = 0;
48 }
a234a3e Paul Brook SDL renderer.
authored
49 while (1) {
c7e656b Paul Brook Add frameskip.
authored
50 if (skip_frame) {
51 if (!SDL_PollEvent (&event)) {
52 return;
53 }
54 } else if (SDL_WaitEvent(&event) == 0) {
a234a3e Paul Brook SDL renderer.
authored
55 fprintf(stderr, "SDL_WaitEvent: %s\n", SDL_GetError());
56 exit(1);
57 }
58 switch (event.type) {
59 case SDL_QUIT:
60 SDL_Quit();
61 exit(0);
62 break;
63 case SDL_USEREVENT:
64 return;
65 case SDL_KEYDOWN:
66 case SDL_KEYUP:
67 sym = event.key.keysym.sym;
68 switch (sym) {
69 case SDLK_ESCAPE:
70 case SDLK_q:
71 SDL_Quit();
72 exit(0);
e434ad0 Paul Brook Add spinny demo mode.
authored
73 case SDLK_o:
e2530ac Paul Brook Improve rotation controls.
authored
74 if (event.type == SDL_KEYUP) {
75 if (projection_mode == 1)
76 projection_mode = 0;
77 } else {
78 projection_mode = 1;
79 }
80 break;
81 case SDLK_i:
82 if (event.type == SDL_KEYUP) {
83 if (projection_mode == 2)
84 projection_mode = 0;
85 else
86 projection_mode = 2;
87 }
e434ad0 Paul Brook Add spinny demo mode.
authored
88 break;
89 case SDLK_f:
90 rotate_x += 0.2;
91 break;
92 case SDLK_h:
93 rotate_x -= 0.2;
94 break;
95 case SDLK_t:
96 rotate_y += 0.2;
97 break;
98 case SDLK_g:
99 rotate_y -= 0.2;
100 break;
a234a3e Paul Brook SDL renderer.
authored
101 case SDLK_l:
102 if (event.type == SDL_KEYUP)
103 cheat = 1;
104 break;
105 default:
106 for (i = 0; i < 5; i++) {
107 keylist = &keys[i][0];
108 while (*keylist != SDLK_ESCAPE) {
109 if (event.key.keysym.sym == *keylist) {
110 if (event.key.state == SDL_PRESSED)
111 buttons |= 1 << i;
112 else
113 buttons &= ~(1 << i);
114 }
115 keylist++;
116 }
117 }
118 break;
119 }
120 break;
390be52 Paul Brook Joystick support.
authored
121 case SDL_JOYAXISMOTION:
122 if (event.jaxis.axis == 0) {
123 buttons &= ~(BUTTON_LEFT | BUTTON_RIGHT);
124 if (event.jaxis.value < -3200) {
125 buttons |= BUTTON_LEFT;
126 } else if (event.jaxis.value > 3200) {
127 buttons |= BUTTON_RIGHT;
128 }
129 } else if (event.jaxis.axis == 1) {
130 buttons &= ~(BUTTON_UP | BUTTON_DOWN);
131 if (event.jaxis.value < -3200) {
132 buttons |= BUTTON_UP;
133 } else if (event.jaxis.value > 3200) {
134 buttons |= BUTTON_DOWN;
135 }
136 }
137 break;
138 case SDL_JOYBUTTONDOWN:
139 /* Don't care which button. */
140 buttons |= BUTTON_JUMP;
141 break;
142 case SDL_JOYBUTTONUP:
143 buttons &= ~BUTTON_JUMP;
144 break;
a234a3e Paul Brook SDL renderer.
authored
145 }
146 }
147 }
148
390be52 Paul Brook Joystick support.
authored
149 static SDL_Joystick *joystick;
150
151 void init_input(void)
152 {
153 int err;
154
155 err = SDL_InitSubSystem(SDL_INIT_JOYSTICK);
156 if (err < 0) {
157 printf("SDL_INIT_JOYSTICK failed\n");
158 return;
159 }
160 if (SDL_NumJoysticks() == 0) {
161 printf("No joystick found\n");
162 return;
163 }
164
165 SDL_JoystickEventState(SDL_ENABLE);
166 joystick = SDL_JoystickOpen(0);
167 }
Something went wrong with that request. Please try again.