home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / gbuk.lha / src / amiga / GFXFUNC.C < prev    next >
C/C++ Source or Header  |  1997-02-08  |  9KB  |  479 lines

  1. #include    <stdlib.h>
  2. #include    <stdio.h>
  3. #include    <string.h>
  4. #include    <stdarg.h>
  5. #include    <time.h>
  6. #include    "gfxfunc.h"
  7. #include    "picture.h"
  8. #include    "keycodes.h"
  9.  
  10. int        FileRequestor(char *FileName, char *title);
  11. void    SaveScreen(void);
  12. void    RestoreScreen(void);
  13. extern     GXIMAGE        *ScreenSaveImage;
  14.  
  15. #define    PRINT_MESSAGES    0
  16. #define    SNAPSHOTS        0
  17.  
  18. #define    MAX_BLIT    (16384/sizeof(GXBLITLIST))
  19.  
  20. GXPALETTE            MasterPalette[256];
  21. char                CantUse[256];
  22. GXBLITLIST            *BlitList=0,*BlitPtr;
  23. int                    NBlit=0;
  24.  
  25.  
  26. int     GBMain(int argc,char *argv[]);
  27.  
  28.  
  29.  
  30. int    main(int argc,char *argv[])
  31. {     
  32.     return GBMain(argc,argv);
  33. }
  34. void    GXReadMouse(GXMOUSEINFO    *info)
  35. {
  36.     ReadMouse(info);
  37. }
  38.  
  39. void    GXSystemUpdate(void)
  40. {
  41.     SystemUpdate();
  42.     if(GXCheckQuit()){
  43.         GXQuitCode("Escape pressed\n");
  44.     }
  45.     #if    SNAPSHOTS
  46.         if(GXKeyState(KEY_F1)){
  47.             char         LoadGame[256]="ScreenShot.lbm";
  48.             GXPICTURE    *pic;
  49.             GXOBLONG    screen;
  50.  
  51.             SaveScreen();
  52.             if( FileRequestor(LoadGame, "Select screen shot") ){
  53.                 screen.X=screen.Y=0;
  54.                 screen.Width=GXImageWidth(ScreenSaveImage);
  55.                 screen.Height=GXImageHeight(ScreenSaveImage);
  56.                 pic=GXGetPicture(ScreenSaveImage, &screen);
  57.                 SavePicture(LoadGame, pic, PICTURE_LBM);
  58.             }
  59.             RestoreScreen();
  60.         }
  61.     #endif
  62. }
  63.  
  64. void    GXInitiateHardware(void)
  65. {
  66.     memset(MasterPalette, 0, sizeof(MasterPalette) );
  67.     memset(CantUse, 0, sizeof(CantUse));
  68.     InitiateHardware();
  69.     BlitList=(GXBLITLIST *)GXGetMem(MAX_BLIT*sizeof(GXBLITLIST), MEMF_ZERO|MEMF_ABORT);
  70.     NBlit=0;
  71.     BlitPtr=BlitList;
  72. }
  73.  
  74. GXBLITLIST    *GXNextBlit(void)
  75. {
  76.     if(NBlit==MAX_BLIT){
  77.         GXDoBlit();
  78.         BlitPtr=BlitList;
  79.         NBlit=0;
  80.     }
  81.     BlitPtr++;
  82.     NBlit++;
  83.     return    BlitPtr-1;
  84. }
  85. void        GXDoBlit(void)
  86. {
  87.     DrawBlitList(BlitList, NBlit);    
  88.     NBlit=0;
  89.     BlitPtr=BlitList;
  90. }
  91.  
  92.  
  93. int        GXGetPaletteRange(GXPALETTE *Pal, int FirstColor, int NumberColors)
  94. {
  95.     if( (FirstColor<0) || ((FirstColor+NumberColors)>256) ){
  96.         return 0;
  97.     }else{
  98.         memcpy(Pal, MasterPalette+FirstColor, NumberColors*sizeof(GXPALETTE) );    
  99.     }
  100. }
  101.  
  102. void    GXSetPaletteRange(GXPALETTE *Pal, int FirstColor, int NumberColors)
  103. {
  104.     SetPaletteRange(Pal, FirstColor, NumberColors);
  105.     memcpy(MasterPalette+FirstColor, Pal, NumberColors*sizeof(GXPALETTE) ); 
  106. }
  107. int        GXMapColor(GXPALETTE *pal)
  108. {
  109.     int            j,best,diff,Red,Green,Blue,total,jj;
  110.     GXPALETTE    *from;
  111.     char        *reserved;
  112.     
  113.     best=0;
  114.     diff=256*3;
  115.     Red=pal->Red;
  116.     Blue=pal->Blue;
  117.     Green=pal->Green;
  118.     from=MasterPalette;
  119.     reserved=CantUse;
  120.  
  121.     for(j=0; j<256; j++){
  122.         if(! (*reserved++) ){
  123.             total=0;
  124.             jj=     ((int)from->Red)-Red;
  125.             if(jj<0){
  126.                 total-=jj;
  127.             }else{
  128.                 total+=jj;
  129.             }
  130.             jj=     ((int)from->Green)-Green;
  131.             if(jj<0){
  132.                 total-=jj;
  133.             }else{
  134.                 total+=jj;
  135.                 }
  136.             jj=((int)from->Blue)-Blue;
  137.             if(jj<0){
  138.                 total-=jj;
  139.             }else{
  140.                 total+=jj;
  141.             }
  142.             if(total<diff){
  143.                 diff=total;
  144.                 best=j;
  145.             }
  146.         }
  147.         from++;
  148.     }
  149.     GXPrintf("Mapped color (%d,%d,%d) to (%d,%d,%d)\n",
  150.         (int)pal->Red,
  151.         (int)pal->Green,
  152.         (int)pal->Blue,
  153.         (int)MasterPalette[best].Red,
  154.         (int)MasterPalette[best].Green,
  155.         (int)MasterPalette[best].Blue);
  156.     return    best;
  157. }
  158.  
  159. GXIMAGE    *GXMakeImage(int width, int height)
  160. {
  161.     return MakeImage(width, height);
  162. }
  163. void    GXKillImage(GXIMAGE *image)
  164. {
  165.     KillImage(image);
  166. }
  167.  
  168. GXIMAGE    *GXPictureToImage(GXPICTURE *pic, long transparency, long TransparentColor)
  169. {
  170.     return    (GXIMAGE *)PictureToImage(pic, transparency, TransparentColor);
  171. }
  172. int    GXSetDrawBuffer(int n)
  173. {
  174.     return    1;
  175. }
  176. int    GXSetDisplayBuffer(int n)
  177. {
  178.     return    1;
  179. }
  180.  
  181. GXIMAGE    *GXStartDrawing(int n)
  182. {
  183.     return    (GXIMAGE *)StartDrawing(n);
  184. }
  185. void    GXEndDrawing(void)
  186. {
  187. }
  188.  
  189.  
  190. void    GXDrawBlitList(GXBLITLIST *b, int nblit)
  191. {
  192.     DrawBlitList(b, nblit);
  193. }
  194. long    GXReadJoystick(int n)
  195. {
  196.     return    ReadJoystick(n);
  197. }
  198. int        GXCheckQuit(void)
  199. {
  200.     return GXKeyDown(KEY_ESCAPE);
  201. //    return    CheckQuit();
  202. }
  203. void    GXQuitCode(char *message, ...)
  204. {
  205.     GXPrintf(message);
  206.     FreeHardware();
  207.     exit(1);
  208. }
  209.  
  210. long    GXImageWidth(GXIMAGE *image)
  211. {
  212.     return    ImageWidth(image);
  213. }
  214. long    GXImageHeight(GXIMAGE *image)
  215. {
  216.     return    ImageHeight(image);
  217. }
  218. void    GXReserveColors(int start, int number, int flags)
  219. {
  220.     ReserveColors(start, number, flags);
  221.     memset( CantUse+start, flags, number*sizeof(char));
  222. }
  223.  
  224. GXVOLUME    *GXMakeVolumeInfo(long *size)
  225. {
  226.     return    MakeVolumeInfo(size);
  227. }
  228. void    GXKillVolumeInfo(GXVOLUME *vol)
  229. {
  230.     KillVolumeInfo(vol);
  231. }
  232. int    GXMakeDirectoryInfo(char *name, GXFILEINFO **output)
  233. {
  234.     return    GetDirectoryInfo(name, output);
  235. }
  236. void    GXKillDirectoryInfo(GXFILEINFO *info)
  237. {
  238.     FreeDirectoryInfo(info);
  239. }
  240.  
  241. typedef struct{
  242.     void    *Next;
  243. }MEMBLOCK;
  244.  
  245. MEMBLOCK    *list=0;
  246.  
  247. //***************************************************************************
  248. //    Allocate system memory
  249. //***************************************************************************
  250. //    input:  amount wanted
  251. //    output: Block allocate or zero
  252. //*************************************************************************
  253. void    *GXGetMem(size_t amount, long flags)
  254. {
  255.     MEMBLOCK    *data;
  256.     
  257.     data=malloc(amount+sizeof(MEMBLOCK));
  258.     if(!data){
  259.         if(flags&MEMF_ABORT){
  260.             GXQuitCode("Ran out of memory\n");
  261.         }else{
  262.             return    0;
  263.         }
  264.     }
  265.     if(flags&MEMF_ZERO){
  266.         memset(data, 0, amount+sizeof(MEMBLOCK));
  267.     }
  268.     data->Next=list;
  269.     list=data;
  270.     return (data+1);
  271. }
  272. //***************************************************************************
  273. //    Free system memory
  274. //***************************************************************************
  275. //    input:  Block to free
  276. //    output: none
  277. //*************************************************************************
  278. void    GXFreeMem(void *data)
  279. {
  280.     MEMBLOCK    *block,*ptr,*last=0;
  281.         
  282.     ptr=(MEMBLOCK *)data;
  283.     ptr--;
  284.     block=list;
  285.     while(block){
  286.         if(block==ptr)
  287.             break;
  288.         last=block;
  289.         block=(MEMBLOCK *)block->Next;
  290.     }
  291.     if(!block){
  292.         GXPrintf("Non existant memory block\n");
  293.         return;
  294.     }else if(last){
  295.         last->Next=block->Next;
  296.     }else{
  297.         list=block->Next;
  298.     }
  299.     free(block);
  300. }
  301. //***************************************************************************
  302. //    Load a file into system memory
  303. //***************************************************************************
  304. //    input:  File name
  305. //    output: Picture structure or zero
  306. //***************************************************************************
  307. void    *GXLoadFileToMemory(char *name, size_t *size)
  308. {
  309.     FILE    *f=0;
  310.     void    *data=0;
  311.     size_t    length;
  312.     
  313.     f=fopen(name,"r");
  314.     if(f==NULL){
  315.         goto fail;
  316.     }
  317.     if(fseek(f, 0, SEEK_END)){
  318.         goto fail;
  319.     }
  320.     length=ftell(f);
  321.     data=GXGetMem(length, 0);
  322.     if(!data){
  323.         goto fail;
  324.     }
  325.     if(fseek(f, 0, SEEK_SET)){
  326.         goto fail;
  327.     }
  328.     if(length!=fread(data, 1, length, f)){
  329.         goto fail;
  330.     }
  331.     fclose(f);
  332.     *size=length;
  333.     return    data;
  334. fail:
  335.     if(data){
  336.         GXFreeMem(data);
  337.     }
  338.     if(f){
  339.         fclose(f);
  340.     }
  341.     return    0;
  342. }
  343.  
  344. //***************************************************************************
  345. //    Save memory to disk
  346. //***************************************************************************
  347. //    input:  data, length of data, file name
  348. //    output: did it work?
  349. //***************************************************************************
  350. int    GXSaveMemoryToFile(void *data, size_t LengthData, char *FileName)
  351. {
  352.     FILE    *handle;
  353.     size_t    bytes;
  354.     
  355.     handle=fopen(FileName, "wb");
  356.     if(handle==NULL){
  357.         return 0;
  358.     }
  359.     bytes=fwrite(data, 1, LengthData, handle);
  360.     fclose(handle);
  361.     return (bytes==LengthData);
  362. }
  363.  
  364. int    GXPrintf(const char *message, ...)
  365. {
  366. #if    PRINT_MESSAGES
  367.     va_list    marker;
  368.  
  369.     va_start(marker, message);
  370.     vprintf( message, marker);
  371. #endif
  372. }
  373.  
  374. GXSCREENMODEINFO    *GXGetScreenModeInfo(long *number)
  375. {
  376.     return    (GXSCREENMODEINFO *)GetScreenModeInfo(number);
  377. }
  378. void                GXKillScreenModeInfo(GXSCREENMODEINFO *info)
  379. {
  380.     KillScreenModeInfo(info);
  381. }
  382. long        GXSetScreenMode(long mode)
  383. {
  384.     return SetScreenMode(mode);
  385. }
  386. long    GXKeyDown(long key)
  387. {
  388.     return KeyState(key);
  389. }
  390. long    GXNextKey(void)
  391. {
  392.     return NextKey();
  393. }
  394. typedef struct{
  395.     long    Width;        
  396.     long    Height;                
  397.     long    PlaneSeperation;        
  398.     long    *XLineSeperation;    
  399.     void    *BitPlanes;            
  400.     long    LineSeperation;        
  401.     long    Depth;            
  402.     void    *Mask;                
  403.     long    MaskLineSeperation;    
  404.     long    *XMaskLineSeperation;    
  405. }THEIMAGE;
  406.  
  407. GXPICTURE    *GXGetPicture(GXIMAGE *image, GXOBLONG *area)
  408. {
  409.     THEIMAGE    *im;
  410.     GXPICTURE    *pic;
  411.     UBYTE        *from,*to,*to2,*from2,*from3,pixel;
  412.     int            ix,iy,iplane,offset;
  413.     UWORD        *tow,data[8],mask,*fromw,jj;
  414.     
  415.     im=(THEIMAGE *)image;
  416.  
  417.     //    Setup picture structure
  418.     pic=(GXPICTURE *)GXGetMem(sizeof(GXPICTURE), MEMF_ZERO);
  419.     if(!pic)goto fail;
  420.     pic->Width=area->Width;
  421.     pic->Height=area->Height;
  422.     pic->BytesAcross=area->Width;
  423.     pic->Data=GXGetMem(area->Width*area->Height, MEMF_ZERO|MEMF_ABORT);
  424.     pic->Palette=(GXPALETTE *)GXGetMem(256*sizeof(GXPALETTE), MEMF_ZERO);
  425.     if(!pic->Palette)goto fail;
  426.     pic->NPalette=256;
  427.     GXGetPaletteRange(pic->Palette, 0, 256);
  428.     
  429.     //    Create byte map data
  430.     to=(UBYTE *)pic->Data;
  431.     from=((UBYTE *)im->BitPlanes)    +    area->Y*im->LineSeperation +
  432.             (area->X/8);
  433.     for(iy=0; iy<area->Height; iy++){
  434.         to2=to;
  435.         from2=from;
  436.         offset = area->X & 0x07;
  437.         mask = (1 << (7-offset) );
  438.         for(ix=0; ix<area->Width; ix++){
  439.             jj=0;
  440.             pixel=0x01;
  441.             from3=from2;
  442.             for(iplane=0; iplane<8; iplane++){
  443.                 if( (*from3)&mask ){
  444.                     jj|=pixel;
  445.                 }
  446.                 from3+=im->PlaneSeperation;
  447.                 pixel*=2;
  448.             }    
  449.             *to2++=jj;
  450.             mask>>=1;
  451.             if(!mask){
  452.                 mask=0x80;
  453.                 from2++;
  454.             }
  455.         }
  456.         to+=pic->BytesAcross;
  457.         from+=im->LineSeperation;
  458.     }
  459.     return pic;
  460.     fail:
  461.         GXQuitCode("Faliure in GXGetPicture()\n");
  462.         return 0;
  463. }
  464. char    *GXGetLastErrorMessage(void)
  465. {
  466.     return GetLastErrorMessage();
  467. }
  468. long    GXTicksPerSecond(void)
  469. {
  470.     return 50;
  471. }
  472. long    GXTimer(void)
  473. {
  474.     return clock();
  475. }
  476.  
  477.  
  478.  
  479.