home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / Libraries / Intuition / ScDemo / scdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  10.3 KB  |  470 lines

  1. /* Screen Demo Program     :ts=8 */
  2.  
  3. /* compile lc -L -iVINCLUDE: -iINCLATTICE: scdemo */
  4.  
  5. /*
  6. Copyright (c) 1989-1999 Amiga, Inc.
  7.  
  8. Executables based on this information may be used in software
  9. for Amiga computers. All other rights reserved.
  10. This information is provided "as is"; no warranties are made.
  11. All use is at your own risk, and no liability or responsibility
  12. is assumed.
  13. */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/lists.h>
  17. #include <exec/memory.h>
  18.  
  19. #include <graphics/displayinfo.h>
  20.  
  21. #include <utility/tagitem.h>
  22. #include <intuition/intuition.h>
  23.  
  24. /* prototypes */
  25. #include <clib/intuition_protos.h>
  26. #include <clib/graphics_protos.h>
  27. #include <clib/exec_protos.h>
  28.  
  29. #include <stdio.h>
  30.  
  31. struct  IntuitionBase   *IntuitionBase;
  32. struct  GfxBase         *GfxBase;
  33.  
  34. struct List    ScreenList;        /* keep track of open screens    */
  35.  
  36. struct ScreenNode    {
  37.     struct MinNode    Sn_Node;
  38.     struct Screen     *sn_Screen;
  39. };
  40.  
  41. struct Screen *addScreen();
  42. void cleanup();
  43. void freeScreens();
  44. void drawRulers();
  45.  
  46. char    kbuff[257];
  47.  
  48. void
  49. showHelp()
  50. {
  51.     printf("commands:\n");
  52.     printf("f - free screens        q - quit\n");
  53.     printf("l - lo-res              L - lo-res interlace\n");
  54.     printf("h - hi-res              H - hi-res interlace\n");
  55.     printf("p - productivity        P - productivity interlace\n");
  56.     printf("s - superhires          S - superhires interlace\n");
  57.     printf("o - std overcan hires   O - video overscan hires\n");
  58.     printf("a - A2024 10 Hz         A - A2024 15 Hz\n");
  59.     printf("w - wide (scrolling) hi res\n");
  60.     printf("z - VGA lores (70ns pixels)\n");
  61.     printf("$ - hex display mode specifier\n");
  62. }
  63.  
  64.  
  65. main()
  66. {
  67.     printf("SCDemo: accompanies V1.4 Beta 1.\n");
  68.  
  69.     NewList( &ScreenList );
  70.  
  71.     if (!(IntuitionBase =
  72.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  73.     { cleanup("no V36 intuition library\n"); }
  74.  
  75.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  76.     { cleanup("no V36 graphics library\n"); }
  77.  
  78.     FOREVER
  79.     {
  80.     printf("screen command (? for list): ");
  81.     if (!gets(kbuff)) cleanup("eof.\n");
  82.  
  83.     switch ( *kbuff )
  84.     {
  85.     case 'o':
  86.         addScreen( 0xf00, 0x555,
  87.             SA_DisplayID,    HIRES_KEY,
  88.         SA_Overscan,    OSCAN_STANDARD,
  89.         SA_Title,    " Hires Standard Overscan ",
  90.         TAG_END );
  91.         break;
  92.  
  93.     case 'O':
  94.         addScreen( 0xf00, 0x555,
  95.             SA_DisplayID,    HIRES_KEY,
  96.         SA_Overscan,    OSCAN_VIDEO,
  97.         SA_Title,    " Hires Video Overscan ",
  98.         TAG_END );
  99.         break;
  100.  
  101.     case 'a':
  102.         addScreen( 0xfff, 0x000,
  103.             SA_DisplayID,    A2024TENHERTZ_KEY,
  104.         SA_Title,    " A2024, 10 Hz ",
  105.         TAG_END );
  106.         break;
  107.  
  108.     case 'A':
  109.         addScreen( 0x000, 0xfff,
  110.             SA_DisplayID,    A2024FIFTEENHERTZ_KEY,
  111.         SA_Title,    " A2024, 15 Hz ",
  112.         TAG_END );
  113.         break;
  114.  
  115.     case 's':
  116.         addScreen( 0x005, 0xff0,
  117.             SA_DisplayID,    SUPER_KEY,
  118.         SA_Title,    " Super-Hires, Non-interlaced ",
  119.         TAG_END );
  120.         break;
  121.  
  122.     case 'S':
  123.         addScreen( 0x005, 0xff0,
  124.             SA_DisplayID,    SUPERLACE_KEY,
  125.         SA_Title,    " Super-Hires, Interlaced ",
  126.         TAG_END );
  127.         break;
  128.  
  129.     case 'p':
  130.         addScreen( 0xfff, 0x000,
  131.             SA_DisplayID,    VGAPRODUCT_KEY,
  132.         SA_Title,    " Productivity Mode ",
  133.         TAG_END );
  134.         break;
  135.  
  136.     case 'P':    /* productivity, lace    */
  137.         addScreen( 0x005, 0xf00,
  138.             SA_DisplayID,    VGAPRODUCTLACE_KEY,
  139.         SA_Title,    " Productivity, Interlaced ",
  140.         TAG_END );
  141.         break;
  142.  
  143.     case 'z':
  144.         addScreen( 0xfff, 0x000,
  145.             SA_DisplayID,    VGALORES_KEY,
  146.         SA_Title,    " 70ns pixels, Doublescan ",
  147.         TAG_END );
  148.         break;
  149.  
  150.     case 'h':
  151.         addScreen( 0x0f0, 0x555,
  152.             SA_DisplayID,    HIRES_KEY,
  153.         SA_Title,    " HiRes Text Overscan ",
  154.         TAG_END );
  155.         break;
  156.  
  157.     case 'H':
  158.         addScreen( 0xf00, 0x555,
  159.             SA_DisplayID,    HIRESLACE_KEY,
  160.         SA_Title,    " HiRes Interlaced ",
  161.         TAG_END );
  162.         break;
  163.  
  164.     case 'L':
  165.         addScreen( 0x0ff, 0x555,
  166.             SA_DisplayID,    LORESLACE_KEY,
  167.         SA_Title,    " LoRes Lace Screen ",
  168.         TAG_END );
  169.         break;
  170.  
  171.     case 'l':
  172.         addScreen( 0x00f, 0x555,
  173.             SA_DisplayID,    LORES_KEY,
  174.         SA_Title,    " LoRes Screen ",
  175.         TAG_END );
  176.         break;
  177.  
  178.      case 'w':
  179.         addScreen( 0xf0f, 0x555,
  180.             SA_DisplayID,    HIRES_KEY,
  181.         SA_Title,    " Wide Scrolling HiRes Screen ",
  182.         /* specify a DisplayClip region to scroll around
  183.          * in.  This is a reasonable region to use.
  184.          */
  185.         SA_Overscan,    OSCAN_TEXT,
  186.         SA_Width,    1100,
  187.         SA_AutoScroll,    TRUE,    /* not working in beta 1? */
  188.         TAG_END );
  189.         break;
  190.  
  191.     case 'f':
  192.         freeScreens();
  193.         break;
  194.  
  195.     case '?':
  196.         showHelp();
  197.         break;
  198.  
  199.     case 'q':
  200.     case 'Q':
  201.         cleanup("bye.\n");
  202.         break;
  203.     case '$':
  204.         {
  205.             long    scanmode;
  206.         int    scancount;
  207.  
  208.         /* skip '$' and get long hex value */
  209.         scancount = sscanf( kbuff + 1, "%lx", &scanmode );
  210.         if ( scancount == 1 )
  211.         {
  212.             sprintf( kbuff,"Direct Mode Specification: %lx",scanmode);
  213.             addScreen( 0x0ff, 0x555,
  214.                 SA_DisplayID,    scanmode,
  215.             TAG_END );
  216.             break;
  217.         }
  218.         else
  219.         {
  220.             printf("couldn't parse: type '$ <hex num>'\n");
  221.         }
  222.         }
  223.         break;
  224.     default:
  225.         printf("don't know (%lx)\n", *kbuff, *kbuff);
  226.     }
  227.     }
  228. }
  229.  
  230. void
  231. cleanup( str )
  232. char    *str;
  233. {
  234.     if (str) printf(str);
  235.  
  236.     freeScreens();
  237.  
  238.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  239.     if (GfxBase) CloseLibrary(GfxBase);
  240.  
  241.     exit (0);
  242.     return;
  243. }
  244.  
  245. /* free all screens    */
  246. void
  247. freeScreens()
  248. {
  249.     struct ScreenNode *sn;
  250.  
  251.     while (sn = (struct ScreenNode *) RemHead( &ScreenList ))
  252.     {
  253.     CloseWindow( sn->sn_Screen->FirstWindow );
  254.     printf("freeing screen at %lx.\n", sn->sn_Screen );
  255.     CloseScreen( sn->sn_Screen );
  256.     FreeMem( sn, (long) sizeof *sn );
  257.     }
  258.     return;
  259. }
  260.  
  261. char    *string = "Test String A B b CdEf m M n N o d D";
  262.  
  263. struct ColorSpec colorspec[] = {
  264.     { 0 },
  265.     { 1 },
  266.     {-1 }
  267. };
  268.  
  269. #define REDPART( color )    (( (color) >> 8 ) & 0xf )
  270. #define GREENPART( color )    (( (color) >> 4 ) & 0xf )
  271. #define BLUEPART( color )    (  (color) & 0xf )
  272.  
  273. struct Screen *
  274. addScreen( color0, color1, tags )
  275. int    color0, color1;
  276. ULONG    tags;
  277. {
  278.     struct ScreenNode *sn;
  279.     struct Screen    *OpenScreenTags();
  280.     struct Window    *OpenWindowTags();
  281.     struct Window    *window;
  282.     ULONG    errorcode;    /* for OpenScreen() errors    */
  283.  
  284.     if (!(sn = (struct ScreenNode *)
  285.           AllocMem((LONG)sizeof (struct ScreenNode), 0L )) )
  286.         return (NULL);
  287.  
  288.     colorspec[0].Red = REDPART( color0 );
  289.     colorspec[0].Green = GREENPART( color0 );
  290.     colorspec[0].Blue = BLUEPART( color0 );
  291.  
  292.     colorspec[1].Red = REDPART( color1 );
  293.     colorspec[1].Green = GREENPART( color1 );
  294.     colorspec[1].Blue = BLUEPART( color1 );
  295.  
  296.     sn->sn_Screen =  OpenScreenTags( NULL,
  297.                 SA_Colors,    colorspec,
  298.             SA_ErrorCode,    &errorcode,
  299.             TAG_MORE,    &tags,    /* jump to varargs list */
  300.             TAG_END );
  301.  
  302.     if ( sn->sn_Screen )
  303.     {
  304.     window = OpenWindowTags( NULL,
  305.         WA_CustomScreen,    sn->sn_Screen,
  306.         WA_Title,         " Window in Screen ",
  307.         TAG_END );
  308.  
  309.     printf("window at %lx\n", window );
  310.  
  311.         if ( window )    /* i.e., success */
  312.         {
  313.         printf("new screen at %lx.\n", sn->sn_Screen);
  314.         AddTail(&ScreenList, (struct Node *) sn);
  315.  
  316.         /* draw a little something at the bottom    */
  317.         drawRulers( sn->sn_Screen );
  318.         return (sn->sn_Screen);
  319.         }
  320.     else
  321.     {
  322.         printf( "couldn't get window for screen\n" );
  323.         CloseScreen( sn->sn_Screen );
  324.     }
  325.     }
  326.  
  327.     else
  328.     {
  329.     printf("can't open screen: ");
  330.         switch ( errorcode )
  331.     {
  332.     case OSERR_NOMONITOR:
  333.         printf("monitor not available.\n");
  334.         break;
  335.     case OSERR_NOCHIPS:
  336.         printf("new chipset not available.\n");
  337.         break;
  338.     case OSERR_NOMEM:
  339.         printf("memory not available.\n");
  340.         break;
  341.     case OSERR_NOCHIPMEM:
  342.         printf("chip memory not available.\n");
  343.         break;
  344.     case OSERR_PUBNOTUNIQUE:
  345.         printf("public screen already open.\n");
  346.         break;
  347.     case OSERR_UNKNOWNMODE:
  348.         printf("mode ID is unknown.\n");
  349.         break;
  350.     default:
  351.         printf("unknown error %ld\n", errorcode );
  352.     }
  353.     }
  354.     FreeMem(sn, (LONG) sizeof (struct ScreenNode));
  355.     return (NULL);
  356. }
  357.  
  358. #define HRULEY        (90L)
  359. #define VRULEX        (250L)
  360. #define VRULESTART    (1)            /* start VRULE at 200    */
  361.  
  362. #define HASHHEIGHT    (20L)        /* size of hash marks    */
  363. #define HASHWIDTH    (20L)
  364.  
  365. /*
  366.  * draw rulers in screen
  367.  */
  368. UBYTE    posbuff[ 81 ];
  369.  
  370. void
  371. drawRulers( sc )
  372. struct Screen *sc;
  373. {
  374. #if 0
  375.     struct RastPort *rp = sc->FirstWindow->RPort;
  376. #else
  377.     struct RastPort *rp = &sc->RastPort;
  378. #endif
  379.     int        hunnert;
  380.     int        pos;
  381.     int        endpos;
  382.     int        tlength;
  383.     int        width = sc->Width;
  384.     int        height = sc->Height;
  385.  
  386.     /* report total dimensions    */
  387.     sprintf( posbuff, "Left %d, Top %d, Width %d, Height %d.",
  388.         sc->LeftEdge, sc->TopEdge, sc->Width, sc->Height );
  389.     SetAPen( rp, 1L );
  390.     Move( rp, 10L, (LONG) HRULEY - 50 );
  391.     Text( rp, posbuff, (LONG) strlen( posbuff ) );
  392.  
  393.     /* draw horizontal counters and hashmarks */
  394.     SetAPen( rp, 1L );
  395.     Move( rp, 0L, (LONG) HRULEY + HASHHEIGHT / 2 );
  396.     Draw( rp, (LONG) width - 1, (LONG) HRULEY + HASHHEIGHT / 2 );
  397.  
  398.     for (hunnert = 0; (hunnert * 100) <= width; ++hunnert)
  399.     {
  400.     /* tall hashmark at X00    */
  401.     pos = hunnert * 100;
  402.     endpos = pos + 100;
  403.     SetAPen( rp, 3L );
  404.     Move( rp, (LONG) pos, HRULEY );
  405.     Draw( rp, (LONG) pos, HRULEY  + 2 * HASHHEIGHT);
  406.  
  407.     while (  (pos += 10) < endpos )
  408.     {
  409.         if ( pos > (width - 1) ) goto BREAK1;
  410.         Move( rp, (LONG) pos, HRULEY );
  411.         Draw( rp, (LONG) pos, HRULEY  + HASHHEIGHT);
  412.     }
  413.  
  414.     sprintf( posbuff, "%d", endpos);
  415.     tlength = TextLength( rp, posbuff, (LONG) strlen( posbuff ) );
  416.     SetAPen( rp, 7L );
  417.     Move( rp, (LONG) endpos - tlength - 1,
  418.     HRULEY  + 2 * HASHHEIGHT);
  419.     Text( rp, posbuff, (LONG) strlen( posbuff ) );
  420.     }
  421. BREAK1:
  422.  
  423.     /* draw vertical counters */
  424.     Move( rp, VRULEX + HASHWIDTH / 2, (LONG) VRULESTART * 100 );
  425.     Draw( rp, VRULEX + HASHWIDTH / 2, (LONG) height - 1 );
  426.  
  427.     for (hunnert = VRULESTART;
  428.     ((hunnert * 100)) <= height; ++hunnert)
  429.     {
  430.     /* wide hashmark at X00    */
  431.     pos = hunnert * 100;
  432.     endpos = pos + 100;
  433.     Move( rp, VRULEX, (LONG) pos);
  434.     Draw( rp,  VRULEX  + 2 * HASHWIDTH, (LONG) pos);
  435.  
  436.     while (  (pos += 10) < endpos )
  437.     {
  438.         if ( pos > (height - 1) ) goto BREAK2;
  439.         Move( rp, VRULEX, (LONG) pos);
  440.         Draw( rp,  VRULEX  + HASHWIDTH, (LONG) pos);
  441.     }
  442.  
  443.     sprintf( posbuff, "%d", endpos);
  444.     tlength = TextLength( rp, posbuff, (LONG) strlen( posbuff ) );
  445.     Move( rp, VRULEX - tlength - 1, (LONG) endpos - 1);
  446.     Text( rp, posbuff, (LONG) strlen( posbuff ) );
  447.     }
  448. BREAK2:
  449.     return;
  450. }
  451.  
  452. struct Window    *
  453. OpenWindowTags( nw,tags)
  454. struct NewWindow    *nw;
  455. ULONG            tags;
  456. {
  457.     struct Window    *OpenWindowTagList();
  458.  
  459.     return ( OpenWindowTagList( nw, &tags ) );
  460. }
  461. struct Screen    *
  462. OpenScreenTags( ns, tag1, data1 )
  463. struct NewScreen    *ns;
  464. ULONG            tag1;
  465. {
  466.     struct Screen    *OpenScreenTagList();
  467.  
  468.     return ( OpenScreenTagList( ns, &tag1 ) );
  469. }
  470.