home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / CDRom / PHOTOC12.LZX / src / photocdaga / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-18  |  9.0 KB  |  398 lines

  1. /* Screen display routines              */
  2. /* written by Günther Röhrich           */
  3. /* this source is Public Domain         */
  4.  
  5. /* this works only with OS 3.0 or higher */
  6.  
  7. #include <stdio.h>
  8.  
  9. #define INTUI_V36_NAMES_ONLY
  10.  
  11. #include <exec/types.h>
  12. #include <intuition/intuition.h>
  13. #include <intuition/screens.h>
  14. #include <graphics/modeid.h>
  15.  
  16.  
  17. #ifndef __GNUC__
  18. #include <pragmas/intuition_pragmas.h>
  19. #include <pragmas/graphics_pragmas.h>
  20. #include <pragmas/exec_pragmas.h>
  21. #include <clib/intuition_protos.h>
  22. #include <clib/graphics_protos.h>
  23. #include <clib/exec_protos.h>
  24. #else
  25. #include <inline/graphics.h>  
  26. #include <inline/intuition.h> 
  27. #include <inline/exec.h>
  28. #endif
  29.  
  30. #define HAM8 1
  31.  
  32. #pragma regcall(newWPL8(d2,a2))
  33. #pragma regcall(newWPL8Pos(a0,d0,d1,d2,a2))
  34.  
  35. #ifdef __GNUC__
  36. extern __inline void
  37. newWPL8(unsigned long width, UBYTE *array)
  38. {
  39.   register unsigned long d2 __asm("d2") = width;
  40.   register UBYTE *a2 __asm("a2") = array;
  41.   __asm __volatile ("jsr _newWPL8"
  42.   : /* no output */
  43.   : "r" (d2), "r" (a2)
  44.   : "a0","a1","a2","d0","d1","d2", "memory");
  45. }
  46. extern __inline void
  47. newWPL8Pos(struct RastPort *rp, unsigned long xstart, unsigned long ystart,
  48.         unsigned long width, UBYTE *array)
  49. {
  50.   register struct RastPort *a0 __asm("a0") = rp;
  51.   register unsigned long d0 __asm("d0") = xstart;
  52.   register unsigned long d1 __asm("d1") = ystart;
  53.   register unsigned long d2 __asm("d2") = width;
  54.   register UBYTE *a2 __asm("a2") = array;
  55.   __asm __volatile ("jsr _newWPL8Pos"
  56.   : /* no output */
  57.   : "r" (a0), "r" (d0), "r" (d1), "r" (d2), "r" (a2)
  58.   : "a0","a1","a2","d0","d1","d2", "memory");
  59. }
  60. #endif
  61.  
  62. struct IntuitionBase *IntuitionBase = NULL;
  63. struct GfxBase *GfxBase = NULL;
  64. extern int VGAenable; /* indicates if user wants to have VGA mode */
  65. extern int DisplayOriginal;
  66. extern int OverView; /* indicates if Overview */
  67. struct BitMap *WPBitMap = NULL;
  68. struct BitPlane *WPPlane = NULL;
  69. static void error_exit(const char *msgtext);
  70. struct BitMap *my_bitmap = NULL;
  71. struct Screen *my_screen = NULL;
  72. struct Window *my_window = NULL;
  73. struct RastPort temprp; /* a copy of the screen's RastPort */
  74. int Drow = 0; 
  75. extern ULONG *ScreenColorTable;
  76.  
  77. /* for newWPL8() */
  78. PLANEPTR PlanePos[8];
  79. UBYTE    *linebuf = NULL;
  80.  
  81. struct TextAttr MyTextAttr = {
  82. "topaz.font", 8, 0, FPF_ROMFONT};
  83.  
  84. struct TagItem MyScreenTags[] = {
  85.  
  86. { SA_Width,     (ULONG)0 },
  87. { SA_Height,    (ULONG)0 },
  88. { SA_Depth,     (ULONG)0 },
  89. { SA_DisplayID, (ULONG)0 },
  90. { SA_Colors32,  (ULONG)NULL},
  91. { SA_BitMap,    (ULONG)NULL},
  92. { SA_Font,      &MyTextAttr},
  93. { SA_Type,      (ULONG)CUSTOMSCREEN|CUSTOMBITMAP },
  94. { SA_Quiet,     (ULONG)TRUE },
  95. { SA_AutoScroll,(ULONG)TRUE },
  96. { SA_Overscan,  (ULONG)OSCAN_STANDARD },
  97. { TAG_DONE,     (ULONG)TRUE }  };
  98.  
  99. struct TagItem MyWindowTags[] = {
  100.  
  101. { WA_Width,        (ULONG)0 },
  102. { WA_Height,       (ULONG)0 },
  103. { WA_CustomScreen, (ULONG)NULL },
  104. { WA_Activate,     (ULONG)TRUE },
  105. { WA_Left,         (ULONG)0 },
  106. { WA_Top,          (ULONG)0 },
  107. { WA_NoCareRefresh,(ULONG)TRUE },
  108. { WA_Borderless,   (ULONG)TRUE },
  109. { WA_Backdrop,     (ULONG)TRUE },
  110. { WA_RMBTrap,      (ULONG)TRUE }, /* disable screen menu drawing */
  111. { WA_IDCMP,        (ULONG)IDCMP_MOUSEBUTTONS | IDCMP_VANILLAKEY },
  112. { WA_BusyPointer,  (ULONG)TRUE }, /* V39 only! */
  113. { TAG_DONE,        (ULONG)TRUE } };
  114.  
  115. void CloseDisplay(void);
  116.  
  117. /* Initialize the display, colors will be set later */
  118.  
  119. int InitDisplay(int cols, int rows, ULONG Mode, int NumPlanes)
  120. {
  121.   ULONG Depth, DisplayID;
  122.  
  123.   if(!(IntuitionBase = OpenLibrary((UBYTE *)"intuition.library", 39)))
  124.    error_exit("Can't open intuition.library V39 or higher");
  125.  
  126.   if(!(GfxBase = OpenLibrary((UBYTE *)"graphics.library",39)))
  127.    error_exit("Can't open graphics.library V39 or higher");
  128.  
  129.   
  130.   /* Calculate a DisplayID */
  131.   /* this should be done better... */
  132.  
  133.  
  134.   if(VGAenable)
  135.   {
  136.     if(Mode == HAM8)
  137.     {
  138.       if(cols > 384 || rows > 384)
  139.         DisplayID = VGAPRODUCTHAM_KEY;
  140.       else
  141.         DisplayID = VGALORESHAMDBL_KEY;
  142.     }
  143.     else
  144.     {
  145.       if(cols > 384 || rows >384) 
  146.         DisplayID = VGAPRODUCT_KEY;
  147.       else
  148.         DisplayID = VGALORESDBL_KEY;        
  149.     }
  150.   }
  151.   else
  152.   {  
  153.     if(Mode == HAM8)
  154.     {
  155.       if(cols > 384 || rows > 384)
  156.         DisplayID = HIRESHAMLACE_KEY;
  157.       else
  158.         DisplayID = HAM_KEY;
  159.     }
  160.     else
  161.     {
  162.       if(cols > 384 || rows >384) 
  163.         DisplayID = HIRESLACE_KEY;
  164.       else
  165.         DisplayID = LORES_KEY; 
  166.     }   
  167.   }
  168.  
  169.   if(Mode == HAM8) Depth = 8;
  170.     else Depth = NumPlanes;
  171.  
  172.   MyScreenTags[0].ti_Data = (ULONG)cols;
  173.   MyScreenTags[1].ti_Data = (ULONG)rows;
  174.   MyScreenTags[2].ti_Data = (ULONG)Depth;
  175.   MyScreenTags[3].ti_Data = (ULONG)DisplayID;
  176.   MyScreenTags[4].ti_Data = (ULONG)((void *)ScreenColorTable);
  177.  
  178.   MyWindowTags[0].ti_Data = (ULONG)cols;
  179.   MyWindowTags[1].ti_Data = (ULONG)rows;
  180.  
  181.   my_bitmap = AllocBitMap(((cols+31)>>5)<<5,rows,Depth,BMF_CLEAR|BMF_DISPLAYABLE,NULL);
  182.  
  183.   if(my_bitmap == NULL) return 0;
  184.  
  185.   MyScreenTags[5].ti_Data = (ULONG)my_bitmap;
  186.   
  187.   my_screen = OpenScreenTagList(NULL, (struct TagItem *)&MyScreenTags);
  188.  
  189.   if(my_screen == NULL) return 0;
  190.     
  191.   /* open a dummy window to allow autoscroll feature      */
  192.   
  193.   MyWindowTags[2].ti_Data = (ULONG)((void *)my_screen);
  194.   my_window = OpenWindowTagList(NULL, (struct TagItem *)&MyWindowTags);
  195.  
  196.   if(my_window == NULL) return 0;
  197.  
  198.   temprp.BitMap = NULL;
  199.  
  200.   if(DisplayOriginal)
  201.   {
  202.     /* initialize temprp for use with WritePixelLine8() */
  203.  
  204.     CopyMem(&my_screen->RastPort, &temprp, sizeof(struct RastPort));
  205.     temprp.Layer = NULL;
  206.     /* V39 function */
  207.     temprp.BitMap = AllocBitMap(cols, 1, my_screen->RastPort.BitMap->Depth, 0, my_screen->RastPort.BitMap);
  208.  
  209.     if(temprp.BitMap == NULL) return 0;
  210.   }
  211.   else
  212.   {
  213.     /* Allocate line buffer */
  214.     linebuf = AllocVec((ULONG)cols+32L, 0);
  215.     if(linebuf == NULL) return 0;
  216.   
  217.     /* initialize WPBitMap for use with newWPL8Pos */
  218.     if(OverView)
  219.     {
  220.       WPBitMap = AllocBitMap(2048, 1, 8, BMF_DISPLAYABLE|BMF_INTERLEAVED, NULL);
  221.       if(WPBitMap == NULL) return 0;
  222.       WPPlane = WPBitMap->Planes[0];
  223.     }
  224.   }
  225.  
  226.   /* set pens and drawing modes */
  227.  
  228.   if(Depth == 8)
  229.   {
  230.     SetAPen(&my_screen->RastPort, 255);
  231.     SetDrMd(&my_screen->RastPort, JAM1);
  232.   }
  233.  
  234.  
  235.   return 1; /* success */  
  236. }
  237.  
  238.  
  239. /* Close the display */
  240.  
  241. void CloseDisplay(void)
  242. {
  243.   if(my_screen) ScreenToBack(my_screen);
  244.  
  245.   if(temprp.BitMap)
  246.   {
  247.     /* V39 function */
  248.     FreeBitMap(temprp.BitMap);
  249.     temprp.BitMap = NULL;
  250.   }
  251.  
  252.   if(my_window)
  253.   {
  254.     CloseWindow(my_window);
  255.     my_window = NULL;
  256.   }
  257.   if(my_screen)
  258.   {
  259.     CloseScreen(my_screen);
  260.     my_screen = NULL;
  261.   }
  262.   if(my_bitmap)
  263.   {
  264.     FreeBitMap(my_bitmap);
  265.     my_bitmap = NULL;
  266.   }
  267.   if(linebuf)
  268.   {
  269.     FreeVec(linebuf);
  270.     linebuf = NULL;
  271.   }
  272.   if(WPBitMap)
  273.   {
  274.     FreeBitMap(WPBitMap);
  275.     WPBitMap = NULL;
  276.   }
  277.   if(IntuitionBase)
  278.   {
  279.    CloseLibrary(IntuitionBase);
  280.    IntuitionBase = NULL;
  281.   }
  282.   if(GfxBase)
  283.   {
  284.     CloseLibrary(GfxBase);
  285.     GfxBase = NULL;
  286.   }
  287. }
  288.  
  289.  
  290. /* display a line of chunky pixel graphics... */
  291.  
  292.  
  293. void DisplayRow(char *array, int cols)
  294. {
  295.   if(my_screen)
  296.   {
  297.     if(DisplayOriginal)
  298.     {
  299.       WritePixelLine8(&my_screen->RastPort, 0, Drow, cols, array, &temprp);
  300.     }
  301.     else
  302.     {   
  303.       int j;
  304.       struct RastPort *MyRastPtr;
  305.       MyRastPtr = &my_screen->RastPort;
  306.       for(j=0; j<8; j++)
  307.         PlanePos[j]=MyRastPtr->BitMap->Planes[j] + MyRastPtr->BitMap->BytesPerRow*Drow;
  308.       newWPL8(cols, (UBYTE *)array);
  309.     }
  310.     Drow++;
  311.   }
  312. }
  313.  
  314. void DisplayRowPos(char *array, int cols, int xpos, int ypos)
  315. {
  316.   if(my_screen)
  317.   {
  318.     if(DisplayOriginal)
  319.     {
  320.       WritePixelLine8(&my_screen->RastPort, (ULONG)xpos, (ULONG)ypos, cols, array, &temprp);
  321.     }
  322.     else
  323.     {
  324.       newWPL8Pos(&my_screen->RastPort, (ULONG)xpos, (ULONG)ypos, cols, (UBYTE *)array); 
  325.     }
  326.   }
  327. }
  328.  
  329.  
  330. void DisplayNrGray(int nr, int xpos, int ypos)
  331. {
  332.   if(my_screen)
  333.   {
  334.     char String[10];
  335.     sprintf(String, "%d", nr);
  336.     Move(&my_screen->RastPort, (SHORT)xpos, (SHORT)ypos);
  337.     Text(&my_screen->RastPort, String, strlen(String));
  338.   }
  339.  
  340. /* check for a right mouse button press */
  341.  
  342. int CheckButton(void)
  343. {
  344.   struct IntuiMessage *msg;
  345.   int Button = 0;
  346.  
  347.   if(my_window)
  348.   {
  349.     while(msg = (struct IntuiMessage *)GetMsg(my_window->UserPort))
  350.     {
  351.       if(msg->Class == IDCMP_MOUSEBUTTONS)
  352.         if(msg->Code == MENUDOWN)
  353.           Button = 1; 
  354.       ReplyMsg((struct Message *)msg);
  355.       if(Button) break;
  356.     }
  357.   }
  358.   return Button;
  359. }
  360.        
  361. /* final wait after the picture is finished */
  362.  
  363. void FinalWait(void)
  364. {
  365.   struct IntuiMessage *msg;
  366.   int Button = 0;
  367.   struct TagItem WinPointerSet[] = { {WA_Pointer, (ULONG)NULL}, {TAG_DONE, (ULONG)NULL} };
  368.  
  369.   /* set the normal pointer */
  370.   /* V39 function */
  371.  
  372.   /* SetWindowPointer(my_window, WA_Pointer, NULL, TAG_DONE); */
  373.   SetWindowPointerA(my_window, WinPointerSet);
  374.   
  375.   if(my_window)
  376.   {
  377.     while(!Button)
  378.     {
  379.       Wait(1L<<my_window->UserPort->mp_SigBit);
  380.       while(msg = (struct IntuiMessage *)GetMsg(my_window->UserPort))
  381.       {
  382.         if(msg->Class == IDCMP_MOUSEBUTTONS)
  383.           if(msg->Code == MENUDOWN) Button = 1; 
  384.         ReplyMsg((struct Message *)msg);
  385.         if(Button) break;
  386.       }
  387.     }
  388.   }
  389. }
  390.       
  391. static void error_exit(const char *msgtext)
  392. {
  393.  printf("%s", msgtext);
  394.  CloseDisplay();
  395.  exit(10);
  396. }
  397.