-
Notifications
You must be signed in to change notification settings - Fork 0
/
psp_sdl.c
executable file
·688 lines (588 loc) · 15.9 KB
/
psp_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_image.h>
#include <png.h>
#include "global.h"
#include "psp_sdl.h"
#include "psp_sound.h"
#include "psp_danzeff.h"
extern unsigned char psp_font_8x8[];
extern unsigned char psp_font_6x10[];
unsigned char *psp_font;
int psp_font_width = 8;
int psp_font_height = 8;
SDL_Surface *back_surface;
SDL_Surface *back2_surface;
SDL_Surface *blit_surface;
SDL_Surface *splash_surface;
SDL_Surface *thumb_surface;
uint
psp_sdl_rgb(uchar R, uchar G, uchar B)
{
return SDL_MapRGB(back_surface->format, R,G,B);
}
ushort *
psp_sdl_get_vram_addr(uint x, uint y)
{
ushort *vram = (ushort *)back_surface->pixels;
return vram + x + (y*PSP_LINE_SIZE);
}
void
loc_psp_debug(char *file, int line, char *message)
{
static int current_line = 0;
static int current_col = 10;
char buffer[128];
current_line += 10;
if (current_line > 220)
{
if (current_col == 200) {
psp_sdl_clear_screen(psp_sdl_rgb(0, 0, 0xff));
current_col = 10;
} else {
current_col = 200;
}
current_line = 10;
}
sprintf(buffer,"%s:%d %s", file, line, message);
psp_sdl_print(current_col, current_line, buffer, psp_sdl_rgb(0xff,0xff,0xff) );
}
void
psp_sdl_print(int x,int y, char *str, int color)
{
int index;
int x0 = x;
for (index = 0; str[index] != '\0'; index++) {
psp_sdl_put_char(x, y, color, 0, str[index], 1, 0);
x += psp_font_width;
if (x >= (PSP_SDL_SCREEN_WIDTH - psp_font_width)) {
x = x0; y+= psp_font_height;
}
if (y >= (PSP_SDL_SCREEN_HEIGHT - psp_font_width)) break;
}
}
void
psp_sdl_clear_screen(int color)
{
int x; int y;
ushort *vram = psp_sdl_get_vram_addr(0,0);
for (y = 0; y < PSP_SDL_SCREEN_HEIGHT; y++) {
for (x = 0; x < PSP_SDL_SCREEN_WIDTH; x++) {
vram[x + (y*PSP_LINE_SIZE)] = color;
}
}
}
void
psp_sdl_black_screen()
{
SDL_FillRect(back_surface,NULL,SDL_MapRGB(back_surface->format,0x0,0x0,0x0));
SDL_Flip(back_surface);
SDL_FillRect(back_surface,NULL,SDL_MapRGB(back_surface->format,0x0,0x0,0x0));
SDL_Flip(back_surface);
}
void
psp_sdl_draw_rectangle(int x, int y, int w, int h, int border, int mode)
{
ushort *vram = (ushort *)psp_sdl_get_vram_addr(x, y);
int xo, yo;
if (mode == PSP_SDL_XOR) {
for (xo = 0; xo < w; xo++) {
vram[xo] ^= border;
vram[xo + h * PSP_LINE_SIZE] ^= border;
}
for (yo = 0; yo < h; yo++) {
vram[yo * PSP_LINE_SIZE] ^= border;
vram[w + yo * PSP_LINE_SIZE] ^= border;
}
vram[w + h * PSP_LINE_SIZE] ^= border;
} else {
for (xo = 0; xo < w; xo++) {
vram[xo] = border;
vram[xo + h * PSP_LINE_SIZE] = border;
}
for (yo = 0; yo < h; yo++) {
vram[yo * PSP_LINE_SIZE] = border;
vram[w + yo * PSP_LINE_SIZE] = border;
}
vram[w + h * PSP_LINE_SIZE] = border;
}
}
void
psp_sdl_fill_rectangle(int x, int y, int w, int h, int color, int mode)
{
ushort *vram = (ushort *)psp_sdl_get_vram_addr(x, y);
int xo, yo;
if (mode == PSP_SDL_XOR) {
for (xo = 0; xo <= w; xo++) {
for (yo = 0; yo <= h; yo++) {
if ( ((xo == 0) && ((yo == 0) || (yo == h))) ||
((xo == w) && ((yo == 0) || (yo == h))) ) {
/* Skip corner */
} else {
vram[xo + yo * PSP_LINE_SIZE] ^= color;
}
}
}
} else {
for (xo = 0; xo <= w; xo++) {
for (yo = 0; yo <= h; yo++) {
vram[xo + yo * PSP_LINE_SIZE] = color;
}
}
}
}
static int
psp_sdl_get_back2_color(int x, int y)
{
uchar *back2 = (uchar *)back2_surface->pixels;
int bytes_per_pixels = back2_surface->format->BytesPerPixel;
int pitch = back2_surface->pitch;
Uint8 r = back2[0 + (y * pitch) + (x * bytes_per_pixels)];
Uint8 g = back2[1 + (y * pitch) + (x * bytes_per_pixels)];
Uint8 b = back2[2 + (y * pitch) + (x * bytes_per_pixels)];
int color = psp_sdl_rgb(r, g, b);
return color;
}
void
psp_sdl_back2_rectangle(int x, int y, int w, int h)
{
if (! back2_surface) {
psp_sdl_fill_rectangle(x, y, w, h, 0x0, 0);
return;
}
ushort *vram = (ushort *)psp_sdl_get_vram_addr(x, y);
int xo, yo;
for (xo = 0; xo <= w; xo++) {
for (yo = 0; yo <= h; yo++) {
vram[xo + yo * PSP_LINE_SIZE] = psp_sdl_get_back2_color(x + xo, y + yo);
}
}
}
void
psp_sdl_put_char(int x, int y, int color, int bgcolor, uchar c, int drawfg, int drawbg)
{
int cx;
int cy;
int b;
int index;
ushort *vram = (ushort *)psp_sdl_get_vram_addr(x, y);
index = ((ushort)c) * psp_font_height;
for (cy=0; cy< psp_font_height; cy++) {
b = 1 << (psp_font_width - 1);
for (cx=0; cx< psp_font_width; cx++) {
if (psp_font[index] & b) {
if (drawfg) vram[cx + cy * PSP_LINE_SIZE] = color;
} else {
if (drawbg) vram[cx + cy * PSP_LINE_SIZE] = bgcolor;
}
b = b >> 1;
}
index++;
}
}
void
psp_sdl_back2_put_char(int x, int y, int color, uchar c)
{
int cx;
int cy;
int bmask;
int index;
if (! back2_surface) {
psp_sdl_put_char(x, y, color, 0x0, c, 1, 1);
return;
}
ushort *vram = (ushort *)psp_sdl_get_vram_addr(x, y);
index = ((ushort)c) * psp_font_height;
for (cy=0; cy< psp_font_height; cy++) {
bmask = 1 << (psp_font_width - 1);
for (cx=0; cx< psp_font_width; cx++) {
if (psp_font[index] & bmask) {
vram[cx + cy * PSP_LINE_SIZE] = color;
} else {
vram[cx + cy * PSP_LINE_SIZE] = psp_sdl_get_back2_color(x + cx, y + cy);
}
bmask = bmask >> 1;
}
index++;
}
}
unsigned char
psp_convert_utf8_to_iso_8859_1(unsigned char c1, unsigned char c2)
{
unsigned char res = 0;
if (c1 == 0xc2) res = c2;
else
if (c1 == 0xc3) res = c2 | 0x40;
return res;
}
void
psp_sdl_fill_print(int x,int y,const char *str, int color, int bgcolor)
{
int index;
int x0 = x;
for (index = 0; str[index] != '\0'; index++) {
uchar c = str[index];
if ((c == 0xc2) || (c == 0xc3)) {
uchar new_c = psp_convert_utf8_to_iso_8859_1(c, str[index+1]);
if (new_c) { c = new_c; index++; }
}
psp_sdl_put_char(x, y, color, bgcolor, c, 1, 1);
x += psp_font_width;
if (x >= (PSP_SDL_SCREEN_WIDTH - psp_font_width)) {
x = x0; y++;
}
if (y >= (PSP_SDL_SCREEN_HEIGHT - psp_font_width)) break;
}
}
void
psp_sdl_back2_print(int x,int y,const char *str, int color)
{
int index;
int x0 = x;
for (index = 0; str[index] != '\0'; index++) {
uchar c = str[index];
if ((c == 0xc2) || (c == 0xc3)) {
uchar new_c = psp_convert_utf8_to_iso_8859_1(c, str[index+1]);
if (new_c) { c = new_c; index++; }
}
psp_sdl_back2_put_char(x, y, color, c);
x += psp_font_width;
if (x >= (PSP_SDL_SCREEN_WIDTH - psp_font_width)) {
x = x0; y++;
}
if (y >= (PSP_SDL_SCREEN_HEIGHT - psp_font_width)) break;
}
}
void
psp_sdl_lock(void)
{
SDL_LockSurface(back_surface);
}
void
psp_sdl_load_background()
{
back2_surface = IMG_Load("./background.png");
thumb_surface = IMG_Load("./thumb.png");
}
void
psp_sdl_blit_background()
{
static int first = 1;
if (first && (back2_surface == NULL)) {
psp_sdl_load_background();
first = 0;
}
if (back2_surface != NULL) {
SDL_BlitSurface(back2_surface, NULL, back_surface, NULL);
} else {
psp_sdl_clear_screen(psp_sdl_rgb(0x00, 0x00, 0x00));
}
}
void
psp_sdl_blit_thumb(int dst_x, int dst_y, SDL_Surface* thumb_surface)
{
SDL_Rect dstRect;
dstRect.x = dst_x;
dstRect.y = dst_y;
dstRect.w = thumb_surface->w;
dstRect.h = thumb_surface->h;
SDL_BlitSurface(thumb_surface, NULL, back_surface, &dstRect);
}
void
psp_sdl_blit_splash()
{
if (! splash_surface) {
splash_surface = IMG_Load("./splash.png");
}
SDL_BlitSurface(splash_surface, NULL, back_surface, NULL);
}
void
psp_sdl_display_splash()
{
psp_sdl_blit_splash();
psp_sdl_flip();
psp_sdl_blit_splash();
psp_sdl_flip();
sleep(2);
}
void
psp_sdl_unlock(void)
{
SDL_UnlockSurface(back_surface);
}
void
psp_sdl_flip(void)
{
SDL_Flip(back_surface);
}
#define systemRedShift (back_surface->format->Rshift)
#define systemGreenShift (back_surface->format->Gshift)
#define systemBlueShift (back_surface->format->Bshift)
#define systemRedMask (back_surface->format->Rmask)
#define systemGreenMask (back_surface->format->Gmask)
#define systemBlueMask (back_surface->format->Bmask)
int
psp_sdl_save_png(SDL_Surface* my_surface, char* filename)
{
int w = my_surface->w;
int h = my_surface->h;
u8* pix = (u8*)my_surface->pixels;
u8 writeBuffer[512 * 3];
FILE *fp = fopen(filename,"wb");
if(!fp) return 0;
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if(!png_ptr) {
fclose(fp);
return 0;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
png_destroy_write_struct(&png_ptr,NULL);
fclose(fp);
return 0;
}
png_init_io(png_ptr,fp);
png_set_IHDR(png_ptr,
info_ptr,
w,
h,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr,info_ptr);
u8 *b = writeBuffer;
int sizeX = w;
int sizeY = h;
int y;
int x;
u16 *p = (u16 *)pix;
for(y = 0; y < sizeY; y++) {
for(x = 0; x < sizeX; x++) {
u16 v = p[x];
*b++ = ((v & systemRedMask ) >> systemRedShift ) << 3; // R
*b++ = ((v & systemGreenMask) >> systemGreenShift) << 2; // G
*b++ = ((v & systemBlueMask ) >> systemBlueShift ) << 3; // B
}
p += my_surface->pitch / 2;
png_write_row(png_ptr,writeBuffer);
b = writeBuffer;
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 1;
}
int
psp_sdl_load_png(SDL_Surface* my_surface, char* filename)
{
int w = my_surface->w;
int h = my_surface->h;
int pitch = my_surface->pitch / 2;
u16* pix = (u16*)my_surface->pixels;
FILE *fp = fopen(filename,"rb");
if (!fp) return 0;
const size_t nSigSize = 8;
u8 signature[nSigSize];
if (fread(signature, sizeof(u8), nSigSize, fp) != nSigSize) {
fclose(fp);
return 0;
}
if (!png_check_sig(signature, nSigSize)) {
fclose(fp);
return 0;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if(!png_ptr) {
fclose(fp);
return 0;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(fp);
return 0;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, nSigSize);
png_read_png(png_ptr, info_ptr,
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING |
PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_BGR , NULL);
png_uint_32 width = info_ptr->width;
png_uint_32 height = info_ptr->height;
int color_type = info_ptr->color_type;
if ((width > w) ||
(height > h)) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return 0;
}
png_byte **pRowTable = info_ptr->row_pointers;
unsigned int x, y;
u8 r, g, b;
for (y=0; y<height; y++) {
png_byte *pRow = pRowTable[y];
for (x=0; x<width; x++) {
switch(color_type) {
case PNG_COLOR_TYPE_GRAY:
r = g = b = *pRow++;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
r = g = b = pRow[0];
pRow += 2;
break;
case PNG_COLOR_TYPE_RGB:
b = pRow[0];
g = pRow[1];
r = pRow[2];
pRow += 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
b = pRow[0];
g = pRow[1];
r = pRow[2];
pRow += 4;
break;
default:
r = g = b = 0;
break;
}
u16 v = (((r >> 3) << systemRedShift ) & systemRedMask) |
(((g >> 2) << systemGreenShift) & systemGreenMask) |
(((b >> 3) << systemBlueShift ) & systemBlueMask);
*pix++= v;
}
pix += pitch - width;
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return 1;
}
int
psp_sdl_save_screenshot_png(char *filename)
{
return psp_sdl_save_png(back_surface, filename);
}
int
psp_sdl_save_thumb_png(SDL_Surface* my_surface, char* filename)
{
psp_atari_blit_image();
/* First dump blit_surface to my_surface */
int x;
int y;
u16* src_pixel = (u16*)blit_surface->pixels;
u16* dst_pixel = (u16*)my_surface->pixels;
for (y = 0; y < SNAP_HEIGHT; y++) {
u16* scan_pixel = src_pixel + (y*ATARI_HEIGHT/SNAP_HEIGHT)*ATARI_WIDTH;
for (x = 0; x < SNAP_WIDTH; x++) {
*dst_pixel++ = scan_pixel[x*ATARI_WIDTH/SNAP_WIDTH];
}
}
/* Then save thumb in file */
return psp_sdl_save_png(my_surface, filename);
}
int
psp_sdl_load_thumb_png(SDL_Surface* my_surface, char* filename)
{
return psp_sdl_load_png( my_surface, filename);
}
void
psp_sdl_save_screenshot(void)
{
char TmpFileName[MAX_PATH];
sprintf(TmpFileName,"%s/scr/screenshot_%d.png", ATARI.atari_home_dir, ATARI.psp_screenshot_id++);
if (ATARI.psp_screenshot_id >= 10) ATARI.psp_screenshot_id = 0;
psp_sdl_save_screenshot_png(TmpFileName);
}
int
psp_sdl_init(void)
{
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_AUDIO) < 0) {
return 0;
}
SDL_JoystickEventState(SDL_ENABLE);
SDL_JoystickOpen(0);
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
return 0;
}
psp_sdl_select_font_6x10();
back_surface=SDL_SetVideoMode(PSP_SDL_SCREEN_WIDTH,PSP_SDL_SCREEN_HEIGHT, 16 ,
SDL_DOUBLEBUF|SDL_HWSURFACE);
if ( !back_surface) {
return 0;
}
SDL_FillRect(back_surface,NULL,SDL_MapRGB(back_surface->format,0x0,0x0,0x0));
SDL_Flip(back_surface);
SDL_FillRect(back_surface,NULL,SDL_MapRGB(back_surface->format,0x0,0x0,0x0));
SDL_Flip(back_surface);
blit_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
ATARI_WIDTH, ATARI_HEIGHT,
back_surface->format->BitsPerPixel,
back_surface->format->Rmask,
back_surface->format->Gmask,
back_surface->format->Bmask, 0);
SDL_ShowCursor(SDL_DISABLE);
psp_sdl_display_splash();
/* Danzeff Keyboard */
danzeff_load();
danzeff_set_screen(back_surface);
/* Create surface for save state */
int Index = 0;
for (Index = 0; Index < ATARI_MAX_SAVE_STATE; Index++) {
ATARI.atari_save_state[Index].surface =
SDL_CreateRGBSurface(SDL_SWSURFACE,
SNAP_WIDTH, SNAP_HEIGHT,
back_surface->format->BitsPerPixel,
back_surface->format->Rmask,
back_surface->format->Gmask,
back_surface->format->Bmask, 0);
}
psp_sound_init();
return 1;
}
void
psp_sdl_exit(int status)
{
SDL_CloseAudio();
SDL_Quit();
exit(status);
}
void
psp_sdl_select_font_8x8()
{
psp_font = psp_font_8x8;
psp_font_height = 8;
psp_font_width = 8;
}
void
psp_sdl_select_font_6x10()
{
psp_font = psp_font_6x10;
psp_font_height = 10;
psp_font_width = 6;
}