home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / gchsrc31 / atarilib / screen.cc < prev    next >
C/C++ Source or Header  |  1992-04-27  |  7KB  |  308 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknoledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11.  
  12. #include "screen.h"
  13. #include "resolution.h"
  14. #include "ca_pack.h"
  15. #include "ca_unpac.h"
  16. #include <stdio.h>
  17. #include <osbind.h>
  18. #include <alloc.h>
  19. #include <builtin.h>
  20.  
  21.  
  22. static void *Page(void *adr)
  23. {
  24.     unsigned long a=(unsigned long)adr;
  25.     return (void *)((a & 0xffffff00)+0x100);
  26. }
  27.  
  28. Screen::Screen(const Screen& Same) :
  29.     Res(Same.Res),
  30.     AllocSize(BytesPerLine[Res]*ScreenHeight[Res]+254),
  31.     AllocArea(new char[AllocSize]),
  32.     location(Page(AllocArea)),
  33.     Palette(new short[NumberOfColours[Res]])
  34. {
  35.     memcpy(location,Same.location,BytesPerLine[Res]*ScreenHeight[Res]);
  36.     memcpy(Palette,Same.Palette,sizeof(Palette[0])*NumberOfColours[Res]);
  37. }
  38.  
  39. Screen::Screen(Resolution res) :
  40.     Res(res),
  41.     AllocSize(BytesPerLine[Res]*ScreenHeight[Res]+254),
  42.     AllocArea(new char[AllocSize]),
  43.     location(Page(AllocArea)),
  44.     Palette(new short[NumberOfColours[Res]])
  45. { }
  46.  
  47. Screen::Screen(short LinesAbove, short LinesBelow) :
  48.     Res(Getrez()),
  49.     AllocSize(BytesPerLine[Res]*(ScreenHeight[Res]+LinesAbove+LinesBelow)+254),
  50.     AllocArea(new char[AllocSize]),
  51.     location(Page(AllocArea)),
  52.     Palette(new short[NumberOfColours[Res]])
  53. { }
  54.  
  55. Screen::Screen() :
  56.     Res(Getrez()),
  57.     AllocSize(BytesPerLine[Res]*ScreenHeight[Res]+254),
  58.     AllocArea(new char[AllocSize]),
  59.     location(Page(AllocArea)),
  60.     Palette(new short[NumberOfColours[Res]])
  61. { }
  62.  
  63. Screen::Screen(char *At)
  64. {
  65.     Res=Getrez();
  66.  
  67.     AllocSize=0;
  68.     AllocArea=0;
  69.     if (At) {
  70.         location=At;
  71.     }
  72.     else {
  73.         location=Physbase();
  74.     }
  75.     Palette=malloc(sizeof(Palette[0])*NumberOfColours[Res]);
  76.     for (int i=0; i<NumberOfColours[Res]; i++) {
  77.         Palette[i]=Setcolor(i,-1);
  78.     }
  79. }
  80.  
  81. Screen::Screen(Resolution res, short LinesAbove, short LinesBelow)
  82. {
  83.     short BPL=BytesPerLine[res];
  84.     int BaseSize=BPL*ScreenHeight[res]+254;
  85.  
  86.     AllocSize=BaseSize+(LinesAbove+LinesBelow)*BPL;
  87.     AllocArea=malloc(AllocSize);
  88.     location=Page(AllocArea+BPL*LinesAbove);
  89.     Palette=malloc(sizeof(Palette[0])*NumberOfColours[res]);
  90.     Res=res;
  91. }
  92.  
  93. Screen::~Screen()
  94. {
  95.     if (AllocArea) delete AllocArea;
  96.     delete Palette;
  97. }
  98.  
  99.  
  100. void Screen::Clear()
  101. {
  102. /* Clear visible region? */
  103.     int i=BytesPerLine[Res]*ScreenHeight[Res]/sizeof(long);
  104.     long * data=(long*) location;
  105.  
  106. /* ...or clear whole area?
  107.     int i=AllocSize/sizeof(long);
  108.     long *data=(long *)AllocArea;
  109. */
  110.  
  111.     while (i--) data[i]=0;
  112. }
  113.  
  114. #define CHECK(x) {if ((x)<=0) {fclose(file); return 0;}}
  115.  
  116. int Screen::LoadDegasPalette(const char *filename)
  117. {
  118.     FILE *file;
  119.     short res;
  120.  
  121.     file=fopen(filename,"rb");
  122.     if (!file) return 0;
  123.  
  124.     CHECK(fread(&res,sizeof(res),1,file));
  125.     CHECK(fread(Palette,sizeof(short),NumberOfColours[Res],file));
  126.  
  127.     fclose(file);
  128.  
  129.     return 1;
  130. }
  131.  
  132. int Screen::LoadDegas(const char *filename)
  133. {
  134.     FILE *file;
  135.     short res;
  136.     short junk;
  137.  
  138.     file=fopen(filename,"rb");
  139.     if (!file) return 0;
  140.  
  141.     CHECK(fread(&res,sizeof(res),1,file));
  142.     if (NumberOfColours[res]!=NumberOfColours[Res]) {
  143.         delete Palette;
  144.         Palette=new short[NumberOfColours[Res]];
  145.     }
  146.     Res=res;
  147.     CHECK(fread(Palette,sizeof(short),NumberOfColours[Res],file));
  148.     for (int ignore=16-NumberOfColours[Res]; ignore>0; ignore--)
  149.         CHECK(fread(&junk,sizeof(short),1,file));
  150.     CHECK(fread(location,1,BytesPerLine[Res]*ScreenHeight[Res],file));
  151.  
  152.     fclose(file);
  153.  
  154.     return 1;
  155. }
  156.  
  157. int Screen::SaveDegas(const char *filename)
  158. {
  159.     FILE *file;
  160.     short res;
  161.  
  162.     file=fopen(filename,"wb");
  163.     if (!file) return 0;
  164.  
  165.     res=Res;
  166.     CHECK(fwrite(&res,sizeof(short),1,file));
  167.     CHECK(fwrite(Palette,sizeof(short),NumberOfColours[Res],file));
  168.     if (NumberOfColours[Res] < 16) {
  169.         short junk=0;
  170.         for (int ignore=16-NumberOfColours[Res]; ignore; ignore--)
  171.             CHECK(fwrite(&junk,sizeof(short),1,file));
  172.     }
  173.     CHECK(fwrite(location,1,BytesPerLine[Res]*ScreenHeight[Res],file));
  174.     fclose(file);
  175.  
  176.     return 1;
  177. }
  178.  
  179.  
  180. PaletteChange::PaletteChange() :
  181.     ncols(16),
  182.     col(new short[ncols])
  183. {
  184.     for (int i=0; i<ncols; i++) {
  185.         col[i]=Setcolor(i,-1);
  186.     }
  187. }
  188.  
  189. PaletteChange::~PaletteChange()
  190. {
  191.     Setpalette(col);
  192.  
  193.     delete col;
  194. }
  195.  
  196. /////////////////////////////////////////////////
  197. //                                             //
  198. //  CrackArt I/O as per GFA basic code:        //
  199. //                                             //
  200. //    CRACK ART 'CA_PACK'                      //
  201. //                                             //
  202. //    Kompressionsroutine für CA?-Bilder       //
  203. //                                             //
  204. //    © Detlef Röttger & Jan Borchers 1989-91  //
  205. //                                             //
  206. /////////////////////////////////////////////////
  207.  
  208. int Screen::LoadCrackArtPalette(const char *Filename)
  209. {
  210.     FILE* fp=fopen(Filename,"rb");
  211.  
  212.     if (!fp) return 0;
  213.  
  214.     char Magic[2];
  215.     fread(Magic,sizeof(char),2,fp);
  216.     if (Magic[0]!='C' || Magic[1]!='A') {
  217.         fclose(fp);
  218.         return 0;
  219.     }
  220.  
  221.     short Rez;
  222.     fread(&Rez,sizeof(short),1,fp);
  223.     Rez&=255;
  224.  
  225.     if (Rez!=Res) {
  226.         return 0;
  227.     }
  228.  
  229.     // NB. CrackArt file format is WRONG for CA3 - it doesn't save a palette
  230.     //     this is very wrong - especially on the TT.  So we can't load one.
  231.     if (Rez==STHigh) {
  232.         Palette[0]=0x777;
  233.         Palette[1]=0x000;
  234.     } else {
  235.         fread(Palette,sizeof(short),NumberOfColours[Rez],fp);
  236.     }
  237.  
  238.     fclose(fp);
  239.  
  240.     return 1;
  241. }
  242.  
  243. int Screen::LoadCrackArt(const char *Filename)
  244. {
  245.     FILE* fp=fopen(Filename,"rb");
  246.  
  247.     if (!fp) return 0;
  248.  
  249.     char Magic[2];
  250.     fread(Magic,sizeof(char),2,fp);
  251.     if (Magic[0]!='C' || Magic[1]!='A') {
  252.         fclose(fp);
  253.         return 0;
  254.     }
  255.  
  256.     short Rez;
  257.     fread(&Rez,sizeof(short),1,fp);
  258.     Rez&=255;
  259.  
  260.     if (Rez!=Res) {
  261.         Res=Rez;
  262.         // Reallocate? Fail? *******************
  263.     }
  264.  
  265.     // NB. CrackArt file format is WRONG for CA3 - it doesn't save a palette
  266.     //     this is very wrong - especially on the TT.  We could load one,
  267.     //     expecting CrackArt to correct the problem, if they ever support
  268.     //     mono rez, but then we couldn't load them!  Perhaps we could add a
  269.     //     hack to set a bit in the reserved byte after Rez, but it's not
  270.     //     our format!  Now the question is, do we make CrackArt's mistake
  271.     //     again and not support a palette in TTHigh?  No.
  272.     if (Rez==STHigh) {
  273.         Palette[0]=0x777;
  274.         Palette[1]=0x000;
  275.     } else {
  276.         fread(Palette,sizeof(short),NumberOfColours[Rez],fp);
  277.     }
  278.  
  279.     LoadCrackArtData((unsigned char*)location,BytesPerLine[Rez]*ScreenHeight[Rez],fp);
  280.  
  281.     fclose(fp);
  282.  
  283.     return 1;
  284. }
  285.  
  286.  
  287. int Screen::SaveCrackArt(const char *Filename, int Compression=3)
  288. {
  289.     FILE* fp=fopen(Filename,"wb");
  290.  
  291.     if (!fp) return 0;
  292.  
  293.     fwrite("CA",sizeof(char),2,fp);
  294.  
  295.     short Rez=256+Res;
  296.     fwrite(&Rez,sizeof(short),1,fp);
  297.  
  298.     // NB. CrackArt file format is WRONG for CA3 - it doesn't save a palette
  299.     //     this is very wrong - especially on the TT.  So we don't save one.
  300.     if (Res!=STHigh) fwrite(Palette,sizeof(short),NumberOfColours[Res],fp);
  301.  
  302.     SaveCrackArtData((unsigned char*)location,BytesPerLine[Res]*ScreenHeight[Res],fp,Compression);
  303.  
  304.     fclose(fp);
  305.  
  306.     return 1;
  307. }
  308.