[net.sources.mac] cubes source

roland@sicsten.UUCP (Roland Karlsson) (07/31/86)

This is a shar file of the source code in SuMacC for a graphics demo. It is
called cubes and is a new and better version of cube. You can also see
it as an "my new address" message.

If you delete appropriate lines you can use this source as a minimal demo
scel. It contains initialisations necessary and a quit menu. As usual i dont
set any restrictions how you use this program. Steal as much as you want.

A seperate posting contains the hex code.

		Roland Karlsson

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	cubes.rc
#	cubes.h
#	cubes.c
# This archive created: Wed Jul 30 09:55:43 1986
export PATH; PATH=/bin:$PATH
if test -f 'cubes.rc'
then
	echo shar: will not over-write existing file "'cubes.rc'"
else
cat << \SHAR_EOF > 'cubes.rc'
cubes.rsrc
*
Type CubE = STR
  ,0
New Spinning Cube by Roland Karlsson 1986.

Type WIND
  ,128
  Spinning Cube by Roland Karlsson
  40 32 320 480
  Visible noGoAway
  0
  0

Type MENU
  ,256(0)
  File
    Quit<B

Type CODE
  b.out,0

Type FREF = HEXA
  ,128(0)
* APPL 0
4150504c 000000ff 

Type BNDL = HEXA
  ,128(0)
* CubE 0
*   ICN#
*     0 128
*   FREF
*     0 128
43756245 00000001 49434e23 00000000 00804652 45460000 00000080

Type ICN# = HEXA
  ,128(0)
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 01fff000 02003000 04005000 08009000 1fff1000 10011000
10011000 10011000 10011000 10011000 10011000 10011000 10011000 10012000
10014000 10018000 1fff0000 00000000 00000000 00000000 00000000 00000000
*
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 01fff000 03fff000 07fff000 0ffff000 1ffff000 1ffff000
1ffff000 1ffff000 1ffff000 1ffff000 1ffff000 1ffff000 1ffff000 1fffe000
1fffc000 1fff8000 1fff0000 00000000 00000000 00000000 00000000 00000000

Type SIZE = HEXA
  ,-1(0)
* 348 K (57000 HEX) memory needed
80000005 70000005 7000

SHAR_EOF
fi # end of overwriting check
if test -f 'cubes.h'
then
	echo shar: will not over-write existing file "'cubes.h'"
else
cat << \SHAR_EOF > 'cubes.h'
#define WINDOW 128
#define m2PI &255

#define FILE 256
MenuHandle quit_menu;
EventRecord event;

Rect show_r1, show_r2, show_r3, text_r;
BitMap picture_bits;
Ptr work_ptr[128];
WindowPtr show_w, dummy_w;
WindowRecord w_record;
int done_flag = 0;

/* PI/2 =  64 PI = 128 3PI/2 = 196 2PI = 256
   PI/4 = 32 3PI/4 = 96 5PI/4 = 160 7PI/4 = 224
*/

/* 64 *  sin(i * 2pi / 256) */
int sinus[256] = {
       0,    2,    3,    5,    6,    8,    9,   11,   12,   14, 
      16,   17,   19,   20,   22,   23,   24,   26,   27,   29, 
      30,   32,   33,   34,   36,   37,   38,   39,   41,   42, 
      43,   44,   45,   46,   47,   48,   49,   50,   51,   52, 
      53,   54,   55,   56,   56,   57,   58,   59,   59,   60, 
      60,   61,   61,   62,   62,   62,   63,   63,   63,   64, 
      64,   64,   64,   64,   64,   64,   64,   64,   64,   64, 
      63,   63,   63,   62,   62,   62,   61,   61,   60,   60, 
      59,   59,   58,   57,   56,   56,   55,   54,   53,   52, 
      51,   50,   49,   48,   47,   46,   45,   44,   43,   42, 
      41,   39,   38,   37,   36,   34,   33,   32,   30,   29, 
      27,   26,   24,   23,   22,   20,   19,   17,   16,   14, 
      12,   11,    9,    8,    6,    5,    3,    2,    0,   -2, 
      -3,   -5,   -6,   -8,   -9,  -11,  -12,  -14,  -16,  -17, 
     -19,  -20,  -22,  -23,  -24,  -26,  -27,  -29,  -30,  -32, 
     -33,  -34,  -36,  -37,  -38,  -39,  -41,  -42,  -43,  -44, 
     -45,  -46,  -47,  -48,  -49,  -50,  -51,  -52,  -53,  -54, 
     -55,  -56,  -56,  -57,  -58,  -59,  -59,  -60,  -60,  -61, 
     -61,  -62,  -62,  -62,  -63,  -63,  -63,  -64,  -64,  -64, 
     -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -63,  -63, 
     -63,  -62,  -62,  -62,  -61,  -61,  -60,  -60,  -59,  -59, 
     -58,  -57,  -56,  -56,  -55,  -54,  -53,  -52,  -51,  -50, 
     -49,  -48,  -47,  -46,  -45,  -44,  -43,  -42,  -41,  -39, 
     -38,  -37,  -36,  -34,  -33,  -32,  -30,  -29,  -27,  -26, 
     -24,  -23,  -22,  -20,  -19,  -17,  -16,  -14,  -12,  -11, 
      -9,   -8,   -6,   -5,   -3,   -2
};

