| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: hamfake.cpp ( POWDER Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * This file implements all the fake ham functions. |
| 14 | * It also stores the global state of the hardware. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifdef WIN32 |
| 19 | #pragma comment(lib, "SDL.lib") |
| 20 | #pragma comment(lib, "SDLmain.lib") |
| 21 | #endif |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <assert.h> |
| 26 | #include <iostream> |
| 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | #include "mygba.h" |
| 31 | #include "hamfake.h" |
| 32 | #include "SDL.h" |
| 33 | |
| 34 | #include "../../gfxengine.h" |
| 35 | #include "../../bmp.h" |
| 36 | #include "../../assert.h" |
| 37 | #include "../../control.h" |
| 38 | |
| 39 | //#include "../../gfx/icon_sdl.bmp.c" |
| 40 | |
| 41 | #define SRAMSIZE 65536 |
| 42 | |
| 43 | #define KEY_REPEAT_INITIAL 15 |
| 44 | #define KEY_REPEAT_AFTER 7 |
| 45 | |
| 46 | SDL_Joystick *joystick = NULL; |
| 47 | |
| 48 | #define MAX_JOY_BUTTON 12 |
| 49 | |
| 50 | // |
| 51 | // Global GBA state: |
| 52 | // |
| 53 | SDL_Surface *glbVideoSurface; |
| 54 | bool glbFullScreen = false; |
| 55 | int glbScreenWidth = HAM_SCRW; |
| 56 | int glbScreenHeight = HAM_SCRH; |
| 57 | int glbScaleFactor = 1; |
| 58 | int glbScreenFudgeX = 0; |
| 59 | int glbScreenFudgeY = 0; |
| 60 | |
| 61 | int glbStylusX = 0, glbStylusY = 0; |
| 62 | bool glbStylusState = false; |
| 63 | |
| 64 | bool glbJoyState[MAX_JOY_BUTTON]; |
| 65 | |
| 66 | BUTTONS glbJoyToButton[MAX_JOY_BUTTON] = |
| 67 | { |
| 68 | BUTTON_Y, // Tri |
| 69 | BUTTON_B, // Circle |
| 70 | BUTTON_A, // Cross |
| 71 | BUTTON_X, // Square |
| 72 | BUTTON_L, |
| 73 | BUTTON_R, |
| 74 | BUTTON_DOWN, |
| 75 | BUTTON_LEFT, |
| 76 | BUTTON_UP, |
| 77 | BUTTON_RIGHT, |
| 78 | BUTTON_SELECT, |
| 79 | BUTTON_START |
| 80 | }; |
| 81 | |
| 82 | struct SPRITEDATA |
| 83 | { |
| 84 | bool active; |
| 85 | int x, y; |
| 86 | u8 data[4*8*8]; |
| 87 | }; |
| 88 | |
| 89 | SPRITEDATA *glbSpriteList; |
| 90 | |
| 91 | #define MAX_SPRITES 128 |
| 92 | |
| 93 | // Do we have an extra tileset loaded? |
| 94 | bool ham_extratileset = false; |
| 95 | |
| 96 | |
| 97 | // Background state: |
| 98 | bg_info ham_bg[4]; |
| 99 | |
| 100 | bool glb_isnewframe = 0, glb_isdirty = false; |
| 101 | |
| 102 | u8 glb_palette[2048]; |
| 103 | |
| 104 | // Mode 3 screen - a raw 15bit bitmap. |
| 105 | u16 glb_rawscreen[HAM_SCRW*HAM_SCRH]; |
| 106 | |
| 107 | // Paletted version of screen which is scaled into final SDL surface |
| 108 | // We use u16 as the high bit selects whether to use sprite or |
| 109 | // global palette |
| 110 | u16 glb_nativescreen[HAM_SCRW*HAM_SCRH]; |
| 111 | |
| 112 | char glb_rawSRAM[SRAMSIZE]; |
| 113 | |
| 114 | int glb_videomode = -1; |
| 115 | |
| 116 | void |
| 117 | rebuildVideoSystemFromGlobals() |
| 118 | { |
| 119 | int flags = SDL_RESIZABLE; |
| 120 | |
| 121 | if (glbFullScreen) |
| 122 | flags |= SDL_FULLSCREEN; |
| 123 | |
| 124 | // do resize, no fullscreen |
| 125 | flags = SDL_SWSURFACE;// | SDL_DOUBLEBUF; |
| 126 | |
| 127 | // Yes, we want a guaranteed 24bit video mode. |
| 128 | glbVideoSurface = SDL_SetVideoMode(glbScreenWidth, glbScreenHeight, |
| 129 | 32, flags); |
| 130 | |
| 131 | if (!glbVideoSurface) |
| 132 | { |
| 133 | //printf("Failed to create screen: %s\n", SDL_GetError()); |
| 134 | //SDL_Quit(); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | // Setup our window environment. |
| 139 | //SDL_WM_SetCaption("POWDER", 0); |
| 140 | |
| 141 | // We want the cursor enabled now that you can click on stuff. |
| 142 | // SDL_ShowCursor(SDL_DISABLE); |
| 143 | |
| 144 | glb_isdirty = true; |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | hamfake_setFullScreen(bool fullscreen) |
| 149 | { |
| 150 | // Pretty straight forward :> |
| 151 | glbFullScreen = fullscreen; |
| 152 | rebuildVideoSystemFromGlobals(); |
| 153 | } |
| 154 | |
| 155 | bool |
| 156 | hamfake_isFullScreen() |
| 157 | { |
| 158 | return glbFullScreen; |
| 159 | } |
| 160 | |
| 161 | // Updates our glbScreenWidth & glbScreenHeight appropriately. Finds a scale |
| 162 | // factor that will fit. |
| 163 | void |
| 164 | setResolution(int width, int height) |
| 165 | { |
| 166 | // Find maximum scale factor that fits. |
| 167 | glbScaleFactor = 2; |
| 168 | while (1) |
| 169 | { |
| 170 | glbScreenWidth = HAM_SCRW * glbScaleFactor; |
| 171 | glbScreenHeight = HAM_SCRH * glbScaleFactor; |
| 172 | if (glbScreenWidth > width || glbScreenHeight > height) |
| 173 | { |
| 174 | glbScaleFactor--; |
| 175 | break; |
| 176 | } |
| 177 | glbScaleFactor++; |
| 178 | } |
| 179 | |
| 180 | glbScreenWidth = width; |
| 181 | glbScreenHeight = height; |
| 182 | |
| 183 | // Do not allow fractional scales. |
| 184 | if (glbScreenWidth < HAM_SCRW) |
| 185 | glbScreenWidth = HAM_SCRW; |
| 186 | if (glbScreenHeight < HAM_SCRH) |
| 187 | glbScreenHeight = HAM_SCRH; |
| 188 | |
| 189 | // Calculate our fudge factor... |
| 190 | glbScreenFudgeX = ((glbScreenWidth - HAM_SCRW * glbScaleFactor) / 2); |
| 191 | glbScreenFudgeY = ((glbScreenHeight - HAM_SCRH * glbScaleFactor) / 2); |
| 192 | |
| 193 | // Take effect! |
| 194 | rebuildVideoSystemFromGlobals(); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | scaleScreenFromPaletted(u8 *dst, int pitch) |
| 199 | { |
| 200 | int x, y, s_y, s_x; |
| 201 | u8 pixel[3]; |
| 202 | u16 idx; |
| 203 | u16 *src; |
| 204 | |
| 205 | // Clear initial dst... |
| 206 | memset(dst, 0, pitch * glbScreenHeight); |
| 207 | |
| 208 | // Add in fudge factor |
| 209 | dst += glbScreenFudgeX * 4 + glbScreenFudgeY * pitch; |
| 210 | |
| 211 | src = glb_nativescreen; |
| 212 | for (y = 0; y < HAM_SCRH; y++) |
| 213 | { |
| 214 | for (s_y = 0; s_y < glbScaleFactor; s_y++) |
| 215 | { |
| 216 | for (x = 0; x < HAM_SCRW; x++) |
| 217 | { |
| 218 | // Read in a pixel & decode |
| 219 | idx = src[x]; |
| 220 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
| 221 | |
| 222 | pixel[0] = glb_palette[idx*4+3]; |
| 223 | pixel[1] = glb_palette[idx*4+2]; |
| 224 | pixel[2] = glb_palette[idx*4+1]; |
| 225 | #else |
| 226 | |
| 227 | pixel[0] = glb_palette[idx*4+1]; |
| 228 | pixel[1] = glb_palette[idx*4+2]; |
| 229 | pixel[2] = glb_palette[idx*4+3]; |
| 230 | #endif |
| 231 | |
| 232 | // Now write out the needed number of times... |
| 233 | for (s_x = 0; s_x < glbScaleFactor; s_x++) |
| 234 | { |
| 235 | *dst++ = pixel[2]; |
| 236 | *dst++ = pixel[1]; |
| 237 | *dst++ = pixel[0]; |
| 238 | *dst++ = 0xff; |
| 239 | } |
| 240 | } |
| 241 | // Add remainder of pitch. |
| 242 | dst += pitch - 4 * glbScaleFactor * HAM_SCRW; |
| 243 | } |
| 244 | src += HAM_SCRW; |
| 245 | } |
| 246 | |
| 247 | //SDL_Flip(glbVideoSurface); |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | scaleScreenFrom15bit(u8 *dst, int pitch) |
| 252 | { |
| 253 | int x, y, s_y, s_x; |
| 254 | u8 pixel[3]; |
| 255 | u16 raw; |
| 256 | u16 *src; |
| 257 | |
| 258 | // Clear initial dst... |
| 259 | memset(dst, 0, pitch * glbScreenHeight); |
| 260 | |
| 261 | // Add in fudge factor |
| 262 | dst += glbScreenFudgeX * 4 + glbScreenFudgeY * pitch; |
| 263 | |
| 264 | src = glb_rawscreen; |
| 265 | for (y = 0; y < HAM_SCRH; y++) |
| 266 | { |
| 267 | for (s_y = 0; s_y < glbScaleFactor; s_y++) |
| 268 | { |
| 269 | for (x = 0; x < HAM_SCRW; x++) |
| 270 | { |
| 271 | // Read in a pixel & decode |
| 272 | raw = src[x]; |
| 273 | |
| 274 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
| 275 | |
| 276 | pixel[0] = (raw & 31) << 3; |
| 277 | pixel[1] = ((raw >> 5) & 31) << 3; |
| 278 | pixel[2] = ((raw >> 10) & 31) << 3; |
| 279 | #else |
| 280 | |
| 281 | pixel[2] = (raw & 31) << 3; |
| 282 | pixel[1] = ((raw >> 5) & 31) << 3; |
| 283 | pixel[0] = ((raw >> 10) & 31) << 3; |
| 284 | #endif |
| 285 | |
| 286 | |
| 287 | // Now write out the needed number of times... |
| 288 | for (s_x = 0; s_x < glbScaleFactor; s_x++) |
| 289 | { |
| 290 | *dst++ = pixel[2]; |
| 291 | *dst++ = pixel[1]; |
| 292 | *dst++ = pixel[0]; |
| 293 | *dst++ = 0xff; |
| 294 | } |
| 295 | } |
| 296 | // Add remainder of pitch. |
| 297 | dst += pitch - 4 * glbScaleFactor * HAM_SCRW; |
| 298 | } |
| 299 | src += HAM_SCRW; |
| 300 | } |
| 301 | |
| 302 | //SDL_Flip(glbVideoSurface); |
| 303 | } |
| 304 | |
| 305 | // Draws the given sprite to the native buffer. |
| 306 | void |
| 307 | blitSprite(const SPRITEDATA &sprite) |
| 308 | { |
| 309 | if (sprite.active) |
| 310 | { |
| 311 | // Finally, blit the sprite. |
| 312 | int sx, sy, x, y, tx, ty, srcoffset; |
| 313 | u8 src; |
| 314 | |
| 315 | for (sy = 0; sy < 16; sy++) |
| 316 | { |
| 317 | for (sx = 0; sx < 16; sx++) |
| 318 | { |
| 319 | x = sprite.x + sx; |
| 320 | y = sprite.y + sy; |
| 321 | // Convert to DS coords from GBA |
| 322 | x += 8; |
| 323 | y += 16; |
| 324 | tx = sx >> 3; |
| 325 | ty = sy >> 3; |
| 326 | srcoffset = (sx & 7) + tx * 8 * 8 + |
| 327 | ty * 2 * 8 * 8 + (sy & 7) * 8; |
| 328 | src = sprite.data[srcoffset]; |
| 329 | if (src && |
| 330 | x >= 0 && x < HAM_SCRW && |
| 331 | y >= 0 && y < HAM_SCRH) |
| 332 | { |
| 333 | glb_nativescreen[x + y * HAM_SCRW] = src + 256; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | hamfake_rebuildScreen() |
| 342 | { |
| 343 | int i, layer, x, y, tileidx, tx, ty; |
| 344 | int offset; |
| 345 | |
| 346 | if (!glb_isdirty) |
| 347 | return; |
| 348 | |
| 349 | glb_isdirty = false; |
| 350 | |
| 351 | if (glb_videomode == 3) |
| 352 | { |
| 353 | u8 *dst = 0; |
| 354 | |
| 355 | //SDL_LockSurface(glbVideoSurface); |
| 356 | |
| 357 | if (glbVideoSurface) |
| 358 | { |
| 359 | dst = (u8 *) glbVideoSurface->pixels; |
| 360 | if (!dst) |
| 361 | { |
| 362 | //printf("Lock failure: %s\n", SDL_GetError()); |
| 363 | } |
| 364 | |
| 365 | scaleScreenFrom15bit(dst, glbVideoSurface->pitch); |
| 366 | |
| 367 | //SDL_UnlockSurface(glbVideoSurface); |
| 368 | // Rebuild from 15bit screen. |
| 369 | SDL_UpdateRect(glbVideoSurface, 0, 0, 0, 0); |
| 370 | } |
| 371 | |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | // Clear our video surface. |
| 376 | memset(glb_nativescreen, 0, HAM_SCRH * HAM_SCRW * sizeof(u16)); |
| 377 | |
| 378 | // Blit each layer in turn. |
| 379 | // Hard coded layer order: |
| 380 | int lay_ord[4] = { 1, 2, 3, 0 }; |
| 381 | for (i = 0; i < 4; i++) |
| 382 | { |
| 383 | layer = lay_ord[i]; |
| 384 | |
| 385 | ty = ham_bg[layer].scrolly / 8; |
| 386 | ty %= ham_bg[layer].mi->height; |
| 387 | if (ty < 0) |
| 388 | ty += ham_bg[layer].mi->height; |
| 389 | for (y = 0; y < HAM_SCRH / 8; y++) |
| 390 | { |
| 391 | tx = ham_bg[layer].scrollx / 8; |
| 392 | tx %= ham_bg[layer].mi->width; |
| 393 | if (tx < 0) |
| 394 | tx += ham_bg[layer].mi->width; |
| 395 | |
| 396 | for (x = 0; x < HAM_SCRW / 8; x++) |
| 397 | { |
| 398 | u8 *tile; |
| 399 | |
| 400 | tileidx = ham_bg[layer].mi->tiles[ty * ham_bg[layer].mi->width + tx]; |
| 401 | |
| 402 | // Out of bound tiles are ignored. |
| 403 | if ((tileidx & 1023) < ham_bg[layer].ti->numtiles) |
| 404 | { |
| 405 | tile = ham_bg[layer].ti->tiles[tileidx & 1023]; |
| 406 | |
| 407 | // Compute offset into native screen |
| 408 | offset = x * 8 + y * 8 * HAM_SCRW; |
| 409 | |
| 410 | // Figure out any flip flags. |
| 411 | if (tileidx & (2048 | 1024)) |
| 412 | { |
| 413 | int sx, sy, fx, fy; |
| 414 | |
| 415 | for (sy = 0; sy < 8; sy++) |
| 416 | { |
| 417 | if (tileidx & 2048) |
| 418 | fy = 7 - sy; |
| 419 | else |
| 420 | fy = sy; |
| 421 | |
| 422 | for (sx = 0; sx < 8; sx++) |
| 423 | { |
| 424 | if (tileidx & 1024) |
| 425 | fx = 7 - sx; |
| 426 | else |
| 427 | fx = sx; |
| 428 | |
| 429 | // Write if non-zero |
| 430 | if (tile[fx + fy * 8]) |
| 431 | glb_nativescreen[offset] = tile[fx + fy * 8]; |
| 432 | offset++; |
| 433 | } |
| 434 | offset += HAM_SCRW - 8; |
| 435 | } |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | int sx, sy; |
| 440 | |
| 441 | // Straight forward write... |
| 442 | for (sy = 0; sy < 8; sy++) |
| 443 | { |
| 444 | for (sx = 0; sx < 8; sx++) |
| 445 | { |
| 446 | if (*tile) |
| 447 | { |
| 448 | glb_nativescreen[offset] = *tile; |
| 449 | } |
| 450 | tile++; |
| 451 | offset++; |
| 452 | } |
| 453 | offset += HAM_SCRW - 8; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | tx++; |
| 459 | if (tx >= ham_bg[layer].mi->width) |
| 460 | tx -= ham_bg[layer].mi->width; |
| 461 | } |
| 462 | |
| 463 | ty++; |
| 464 | if (ty >= ham_bg[layer].mi->height) |
| 465 | ty -= ham_bg[layer].mi->height; |
| 466 | } |
| 467 | |
| 468 | // Just before the text layer we draw our sprites |
| 469 | // This ensures the buttons are under the text. |
| 470 | if (i == 2) |
| 471 | { |
| 472 | int j; |
| 473 | |
| 474 | // Since we composite on top, we want to do in reverse order of |
| 475 | // priority. Hence the backwards loop. |
| 476 | // The last sprite is the cursor and is done at the very end. |
| 477 | for (j = MAX_SPRITES; j --> 1; ) |
| 478 | { |
| 479 | blitSprite(glbSpriteList[j]); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Draw the cursor sprite |
| 485 | blitSprite(glbSpriteList[0]); |
| 486 | |
| 487 | // Lock and update the final surface using our lookup... |
| 488 | { |
| 489 | u8 *dst = 0; |
| 490 | |
| 491 | //SDL_LockSurface(glbVideoSurface); |
| 492 | if (glbVideoSurface) |
| 493 | { |
| 494 | dst = (u8 *) glbVideoSurface->pixels; |
| 495 | if (!dst) |
| 496 | { |
| 497 | //printf("Lock failure: %s\n", SDL_GetError()); |
| 498 | } |
| 499 | |
| 500 | scaleScreenFromPaletted(dst, glbVideoSurface->pitch); |
| 501 | |
| 502 | //SDL_UnlockSurface(glbVideoSurface); |
| 503 | // Rebuild from 8bit screen. |
| 504 | SDL_UpdateRect(glbVideoSurface, 0, 0, 0, 0); |
| 505 | } |
| 506 | |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // Wait for an event to occur. |
| 511 | void |
| 512 | hamfake_awaitEvent() |
| 513 | { |
| 514 | SDL_WaitEvent(0); |
| 515 | } |
| 516 | |
| 517 | // Return our internal screen. |
| 518 | u16 * |
| 519 | hamfake_lockScreen() |
| 520 | { |
| 521 | // Note we have to offset to the normal GBA start |
| 522 | // location to account for our official offset. |
| 523 | return glb_rawscreen + 8 + 16 * 256; |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | hamfake_unlockScreen(u16 *) |
| 528 | { |
| 529 | glb_isdirty = true; |
| 530 | } |
| 531 | |
| 532 | // Deal with our SRAM buffer. |
| 533 | char * |
| 534 | hamfake_writeLockSRAM() |
| 535 | { |
| 536 | return glb_rawSRAM; |
| 537 | } |
| 538 | |
| 539 | void |
| 540 | hamfake_writeUnlockSRAM(char *) |
| 541 | {} |
| 542 | |
| 543 | char * |
| 544 | hamfake_readLockSRAM() |
| 545 | { |
| 546 | return glb_rawSRAM; |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | hamfake_readUnlockSRAM(char *) |
| 551 | {} |
| 552 | |
| 553 | void |
| 554 | hamfake_endWritingSession() |
| 555 | { |
| 556 | FILE *fp; |
| 557 | |
| 558 | fp = fopen("powder.sav", "wb"); |
| 559 | |
| 560 | if (!fp) |
| 561 | { |
| 562 | cerr << "Failure to open powder.sav for writing!" << endl; |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | fwrite(glb_rawSRAM, SRAMSIZE, 1, fp); |
| 567 | |
| 568 | fclose(fp); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | int getJoyButton(BUTTONS button) |
| 573 | { |
| 574 | int i; |
| 575 | |
| 576 | for (i = 0; i < MAX_JOY_BUTTON; i++) |
| 577 | if (glbJoyToButton[i] == button) |
| 578 | return i; |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | // Called to run our event poll |
| 583 | void |
| 584 | hamfake_pollEvents() |
| 585 | { |
| 586 | SDL_Event event; |
| 587 | |
| 588 | while (SDL_PollEvent(&event)) |
| 589 | { |
| 590 | switch (event.type) |
| 591 | { |
| 592 | case SDL_VIDEORESIZE: |
| 593 | setResolution(event.resize.w, event.resize.h); |
| 594 | break; |
| 595 | |
| 596 | case SDL_JOYBUTTONDOWN: |
| 597 | // If button press is in range, set our state |
| 598 | if (event.jbutton.button < MAX_JOY_BUTTON) |
| 599 | glbJoyState[event.jbutton.button] = true; |
| 600 | break; |
| 601 | case SDL_JOYBUTTONUP: |
| 602 | if (event.jbutton.button < MAX_JOY_BUTTON) |
| 603 | glbJoyState[event.jbutton.button] = false; |
| 604 | break; |
| 605 | |
| 606 | case SDL_QUIT: |
| 607 | SDL_Quit(); |
| 608 | exit(0); |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | |
| 615 | bool |
| 616 | hamfake_isPressed(BUTTONS button) |
| 617 | { |
| 618 | int bnum; |
| 619 | |
| 620 | hamfake_pollEvents(); |
| 621 | hamfake_rebuildScreen(); |
| 622 | |
| 623 | bnum = getJoyButton(button); |
| 624 | if (bnum < 0) |
| 625 | return false; |
| 626 | |
| 627 | return glbJoyState[bnum]; |
| 628 | } |
| 629 | |
| 630 | int |
| 631 | hamfake_isAnyPressed() |
| 632 | { |
| 633 | hamfake_pollEvents(); |
| 634 | hamfake_rebuildScreen(); |
| 635 | |
| 636 | int i; |
| 637 | for (i = 0; i < MAX_JOY_BUTTON; i++) |
| 638 | if (glbJoyState[i]) |
| 639 | return 0x0; |
| 640 | |
| 641 | return 0x3FF; |
| 642 | } |
| 643 | |
| 644 | int |
| 645 | hamfake_peekKeyPress() |
| 646 | { |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | int |
| 651 | hamfake_getKeyPress(bool onlyascii) |
| 652 | { |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | void |
| 657 | hamfake_insertKeyPress(int key) |
| 658 | { |
| 659 | } |
| 660 | |
| 661 | void |
| 662 | hamfake_clearKeyboardBuffer() |
| 663 | { |
| 664 | } |
| 665 | |
| 666 | void |
| 667 | hamfake_setScrollX(int layer, int scroll) |
| 668 | { |
| 669 | ham_bg[layer].scrollx = scroll; |
| 670 | } |
| 671 | |
| 672 | void |
| 673 | hamfake_setScrollY(int layer, int scroll) |
| 674 | { |
| 675 | ham_bg[layer].scrolly = scroll; |
| 676 | } |
| 677 | |
| 678 | u8 * |
| 679 | convertTo32Bit(const unsigned short *s, int numpixel) |
| 680 | { |
| 681 | u8 *result, *d; |
| 682 | unsigned short p; |
| 683 | |
| 684 | result = (u8 *) malloc(numpixel * 4); |
| 685 | d = result; |
| 686 | |
| 687 | while (numpixel--) |
| 688 | { |
| 689 | p = *s++; |
| 690 | *d++ = (p & 31) * 8 + 4; |
| 691 | p >>= 5; |
| 692 | *d++ = (p & 31) * 8 + 4; |
| 693 | p >>= 5; |
| 694 | *d++ = (p & 31) * 8 + 4; |
| 695 | *d++ = 0xff; |
| 696 | } |
| 697 | return result; |
| 698 | } |
| 699 | |
| 700 | void |
| 701 | ham_Init() |
| 702 | { |
| 703 | int i; |
| 704 | |
| 705 | printf("\nPOWDER initializing...\n"); |
| 706 | |
| 707 | /* initialize SDL */ |
| 708 | if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) < 0 ) |
| 709 | { |
| 710 | printf("Video initialization failed: %s\n", SDL_GetError( ) ); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | SDL_ShowCursor(0); |
| 715 | |
| 716 | if(SDL_NumJoysticks()) |
| 717 | { |
| 718 | joystick = SDL_JoystickOpen(0); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | printf("no joystick detected\n"); |
| 723 | } |
| 724 | |
| 725 | SDL_WM_SetCaption("Powder", 0); |
| 726 | SDL_Delay(1000); |
| 727 | |
| 728 | glbSpriteList = new SPRITEDATA[MAX_SPRITES]; |
| 729 | for (i = 0; i < MAX_SPRITES; i++) |
| 730 | { |
| 731 | glbSpriteList[i].active = false; |
| 732 | } |
| 733 | |
| 734 | memset(glbJoyState, 0, MAX_JOY_BUTTON * sizeof(bool)); |
| 735 | |
| 736 | // Load any save games. |
| 737 | FILE *fp; |
| 738 | |
| 739 | fp = fopen("powder.sav", "rb"); |
| 740 | if (fp) |
| 741 | { |
| 742 | fread(glb_rawSRAM, SRAMSIZE, 1, fp); |
| 743 | fclose(fp); |
| 744 | } |
| 745 | |
| 746 | // Load any extra tilesets. |
| 747 | if (bmp_loadExtraTileset()) |
| 748 | { |
| 749 | ham_extratileset = true; |
| 750 | printf("External tileset loaded.\n"); |
| 751 | } |
| 752 | |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | void (*glb_vblcallback)() = 0; |
| 757 | |
| 758 | Uint32 |
| 759 | glb_VBLTimerCallback(Uint32 interval, void *fp) |
| 760 | { |
| 761 | glb_isnewframe = 1; |
| 762 | (*glb_vblcallback)(); |
| 763 | |
| 764 | // Push a fake event so we wake up. |
| 765 | SDL_Event event; |
| 766 | |
| 767 | event.type = SDL_USEREVENT; |
| 768 | SDL_PushEvent(&event); |
| 769 | |
| 770 | return 16; |
| 771 | } |
| 772 | |
| 773 | void |
| 774 | ham_StartIntHandler(u8 intno, void (*fp)()) |
| 775 | { |
| 776 | assert(intno == INT_TYPE_VBL); |
| 777 | |
| 778 | glb_vblcallback = fp; |
| 779 | SDL_AddTimer(16, glb_VBLTimerCallback, 0); |
| 780 | } |
| 781 | |
| 782 | void |
| 783 | ham_SetBgMode(u8 mode) |
| 784 | { |
| 785 | if (glb_videomode == -1) |
| 786 | { |
| 787 | setResolution(HAM_SCRW * 2, HAM_SCRH * 2); |
| 788 | rebuildVideoSystemFromGlobals(); |
| 789 | } |
| 790 | |
| 791 | glb_videomode = mode; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | void |
| 796 | ham_LoadBGPal(void *vpal, int bytes) |
| 797 | { |
| 798 | // Need to promote our 15bit palette to a 24 bit one. |
| 799 | int entries = bytes / 2, i; |
| 800 | u16 *pal = (u16 *) vpal; |
| 801 | |
| 802 | for (i = 0; i < entries; i++) |
| 803 | { |
| 804 | glb_palette[i*4+3] = (pal[i] & 31) << 3; |
| 805 | glb_palette[i*4+2] = ((pal[i] >> 5) & 31) << 3; |
| 806 | glb_palette[i*4+1] = ((pal[i] >> 10) & 31) << 3; |
| 807 | if (i) |
| 808 | glb_palette[i*4] = 255; |
| 809 | else |
| 810 | glb_palette[i*4] = 0; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | map_info_ptr |
| 815 | ham_InitMapEmptySet(u8 size, u8 ) |
| 816 | { |
| 817 | map_info_ptr mi; |
| 818 | |
| 819 | mi = new map_info; |
| 820 | |
| 821 | if (size == 0) |
| 822 | { |
| 823 | mi->width = 32; |
| 824 | mi->height = 32; |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | mi->width = 64; |
| 829 | mi->height = 64; |
| 830 | } |
| 831 | |
| 832 | mi->tiles = new int[mi->width * mi->height]; |
| 833 | memset(mi->tiles, 0, sizeof(int) * mi->width * mi->height); |
| 834 | |
| 835 | return mi; |
| 836 | } |
| 837 | |
| 838 | tile_info_ptr |
| 839 | ham_InitTileEmptySet(int entries, int , int) |
| 840 | { |
| 841 | tile_info_ptr ti; |
| 842 | int i; |
| 843 | |
| 844 | ti = new tile_info; |
| 845 | ti->numtiles = entries; |
| 846 | ti->tiles = new u8 *[entries]; |
| 847 | |
| 848 | for (i = 0; i < entries; i++) |
| 849 | { |
| 850 | ti->tiles[i] = new u8 [8 * 8]; |
| 851 | } |
| 852 | return ti; |
| 853 | } |
| 854 | |
| 855 | void |
| 856 | ham_InitBg(int layer, int foo, int bar, int baz) |
| 857 | { |
| 858 | ham_bg[layer].scrollx = 0; |
| 859 | ham_bg[layer].scrolly = 0; |
| 860 | } |
| 861 | |
| 862 | void |
| 863 | ham_CreateWin(int , int, int, int, int, int, int, int) |
| 864 | {} |
| 865 | |
| 866 | void |
| 867 | ham_DeleteWin(int wid) |
| 868 | {} |
| 869 | |
| 870 | void |
| 871 | ham_SetMapTile(int layer, int mx, int my, int tileidx) |
| 872 | { |
| 873 | map_info_ptr mi; |
| 874 | |
| 875 | mi = ham_bg[layer].mi; |
| 876 | |
| 877 | mx = (mx % mi->width); |
| 878 | if (mx < 0) |
| 879 | mx += mi->width; |
| 880 | my = (my % mi->height); |
| 881 | if (my < 0) |
| 882 | my += mi->height; |
| 883 | |
| 884 | mi->tiles[mx + my * mi->width] = tileidx; |
| 885 | |
| 886 | glb_isdirty = true; |
| 887 | } |
| 888 | |
| 889 | void |
| 890 | ham_ReloadTileGfx(tile_info_ptr tiledata, const u16 *data, int destidx, |
| 891 | int numtile) |
| 892 | { |
| 893 | int t; |
| 894 | u8 *raw; |
| 895 | u8 *dst; |
| 896 | |
| 897 | raw = (u8*) data; |
| 898 | for (t = destidx; t < destidx + numtile; t++) |
| 899 | { |
| 900 | dst = tiledata->tiles[t]; |
| 901 | |
| 902 | memcpy(dst, raw, 8*8); |
| 903 | raw += 8 * 8; |
| 904 | } |
| 905 | glb_isdirty = true; |
| 906 | } |
| 907 | |
| 908 | void |
| 909 | hamfake_ReloadSpriteGfx(const u16 *data, int tileno, int numtile) |
| 910 | { |
| 911 | tileno /= 4; |
| 912 | UT_ASSERT(tileno >= 0 && tileno < MAX_SPRITES); |
| 913 | if (tileno < 0 || tileno >= MAX_SPRITES) |
| 914 | return; |
| 915 | |
| 916 | memcpy(&glbSpriteList[tileno].data, data, numtile * 8 * 8); |
| 917 | glb_isdirty = true; |
| 918 | } |
| 919 | |
| 920 | void |
| 921 | hamfake_LoadSpritePal(void *vpal, int bytes) |
| 922 | { |
| 923 | // Need to promote our 15bit palette to a 24 bit one. |
| 924 | int entries = bytes / 2, i; |
| 925 | u16 *pal = (u16 *) vpal; |
| 926 | |
| 927 | for (i = 0; i < entries; i++) |
| 928 | { |
| 929 | glb_palette[1024+i*4+3] = (pal[i] & 31) << 3; |
| 930 | glb_palette[1024+i*4+2] = ((pal[i] >> 5) & 31) << 3; |
| 931 | glb_palette[1024+i*4+1] = ((pal[i] >> 10) & 31) << 3; |
| 932 | if (i) |
| 933 | glb_palette[1024+i*4] = 255; |
| 934 | else |
| 935 | glb_palette[1024+i*4] = 0; |
| 936 | } |
| 937 | glb_isdirty = true; |
| 938 | } |
| 939 | |
| 940 | void |
| 941 | hamfake_softReset() |
| 942 | { |
| 943 | SDL_Quit(); |
| 944 | exit(0); |
| 945 | } |
| 946 | |
| 947 | bool |
| 948 | hamfake_extratileset() |
| 949 | { |
| 950 | return ham_extratileset; |
| 951 | } |
| 952 | |
| 953 | void |
| 954 | hamfake_getstyluspos(int &x, int &y) |
| 955 | { |
| 956 | x = glbStylusX; |
| 957 | y = glbStylusY; |
| 958 | // Convert accordint to our scale. |
| 959 | x -= glbScreenFudgeX; |
| 960 | y -= glbScreenFudgeY; |
| 961 | x /= glbScaleFactor; |
| 962 | y /= glbScaleFactor; |
| 963 | // After conversion we can be out of the valid bounds. |
| 964 | if (x < 0) |
| 965 | x = 0; |
| 966 | if (y < 0) |
| 967 | y = 0; |
| 968 | if (x >= HAM_SCRW) |
| 969 | x = HAM_SCRW-1; |
| 970 | if (y >= HAM_SCRH) |
| 971 | y = HAM_SCRH-1; |
| 972 | |
| 973 | // Now return to the GBA coordinate system by adjusting for the DS coords. |
| 974 | x -= 8; |
| 975 | y -= 16; |
| 976 | } |
| 977 | |
| 978 | bool |
| 979 | hamfake_getstylusstate() |
| 980 | { |
| 981 | return glbStylusState; |
| 982 | } |
| 983 | |
| 984 | void |
| 985 | hamfake_movesprite(int spriteno, int x, int y) |
| 986 | { |
| 987 | UT_ASSERT(spriteno >= 0 && spriteno < MAX_SPRITES); |
| 988 | if (spriteno < 0 || spriteno >= MAX_SPRITES) |
| 989 | return; |
| 990 | glb_isdirty = true; |
| 991 | glbSpriteList[spriteno].x = x; |
| 992 | glbSpriteList[spriteno].y = y; |
| 993 | } |
| 994 | |
| 995 | void |
| 996 | hamfake_enablesprite(int spriteno, bool enabled) |
| 997 | { |
| 998 | UT_ASSERT(spriteno >= 0 && spriteno < MAX_SPRITES); |
| 999 | if (spriteno < 0 || spriteno >= MAX_SPRITES) |
| 1000 | return; |
| 1001 | glb_isdirty = true; |
| 1002 | glbSpriteList[spriteno].active = enabled; |
| 1003 | } |
| 1004 | |
| 1005 | FILE * |
| 1006 | hamfake_fopen(const char *path, const char *mode) |
| 1007 | { |
| 1008 | // Use the local directory on these platforms. |
| 1009 | return fopen(path, mode); |
| 1010 | } |
| 1011 | |
| 1012 | bool |
| 1013 | hamfake_fatvalid() |
| 1014 | { |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
| 1018 | void |
| 1019 | hamfake_setTileSize(int width, int height) |
| 1020 | { |
| 1021 | } |
| 1022 | |
| 1023 | int |
| 1024 | hamfake_getKeyModifiers() |
| 1025 | { |
| 1026 | return 0; |
| 1027 | } |