Debian Codegraph @

port/gp2x/hamfake.cpp

raw symbols SCIP
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 <memory.h>
26 #include <assert.h>
27 #include <iostream>
28
29 using namespace std;
30
31 #include "mygba.h"
32 #include "hamfake.h"
33 #include "SDL.h"
34 #include "gp2x.h"
35
36 #include "../../gfxengine.h"
37 #include "../../bmp.h"
38 #include "../../assert.h"
39 #include "../../control.h"
40
41 #include "../../gfx/icon_sdl.bmp.c"
42
43 #define SRAMSIZE 65536
44
45 #define KEY_REPEAT_INITIAL 15
46 #define KEY_REPEAT_AFTER 7
47
48 SDL_Joystick *joystick = NULL;
49
50
51 //
52 // Global GBA state:
53 //
54 SDL_Surface *glbVideoSurface;
55 bool glbFullScreen = false;
56 int glbScreenWidth = 256;
57 int glbScreenHeight = 192;
58 int glbScaleFactor = 1;
59 int glbScreenFudgeX = 0;
60 int glbScreenFudgeY = 0;
61
62 int glbStylusX = 0, glbStylusY = 0;
63 bool glbStylusState = false;
64
65 int glbTileWidth = 8;
66 int glbTileHeight = 8;
67
68 tile_info_ptr glbTheTileSet = 0;
69
70 #define TILEWIDTH (glbTileWidth)
71 #define TILEHEIGHT (glbTileHeight)
72
73 #define HAM_SCRW (TILEWIDTH*HAM_SCRW_T)
74 #define HAM_SCRH (TILEWIDTH*HAM_SCRH_T)
75
76 struct SPRITEDATA
77 {
78 bool active;
79 int x, y;
80 u8 *data;
81 };
82
83 SPRITEDATA *glbSpriteList;
84
85 #define MAX_SPRITES 128
86
87 // Do we have an extra tileset loaded?
88 bool ham_extratileset = false;
89
90 // Keyboard state:
91 bool glb_keystate[SDLK_LAST];
92
93 // Key to push
94 int glb_keypusher = 0;
95 int glb_keypusherraw = 0;
96 // Frame to push on.
97 int glb_keypushtime = 0;
98
99 int glb_keybuf[16];
100 s8 glb_keybufentry = -1;
101
102 // Background state:
103 bg_info ham_bg[4];
104
105 bool glb_isnewframe = 0, glb_isdirty = false;
106
107 u8 glb_palette[2048];
108
109 // Mode 3 screen - a raw 15bit bitmap.
110 u16 *glb_rawscreen = 0;
111
112 // Paletted version of screen which is scaled into final SDL surface
113 // We use u16 as the high bit selects whether to use sprite or
114 // global palette
115 u16 *glb_nativescreen = 0;
116
117 char glb_rawSRAM[SRAMSIZE];
118
119 int glb_videomode = -1;
120
121 void
122 rebuildVideoSystemFromGlobals()
123 {
124 int flags = SDL_RESIZABLE;
125
126 if (glbFullScreen)
127 flags |= SDL_FULLSCREEN;
128
129 // Yes, we want a guaranteed 24bit video mode.
130 glbVideoSurface = SDL_SetVideoMode(glbScreenWidth, glbScreenHeight,
131 24, flags);
132
133 if (!glbVideoSurface)
134 {
135 fprintf(stderr, "Failed to create screen: %s\n",
136 SDL_GetError());
137 SDL_Quit();
138 exit(1);
139 }
140
141 // Setup our window environment.
142 SDL_WM_SetCaption("POWDER", 0);
143
144 SDL_ShowCursor(SDL_DISABLE);
145
146 glb_isdirty = true;
147 }
148
149 void
150 hamfake_setFullScreen(bool fullscreen)
151 {
152 // Pretty straight forward :>
153 glbFullScreen = fullscreen;
154
155 rebuildVideoSystemFromGlobals();
156 }
157
158 bool
159 hamfake_isFullScreen()
160 {
161 return glbFullScreen;
162 }
163
164 // Updates our glbScreenWidth & glbScreenHeight appropriately. Finds a scale
165 // factor that will fit.
166 void
167 setResolution(int width, int height)
168 {
169 // Find maximum scale factor that fits.
170 glbScaleFactor = 2;
171 while (1)
172 {
173 glbScreenWidth = HAM_SCRW * glbScaleFactor;
174 glbScreenHeight = HAM_SCRH * glbScaleFactor;
175 if (glbScreenWidth > width || glbScreenHeight > height)
176 {
177 glbScaleFactor--;
178 break;
179 }
180 glbScaleFactor++;
181 }
182
183 glbScreenWidth = width;
184 glbScreenHeight = height;
185
186 // Do not allow fractional scales.
187 if (glbScreenWidth < HAM_SCRW)
188 glbScreenWidth = HAM_SCRW;
189 if (glbScreenHeight < HAM_SCRH)
190 glbScreenHeight = HAM_SCRH;
191
192 // Calculate our fudge factor...
193 glbScreenFudgeX = ((glbScreenWidth - HAM_SCRW * glbScaleFactor) / 2);
194 glbScreenFudgeY = ((glbScreenHeight - HAM_SCRH * glbScaleFactor) / 2);
195
196 // Take effect!
197 rebuildVideoSystemFromGlobals();
198 }
199
200 void
201 scaleScreenFromPaletted(u8 *dst, int pitch)
202 {
203 int x, y, s_y, s_x;
204 u8 pixel[3];
205 u16 idx;
206 u16 *src;
207
208 // Clear initial dst...
209 memset(dst, 0, pitch * glbScreenHeight);
210
211 // Add in fudge factor
212 dst += glbScreenFudgeX * 3 + glbScreenFudgeY * pitch;
213
214 src = glb_nativescreen;
215 for (y = 0; y < HAM_SCRH; y++)
216 {
217 for (s_y = 0; s_y < glbScaleFactor; s_y++)
218 {
219 for (x = 0; x < HAM_SCRW; x++)
220 {
221 // Read in a pixel & decode
222 idx = src[x];
223 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
224 pixel[0] = glb_palette[idx*4+3];
225 pixel[1] = glb_palette[idx*4+2];
226 pixel[2] = glb_palette[idx*4+1];
227 #else
228 pixel[0] = glb_palette[idx*4+1];
229 pixel[1] = glb_palette[idx*4+2];
230 pixel[2] = glb_palette[idx*4+3];
231 #endif
232 // Now write out the needed number of times...
233 for (s_x = 0; s_x < glbScaleFactor; s_x++)
234 {
235 *dst++ = pixel[0];
236 *dst++ = pixel[1];
237 *dst++ = pixel[2];
238 }
239 }
240 // Add remainder of pitch.
241 dst += pitch - 3 * glbScaleFactor * HAM_SCRW;
242 }
243 src += HAM_SCRW;
244 }
245 }
246
247 void
248 scaleScreenFrom15bit(u8 *dst, int pitch)
249 {
250 int x, y, s_y, s_x;
251 u8 pixel[3];
252 u16 raw;
253 u16 *src;
254
255 // Clear initial dst...
256 memset(dst, 0, pitch * glbScreenHeight);
257
258 // Add in fudge factor
259 dst += glbScreenFudgeX * 3 + glbScreenFudgeY * pitch;
260
261 src = glb_rawscreen;
262 for (y = 0; y < HAM_SCRH; y++)
263 {
264 for (s_y = 0; s_y < glbScaleFactor; s_y++)
265 {
266 for (x = 0; x < HAM_SCRW; x++)
267 {
268 // Read in a pixel & decode
269 raw = src[x];
270
271 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
272 pixel[0] = (raw & 31) << 3;
273 pixel[1] = ((raw >> 5) & 31) << 3;
274 pixel[2] = ((raw >> 10) & 31) << 3;
275 #else
276 pixel[2] = (raw & 31) << 3;
277 pixel[1] = ((raw >> 5) & 31) << 3;
278 pixel[0] = ((raw >> 10) & 31) << 3;
279 #endif
280
281 // Now write out the needed number of times...
282 for (s_x = 0; s_x < glbScaleFactor; s_x++)
283 {
284 *dst++ = pixel[0];
285 *dst++ = pixel[1];
286 *dst++ = pixel[2];
287 }
288 }
289 // Add remainder of pitch.
290 dst += pitch - 3 * glbScaleFactor * HAM_SCRW;
291 }
292 src += HAM_SCRW;
293 }
294 }
295
296 // Draws the given sprite to the native buffer.
297 void
298 blitSprite(const SPRITEDATA &sprite)
299 {
300 if (sprite.active)
301 {
302 // Finally, blit the sprite.
303 int sx, sy, x, y, tx, ty, srcoffset;
304 u8 src;
305
306 for (sy = 0; sy < TILEHEIGHT*2; sy++)
307 {
308 for (sx = 0; sx < TILEWIDTH*2; sx++)
309 {
310 x = sprite.x + sx;
311 y = sprite.y + sy;
312 // Convert to DS coords from GBA
313 x += TILEWIDTH;
314 y += 2*TILEHEIGHT;
315
316 tx = (sx >= TILEWIDTH) ? 1 : 0;
317 ty = (sy >= TILEHEIGHT) ? 1 : 0;
318 srcoffset = sx + tx * TILEWIDTH * (TILEHEIGHT-1) +
319 ty * 1 * TILEWIDTH * (TILEHEIGHT) + sy * TILEWIDTH;
320 src = sprite.data[srcoffset];
321 if (src &&
322 x >= 0 && x < HAM_SCRW &&
323 y >= 0 && y < HAM_SCRH)
324 {
325 glb_nativescreen[x + y * HAM_SCRW] = src + 256;
326 }
327 }
328 }
329 }
330 }
331
332 void
333 hamfake_rebuildScreen()
334 {
335 int i, layer, x, y, tileidx, tx, ty, px, py;
336 int offset;
337
338 if (!glb_isdirty)
339 return;
340
341 glb_isdirty = false;
342
343 if (glb_videomode == 3)
344 {
345 u8 *dst;
346
347 SDL_LockSurface(glbVideoSurface);
348
349 dst = (u8 *) glbVideoSurface->pixels;
350 if (!dst)
351 {
352 printf("Lock failure: %s\n", SDL_GetError());
353 }
354
355 scaleScreenFrom15bit(dst, glbVideoSurface->pitch);
356
357 SDL_UnlockSurface(glbVideoSurface);
358 // Rebuild from 15bit screen.
359 SDL_UpdateRect(glbVideoSurface, 0, 0, 0, 0);
360 return;
361 }
362
363 // Clear our video surface.
364 memset(glb_nativescreen, 0, HAM_SCRH * HAM_SCRW * sizeof(u16));
365
366 // Blit each layer in turn.
367 // Hard coded layer order:
368 int lay_ord[4] = { 1, 2, 3, 0 };
369 for (i = 0; i < 4; i++)
370 {
371 layer = lay_ord[i];
372
373 ty = ham_bg[layer].scrolly;
374 if (ty < 0)
375 ty = - ((-ty + TILEHEIGHT-1) / TILEHEIGHT);
376 else
377 ty = (ty + TILEHEIGHT-1) / TILEHEIGHT;
378 py = ham_bg[layer].scrolly % TILEHEIGHT;
379 if (py < 0)
380 {
381 ty++;
382 py += TILEHEIGHT;
383 }
384
385 ty %= ham_bg[layer].mi->height;
386 if (ty < 0)
387 ty += ham_bg[layer].mi->height;
388
389 for (y = (py ? 1 : 0); y < HAM_SCRH_T; y++)
390 {
391 tx = ham_bg[layer].scrollx;
392 if (tx < 0)
393 tx = - ((-tx + TILEWIDTH-1) / TILEWIDTH);
394 else
395 tx = (tx + TILEWIDTH-1) / TILEWIDTH;
396 px = ham_bg[layer].scrollx % TILEWIDTH;
397 if (px < 0)
398 {
399 px += TILEWIDTH;
400 tx++;
401 }
402
403 tx %= ham_bg[layer].mi->width;
404 if (tx < 0)
405 tx += ham_bg[layer].mi->width;
406
407 for (x = (px ? 1 : 0); x < HAM_SCRW_T; x++)
408 {
409 u8 *tile;
410
411 tileidx = ham_bg[layer].mi->tiles[ty * ham_bg[layer].mi->width + tx];
412
413 // Out of bound tiles are ignored.
414 if ((tileidx & 1023) < ham_bg[layer].ti->numtiles)
415 {
416 tile = ham_bg[layer].ti->tiles[tileidx & 1023];
417
418 // Compute offset into native screen
419 // This computation is very suspect and has only
420 // been vetted with px == 4, so may not work with
421 // other sub pixel scroll values.
422 offset = x * TILEWIDTH - px + (y * TILEHEIGHT - py) * HAM_SCRW;
423
424 // Figure out any flip flags.
425 if (tileidx & (2048 | 1024))
426 {
427 int sx, sy, fx, fy;
428
429 for (sy = 0; sy < TILEHEIGHT; sy++)
430 {
431 if (tileidx & 2048)
432 fy = TILEHEIGHT-1 - sy;
433 else
434 fy = sy;
435
436 for (sx = 0; sx < TILEWIDTH; sx++)
437 {
438 if (tileidx & 1024)
439 fx = TILEWIDTH-1 - sx;
440 else
441 fx = sx;
442
443 // Write if non-zero
444 if (tile[fx + fy * TILEWIDTH])
445 glb_nativescreen[offset] = tile[fx + fy * TILEWIDTH];
446 offset++;
447 }
448 offset += HAM_SCRW - TILEWIDTH;
449 }
450 }
451 else
452 {
453 int sx, sy;
454
455 // Straight forward write...
456 for (sy = 0; sy < TILEHEIGHT; sy++)
457 {
458 for (sx = 0; sx < TILEWIDTH; sx++)
459 {
460 if (*tile)
461 {
462 glb_nativescreen[offset] = *tile;
463 }
464 tile++;
465 offset++;
466 }
467 offset += HAM_SCRW - TILEWIDTH;
468 }
469 }
470 }
471
472 tx++;
473 if (tx >= ham_bg[layer].mi->width)
474 tx -= ham_bg[layer].mi->width;
475 }
476
477 ty++;
478 if (ty >= ham_bg[layer].mi->height)
479 ty -= ham_bg[layer].mi->height;
480 }
481
482 // Just before the text layer we draw our sprites
483 // This ensures the buttons are under the text.
484 if (i == 2)
485 {
486 int j;
487
488 // Since we composite on top, we want to do in reverse order of
489 // priority. Hence the backwards loop.
490 // The last sprite is the cursor and is done at the very end.
491 for (j = MAX_SPRITES; j --> 1; )
492 {
493 blitSprite(glbSpriteList[j]);
494 }
495 }
496 }
497
498 // Draw the cursor sprite
499 blitSprite(glbSpriteList[0]);
500
501 // Lock and update the final surface using our lookup...
502 {
503 u8 *dst;
504
505 SDL_LockSurface(glbVideoSurface);
506 dst = (u8 *) glbVideoSurface->pixels;
507 if (!dst)
508 {
509 printf("Lock failure: %s\n", SDL_GetError());
510 }
511
512 scaleScreenFromPaletted(dst, glbVideoSurface->pitch);
513
514 SDL_UnlockSurface(glbVideoSurface);
515 // Rebuild from 8bit screen.
516 SDL_UpdateRect(glbVideoSurface, 0, 0, 0, 0);
517 }
518 }
519
520 // Converts a unicode key into an ASCII key.
521 int
522 processSDLKey(int unicode, int sdlkey)
523 {
524 // Check to see if we are intelligble unicode.
525 if (unicode && unicode < 128)
526 return unicode;
527
528 // Process stupid exceptions.
529 switch (sdlkey)
530 {
531 // Insert several curses here... This isn't necessary
532 // anywhere except WIndows XP it seems...
533 case SDLK_KP0:
534 return '0';
535 case SDLK_KP1:
536 return '1';
537 case SDLK_KP2:
538 return '2';
539 case SDLK_KP3:
540 return '3';
541 case SDLK_KP4:
542 return '4';
543 case SDLK_KP5:
544 return '5';
545 case SDLK_KP6:
546 return '6';
547 case SDLK_KP7:
548 return '7';
549 case SDLK_KP8:
550 return '8';
551 case SDLK_KP9:
552 return '9';
553 case SDLK_UP:
554 return GFX_KEYUP;
555 case SDLK_DOWN:
556 return GFX_KEYDOWN;
557 case SDLK_LEFT:
558 return GFX_KEYLEFT;
559 case SDLK_RIGHT:
560 return GFX_KEYRIGHT;
561 case SDLK_PAGEUP:
562 return GFX_KEYPGUP;
563 case SDLK_PAGEDOWN:
564 return GFX_KEYPGDOWN;
565 case SDLK_F1:
566 return GFX_KEYF1;
567 case SDLK_F2:
568 return GFX_KEYF2;
569 case SDLK_F3:
570 return GFX_KEYF3;
571 case SDLK_F4:
572 return GFX_KEYF4;
573 case SDLK_F5:
574 return GFX_KEYF5;
575 case SDLK_F6:
576 return GFX_KEYF6;
577 case SDLK_F7:
578 return GFX_KEYF7;
579 case SDLK_F8:
580 return GFX_KEYF8;
581 case SDLK_F9:
582 return GFX_KEYF9;
583 case SDLK_F10:
584 return GFX_KEYF10;
585 case SDLK_F11:
586 return GFX_KEYF11;
587 case SDLK_F12:
588 return GFX_KEYF12;
589 case SDLK_F13:
590 return GFX_KEYF13;
591 case SDLK_F14:
592 return GFX_KEYF14;
593 case SDLK_F15:
594 return GFX_KEYF15;
595 }
596
597 // Failed to parse key, 0.
598 return 0;
599 }
600
601 // Wait for an event to occur.
602 void
603 hamfake_awaitEvent()
604 {
605 SDL_WaitEvent(0);
606 }
607
608 // Return our internal screen.
609 u16 *
610 hamfake_lockScreen()
611 {
612 // Note we have to offset to the normal GBA start
613 // location to account for our official offset.
614 return glb_rawscreen + TILEWIDTH + 2*TILEHEIGHT * HAM_SCRW;
615 }
616
617 void
618 hamfake_unlockScreen(u16 *)
619 {
620 glb_isdirty = true;
621 }
622
623 // Deal with our SRAM buffer.
624 char *
625 hamfake_writeLockSRAM()
626 {
627 return glb_rawSRAM;
628 }
629
630 void
631 hamfake_writeUnlockSRAM(char *)
632 {
633 }
634
635 char *
636 hamfake_readLockSRAM()
637 {
638 return glb_rawSRAM;
639 }
640
641 void
642 hamfake_readUnlockSRAM(char *)
643 {
644 }
645
646 void
647 hamfake_endWritingSession()
648 {
649 FILE *fp;
650
651 fp = fopen("powder.sav", "wb");
652
653 if (!fp)
654 {
655 cerr << "Failure to open powder.sav for writing!" << endl;
656 return;
657 }
658
659 fwrite(glb_rawSRAM, SRAMSIZE, 1, fp);
660
661 fclose(fp);
662 }
663
664 // Called to run our event poll
665 void
666 hamfake_pollEvents()
667 {
668 SDL_Event event;
669 int cookedkey;
670
671 while (SDL_PollEvent(&event))
672 {
673 switch (event.type)
674 {
675 case SDL_VIDEORESIZE:
676 setResolution(event.resize.w, event.resize.h);
677 break;
678 case SDL_KEYDOWN:
679 if (event.key.keysym.sym < SDLK_LAST)
680 {
681 glb_keystate[event.key.keysym.sym] = 1;
682
683 cookedkey = processSDLKey(event.key.keysym.unicode, event.key.keysym.sym);
684
685 // What sort of drunk would insert the keypress
686 // on a key up?
687 hamfake_insertKeyPress(cookedkey);
688
689 if (cookedkey)
690 {
691 glb_keypushtime = gfx_getframecount() + KEY_REPEAT_INITIAL;
692 glb_keypusher = cookedkey;
693 glb_keypusherraw = event.key.keysym.sym;
694 }
695 }
696 break;
697 case SDL_KEYUP:
698 if (event.key.keysym.sym < SDLK_LAST)
699 {
700 glb_keystate[event.key.keysym.sym] = 0;
701
702 // Sadly, unicode is *not* cooked on a keyup!
703 // Damn SDL!
704 // Thus, we always clear out on a key going high.
705 // cookedkey = processSDLKey(event.key.keysym.unicode);
706 // This will annoy copx, however, so we instead
707 // stop being lazy and store the original raw code.
708
709 // If our currently repeated key goes high,
710 // clear it out.
711 if (event.key.keysym.sym == glb_keypusherraw)
712 {
713 glb_keypusher = 0;
714 }
715 }
716 break;
717
718 case SDL_MOUSEMOTION:
719 {
720 glbStylusX = event.motion.x;
721 glbStylusY = event.motion.y;
722
723 int x, y;
724 hamfake_getstyluspos(x, y);
725 break;
726 }
727
728 case SDL_MOUSEBUTTONDOWN:
729 glbStylusX = event.button.x;
730 glbStylusY = event.button.y;
731 cookedkey = 0;
732 switch (event.button.button)
733 {
734 case SDL_BUTTON_LEFT:
735 cookedkey = GFX_KEYLMB;
736 glbStylusState = true;
737 break;
738 case SDL_BUTTON_MIDDLE:
739 cookedkey = GFX_KEYMMB;
740 break;
741 case SDL_BUTTON_RIGHT:
742 cookedkey = GFX_KEYRMB;
743 break;
744 }
745 if (cookedkey)
746 {
747 hamfake_insertKeyPress(cookedkey);
748 glb_keypushtime = gfx_getframecount() + KEY_REPEAT_INITIAL;
749 glb_keypusher = cookedkey;
750 // Special value for these...
751 glb_keypusherraw = -cookedkey;
752 }
753 break;
754
755 case SDL_MOUSEBUTTONUP:
756 glbStylusX = event.button.x;
757 glbStylusY = event.button.y;
758 cookedkey = 0;
759 switch (event.button.button)
760 {
761 case SDL_BUTTON_LEFT:
762 glbStylusState = false;
763 cookedkey = GFX_KEYLMB;
764 break;
765 case SDL_BUTTON_MIDDLE:
766 cookedkey = GFX_KEYMMB;
767 break;
768 case SDL_BUTTON_RIGHT:
769 cookedkey = GFX_KEYRMB;
770 break;
771 }
772 // Wipe out the pusher if we match.
773 if (-cookedkey == glb_keypusherraw)
774 {
775 glb_keypusher = 0;
776 }
777 break;
778
779 case SDL_QUIT:
780 SDL_Quit();
781 exit(0);
782 break;
783 }
784 }
785 }
786
787 bool
788 hamfake_isPressed(BUTTONS button)
789 {
790 hamfake_pollEvents();
791 hamfake_rebuildScreen();
792
793 // Filter directions for e.g. LEFT with LEFT_UP etc
794 if (button == BUTTON_LEFT &&
795 SDL_JoystickGetButton(joystick, GP2X_BUTTON_LEFT))
796 {
797 return true;
798 }
799
800 if (button == BUTTON_RIGHT &&
801 SDL_JoystickGetButton(joystick, GP2X_BUTTON_RIGHT))
802 {
803 return true;
804 }
805
806 if (button == BUTTON_UP &&
807 SDL_JoystickGetButton(joystick, GP2X_BUTTON_UP))
808 {
809 return true;
810 }
811
812 if (button == BUTTON_DOWN &&
813 SDL_JoystickGetButton(joystick, GP2X_BUTTON_DOWN))
814 {
815 return true;
816 }
817
818 // Straight buttons
819 switch(button)
820 {
821 case BUTTON_LEFT:
822 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_UPLEFT) ||
823 SDL_JoystickGetButton(joystick, GP2X_BUTTON_DOWNLEFT));
824
825 case BUTTON_RIGHT:
826 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_UPRIGHT) ||
827 SDL_JoystickGetButton(joystick, GP2X_BUTTON_DOWNRIGHT));
828
829 case BUTTON_UP:
830 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_UPLEFT) ||
831 SDL_JoystickGetButton(joystick, GP2X_BUTTON_UPRIGHT));
832
833 case BUTTON_DOWN:
834 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_DOWNLEFT) ||
835 SDL_JoystickGetButton(joystick, GP2X_BUTTON_DOWNRIGHT));
836
837 case BUTTON_A:
838 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_A));
839
840 case BUTTON_B:
841 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_B));
842
843 case BUTTON_R:
844 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_R));
845
846 case BUTTON_L:
847 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_L));
848
849 case BUTTON_X:
850 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_X));
851
852 case BUTTON_Y:
853 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_Y));
854
855 case BUTTON_START:
856 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_START));
857
858 case BUTTON_SELECT:
859 return (SDL_JoystickGetButton(joystick, GP2X_BUTTON_SELECT));
860 };
861
862 return false;
863 }
864
865 int
866 hamfake_isAnyPressed()
867 {
868 hamfake_pollEvents();
869 hamfake_rebuildScreen();
870
871 int i;
872 for (i = 0; i < GP2X_NUM_BUTTONS; i++)
873 if (SDL_JoystickGetButton(joystick, i)) return 0x0;
874 return 0x3FF;
875 }
876
877 int
878 hamfake_peekKeyPress()
879 {
880 return 0;
881 }
882
883 int
884 hamfake_getKeyPress(bool onlyascii)
885 {
886 return 0;
887 }
888
889 void
890 hamfake_insertKeyPress(int key)
891 {
892 }
893
894 void
895 hamfake_clearKeyboardBuffer()
896 {
897 }
898
899 void
900 hamfake_setScrollX(int layer, int scroll)
901 {
902 ham_bg[layer].scrollx = scroll;
903 }
904
905 void
906 hamfake_setScrollY(int layer, int scroll)
907 {
908 ham_bg[layer].scrolly = scroll;
909 }
910
911 u8 *
912 convertTo32Bit(const unsigned short *s, int numpixel)
913 {
914 u8 *result, *d;
915 unsigned short p;
916
917 result = (u8 *) malloc(numpixel * 4);
918 d = result;
919
920 while (numpixel--)
921 {
922 p = *s++;
923 *d++ = (p & 31) * 8 + 4;
924 p >>= 5;
925 *d++ = (p & 31) * 8 + 4;
926 p >>= 5;
927 *d++ = (p & 31) * 8 + 4;
928 *d++ = 0xff;
929 }
930 return result;
931 }
932
933 void
934 ham_Init()
935 {
936 int i;
937
938 printf("\nPOWDER initializing...\n");
939
940 /* initialize SDL */
941 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 )
942 {
943 fprintf( stderr, "Video initialization failed: %s\n",
944 SDL_GetError( ) );
945 SDL_Quit( );
946 }
947
948 if (SDL_InitSubSystem (SDL_INIT_JOYSTICK) == -1)
949 {
950 printf("Unable to initialise joystick: %s\n", SDL_GetError());
951 }
952
953 if(SDL_NumJoysticks())
954 {
955 joystick = SDL_JoystickOpen(0);
956 }
957 else
958 {
959 printf("no joystick detected\n");
960 }
961
962 if (joystick)
963 {
964 SDL_JoystickEventState (SDL_ENABLE);
965 }
966
967 u8 *rgbpixel;
968
969 rgbpixel = convertTo32Bit(bmp_icon_sdl, 32 * 32);
970
971 /* SDL interprets each pixel as a 32-bit number, so our masks must depend
972 on the endianness (byte order) of the machine */
973 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
974 const Uint32 rmask = 0xff000000;
975 const Uint32 gmask = 0x00ff0000;
976 const Uint32 bmask = 0x0000ff00;
977 const Uint32 amask = 0x000000ff;
978 #else
979 const Uint32 rmask = 0x000000ff;
980 const Uint32 gmask = 0x0000ff00;
981 const Uint32 bmask = 0x00ff0000;
982 const Uint32 amask = 0xff000000;
983 #endif
984
985 SDL_Surface *surf = SDL_CreateRGBSurfaceFrom((char *)rgbpixel, 32, 32, 32, 32*4,
986 rmask, gmask, bmask, amask);
987
988 SDL_WM_SetIcon(surf, NULL);
989
990 SDL_EnableUNICODE(1);
991
992 glbSpriteList = new SPRITEDATA[MAX_SPRITES];
993 for (i = 0; i < MAX_SPRITES; i++)
994 {
995 glbSpriteList[i].active = false;
996 glbSpriteList[i].data = new u8[TILEWIDTH * TILEHEIGHT * 4];
997 }
998
999 memset(glb_keystate, 0, SDLK_LAST);
1000
1001 glb_rawscreen = new u16[HAM_SCRW*HAM_SCRH];
1002 memset(glb_rawscreen, 0, HAM_SCRW*HAM_SCRH*sizeof(u16));
1003 glb_nativescreen = new u16[HAM_SCRW*HAM_SCRH];
1004
1005 // Load any save games.
1006 FILE *fp;
1007
1008 fp = fopen("powder.sav", "rb");
1009 if (fp)
1010 {
1011 fread(glb_rawSRAM, SRAMSIZE, 1, fp);
1012 fclose(fp);
1013 }
1014
1015 // Load any extra tilesets.
1016 if (bmp_loadExtraTileset())
1017 {
1018 ham_extratileset = true;
1019 printf("External tileset loaded.\n");
1020 }
1021
1022 return;
1023 }
1024
1025 void (*glb_vblcallback)() = 0;
1026
1027 Uint32
1028 glb_VBLTimerCallback(Uint32 interval, void *fp)
1029 {
1030 glb_isnewframe = 1;
1031 (*glb_vblcallback)();
1032
1033 // Push a fake event so we wake up.
1034 SDL_Event event;
1035
1036 event.type = SDL_USEREVENT;
1037 SDL_PushEvent(&event);
1038
1039 return 16;
1040 }
1041
1042 void
1043 ham_StartIntHandler(u8 intno, void (*fp)())
1044 {
1045 assert(intno == INT_TYPE_VBL);
1046
1047 glb_vblcallback = fp;
1048 SDL_AddTimer(16, glb_VBLTimerCallback, 0);
1049 }
1050
1051 void
1052 ham_SetBgMode(u8 mode)
1053 {
1054 if (glb_videomode == -1)
1055 {
1056 setResolution(HAM_SCRW * 2, HAM_SCRH * 2);
1057 rebuildVideoSystemFromGlobals();
1058 }
1059
1060 glb_videomode = mode;
1061 }
1062
1063
1064 void
1065 ham_LoadBGPal(void *vpal, int bytes)
1066 {
1067 // Need to promote our 15bit palette to a 24 bit one.
1068 int entries = bytes / 2, i;
1069 u16 *pal = (u16 *) vpal;
1070
1071 for (i = 0; i < entries; i++)
1072 {
1073 glb_palette[i*4+3] = (pal[i] & 31) << 3;
1074 glb_palette[i*4+2] = ((pal[i] >> 5) & 31) << 3;
1075 glb_palette[i*4+1] = ((pal[i] >> 10) & 31) << 3;
1076 if (i)
1077 glb_palette[i*4] = 255;
1078 else
1079 glb_palette[i*4] = 0;
1080 }
1081 }
1082
1083 map_info_ptr
1084 ham_InitMapEmptySet(u8 size, u8 )
1085 {
1086 map_info_ptr mi;
1087
1088 mi = new map_info;
1089
1090 if (size == 0)
1091 {
1092 mi->width = 32;
1093 mi->height = 32;
1094 }
1095 else
1096 {
1097 mi->width = 64;
1098 mi->height = 64;
1099 }
1100
1101 mi->tiles = new int[mi->width * mi->height];
1102 memset(mi->tiles, 0, sizeof(int) * mi->width * mi->height);
1103
1104 return mi;
1105 }
1106
1107 tile_info_ptr
1108 ham_InitTileEmptySet(int entries, int , int)
1109 {
1110 tile_info_ptr ti;
1111 int i;
1112
1113 ti = new tile_info;
1114 ti->numtiles = entries;
1115 ti->tiles = new u8 *[entries];
1116
1117 for (i = 0; i < entries; i++)
1118 {
1119 ti->tiles[i] = new u8 [TILEWIDTH * TILEHEIGHT];
1120 }
1121 glbTheTileSet = ti;
1122 return ti;
1123 }
1124
1125 void
1126 ham_InitBg(int layer, int foo, int bar, int baz)
1127 {
1128 ham_bg[layer].scrollx = 0;
1129 ham_bg[layer].scrolly = 0;
1130 }
1131
1132 void
1133 ham_CreateWin(int , int, int, int, int, int, int, int)
1134 {
1135 }
1136
1137 void
1138 ham_DeleteWin(int wid)
1139 {
1140 }
1141
1142 void
1143 ham_SetMapTile(int layer, int mx, int my, int tileidx)
1144 {
1145 map_info_ptr mi;
1146
1147 mi = ham_bg[layer].mi;
1148
1149 mx = (mx % mi->width);
1150 if (mx < 0)
1151 mx += mi->width;
1152 my = (my % mi->height);
1153 if (my < 0)
1154 my += mi->height;
1155
1156 mi->tiles[mx + my * mi->width] = tileidx;
1157
1158 glb_isdirty = true;
1159 }
1160
1161 void
1162 hamfake_setTileSize(int tilewidth, int tileheight)
1163 {
1164 int i;
1165
1166 glbTileWidth = tilewidth;
1167 glbTileHeight = tileheight;
1168
1169 delete [] glb_rawscreen;
1170 delete [] glb_nativescreen;
1171 glb_rawscreen = new u16[HAM_SCRW*HAM_SCRH];
1172 memset(glb_rawscreen, 0, HAM_SCRW*HAM_SCRH*sizeof(u16));
1173 glb_nativescreen = new u16[HAM_SCRW*HAM_SCRH];
1174
1175 for (i = 0; glbTheTileSet && (i < glbTheTileSet->numtiles); i++)
1176 {
1177 delete [] glbTheTileSet->tiles[i];
1178 glbTheTileSet->tiles[i] = new u8 [TILEWIDTH * TILEHEIGHT];
1179 }
1180
1181 for (i = 0; i < MAX_SPRITES; i++)
1182 {
1183 delete [] glbSpriteList[i].data;
1184 glbSpriteList[i].data = new u8[TILEWIDTH * TILEHEIGHT * 4];
1185 }
1186 setResolution(glbScreenWidth, glbScreenHeight);
1187 }
1188
1189 void
1190 ham_ReloadTileGfx(tile_info_ptr tiledata, const u16 *data, int destidx,
1191 int numtile)
1192 {
1193 int t;
1194 u8 *raw;
1195 u8 *dst;
1196
1197 raw = (u8*) data;
1198 for (t = destidx; t < destidx + numtile; t++)
1199 {
1200 dst = tiledata->tiles[t];
1201
1202 memcpy(dst, raw, TILEWIDTH*TILEHEIGHT);
1203 raw += TILEWIDTH*TILEHEIGHT;
1204 }
1205 glb_isdirty = true;
1206 }
1207
1208 void
1209 hamfake_ReloadSpriteGfx(const u16 *data, int tileno, int numtile)
1210 {
1211 tileno /= 4;
1212 UT_ASSERT(tileno >= 0 && tileno < MAX_SPRITES);
1213 if (tileno < 0 || tileno >= MAX_SPRITES)
1214 return;
1215
1216 memcpy(glbSpriteList[tileno].data, data, numtile * TILEWIDTH*TILEHEIGHT);
1217 glb_isdirty = true;
1218 }
1219
1220 void
1221 hamfake_LoadSpritePal(void *vpal, int bytes)
1222 {
1223 // Need to promote our 15bit palette to a 24 bit one.
1224 int entries = bytes / 2, i;
1225 u16 *pal = (u16 *) vpal;
1226
1227 for (i = 0; i < entries; i++)
1228 {
1229 glb_palette[1024+i*4+3] = (pal[i] & 31) << 3;
1230 glb_palette[1024+i*4+2] = ((pal[i] >> 5) & 31) << 3;
1231 glb_palette[1024+i*4+1] = ((pal[i] >> 10) & 31) << 3;
1232 if (i)
1233 glb_palette[1024+i*4] = 255;
1234 else
1235 glb_palette[1024+i*4] = 0;
1236 }
1237 glb_isdirty = true;
1238 }
1239
1240 void
1241 hamfake_softReset()
1242 {
1243 SDL_Quit();
1244 exit(0);
1245 }
1246
1247 bool
1248 hamfake_extratileset()
1249 {
1250 return ham_extratileset;
1251 }
1252
1253 void
1254 hamfake_getstyluspos(int &x, int &y)
1255 {
1256 x = glbStylusX;
1257 y = glbStylusY;
1258 // Convert accordint to our scale.
1259 x -= glbScreenFudgeX;
1260 y -= glbScreenFudgeY;
1261 x /= glbScaleFactor;
1262 y /= glbScaleFactor;
1263 // After conversion we can be out of the valid bounds.
1264 if (x < 0)
1265 x = 0;
1266 if (y < 0)
1267 y = 0;
1268 if (x >= HAM_SCRW)
1269 x = HAM_SCRW-1;
1270 if (y >= HAM_SCRH)
1271 y = HAM_SCRH-1;
1272
1273 // Now return to the GBA coordinate system by adjusting for the DS coords.
1274 x -= TILEWIDTH;
1275 y -= 2*TILEHEIGHT;
1276 }
1277
1278 bool
1279 hamfake_getstylusstate()
1280 {
1281 return glbStylusState;
1282 }
1283
1284 void
1285 hamfake_movesprite(int spriteno, int x, int y)
1286 {
1287 UT_ASSERT(spriteno >= 0 && spriteno < MAX_SPRITES);
1288 if (spriteno < 0 || spriteno >= MAX_SPRITES)
1289 return;
1290 glb_isdirty = true;
1291 glbSpriteList[spriteno].x = x;
1292 glbSpriteList[spriteno].y = y;
1293 }
1294
1295 void
1296 hamfake_enablesprite(int spriteno, bool enabled)
1297 {
1298 UT_ASSERT(spriteno >= 0 && spriteno < MAX_SPRITES);
1299 if (spriteno < 0 || spriteno >= MAX_SPRITES)
1300 return;
1301 glb_isdirty = true;
1302 glbSpriteList[spriteno].active = enabled;
1303 }
1304
1305 FILE *
1306 hamfake_fopen(const char *path, const char *mode)
1307 {
1308 // Use the local directory on these platforms.
1309 return fopen(path, mode);
1310 }
1311
1312 bool
1313 hamfake_fatvalid()
1314 {
1315 return true;
1316 }
1317
1318 int
1319 hamfake_getKeyModifiers()
1320 {
1321 return 0;
1322 }