home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / REND.LZH / REND / BACK.C next >
C/C++ Source or Header  |  1996-06-12  |  5KB  |  254 lines

  1. /*
  2.  *        背景の読み込み
  3.  *
  4.  *
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #include "reader.h"
  12. #include "glib.h"
  13. #include "assert.h"
  14. #ifndef NO_PICLIB
  15. #include "piclib.h"
  16. #endif
  17.  
  18. #define    MAXSTRING    10
  19.  
  20. #ifdef NO_PICLIB
  21. static int backfullcolorflag = FALSE;
  22. #endif
  23.  
  24. #ifdef NO_PICLIB
  25. static    ColorCode    Code ;
  26. static    int        Length ;
  27. static    int        FileXPixel, FileYPixel ;
  28. #else
  29. static    PicData    *pd;
  30. static    Pixel    *ReadBuf = NULL;
  31. static    int        lastX = 0;
  32. #endif
  33. static    int        BackXPixel, BackYPixel ;
  34. static    int        Ycount;
  35. static    int        Line ;
  36. static    FILE    *fp ;
  37. static    ColorCode    *FrameBuf ;
  38.  
  39. /*
  40.     proto -s back.c > temp
  41. */
  42. static    void    BackCopyLine1( ColorCode* );
  43. static    void    BackError( char*, char* );
  44. static    int        getword( char* );
  45.  
  46. /*  画像ファイルのオープン  */
  47. void    BackOpen( filename )
  48. char    *filename ;
  49. {
  50.     int        i ;
  51.     char    str[MAXSTRING+1] ;
  52.  
  53. #ifdef NO_PICLIB
  54.     fp = fopen( filename, "rb" );
  55.     if ( fp == NULL )
  56.         BackError( "画像ファイル %s がオープンできません。", filename );
  57.     if ( getword( str ) && strcmp( str, "image" ) != 0 )
  58.         BackError( "%s は画像ファイルではありません。", filename );
  59.  
  60.     backfullcolorflag = FALSE;
  61.  
  62.     /*  ヘッダ部分  */
  63.     for(;;)
  64.     {
  65.         if ( getword( str ) == EOF )
  66.             break ;
  67.         if ( strcmp( str, "pxn" ) == 0 )
  68.         {
  69.             getword( str );
  70.             FileXPixel = atoi( str );
  71.             getword( str );
  72.             FileYPixel = atoi( str );
  73.         }
  74.         else if ( strcmp( str, "col" ) == 0 )
  75.         {
  76.             getword( str );
  77.             if (atoi(str) == 16) {
  78.                 backfullcolorflag = FALSE;
  79.             } else if (atoi(str) == 25) {
  80.                 backfullcolorflag = TRUE;
  81.             }
  82.         }
  83.         else if ( strcmp( str, "mod" ) == 0 )
  84.         {
  85.             getword( str );
  86.             if ( strcmp( str, "rln" ) != 0 )
  87.                 BackError( "画像ファイル %s は読み込めません。", filename );
  88.         }
  89.     }
  90.  
  91.     for( i = 0 ; i < 4 ; ++i )
  92.         getc( fp );
  93.     {
  94.         extern int antiareas;
  95.         BackXPixel = XPixel;
  96.         BackYPixel = YPixel;
  97.         if ( XPixel / antiareas == 320 && YPixel / antiareas == 240) {
  98.             BackXPixel = 512 * antiareas;
  99.             BackYPixel = 512 * antiareas;
  100.         }
  101.     }
  102.  
  103.     if (backfullcolorflag) {
  104.         extern unsigned long getlong(FILE *);
  105.         unsigned long c;
  106.         c = getlong(fp);
  107.         Code = (c & 0xffffff00L) | ((c & 0x00000080L) ? 1 : 0);
  108.         Length = (int)(c & 0x7fL);
  109.     } else {
  110.         Code = X68kcolorToColorCode( getshort(fp) );
  111.         Length = getshort( fp );
  112.     }
  113. #else
  114.     pd = PicReadOpen(filename);
  115.     if ( pd == NULL )
  116.         BackError( "画像ファイル %s がオープンできません。", filename );
  117.     {
  118.         extern int antiareas;
  119.         BackXPixel = XPixel;
  120.         BackYPixel = YPixel;
  121.         if (pd->pixelX == 512 && pd->pixelY == 512
  122.          && XPixel / antiareas == 320 && YPixel / antiareas == 240) {
  123.             BackXPixel = 512 * antiareas;
  124.             BackYPixel = 512 * antiareas;
  125.         }
  126.     }
  127.     /*    フレームバッファ確保  */
  128.     if (lastX < pd->pixelX) {
  129.         if (ReadBuf != NULL) {
  130.             tempfree(ReadBuf);
  131.         }
  132.         ReadBuf = (Pixel*)tempalloc( sizeof( Pixel ) * pd->pixelX );
  133.     }
  134.  
  135. #endif
  136.  
  137.     Ycount = 0;
  138.     Line = 0 ;
  139.     /*    フレームバッファ確保  */
  140.     if ( FrameBuf == NULL )
  141.         FrameBuf = (ColorCode*)tempalloc( sizeof( ColorCode ) * BackXPixel );
  142. }
  143.  
  144. /*    読み込み  */
  145. void    BackCopy( framebuf )
  146. ColorCode    *framebuf ;
  147. {
  148. #ifdef NO_PICLIB
  149.     Ycount -= FileYPixel;
  150. #else
  151.     Ycount -= pd->pixelY;
  152. #endif
  153.     while (Ycount < 0) {
  154.         BackCopyLine1(FrameBuf);
  155.         Ycount += BackYPixel;
  156.     }
  157.     memcpy( (char*)framebuf, (char*)FrameBuf, sizeof( ColorCode )*XPixel );
  158.     Line ++ ;
  159. }
  160.  
  161. /*  1ライン読み込み  */
  162. static    void    BackCopyLine1( framebuf )
  163. ColorCode    *framebuf ;
  164. {
  165. #ifdef NO_PICLIB
  166.     int        x;
  167.     int count = 0;
  168.     for( x = FileXPixel ; x > 0 ; --x )
  169.     {
  170.         count -= BackXPixel;
  171.         while (count < 0) {
  172.             *framebuf++ = Code;
  173.             count += FileXPixel;
  174.         }
  175.         Length -- ;
  176.         if ( Length == 0 )
  177.         {
  178.             if (backfullcolorflag) {
  179.                 extern unsigned long getlong(FILE *);
  180.                 unsigned long c;
  181.                 c = getlong(fp);
  182.                 Code = (c & 0xffffff00L) | ((c & 0x00000080L) ? 1 : 0);
  183.                 Length = (int)(c & 0x7fL);
  184.             } else {
  185.                 Code = X68kcolorToColorCode( getshort(fp) );
  186.                 Length = getshort( fp );
  187.             }
  188.         }
  189.     }
  190. #else
  191.     int        x;
  192.     int count = 0;
  193.     ColorCode code;
  194.     Pixel *p;
  195.     if (PicInputLine(pd, ReadBuf) == FALSE) {
  196.         BackError( "背景の読み込みに失敗しました。", "" );
  197.     }
  198.     p = ReadBuf;
  199.     for( x = pd->pixelX ; x > 0 ; --x )
  200.     {
  201.         code = (*p & 0xffffff00L) | ((*p & 0x00000080L) ? 1 : 0);
  202.         count -= BackXPixel;
  203.         while (count < 0) {
  204.             *framebuf++ = code;
  205.             count += pd->pixelX;
  206.         }
  207.         p++;
  208.     }
  209. #endif
  210. }
  211.  
  212. void    BackClose()
  213. {
  214. #ifdef NO_PICLIB
  215.     fclose( fp );
  216. #else
  217.     PicClose(pd);
  218. #endif
  219. }
  220.  
  221. static    void    BackError( msg, arg )
  222. char    *msg ;
  223. char    *arg ;
  224. {
  225. #ifdef MESSAGE
  226.     extern int printwarning(const char *format, ...);
  227.     printwarning( msg, arg );
  228. #endif
  229.     fprintf( stderr, msg, arg );
  230.     fprintf( stderr, "\nエラーが発生しました。\n\n" );
  231.     exit( 1 );
  232. }
  233.  
  234. #ifdef NO_PICLIB
  235. static    int        getword( str )
  236. char    *str ;
  237. {
  238.     int        i, c ;
  239.  
  240.     while( ! isalnum( c = getc( fp ) ) && c != 0x1A );
  241.  
  242.     if ( c == 0x1A )
  243.         return( EOF );
  244.  
  245.     for( i = 0 ; i < MAXSTRING && isalnum( c ) ; ++i )
  246.     {
  247.         *str++ = (char)c ;
  248.         c = getc( fp );
  249.     }
  250.     *str = '\0' ;
  251.     return TRUE;
  252. }
  253. #endif
  254.