| 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 "xmas.h" |
| 20 | |
| 21 | #include "font.h" |
| 22 | #include "joystick.h" |
| 23 | #include "keyboard.h" |
| 24 | #include "mouse.h" |
| 25 | #include "palette.h" |
| 26 | #include "sprite.h" |
| 27 | #include "vga256d.h" |
| 28 | #include "video.h" |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <time.h> |
| 32 | |
| 33 | bool xmas = false; |
| 34 | |
| 35 | bool xmas_time(void) |
| 36 | { |
| 37 | time_t now = time(NULL); |
| 38 | return localtime(&now)->tm_mon == 11; |
| 39 | } |
| 40 | |
| 41 | bool xmas_prompt(void) |
| 42 | { |
| 43 | static const char *const prompt[] = |
| 44 | { |
| 45 | "Christmas has been detected.", |
| 46 | "Activate Christmas?", |
| 47 | }; |
| 48 | static const char *const choices[] = |
| 49 | { |
| 50 | "Yes", |
| 51 | "No", |
| 52 | }; |
| 53 | |
| 54 | if (shopSpriteSheet.data == NULL) |
| 55 | JE_loadCompShapes(&shopSpriteSheet, '1'); // need mouse pointer sprites |
| 56 | |
| 57 | bool restart = true; |
| 58 | |
| 59 | struct |
| 60 | { |
| 61 | Sint16 x; |
| 62 | Sint16 y; |
| 63 | Uint8 dyAcc; |
| 64 | Uint8 dyAdd; |
| 65 | } |
| 66 | flakes[80]; |
| 67 | |
| 68 | static const Uint8 dyDen = 128; |
| 69 | |
| 70 | const int xCenter = 320 / 2; |
| 71 | const int yPrompt = 85; |
| 72 | const int dyPrompt = 15; |
| 73 | const int wChoice = 40; |
| 74 | const int yChoice = 120; |
| 75 | const int hChoice = 13; |
| 76 | |
| 77 | size_t selectedIndex = 0; |
| 78 | |
| 79 | for (; ; ) |
| 80 | { |
| 81 | bool mouseMoved = false; |
| 82 | do |
| 83 | { |
| 84 | if (restart) |
| 85 | { |
| 86 | for (size_t i = 0; i < COUNTOF(flakes); ++i) |
| 87 | { |
| 88 | flakes[i].y = 200 + rand() % 200; |
| 89 | flakes[i].dyAdd = dyDen / 2 + i * dyDen / COUNTOF(flakes); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | for (size_t i = 0; i < COUNTOF(flakes); ++i) |
| 94 | { |
| 95 | if (flakes[i].y >= 200) |
| 96 | { |
| 97 | flakes[i].x = rand() % 320; |
| 98 | flakes[i].y = 200 - 14 - flakes[i].y; |
| 99 | flakes[i].dyAcc = flakes[i].dyAdd; |
| 100 | } |
| 101 | |
| 102 | int temp = rand() & 0xF; |
| 103 | if ((temp & 0xE) == 0) |
| 104 | flakes[i].x += (temp & 1) * 2 - 1; |
| 105 | |
| 106 | Uint16 dyNum = flakes[i].dyAcc + flakes[i].dyAdd; |
| 107 | Uint8 dy = dyNum / dyDen; |
| 108 | flakes[i].dyAcc = dyNum % dyDen; |
| 109 | flakes[i].y += dy; |
| 110 | } |
| 111 | |
| 112 | fill_rectangle_wh(VGAScreen, 0, 0, 320, 200, 0x8F); |
| 113 | |
| 114 | // Draw background snowflakes. |
| 115 | for (size_t i = 0; i < COUNTOF(flakes) * 2 / 3; ++i) |
| 116 | blit_sprite2_blend(VGAScreen, flakes[i].x, flakes[i].y, spriteSheet8, 225); |
| 117 | |
| 118 | // Draw prompt. |
| 119 | for (uint i = 0; i < COUNTOF(prompt); ++i) |
| 120 | draw_font_hv_full_shadow(VGAScreen, xCenter, yPrompt + dyPrompt * i, prompt[i], normal_font, centered, (i % 2) ? 2 : 4, -2, true, 1); |
| 121 | |
| 122 | // Draw choices. |
| 123 | for (size_t i = 0; i < COUNTOF(choices); ++i) |
| 124 | { |
| 125 | const int x = xCenter - wChoice / 2 + wChoice * (int)i; |
| 126 | |
| 127 | const bool selected = (selectedIndex == i); |
| 128 | |
| 129 | draw_font_hv_full_shadow(VGAScreen, x, yChoice, choices[i], normal_font, centered, 15, selected ? -2 : -4, true, 1); |
| 130 | } |
| 131 | |
| 132 | // Draw foreground snowflakes. |
| 133 | for (size_t i = COUNTOF(flakes) * 2 / 3; i < COUNTOF(flakes); ++i) |
| 134 | blit_sprite2_blend(VGAScreen, flakes[i].x, flakes[i].y, spriteSheet8, 226); |
| 135 | |
| 136 | if (restart) |
| 137 | { |
| 138 | mouseCursor = MOUSE_POINTER_NORMAL; |
| 139 | |
| 140 | fade_palette(palettes[0], 10, 0, 255); |
| 141 | |
| 142 | restart = false; |
| 143 | } |
| 144 | |
| 145 | service_SDL_events(true); |
| 146 | |
| 147 | JE_mouseStart(); |
| 148 | JE_showVGA(); |
| 149 | JE_mouseReplace(); |
| 150 | |
| 151 | SDL_Delay(16); |
| 152 | |
| 153 | Uint16 oldMouseX = mouse_x; |
| 154 | Uint16 oldMouseY = mouse_y; |
| 155 | |
| 156 | push_joysticks_as_keyboard(); |
| 157 | service_SDL_events(false); |
| 158 | |
| 159 | mouseMoved = mouse_x != oldMouseX || mouse_y != oldMouseY; |
| 160 | } while (!(newkey || newmouse || mouseMoved)); |
| 161 | |
| 162 | // Handle interaction. |
| 163 | |
| 164 | bool action = false; |
| 165 | bool cancel = false; |
| 166 | |
| 167 | if (mouseMoved || newmouse) |
| 168 | { |
| 169 | // Find choice that was hovered or clicked. |
| 170 | if (mouse_y >= yChoice && mouse_y < yChoice + hChoice) |
| 171 | { |
| 172 | for (size_t i = 0; i < COUNTOF(choices); ++i) |
| 173 | { |
| 174 | const int xChoice = xCenter - wChoice + wChoice * (int)i; |
| 175 | if (mouse_x >= xChoice && mouse_x < xChoice + wChoice) |
| 176 | { |
| 177 | selectedIndex = i; |
| 178 | |
| 179 | if (newmouse && lastmouse_but == SDL_BUTTON_LEFT && |
| 180 | lastmouse_x >= xChoice && lastmouse_x < xChoice + wChoice && |
| 181 | lastmouse_y >= yChoice && lastmouse_y < yChoice + hChoice) |
| 182 | { |
| 183 | action = true; |
| 184 | } |
| 185 | |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (newmouse) |
| 193 | { |
| 194 | if (lastmouse_but == SDL_BUTTON_RIGHT) |
| 195 | { |
| 196 | cancel = true; |
| 197 | } |
| 198 | } |
| 199 | else if (newkey) |
| 200 | { |
| 201 | switch (lastkey_scan) |
| 202 | { |
| 203 | case SDL_SCANCODE_LEFT: |
| 204 | { |
| 205 | selectedIndex = selectedIndex == 0 |
| 206 | ? COUNTOF(choices) - 1 |
| 207 | : selectedIndex - 1; |
| 208 | break; |
| 209 | } |
| 210 | case SDL_SCANCODE_RIGHT: |
| 211 | { |
| 212 | selectedIndex = selectedIndex == COUNTOF(choices) - 1 |
| 213 | ? 0 |
| 214 | : selectedIndex + 1; |
| 215 | break; |
| 216 | } |
| 217 | case SDL_SCANCODE_SPACE: |
| 218 | case SDL_SCANCODE_RETURN: |
| 219 | { |
| 220 | action = true; |
| 221 | break; |
| 222 | } |
| 223 | case SDL_SCANCODE_ESCAPE: |
| 224 | { |
| 225 | cancel = true; |
| 226 | break; |
| 227 | } |
| 228 | default: |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (action || cancel) |
| 234 | { |
| 235 | fade_black(10); |
| 236 | |
| 237 | return !cancel && selectedIndex == 0; |
| 238 | } |
| 239 | } |
| 240 | } |