forked from alekmaul/spout
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpiece.c
312 lines (256 loc) · 5.55 KB
/
piece.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include "piece.h"
#include "font.h"
SDL_Surface *video;
unsigned char *vBuffer = NULL;
SDL_Color pltTbl[20] = {
{255,255,255}, // Normal colors
{168,168,168},
{96, 96, 96},
{0, 0, 0},
{28*8,30*8, 9*8}, // Spout colors
{30*8,25*8, 9*8},
{30*8,17*8, 9*8},
{20*8, 0, 0},
{12*8,12*8,12*8}, // Wall color #1
{21*8,21*8,0}, // Wall color #2
{12*8,12*8,0}, // Wall color #3
{255, 0,255}, // Wall color #4
{168, 0,168}, // Wall color #5
{96, 0, 96}, // Wall color #6
{0,255,255}, // Wall color #7
{0,168,168}, // Wall color #8
{0, 96, 96}, // Wall color #9
};
void pceLCDDispStop()
{
}
void pceLCDDispStart()
{
}
void initSDL() {
SDL_PixelFormat *pfrm;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
video = SDL_SetVideoMode(320, 240, 16, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWPALETTE);
if(video == NULL) {
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
SDL_ShowCursor(SDL_DISABLE);
{
SDL_SetColors(video, pltTbl, 0, 20);
}
}
void SDL_ScaleSurface(unsigned int Width, unsigned int Height) {
unsigned short *buffer_scr = video->pixels;
unsigned int W,H,ix,iy,x,y;
x=0;
y=0;
W=320;
H=240;
ix=(128<<16)/W;
iy=(88<<16)/H;
do
{
unsigned char *buffer_mem=vBuffer+((y>>16)*128);
W=320; x=0;
do {
SDL_Color c = pltTbl[buffer_mem[x>>16]];
*buffer_scr++=PIX_TO_RGB(video->format,c.r, c.g, c.b);
x+=ix;
} while (--W);
y+=iy;
} while (--H);
}
void pceLCDTrans() {
if(SDL_MUSTLOCK(video)) SDL_LockSurface(video);
SDL_ScaleSurface(320,240);
if (SDL_MUSTLOCK(video)) SDL_UnlockSurface(video);
SDL_Flip(video);
}
unsigned char *keys;
int pcePadGet() {
static int pad = 0;
int i = 0, op = pad & 0x00ff;
int k[] = {
SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT,
SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT,
#ifdef OPENDINGUX
SDLK_LCTRL, SDLK_LALT, SDLK_LCTRL, SDLK_LALT,
SDLK_ESCAPE, SDLK_RETURN, SDLK_RETURN
#else
SDLK_a, SDLK_b, SDLK_a, SDLK_b,
SDLK_x, SDLK_y, SDLK_y
#endif
// ALEK SDLK_KP8, SDLK_KP2, SDLK_KP4, SDLK_KP6,
// ALEK SDLK_x, SDLK_z, SDLK_SPACE, SDLK_RETURN,
// ALEK SDLK_ESCAPE, SDLK_LSHIFT, SDLK_RSHIFT
};
int p[] = {
PAD_UP, PAD_DN, PAD_LF, PAD_RI,
PAD_UP, PAD_DN, PAD_LF, PAD_RI,
PAD_A, PAD_B, PAD_A, PAD_B,
PAD_C, PAD_D, PAD_D,
-1
};
pad = 0;
do {
if(keys[k[i]] == SDL_PRESSED) {
pad |= p[i];
}
i ++;
} while(p[i] >= 0);
pad |= (pad & (~op)) << 8;
// ALEK
if (keys[SDLK_RETURN] == SDL_PRESSED)
pad |= TRG_ST;
// ALEK
return pad;
}
int interval = 0;
void pceAppSetProcPeriod(int period) {
interval = period;
}
int exec = 1;
void pceAppReqExit(int c) {
exec = 0;
}
unsigned char *pceLCDSetBuffer(unsigned char *pbuff)
{
if(pbuff) {
vBuffer = pbuff;
}
return vBuffer;
}
int font_posX = 0, font_posY = 0, font_width = 4, font_height = 6;
unsigned char font_fgcolor = 3, font_bgcolor = 0, font_bgclear = 0;
const char *font_adr = FONT6;
void pceFontSetType(int type)
{
const int width[] = {5, 8, 4};
const int height[] = {10, 16, 6};
const char* adr[] ={FONT6, FONT16, FONT6};
type &= 3;
font_width = width[type];
font_height = height[type];
font_adr = adr[type];
}
void pceFontSetTxColor(int color)
{
font_fgcolor = (unsigned char)color;
}
void pceFontSetBkColor(int color)
{
if(color >= 0) {
font_bgcolor = (unsigned char)color;
font_bgclear = 0;
} else {
font_bgclear = 1;
}
}
void pceFontSetPos(int x, int y)
{
font_posX = x;
font_posY = y;
}
int pceFontPrintf(const char *fmt, ...)
{
unsigned char *adr = vBuffer + font_posX + font_posY * 128;
unsigned char *pC;
char c[1024];
va_list argp;
va_start(argp, fmt);
vsprintf(c, fmt, argp);
va_end(argp);
pC = c;
while(*pC) {
int i, x, y;
const unsigned char *sAdr;
if(*pC >= 0x20 && *pC < 0x80) {
i = *pC - 0x20;
} else {
i = 0;
}
sAdr = font_adr + (i & 15) + (i >> 4) * 16 * 16;
for(y = 0; y < font_height; y ++) {
unsigned char c = *sAdr;
for(x = 0; x < font_width; x ++) {
if(c & 0x80) {
*adr = font_fgcolor;
} else if(font_bgclear == 0) {
*adr = font_bgcolor;
}
adr ++;
c <<= 1;
}
adr += 128 - font_width;
sAdr += 16;
}
adr -= 128 * font_height - font_width;
pC ++;
}
}
#ifndef O_BINARY
#define O_BINARY 0
#endif
int pceFileOpen(FILEACC *pfa, const char *fname, int mode)
{
if(mode == FOMD_RD) {
*pfa = open(fname, O_RDONLY | O_BINARY);
} else if(mode == FOMD_WR) {
*pfa = open(fname, O_CREAT | O_RDWR | O_BINARY | O_TRUNC, S_IREAD | S_IWRITE);
}
if(*pfa >= 0) {
return 0;
} else {
return 1;
}
}
int pceFileReadSct(FILEACC *pfa, void *ptr, int sct, int len)
{
return read(*pfa, ptr, len);
}
int pceFileWriteSct(FILEACC *pfa, const void *ptr, int sct, int len)
{
return write(*pfa, ptr, len);
}
int pceFileClose(FILEACC *pfa)
{
close(*pfa);
return 0;
}
int main(int argc, char *argv[])
{
SDL_Event event;
long nextTick, wait;
int cnt = 0;
initSDL();
pceAppInit();
SDL_WM_SetCaption("spout", NULL);
nextTick = SDL_GetTicks() + interval;
while(exec) {
SDL_PollEvent(&event);
keys = SDL_GetKeyState(NULL);
wait = nextTick - SDL_GetTicks();
if(wait > 0) {
SDL_Delay(wait);
}
pceAppProc(cnt);
nextTick += interval;
cnt ++;
/* if((keys[SDLK_ESCAPE] == SDL_PRESSED && (keys[SDLK_RETURN] == SDL_PRESSED )) || event.type == SDL_QUIT) {*/
/* exec = 0;*/
/* }*/
}
pceAppExit();
exit(0);
}