7 #include "sdlv.h" // Dispatching video-related events
12 unsigned short x2, y2;
17 #define TOUCH_BUTTON_INITIALIZER(name, x, y, w, h) \
18 {SNES_##name##_MASK, 0, 0, 0, 0, x, y, w, h}
20 #define kCornerButtonWidth (0.375)
21 #define kCornerButtonHeight (0.0833333333334)
22 #define kBigButtonWidth (0.125)
23 #define kBigButtonHeight (0.2777777777778)
25 static TouchButton touchbuttons[] = {
26 TOUCH_BUTTON_INITIALIZER(TL, 0.0, 0.0, kCornerButtonWidth, kCornerButtonHeight),
27 TOUCH_BUTTON_INITIALIZER(TR, 0.625, 0.0, kCornerButtonWidth, kCornerButtonHeight),
28 TOUCH_BUTTON_INITIALIZER(UP, kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
29 TOUCH_BUTTON_INITIALIZER(LEFT, 0.0, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
30 TOUCH_BUTTON_INITIALIZER(RIGHT, 2.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
31 TOUCH_BUTTON_INITIALIZER(DOWN, kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
32 TOUCH_BUTTON_INITIALIZER(SELECT, 0.0, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
33 TOUCH_BUTTON_INITIALIZER(X, 1.0 - 2.0 * kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
34 TOUCH_BUTTON_INITIALIZER(Y, 1.0 - 3.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
35 TOUCH_BUTTON_INITIALIZER(A, 1.0 - kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
36 TOUCH_BUTTON_INITIALIZER(B, 1.0 - 2.0 * kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
37 TOUCH_BUTTON_INITIALIZER(START, 1.0 - kCornerButtonWidth, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
40 static TouchButton* current = 0;
42 static uint32 joypads[2];
46 bool enabled, pressed;
49 static TouchButton* getButtonFor(unsigned int x, unsigned int y) {
52 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
53 if (x >= touchbuttons[i].x && x < touchbuttons[i].x2 &&
54 y >= touchbuttons[i].y && y < touchbuttons[i].y2) {
56 return &touchbuttons[i];
63 static inline void unpress(TouchButton* b) {
64 joypads[0] &= ~b->mask;
66 static inline void press(TouchButton* b) {
67 joypads[0] |= b->mask;
70 static void processMouse(unsigned int x, unsigned int y, int pressed = 0)
73 /* no fullscreen escape button, we have to simulate one! */
74 /* TODO: don't hardcode sizes */
75 if (Config.fullscreen && x > (800 - 100) && y < 50 && pressed > 0) {
76 S9xDoAction(kActionQuit);
79 if (Config.touchscreenInput) {
88 // Button down, or mouse motion.
89 TouchButton* b = getButtonFor(x, y);
90 if (current && b && current != b) {
91 // Moving from button to button
95 } else if (current && !b) {
99 } else if (!current && b) {
105 } else if (mouse.enabled) {
109 if (mouse.x < GUI.RenderX) mouse.x = 0;
111 mouse.x -= GUI.RenderX;
112 if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
115 if (mouse.y < GUI.RenderY) mouse.y = 0;
117 mouse.y -= GUI.RenderY;
118 if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
121 // Take care of scaling
122 mouse.x /= GUI.ScaleX;
123 mouse.y /= GUI.ScaleY;
126 mouse.pressed = true;
127 else if (pressed < 0)
128 mouse.pressed = false;
132 static void processEvent(const SDL_Event& event)
137 if (Config.action[event.key.keysym.scancode])
138 S9xDoAction(Config.action[event.key.keysym.scancode]);
139 joypads[0] |= Config.joypad1Mapping[event.key.keysym.scancode];
142 joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
144 case SDL_MOUSEBUTTONUP:
145 case SDL_MOUSEBUTTONDOWN:
146 processMouse(event.button.x, event.button.y,
147 (event.button.state == SDL_PRESSED) ? 1 : - 1);
149 case SDL_MOUSEMOTION:
150 processMouse(event.motion.x, event.motion.y);
153 Config.quitting = true;
155 case SDL_ACTIVEEVENT:
157 processVideoEvent(event);
162 uint32 S9xReadJoypad (int which)
164 if (which < 0 || which > 2) {
168 return joypads[which];
171 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
173 if (which1 != 0) return FALSE;
177 buttons = mouse.pressed ? 1 : 0;
182 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
186 buttons = mouse.pressed ? 8 : 0;
191 void S9xProcessEvents(bool8_32 block)
196 SDL_WaitEvent(&event);
199 while(SDL_PollEvent(&event)) {
205 void S9xInitInputDevices()
209 mouse.enabled = false;
210 mouse.pressed = false;
212 switch (Settings.ControllerOption) {
214 joypads[0] = 0x80000000UL;
215 printf("Input: 1 joypad, keyboard only\n");
218 joypads[0] = 0x80000000UL;
219 mouse.enabled = true;
220 printf("Input: 1 joypad + mouse\n");
222 case SNES_MOUSE_SWAPPED:
223 printf("Input: mouse\n");
224 mouse.enabled = true;
226 case SNES_SUPERSCOPE:
227 joypads[0] = 0x80000000UL;
228 mouse.enabled = true;
229 printf("Input: 1 joypad + superscope\n");
232 printf("Input: unknown\n");
236 S9xInputScreenChanged();
239 void S9xDeinitInputDevices()
244 void S9xInputScreenChanged()
247 const unsigned int w = GUI.Width, h = GUI.Height;
248 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
249 touchbuttons[i].x = (unsigned int)(touchbuttons[i].fx * w);
250 touchbuttons[i].y = (unsigned int)(touchbuttons[i].fy * h);
251 touchbuttons[i].x2 = (unsigned int)(touchbuttons[i].x + touchbuttons[i].fw * w);
252 touchbuttons[i].y2 = (unsigned int)(touchbuttons[i].y + touchbuttons[i].fh * h);
256 template <typename T>
257 static void drawControls(T * buffer, const int pitch)
261 const T black = static_cast<T>(0xFFFFFFFFU);
264 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
265 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
266 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
270 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
271 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
275 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
276 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
280 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
281 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
288 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
293 drawControls(reinterpret_cast<uint8*>(buffer), pitch);
296 drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);