File: piece.c

package info (click to toggle)
spout 1.3-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 512 kB
  • ctags: 116
  • sloc: sh: 3,162; ansic: 1,587; makefile: 66
file content (362 lines) | stat: -rw-r--r-- 5,905 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>

#include "SDL.h"
#include "piece.h"
#include "font.h"

SDL_Surface *video, *layer;
SDL_Rect layerRect;

unsigned char *vBuffer = NULL;

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);

  if (fullscreen == 1)
    {
      video =
	SDL_SetVideoMode (SDL_WIDTH, SDL_HEIGHT, 8,
			  SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWPALETTE |
			  SDL_FULLSCREEN);
      SDL_ShowCursor (0);
    }
  else
    video =
      SDL_SetVideoMode (SDL_WIDTH, SDL_HEIGHT, 8,
			SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWPALETTE);

  if (video == NULL)
    {
      fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ());
      exit (1);
    }

  pfrm = video->format;
  layer =
    SDL_CreateRGBSurface (SDL_SWSURFACE, SDL_WIDTH, SDL_HEIGHT, 8,
			  pfrm->Rmask, pfrm->Gmask, pfrm->Bmask, pfrm->Amask);
  if (layer == NULL)
    {
      fprintf (stderr, "Couldn't create surface: %s\n", SDL_GetError ());
      exit (1);
    }

  layerRect.x = 0;
  layerRect.y = 0;
  layerRect.w = SDL_WIDTH;
  layerRect.h = SDL_HEIGHT;

  {
    static SDL_Color pltTbl[4] = {
      {255, 255, 255},
      {170, 170, 170},
      {85, 85, 85},
      {0, 0, 0}
    };
    SDL_SetColors (video, pltTbl, 0, 4);
    SDL_SetColors (layer, pltTbl, 0, 4);
  }
}

void
pceLCDTrans ()
{
  int x, y;
  unsigned char *vbi, *bi;

  bi = layer->pixels;
  for (y = 0; y < SDL_HEIGHT; y++)
    {
      vbi = vBuffer + (y / zoom) * 128;
      for (x = 0; x < SDL_WIDTH; x++)
	{
	  *bi++ = *(vbi + x / zoom);
	}
      bi += layer->pitch - SDL_WIDTH;
    }

  SDL_BlitSurface (layer, NULL, video, &layerRect);
  SDL_Flip (video);
}

unsigned char *keys;

int
pcePadGet ()
{
  static int pad = 0;
  int i = 0, op = pad & 0x00ff;

  int k[] = {
    SDLK_PAGEUP, SDLK_PAGEDOWN, SDLK_LEFT, SDLK_RIGHT,
    SDLK_KP4, SDLK_KP6,
    SDLK_x, SDLK_z, SDLK_SPACE, SDLK_RETURN,
    SDLK_ESCAPE, SDLK_LSHIFT, SDLK_RSHIFT,
    SDLK_PLUS, SDLK_MINUS
  };

  int p[] = {
    PAD_UP, PAD_DN, PAD_LF, PAD_RI,
    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;

  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++;
    }
}

int
pceFileOpen (int * pfa, const char *fname, int mode)
{
  if (mode == FOMD_RD)
    {
      *pfa = open (fname, O_RDONLY);
    }
  else if (mode == FOMD_WR)
    {
      *pfa = open (fname, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
    }

  if (*pfa >= 0)
    {
      return 0;
    }
  else
    {
      return 1;
    }
}

int
pceFileReadSct (int * pfa, void *ptr, int sct, int len)
{
  return read (*pfa, ptr, len);
}

int
pceFileWriteSct (int * pfa, const void *ptr, int sct, int len)
{
  return write (*pfa, ptr, len);
}

int
pceFileClose (int * pfa)
{
  close (*pfa);
  return 0;
}

int
main (int argc, char *argv[])
{
  SDL_Event event;
  long nextTick, wait;
  int cnt = 0;
  int pzoom;
  char *tail;
  zoom = 3;
  fullscreen = 0;
  if (argc > 1)
    {
      if (strcmp (argv[1], "f") == 0)
	fullscreen = 1;
      else
	{
	  pzoom = strtol (argv[1], &tail, 0);
	  if (pzoom >= 1)
	    zoom = pzoom;
	}
    }

  initSDL ();
  pceAppInit ();

  SDL_WM_SetCaption (PACKAGE_STRING, 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);
      //      SDL_Flip(video);

      nextTick += interval;
      cnt++;

      if ((keys[SDLK_ESCAPE] == SDL_PRESSED
	   && (keys[SDLK_LSHIFT] == SDL_PRESSED
	       || keys[SDLK_RSHIFT] == SDL_PRESSED))
	  || event.type == SDL_QUIT)
	{
	  exec = 0;
	}
    }

  pceAppExit ();
}