home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / spout11.zip / SDL / piece.c < prev    next >
C/C++ Source or Header  |  2002-10-12  |  5KB  |  279 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <sys\stat.h>
  5. #include <fcntl.h>
  6. #include <math.h>
  7.  
  8. #include "SDL.h"
  9. #include "piece.h"
  10. #include "font.h"
  11.  
  12. SDL_Surface *video, *layer;
  13. SDL_Rect layerRect;
  14.  
  15. unsigned char *vBuffer = NULL;
  16.  
  17. void pceLCDDispStop()
  18. {
  19. }
  20.  
  21. void pceLCDDispStart()
  22. {
  23. }
  24.  
  25. void initSDL() {
  26.     SDL_PixelFormat *pfrm;
  27.  
  28.     if(SDL_Init(SDL_INIT_VIDEO) < 0) {
  29.         fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  30.         exit(1);
  31.     }
  32.     atexit(SDL_Quit);
  33.  
  34.     video = SDL_SetVideoMode(SDL_WIDTH, SDL_HEIGHT, 8, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWPALETTE);
  35.     if(video == NULL) {
  36.         fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  37.         exit(1);
  38.     }
  39.  
  40.     pfrm = video->format;
  41.     layer = SDL_CreateRGBSurface(SDL_SWSURFACE, SDL_WIDTH, SDL_HEIGHT, 8, pfrm->Rmask, pfrm->Gmask, pfrm->Bmask, pfrm->Amask);
  42.     if(layer == NULL) {
  43.         fprintf(stderr, "Couldn't create surface: %s\n", SDL_GetError());
  44.         exit(1);
  45.     }
  46.  
  47.     layerRect.x = 0;
  48.     layerRect.y = 0;
  49.     layerRect.w = SDL_WIDTH;
  50.     layerRect.h = SDL_HEIGHT;
  51.  
  52.     {
  53.         static SDL_Color pltTbl[4] = {
  54.             {255, 255, 255},
  55.             {170, 170, 170},
  56.             {85, 85, 85},
  57.             {0, 0, 0}
  58.         };
  59.         SDL_SetColors(video, pltTbl, 0, 4);
  60.         SDL_SetColors(layer, pltTbl, 0, 4);
  61.     }
  62. }
  63.  
  64. void pceLCDTrans() {
  65.     int x, y;
  66.     unsigned char *vbi, *bi;
  67.  
  68.     bi = layer->pixels;
  69.     for(y = 0; y < SDL_HEIGHT; y ++) {
  70.         vbi = vBuffer + (y / ZOOM) * 128;
  71.         for(x = 0; x < SDL_WIDTH; x ++) {
  72.             *bi ++ = *(vbi + x / ZOOM);
  73.         }
  74.         bi += layer->pitch - SDL_WIDTH;
  75.     }
  76.  
  77.     SDL_BlitSurface(layer, NULL, video, &layerRect);
  78.     SDL_Flip(video);
  79. }
  80.  
  81. unsigned char *keys;
  82.  
  83. int pcePadGet() {
  84.     static int pad = 0;
  85.     int i = 0, op = pad & 0x00ff;
  86.  
  87.     int k[] = {
  88.         SDLK_UP,        SDLK_DOWN,        SDLK_LEFT,        SDLK_RIGHT,
  89.         SDLK_KP8,        SDLK_KP2,        SDLK_KP4,        SDLK_KP6,
  90.         SDLK_x,            SDLK_z,            SDLK_SPACE,        SDLK_RETURN,
  91.         SDLK_ESCAPE,    SDLK_LSHIFT,    SDLK_RSHIFT
  92.     };
  93.  
  94.     int p[] = {
  95.         PAD_UP,            PAD_DN,            PAD_LF,            PAD_RI,
  96.         PAD_UP,            PAD_DN,            PAD_LF,            PAD_RI,
  97.         PAD_A,            PAD_B,            PAD_A,            PAD_B,
  98.         PAD_C,            PAD_D,            PAD_D,
  99.         -1
  100.     };
  101.  
  102.     pad = 0;
  103.  
  104.     do {
  105.         if(keys[k[i]] == SDL_PRESSED) {
  106.             pad |= p[i];
  107.         }
  108.         i ++;
  109.     } while(p[i] >= 0);
  110.  
  111.     pad |= (pad & (~op)) << 8;
  112.  
  113.     return pad;
  114. }
  115.  
  116. int interval = 0;
  117.  
  118. void pceAppSetProcPeriod(int period) {
  119.     interval = period;
  120. }
  121.  
  122. int exec = 1;
  123.  
  124. void pceAppReqExit(int c) {
  125.     exec = 0;
  126. }
  127.  
  128. unsigned char *pceLCDSetBuffer(unsigned char *pbuff)
  129. {
  130.     if(pbuff) {
  131.         vBuffer = pbuff;
  132.     }
  133.     return vBuffer;
  134. }
  135.  
  136. int font_posX = 0, font_posY = 0, font_width = 4, font_height = 6;
  137. unsigned char font_fgcolor = 3, font_bgcolor = 0, font_bgclear = 0;
  138. const char *font_adr = FONT6;
  139.  
  140. void pceFontSetType(int type)
  141. {
  142.     const int width[] = {5, 8, 4};
  143.     const int height[] = {10, 16, 6};
  144.     const char* adr[] ={FONT6, FONT16, FONT6};
  145.  
  146.     type &= 3;
  147.     font_width = width[type];
  148.     font_height = height[type];
  149.     font_adr = adr[type];
  150. }
  151.  
  152. void pceFontSetTxColor(int color)
  153. {
  154.     font_fgcolor = (unsigned char)color;
  155. }
  156.  
  157. void pceFontSetBkColor(int color)
  158. {
  159.     if(color >= 0) {
  160.         font_bgcolor = (unsigned char)color;
  161.         font_bgclear = 0;
  162.     } else {
  163.         font_bgclear = 1;
  164.     }
  165. }
  166.  
  167. void pceFontSetPos(int x, int y)
  168. {
  169.     font_posX = x;
  170.     font_posY = y;
  171. }
  172.  
  173. int pceFontPrintf(const char *fmt, ...)
  174. {
  175.     unsigned char *adr = vBuffer + font_posX + font_posY * 128;
  176.     unsigned char *pC;
  177.     char c[1024];
  178.     va_list argp;
  179.  
  180.     va_start(argp, fmt);
  181.     vsprintf(c, fmt, argp);
  182.     va_end(argp);
  183.  
  184.     pC = c;
  185.     while(*pC) {
  186.         int i, x, y;
  187.         const unsigned char *sAdr;
  188.         if(*pC >= 0x20 && *pC < 0x80) {
  189.             i = *pC - 0x20;
  190.         } else {
  191.             i = 0;
  192.         }
  193.         sAdr = font_adr + (i & 15) + (i >> 4) * 16 * 16;
  194.         for(y = 0; y < font_height; y ++) {
  195.             unsigned char c = *sAdr;
  196.             for(x = 0; x < font_width; x ++) {
  197.                 if(c & 0x80) {
  198.                     *adr = font_fgcolor;
  199.                 } else if(font_bgclear == 0) {
  200.                     *adr = font_bgcolor;
  201.                 }
  202.                 adr ++;
  203.                 c <<= 1;
  204.             }
  205.             adr += 128 - font_width;
  206.             sAdr += 16;
  207.         }
  208.         adr -= 128 * font_height - font_width;
  209.         pC ++;
  210.     }
  211. }
  212.  
  213. int pceFileOpen(FILEACC *pfa, const char *fname, int mode)
  214. {
  215.     if(mode == FOMD_RD) {
  216.         *pfa = open(fname, O_RDONLY | O_BINARY);
  217.     } else if(mode == FOMD_WR) {
  218.         *pfa = open(fname, O_CREAT | O_RDWR | O_BINARY | O_TRUNC, S_IREAD | S_IWRITE);
  219.     }
  220.  
  221.     if(*pfa >= 0) {
  222.         return 0;
  223.     } else {
  224.         return 1;
  225.     }
  226. }
  227.  
  228. int pceFileReadSct(FILEACC *pfa, void *ptr, int sct, int len)
  229. {
  230.     return read(*pfa, ptr, len);
  231. }
  232.  
  233. int pceFileWriteSct(FILEACC *pfa, const void *ptr, int sct, int len)
  234. {
  235.     return write(*pfa, ptr, len);
  236. }
  237.  
  238. int pceFileClose(FILEACC *pfa)
  239. {
  240.     close(*pfa);
  241.     return 0;
  242. }
  243.  
  244. int main(int argc, char *argv[])
  245. {
  246.     SDL_Event event;
  247.     long nextTick, wait;
  248.     int cnt = 0;
  249.  
  250.     initSDL();
  251.     pceAppInit();
  252.  
  253.     SDL_WM_SetCaption("spout", NULL);
  254.  
  255.     nextTick = SDL_GetTicks() + interval;
  256.     while(exec) {
  257.         SDL_PollEvent(&event);
  258.         keys = SDL_GetKeyState(NULL);
  259.  
  260.         wait = nextTick - SDL_GetTicks();
  261.         if(wait > 0) {
  262.             SDL_Delay(wait);
  263.         }
  264.  
  265.         pceAppProc(cnt);
  266.     //    SDL_Flip(video);
  267.  
  268.         nextTick += interval;
  269.         cnt ++;
  270.  
  271.         if((keys[SDLK_ESCAPE] == SDL_PRESSED && (keys[SDLK_LSHIFT] == SDL_PRESSED || keys[SDLK_RSHIFT] == SDL_PRESSED)) || event.type == SDL_QUIT) {
  272.             exec = 0;
  273.         }
  274.     }
  275.  
  276.     pceAppExit();
  277. }
  278.  
  279.