home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 106 / EnigmaAmiga106CD.iso / indispensabili / grafica / amipeg_1.1 / src / cybergfx.c < prev    next >
C/C++ Source or Header  |  1998-01-20  |  7KB  |  262 lines

  1.  
  2. /* this source implements aMiPEG's interface to CyberGraphX */
  3.  
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. #include <exec/exec.h>
  9. #include <cybergraphics/cybergraphics.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/screens.h>
  12. #include <dos/dos.h>
  13.  
  14. #include <cybergraphics/cgxvideo.h>
  15. #include <pragmas/cgxvideo_pragmas.h>
  16. #include <clib/cgxvideo_protos.h>
  17.  
  18. #include <proto/graphics.h>
  19. #include <proto/exec.h>
  20. #include <proto/cybergraphics.h>
  21. #include <proto/intuition.h>
  22. #include <proto/dos.h>
  23. #include <proto/asyncio.h>
  24.  
  25. #include "video.h"
  26. #include "proto.h"
  27.  
  28. extern int doneFlag;
  29. extern int fullFlag;
  30. extern int ditherType;
  31. extern int cyber_pub;
  32. extern struct GfxBase *GfxBase;
  33.  
  34. struct Library *CyberGfxBase;
  35. struct Library *CGXVideoBase;
  36.  
  37. ULONG cyber_mode;
  38. ULONG depth;
  39. struct Screen *cyber_screen;
  40. struct Window *cyber_window;
  41. int original_x, original_y;
  42. VLayerHandle VLH;
  43.  
  44.  
  45. static void Quit(char *why, int failcode)
  46. {
  47.     puts(why);
  48.     exit(failcode);
  49. }
  50.  
  51. static void output_term(void)
  52. {
  53.     close_timer();
  54.  
  55.     if(input)
  56.     {
  57.         CloseAsync(input);
  58.         input = NULL;
  59.     }
  60.  
  61.     if(VLH)
  62.     {
  63.         DetachVLayer(VLH);
  64.         DeleteVLayerHandle(VLH);
  65.     }
  66.  
  67.     if(cyber_window)
  68.         CloseWindow(cyber_window);
  69.  
  70.     if(!cyber_pub && cyber_screen)
  71.         CloseScreen(cyber_screen);
  72.  
  73.     if(CGXVideoBase)
  74.         CloseLibrary(CGXVideoBase);
  75.  
  76.     if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  77.     if (CyberGfxBase) CloseLibrary(CyberGfxBase);
  78.     if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
  79. }
  80.  
  81. void DrawCyberGfxImage(UWORD *data, int x, int y)
  82. {
  83.     int win_x, win_y, i;
  84.  
  85.     if(ditherType == CYBERGFXVLAYER_DITHER || ditherType == CYBERGFXVLAYERGRAY_DITHER)
  86.     {
  87.         if(LockVLayer(VLH))
  88.         {
  89.             UWORD *address = (UWORD *)GetVLayerAttr(VLH, VOA_BaseAddress);
  90.  
  91.             for(i = 0; i < y; i++)
  92.             {
  93.                 memcpy(address, data, x << 1);
  94.                 address += x;
  95.                 data += ((x + 15) >> 4) << 4;
  96.             }
  97.  
  98.             UnlockVLayer(VLH);
  99.  
  100.             return;
  101.         }
  102.     }
  103.  
  104.     win_x = cyber_window->Width - cyber_window->BorderLeft - cyber_window->BorderRight;
  105.     win_y = cyber_window->Height - cyber_window->BorderTop - cyber_window->BorderBottom;
  106.  
  107.     if(win_x == x && win_y == y)
  108.     {
  109.         if(ditherType == CYBERGFXGRAY_DITHER)
  110.             WritePixelArray(data, 0, 0, x & 0x0f ? ((x >> 4) + 1 ) << 4 : x, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, x, y, RECTFMT_GREY8);
  111.         else
  112.             WritePixelArray(data, 0, 0, (x & 0x0f ? ((x >> 4) + 1 ) << 4 : x ) * 4, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, x, y, RECTFMT_ARGB);
  113.     }
  114.     else
  115.     {
  116.         if(ditherType == CYBERGFXGRAY_DITHER)
  117.             ScalePixelArray(data, x, y, x & 0x0f ? ((x >> 4) + 1 ) << 4 : x, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, win_x, win_y, RECTFMT_GREY8);
  118.         else
  119.             ScalePixelArray(data, x, y, (x & 0x0f ? ((x >> 4) + 1 ) << 4 : x) * 4, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, win_x, win_y, RECTFMT_ARGB);
  120.     }
  121. }
  122.  
  123. int handle_window(void)
  124. {
  125.     struct IntuiMessage *imsg;
  126.     struct IntuiMessage msg;
  127.     static ULONG last_secs, last_micros;
  128.  
  129.     if(ditherType == PIV_DITHER)
  130.     {
  131.         return (p96_handle_window());
  132.     }
  133.     if(cyber_window)
  134.     {
  135.         while(imsg = (struct IntuiMessage *)GetMsg(cyber_window->UserPort))
  136.         {
  137.             CopyMem((char *)imsg, (char *)&msg, (long)sizeof(struct IntuiMessage));
  138.             ReplyMsg((struct Message *)imsg);
  139.             switch(msg.Class)
  140.             {
  141.                 case IDCMP_VANILLAKEY:
  142.                     if(msg.Code == '\x1b' || msg.Code == 'q')
  143.                     {
  144.                                             doneFlag = DONE_REALLY;
  145.                                             return(FALSE);
  146.                     }
  147.                     if(msg.Code == 13 || msg.Code == 32)    /* space & return */
  148.                         return(FALSE);
  149.                     break;
  150.  
  151.                 case IDCMP_CLOSEWINDOW:
  152.                 {
  153.                     doneFlag = DONE_NOT_REALLY;
  154.                     return(FALSE);
  155.                 }
  156.  
  157.                 case IDCMP_MOUSEBUTTONS:
  158.                     if(!fullFlag && msg.Code == SELECTDOWN)
  159.                     {
  160.                         if(DoubleClick(last_secs, last_micros, msg.Seconds, msg.Micros))
  161.                             ChangeWindowBox(cyber_window, cyber_window->LeftEdge, cyber_window->TopEdge, original_x, original_y);
  162.  
  163.                         last_secs = msg.Seconds;
  164.                         last_micros = msg.Micros;
  165.                     }
  166.                 break;
  167.  
  168.                 default:
  169.                 break;
  170.             }
  171.         }
  172.     }
  173.     return(TRUE);
  174. }
  175.  
  176.  
  177. BOOL init_cybergfx(void)
  178. {
  179.     if(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",37))
  180.         if(CyberGfxBase = OpenLibrary("cybergraphics.library", 40))
  181.             if(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39))
  182.                 if(ditherType == CYBERGFXVLAYER_DITHER || ditherType == CYBERGFXVLAYERGRAY_DITHER)
  183.                 {
  184.                     if(CGXVideoBase = OpenLibrary("cgxvideo.library", 41))
  185.                         return(TRUE);
  186.                 }
  187.                 else
  188.                     return(TRUE);
  189.             else
  190.                 Quit("graphics.library v39+ not present", 25);
  191.         else
  192.             Quit("cybergraphics.library v40+ not present", 25);
  193.     else
  194.         Quit("intuition.library is too old, <V39", 25);
  195. }
  196.  
  197. int get_cyber_mode(void)
  198. {
  199.     return((int)CModeRequestTags(NULL,
  200.                                                     CYBRMREQ_WinTitle, "Select a screenmode for MPEG window",
  201.                                                     CYBRMREQ_MinDepth, 15,
  202.                                                     TAG_DONE));
  203. }
  204.  
  205.  
  206. unsigned long InitCyberGfxDisplay(unsigned long modeid)
  207. {
  208.     UWORD pens[] = { ~0 };
  209.  
  210.     atexit(output_term);
  211.  
  212.     if(init_cybergfx())
  213.     {
  214.         if(cyber_pub || (modeid != 0xffffffff) || (modeid = get_cyber_mode()) != 0xffffffff)
  215.         {
  216.             if(cyber_pub)
  217.             {
  218.                 cyber_screen = LockPubScreen(pubname_ptr);
  219.                 modeid = GetVPModeID(&cyber_screen->ViewPort);
  220.                 ScreenToFront(cyber_screen);
  221.                 UnlockPubScreen(NULL, cyber_screen);
  222.             }
  223.  
  224.             cyber_mode = modeid;
  225.             depth = GetCyberIDAttr(CYBRIDATTR_DEPTH, cyber_mode);
  226.             if(depth < 15 && ditherType != CYBERGFXVLAYER_DITHER && ditherType != CYBERGFXVLAYERGRAY_DITHER)
  227.                 Quit("aMiPEG: this screen is not deep enough (needed at least 15 bits).", 25);
  228.  
  229.             if(cyber_screen || (cyber_screen = OpenScreenTags(NULL,
  230.                             SA_Width, STDSCREENWIDTH,
  231.                             SA_Height, STDSCREENHEIGHT,
  232.                             SA_DisplayID, cyber_mode,
  233.                             SA_Title, (UBYTE *)"aMiPEG 1.0 by Michael Rausch and Miloslaw Smyk",
  234.                             SA_Pens, &pens,
  235.                             SA_SysFont, 1,
  236.                             SA_Depth, depth,
  237.                             TAG_DONE)))
  238.             {
  239.  
  240.  
  241.                 if(cyber_window = OpenWindowTags(NULL,
  242.                                     cyber_pub ? WA_PubScreenName : WA_CustomScreen, cyber_pub ? pubname_ptr : cyber_screen,
  243.                                     WA_Flags, WFLG_ACTIVATE | (fullFlag ? WFLG_BORDERLESS : WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM),
  244.                     WA_IDCMP, CLOSEWINDOW | MOUSEBUTTONS | VANILLAKEY,
  245.                     (fullFlag ? TAG_SKIP : TAG_IGNORE), 8,
  246.                     WA_InnerWidth, 160,
  247.                     WA_InnerHeight,120,
  248.                     WA_MaxWidth, ~0,
  249.                     WA_MaxHeight, ~0,
  250.                     WA_MinWidth, 60,
  251.                     WA_MinHeight, 60,
  252.                     WA_Title, "Playing MPEG animation",
  253.                     WA_ScreenTitle, (UBYTE *)"aMiPEG 1.0 by Michael Rausch and Miloslaw Smyk",
  254.                     TAG_DONE))
  255.  
  256.                     return(modeid);
  257.             }
  258.         }
  259.     }
  260.     Quit("aMiPEG: unable to open screen/window...", 25);
  261. }
  262.