| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "mouse.h" |
| 20 | |
| 21 | #include "keyboard.h" |
| 22 | #include "nortvars.h" |
| 23 | #include "sprite.h" |
| 24 | #include "video.h" |
| 25 | #include "vga256d.h" |
| 26 | |
| 27 | #if defined(TARGET_GP2X) || defined(TARGET_DINGUX) |
| 28 | bool has_mouse = false; |
| 29 | #else |
| 30 | bool has_mouse = true; |
| 31 | #endif |
| 32 | bool mouse_has_three_buttons = true; |
| 33 | |
| 34 | bool mouseInactive = true; |
| 35 | JE_byte mouseCursor; |
| 36 | JE_word mouseX, mouseY, mouseButton; |
| 37 | JE_word mouseXB, mouseYB; |
| 38 | |
| 39 | static JE_word mouseGrabX, mouseGrabY; |
| 40 | static JE_byte mouseGrabShape[24 * 28]; |
| 41 | |
| 42 | static void JE_drawShapeTypeOne(JE_word x, JE_word y, JE_byte *shape) |
| 43 | { |
| 44 | JE_word xloop = 0, yloop = 0; |
| 45 | JE_byte *p = shape; /* shape pointer */ |
| 46 | Uint8 *s; /* screen pointer, 8-bit specific */ |
| 47 | Uint8 *s_limit; /* buffer boundary */ |
| 48 | |
| 49 | s = (Uint8 *)VGAScreen->pixels; |
| 50 | s += y * VGAScreen->pitch + x; |
| 51 | |
| 52 | s_limit = (Uint8 *)VGAScreen->pixels; |
| 53 | s_limit += VGAScreen->h * VGAScreen->pitch; |
| 54 | |
| 55 | for (yloop = 0; yloop < 28; yloop++) |
| 56 | { |
| 57 | for (xloop = 0; xloop < 24; xloop++) |
| 58 | { |
| 59 | if (s >= s_limit) |
| 60 | return; |
| 61 | *s = *p; |
| 62 | s++; p++; |
| 63 | } |
| 64 | s -= 24; |
| 65 | s += VGAScreen->pitch; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void JE_grabShapeTypeOne(JE_word x, JE_word y, JE_byte *shape) |
| 70 | { |
| 71 | JE_word xloop = 0, yloop = 0; |
| 72 | JE_byte *p = shape; /* shape pointer */ |
| 73 | Uint8 *s; /* screen pointer, 8-bit specific */ |
| 74 | Uint8 *s_limit; /* buffer boundary */ |
| 75 | |
| 76 | s = (Uint8 *)VGAScreen->pixels; |
| 77 | s += y * VGAScreen->pitch + x; |
| 78 | |
| 79 | s_limit = (Uint8 *)VGAScreen->pixels; |
| 80 | s_limit += VGAScreen->h * VGAScreen->pitch; |
| 81 | |
| 82 | for (yloop = 0; yloop < 28; yloop++) |
| 83 | { |
| 84 | for (xloop = 0; xloop < 24; xloop++) |
| 85 | { |
| 86 | if (s >= s_limit) |
| 87 | return; |
| 88 | *p = *s; |
| 89 | s++; p++; |
| 90 | } |
| 91 | s -= 24; |
| 92 | s += VGAScreen->pitch; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | typedef struct |
| 97 | { |
| 98 | Uint16 index; |
| 99 | Uint8 x; |
| 100 | Uint8 y; |
| 101 | Uint8 w; |
| 102 | Uint8 h; |
| 103 | Uint8 fx; |
| 104 | Uint8 fy; |
| 105 | } MousePointerSpriteInfo; |
| 106 | |
| 107 | static const MousePointerSpriteInfo mousePointerSprites[] = // fka mouseCursorGr |
| 108 | { |
| 109 | { 273, 0, 0, 11, 16, 0, 0 }, |
| 110 | { 275, 0, 0, 21, 16, 10, 8 }, |
| 111 | { 277, 0, 0, 21, 16, 10, 7 }, |
| 112 | { 279, 0, 0, 16, 21, 8, 10 }, |
| 113 | { 281, 8, 0, 16, 21, 7, 10 }, |
| 114 | }; |
| 115 | |
| 116 | void JE_mouseStart(void) |
| 117 | { |
| 118 | if (has_mouse) |
| 119 | { |
| 120 | service_SDL_events(false); |
| 121 | |
| 122 | mouseButton = mousedown ? lastmouse_but : 0; /* incorrect, possibly unimportant */ |
| 123 | |
| 124 | const MousePointerSpriteInfo *spriteInfo = &mousePointerSprites[mouseCursor]; |
| 125 | |
| 126 | mouseGrabX = MIN(MAX(spriteInfo->fx, mouse_x), 320 - (spriteInfo->w - spriteInfo->fx)) - spriteInfo->fx; |
| 127 | mouseGrabY = MIN(MAX(spriteInfo->fy, mouse_y), 200 - (spriteInfo->h - spriteInfo->fy)) - spriteInfo->fy; |
| 128 | |
| 129 | JE_grabShapeTypeOne(mouseGrabX, mouseGrabY, mouseGrabShape); |
| 130 | |
| 131 | if (!mouseInactive) |
| 132 | { |
| 133 | const Sint32 x = mouse_x - spriteInfo->x - spriteInfo->fx; |
| 134 | const Sint32 y = mouse_y - spriteInfo->y - spriteInfo->fy; |
| 135 | blit_sprite2x2_clip(VGAScreen, x, y, shopSpriteSheet, spriteInfo->index); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void JE_mouseStartFilter(Uint8 filter) |
| 141 | { |
| 142 | if (has_mouse) |
| 143 | { |
| 144 | mouseButton = mousedown ? lastmouse_but : 0; /* incorrect, possibly unimportant */ |
| 145 | |
| 146 | const MousePointerSpriteInfo *spriteInfo = &mousePointerSprites[mouseCursor]; |
| 147 | |
| 148 | mouseGrabX = MIN(MAX(spriteInfo->fx, mouse_x), 320 - (spriteInfo->w - spriteInfo->fx)) - spriteInfo->fx; |
| 149 | mouseGrabY = MIN(MAX(spriteInfo->fy, mouse_y), 200 - (spriteInfo->h - spriteInfo->fy)) - spriteInfo->fy; |
| 150 | |
| 151 | JE_grabShapeTypeOne(mouseGrabX, mouseGrabY, mouseGrabShape); |
| 152 | |
| 153 | if (!mouseInactive) |
| 154 | { |
| 155 | const Sint32 x = mouse_x - spriteInfo->x - spriteInfo->fx; |
| 156 | const Sint32 y = mouse_y - spriteInfo->y - spriteInfo->fy; |
| 157 | blit_sprite2x2_filter_clip(VGAScreen, x, y, shopSpriteSheet, spriteInfo->index, filter); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void JE_mouseReplace(void) |
| 163 | { |
| 164 | if (has_mouse) |
| 165 | JE_drawShapeTypeOne(mouseGrabX, mouseGrabY, mouseGrabShape); |
| 166 | } |