home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / STFORMAT / STF11.MSA / SIDE_B_C_TUTOR_DEGAS.C < prev    next >
C/C++ Source or Header  |  1990-04-15  |  3KB  |  118 lines

  1. /* ----------------- ST FORMAT PINBALL PROGRAM ------- ------------------------
  2.  
  3.     Title:        PINBALL
  4.  
  5.     Module:        degas.c
  6.  
  7.     Version:    1.1
  8.  
  9.     Author:        Warwick Grigg
  10.  
  11.     Copyright (c) Warwick Grigg 1990. All rights reserved.
  12.  
  13. ----------------------------------------------------------------------------- */
  14.  
  15. #include <osbind.h>
  16. #include <stdio.h>
  17. #include "scratt.h"
  18. #include "errexit.h"
  19.  
  20. struct degashdr_s {    /* structure of first part of a degas picture    */
  21.     int        res;
  22.     int        pallette[16];
  23. };
  24.  
  25. static int  nplanes;    /* number of planes at current resolution    */
  26. static int  screendimx;    /* number of pixels wide            */
  27. static int  screendimy;    /* number of pixels deep            */
  28.  
  29. static int  row;    /* current row number                */
  30. static int  plane;    /* current plane number                */
  31. static int  bytix;    /* byte index for this row and plane        */
  32. static char *planep;    /* pointer to first word in this row and plane    */
  33. static int  bytesperrow;/* number of bytes per row * planes        */
  34.  
  35. static char *msg[] = {    
  36.     "[1][ Sorry, | I can't read the picture file][OK]",
  37.     "[1][ Sorry, | I can't find the picture file][OK]",
  38.     "[1][ Sorry, | the picture file format is wrong][OK]" 
  39. };
  40.  
  41. static void DgPutInit(buf)    /* Initialise unpacking of Degas screen        */
  42. char *buf;
  43. {
  44.     nplanes = planes();
  45.     screendimx = scrdimx();
  46.     screendimy = scrdimy();
  47.     bytesperrow = (screendimx/8) * nplanes;
  48.     planep = buf;
  49.     row =
  50.     plane =
  51.     bytix = 0;
  52. }
  53.  
  54. static void DegasPutByte(b)    /* Puts b into screen buffer */
  55. char b;
  56. {
  57.     planep[bytix++] = b;
  58.     if ((bytix&1)==0) {
  59.     bytix += (nplanes+nplanes-2);
  60.     if (bytix >= bytesperrow) {
  61.         plane++;
  62.         if (plane >= nplanes) {
  63.         plane = 0;
  64.         planep += bytesperrow;
  65.         row++;
  66.             }
  67.         bytix = plane+plane;
  68.     }
  69.     }
  70. }
  71.  
  72. void DegasGet(buf, filename)    /* Gets Degas picture into screen buffer */
  73. char *buf;    /* screen buffer, must be 32000 bytes long         */
  74. char *filename;    /* file name, must be for current resolution, compressed */
  75. {
  76.     FILE *f;            /* file handle            */
  77.     struct degashdr_s hdr;    /* Degas picture header        */
  78.     int prefix;            /* character buffer        */
  79.     int c;            /* another character buffer    */
  80.  
  81.     if ((f=fopen(filename, "rb")) != NULL) { 
  82.     if (fread(&hdr, sizeof(struct degashdr_s), 1, f) != 1)
  83.         errexit(msg[0]);
  84.     DgPutInit(buf);
  85.     do {
  86.         prefix = fgetc(f);
  87.         if (prefix==EOF)
  88.         errexit(msg[2]);
  89.         if (prefix>=0 && prefix<=127 ) {
  90.         prefix++;
  91.         while ((prefix--)>0) {
  92.             c = fgetc(f);
  93.             if (c==EOF || row>=screendimy)
  94.             errexit(msg[2]);
  95.             DegasPutByte(c);
  96.             }
  97.         }
  98.         else if (prefix>=129 && prefix<=255) {
  99.         prefix = 257-prefix;
  100.         c = fgetc(f);
  101.         if (c==EOF)
  102.             errexit(msg[2]);
  103.         while ((prefix--)>0) {
  104.             if (row>=screendimy)
  105.             errexit(msg[2]);
  106.             DegasPutByte(c);
  107.         }
  108.         }
  109.     } while (row<screendimy);
  110.     fclose(f);
  111.     }
  112.     else {
  113.     errexit(msg[1]);
  114.     }
  115.     Setpallete(&hdr.pallette[0]);
  116.     Vsync();
  117. }
  118.