int b, c, fi1, fi2, i;
int p0h, p0v, p1h, p1v, p2h, p2v, p3h, p3v;
SHAR_EOF
fi # end of overwriting check
if test -f 'cubes.c'
then
	echo shar: will not over-write existing file "'cubes.c'"
else
cat << \SHAR_EOF > 'cubes.c'
#include <mac/quickdraw.h>
#include <mac/toolintf.h>
#include <mac/osintf.h>
#include "cubes.h"

main() {
  struct QDVar QDVar;

  QD = &QDVar;

  InitGraf(&thePort);
  InitFonts();
  FlushEvents(everyEvent, 0);

  InitWindows();
  show_w = GetNewWindow(WINDOW, &w_record, (WindowPtr) 0);
  SetRect(&show_r1, 32, 70, 192, 200);
  SetRect(&show_r2,272, 10, 432, 140);
  SetRect(&show_r3,224,140, 384, 270);
  SetRect(&text_r,   0, 10, 280, 100);
  TEInit();
  InitMenus();
  InitDialogs((ProcPtr) 0 );
  InitCursor();
  quit_menu = GetMenu(FILE);
  InsertMenu(quit_menu, 0);
  DisableItem(quit_menu, 0);
  DrawMenuBar();

  SetPort(show_w);
  TextBox("Swedish Institute of Computer Science\r\
	   Isafjordsgatan 11 / Box 1263\r\
	   S-163 13 Kista (Stockholm), SWEDEN\r\
	   roland@sics (..enea!sics!roland)\r",
	   146, &text_r, teJustCenter);
  MoveTo(40, 260); DrawString("Push button to exit");

  /* Make a copy of the port bit map parameters */
  picture_bits.baseAddr = show_w->portBits.baseAddr;
  picture_bits.rowBytes = show_w->portBits.rowBytes;
  picture_bits.bounds.top = show_w->portBits.bounds.top;
  picture_bits.bounds.left = show_w->portBits.bounds.left;
  picture_bits.bounds.bottom = show_w->portBits.bounds.bottom;
  picture_bits.bounds.right = show_w->portBits.bounds.right;

  /* Change size of bit map */
  show_w->portBits.rowBytes = 20;
  SetRect(&show_w->portBits.bounds, 0, 0, 160, 130);

  done_flag = fi1 = fi2 = 0;
  for(i = 0; i < 128; i++){

    fi1 = (fi1 + 8) m2PI;
    fi2 = (fi2 + 2) m2PI;

    /* Create pointer to new bits */
    show_w->portBits.baseAddr = work_ptr[i] = NewPtr(2600);

    b = sinus[fi2];
    c = (sinus[( 64 - fi2) m2PI] * 91) / 128; /* 91/128 = sqrt(2) */

    /* Compute corners of cube */
    p0v = 66 +  sinus[fi1];
    p0h = 80 +  (b * sinus[( 64 - fi1) m2PI]) / 64;

    p1v = 66 +  sinus[(64 + fi1) m2PI];
    p1h = 80 +  (b * sinus[( - fi1) m2PI]) / 64;

    p2v = 66 +  sinus[(128 + fi1) m2PI];
    p2h = 80 +  (b * sinus[(- 64 - fi1) m2PI]) / 64;

    p3v = 66 +  sinus[(192 + fi1) m2PI];
    p3h = 80 +  (b * sinus[(- 128 - fi1) m2PI]) / 64;

    EraseRect(&show_w->portBits.bounds);

    /* Draw lines that is not hidden */
    if( ! ( ((((fi2 - 192)m2PI) < 64) && (((fi1 -  32)m2PI) < 128)) ||
	    ((((fi2 - 128)m2PI) < 64) && (((fi1 - 160)m2PI) < 128)) ) ){
      MoveTo(p0h - c, p0v); LineTo(p1h - c, p1v);
    }
    if( ! ( ((((fi2 - 192)m2PI) < 64) && (((fi1 - 224)m2PI) < 128)) ||
	    ((((fi2 - 128)m2PI) < 64) && (((fi1 -  96)m2PI) < 128)) ) ){
      MoveTo(p1h - c, p1v); LineTo(p2h - c, p2v);
    }
    if( ! ( ((((fi2 - 192)m2PI) < 64) && (((fi1 - 160)m2PI) < 128)) ||
	    ((((fi2 - 128)m2PI) < 64) && (((fi1 -  32)m2PI) < 128)) ) ){
      MoveTo(p2h - c, p2v); LineTo(p3h - c, p3v);
    }
    if( ! ( ((((fi2 - 192)m2PI) < 64) && (((fi1 -  96)m2PI) < 128)) ||
	    ((((fi2 - 128)m2PI) < 64) && (((fi1 - 224)m2PI) < 128)) ) ){
      MoveTo(p3h - c, p3v); LineTo(p0h - c, p0v);
    }

    if( ! ( ((((fi2      )m2PI) < 64) && (((fi1 -  32)m2PI) < 128)) ||
	    ((((fi2 -  64)m2PI) < 64) && (((fi1 - 160)m2PI) < 128)) ) ){
      MoveTo(p0h + c, p0v); LineTo(p1h + c, p1v);
    }
    if( ! ( ((((fi2      )m2PI) < 64) && (((fi1 - 224)m2PI) < 128)) ||
	    ((((fi2 -  64)m2PI) < 64) && (((fi1 -  96)m2PI) < 128)) ) ){
      MoveTo(p1h + c, p1v); LineTo(p2h + c, p2v);
    }
    if( ! ( ((((fi2    )m2PI) < 64) && (((fi1 - 160)m2PI) < 128)) ||
	  ((((fi2 -  64)m2PI) < 64) && (((fi1 -  32)m2PI) < 128)) ) ){
      MoveTo(p2h + c, p2v); LineTo(p3h + c, p3v);
    }
    if( ! ( ((((fi2      )m2PI) < 64) && (((fi1 -  96)m2PI) < 128)) ||
	    ((((fi2 -  64)m2PI) < 64) && (((fi1 - 224)m2PI) < 128)) ) ){
      MoveTo(p3h + c, p3v); LineTo(p0h + c, p0v);
    }

    if( ! ( ((((fi2 -  64)m2PI) < 128) && (((fi1 - 224)m2PI) < 64)) ||
	    ((((fi2 - 192)m2PI) < 128) && (((fi1 -  96)m2PI) < 64)) ) ){
      MoveTo(p0h + c, p0v); Line(-2 * c, 0);
    }
    if( ! ( ((((fi2 -  64)m2PI) < 128) && (((fi1 - 160)m2PI) < 64)) ||
	    ((((fi2 - 192)m2PI) < 128) && (((fi1 -  32)m2PI) < 64)) ) ){
      MoveTo(p1h + c, p1v); Line(-2 * c, 0);
    }
    if( ! ( ((((fi2 -  64)m2PI) < 128) && (((fi1 -  96)m2PI) < 64)) ||
	    ((((fi2 - 192)m2PI) < 128) && (((fi1 - 224)m2PI) < 64)) ) ){
      MoveTo(p2h + c, p2v); Line(-2 * c, 0);
    }
    if( ! ( ((((fi2 -  64)m2PI) < 128) && (((fi1 -  32)m2PI) < 64)) ||
	    ((((fi2 - 192)m2PI) < 128) && (((fi1 - 160)m2PI) < 64)) ) ){
      MoveTo(p3h + c, p3v); Line(-2 * c, 0);
    }

    /* Dump on screen */
    CopyBits(&show_w->portBits, &picture_bits,
	 &show_w->portBits.bounds, &show_r1,
	 srcCopy, (RgnHandle) 0);
  }

  EnableItem(quit_menu, 0);
  DrawMenuBar();

  while(1){
    for(i = 0; i < 128; i++){

      GetNextEvent(everyEvent, &event);
      if(event.what == mouseDown)
	if(FindWindow(&event.where, &dummy_w) == inMenuBar){
	  if(HiWord(MenuSelect(&event.where)) == FILE)
	    done_flag++;
	  HiliteMenu(0);
	}

      if(done_flag){
	OffsetRect(&show_r1, 0, 2);
	OffsetRect(&show_r2, 2, 0);
	OffsetRect(&show_r3, 2, 2);
	if(done_flag++ == 200){
	  /* A simple and frightening sound effect */
	  for(i = 0; i < 10; i++)
	    SysBeep(0);
	  SysBeep(5);
	  ShowCursor();
	  ExitToShell();
	}
      }

      /* Set pointer to work bits */
      show_w->portBits.baseAddr = work_ptr[i];

      /* Dump on screen */
      CopyBits(&show_w->portBits, &picture_bits,
	   &show_w->portBits.bounds, &show_r1,
	   srcCopy, (RgnHandle) 0);

      show_w->portBits.baseAddr = work_ptr[(i + 33)&127];

      CopyBits(&show_w->portBits, &picture_bits,
	   &show_w->portBits.bounds, &show_r2,
	   srcCopy, (RgnHandle) 0);

      show_w->portBits.baseAddr = work_ptr[(-i)&127];

      CopyBits(&show_w->portBits, &picture_bits,
	   &show_w->portBits.bounds, &show_r3,
	   srcCopy, (RgnHandle) 0);
    }
  }
}




SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0