home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / screen / shdwmstr.lha / source / savers / stars.c < prev    next >
C/C++ Source or Header  |  1991-12-03  |  5KB  |  164 lines

  1. /*
  2.  * Leo Schwab's stars hack, turned into a screen saver module.
  3.  *
  4.  * Copyright 1991, Mike Meyer
  5.  * All Rights Reserved
  6.  *
  7.  * See the file "ShadowMaster:Distribution" for information on distribution.
  8.  *
  9.  * ===build instructions
  10.  * % lc stars ; output= stars.o input= stars.c savermain.h
  11.  * % blink stars.o LIB lib:lcr.lib SC SD ND ; output= stars input=stars.o
  12.  * % copy stars //savers
  13.  * ===endbuild
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <intuition/intuition.h>
  18. #include <graphics/gfx.h>
  19. #include <dos/dos.h>
  20. #include <proto/dos.h>
  21. #include <proto/exec.h>
  22. #include <proto/graphics.h>
  23. #include <proto/intuition.h>
  24.  
  25. int rand(void) ;
  26. void srand(int) ;
  27. static void dographics(void) ;
  28.  
  29. struct IntuitionBase    *IntuitionBase = NULL ;
  30. struct GfxBase        *GfxBase = NULL ;
  31. struct ExecBase        *SysBase = NULL ;
  32. struct DosLibrary    *DOSBase = NULL ;
  33. struct Screen        *screen = NULL ;
  34. struct Window        *window = NULL ;
  35.  
  36. /* Don't change anything above this point... */
  37.  
  38. /*
  39.  * This is the initial color table for the blanker screen. The format of a
  40.  * ColorSpec is pen number, R, G, B. Add pens as required by your screen. You
  41.  * should really set all pens, so you avoid color flashes after opening.
  42.  * If you insist on doing it another way, add SA_ScreenBehind to the screen
  43.  * and do a ScreenToFront after setting the colormap, but before you start
  44.  * drawing.
  45.  *
  46.  * Don't forget to set the SA_Depth tag in ScreenTags...
  47.  */
  48. struct ColorSpec colorspec[] = {
  49.     {0, 0, 0, 0},
  50.     {1, 1, 1, 1},
  51.     {2, 2, 2, 2},
  52.     {3, 3, 3, 3},
  53.     {4, 4, 4, 4},
  54.     {5, 5, 5, 5},
  55.     {6, 6, 6, 6},
  56.     {7, 7, 7, 7},
  57.     {8, 8, 8, 8},
  58.     {9, 9, 9, 9},
  59.     {10, 10, 10, 10},
  60.     {11, 11, 11, 11},
  61.     {12, 12, 12, 12},
  62.     {13, 13, 13, 13},
  63.     {14, 14, 14, 14},
  64.     {15, 15, 15, 15},
  65.     { -1 } } ;
  66.  
  67. /*
  68.  * You must have a better name to use here, right?
  69.  */
  70. static char *Title = "Stars" ;
  71.  
  72. /*
  73.  * Screen open data. You'll probably want to change this to set your own
  74.  * depth and mode. I'd recommend leaving the overscan as is, but it's your
  75.  * graphics hack. Oh yes, feel free to give it your own title. 
  76.  */
  77. static struct TagItem    ScreenTags[] = {
  78.     {SA_Depth, 4},
  79.     {SA_Colors, &colorspec},
  80.     {SA_DisplayID, LORES_KEY},
  81.     {SA_Overscan, OSCAN_MAX},
  82.     {SA_Title, &Title},
  83.     {SA_ShowTitle, FALSE},
  84.     {SA_Quiet, TRUE},
  85.     {TAG_END, 0}
  86.     } ;
  87.  
  88. /*
  89.  * The window is for turning off the sprite, and that's about it. However,
  90.  * if you want clipped rendering (which means part of your graphics aren't
  91.  * going to be seen), you can use it's rastport. Until you're sure that's
  92.  * not going on, you probably want to do that anyway. After you trust your
  93.  * grahics code, render through the screen rastport to get extra speed.
  94.  *
  95.  * WARNING: WA_CustomScreen _MUST_ be the first entry!!!
  96.  */
  97. static struct TagItem WindowTags[] = {
  98.     {WA_CustomScreen, FALSE},
  99.     {WA_Activate, TRUE},
  100.     {WA_SimpleRefresh, TRUE},
  101.     {WA_Borderless, TRUE},
  102.     {TAG_END, 0}
  103.     };
  104.  
  105. #include "savermain.h"
  106.  
  107. /*
  108.  * Add whatever graphics you want here. Be sure and do a
  109.  * CheckSignal(SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C at regular intervals, as
  110.  * that's how you're told to unblank. When that evaluates to true, you should
  111.  * free everything you've allocated and return.
  112.  *
  113.  * Note that we seed the rand() number generator in _main, so that it can
  114.  * happen before we drop the task priority. For saver hacks, that random
  115.  * number generator should be good enough. If you need better, change that
  116.  * seeding. Otherwise, you can just use rand() knowing you'll get different
  117.  * sequences each time you get started. If you really don't want this, just
  118.  * delete the stuff in main.
  119.  *
  120.  * This example doesn't _do_ anything, so it uses a Wait instead of checking
  121.  * signals. We want to be a good citizen.
  122.  */
  123. #define NSTARS    128
  124. short x[NSTARS], y[NSTARS], z[NSTARS] ;
  125. short xo[NSTARS], yo[NSTARS] ;
  126.  
  127. void
  128. mkpoint(short i) {
  129.         x[i] = (rand() >> 8) % 256 - 128;
  130.         y[i] = (rand() >> 8) % 150 - 75;
  131.         z[i] = 255;
  132.     }
  133.  
  134. #define    magic    256
  135. #define    inc    3
  136. void
  137. dographics(void) {
  138.     long    xs, ys ;
  139.     int    i ;
  140.     struct RastPort *rp = &screen->RastPort ;
  141.  
  142.         for (i=0; i<NSTARS; i++)
  143.                 mkpoint (i);
  144.  
  145.         for (;;) {
  146.                 for (i=0; i<NSTARS; i++) {
  147.                         if ((z[i] -= inc) <= 0)
  148.                                 mkpoint (i);
  149.                         xs = x[i] * magic / z[i] + 160;
  150.                         ys = y[i] * magic / z[i] + 100;
  151.                         SetAPen (rp, 0L);
  152.                         WritePixel (rp, (long) xo[i], (long) yo[i]);
  153.                         if (xs < 0 || xs > screen->Width - 1 || ys < 0 || ys > screen->Height - 1)
  154.                                 mkpoint (i);
  155.                         else {
  156.                                 SetAPen (rp, (long) (256-z[i] >> 4));
  157.                                 WritePixel (rp, xs, ys);
  158.                                 xo[i] = xs;  yo[i] = ys;
  159.                             }
  160.                     }
  161.         if (CheckSignal(SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) return ;
  162.         }
  163.     }
  164.