--- /dev/null
+/* topblock, Copyright (c) 2006 rednuht <topblock.xscreensaver@jumpstation.co.uk>\r
+ *\r
+ * Permission to use, copy, modify, distribute, and sell this software and its\r
+ * documentation for any purpose is hereby granted without fee, provided that\r
+ * the above copyright notice appear in all copies and that both that\r
+ * copyright notice and this permission notice appear in supporting\r
+ * documentation. No representations are made about the suitability of this\r
+ * software for any purpose. It is provided "as is" without express or \r
+ * implied warranty.\r
+ *\r
+ * \r
+ *\r
+\r
+topBlock - a simple openGL 3D hack of falling blocks\r
+based on jwz's dangerball hack\r
+\r
+The proporations of the blocks and their features is not even close to the commercial building block products offered by a variety companies.\r
+\r
+information on this hack might be found at \r
+http://www.jumpstation.co.uk/xscreensaver/topblock/\r
+\r
+History\r
+25/02/2006 v1.0 release\r
+29/04/2006 v1.11 updated to better fit with xscreensaver v5\r
+ colors defaults to 7 (no black)\r
+*/\r
+\r
+#include <math.h>\r
+\r
+# define refresh_topBlock 0\r
+\r
+#define DEFAULTS "*delay: 10000 \n" \\r
+ "*count: 30 \n" \\r
+ "*showFPS: False \n" \\r
+ "*wireframe: False \n" \\r
+\r
+#undef countof\r
+#define countof(x) (sizeof((x))/sizeof((*x)))\r
+\r
+#include "xlockmore.h"\r
+#include "topblock.h"\r
+#include "sphere.h"\r
+#include <ctype.h>\r
+\r
+#ifdef USE_GL /* whole file */\r
+\r
+#ifdef HAVE_COCOA\r
+# include <OpenGL/glu.h>\r
+#else\r
+# include <GL/glu.h>\r
+#endif\r
+\r
+typedef struct\r
+{\r
+ GLXContext *glx_context;\r
+ int numFallingBlocks;\r
+ GLfloat highest,highestFalling;\r
+ GLfloat eyeLine,eyeX,eyeY,eyeZ;\r
+ int carpetWidth, carpetLength;\r
+ int followMode;\r
+ GLfloat followRadius,followAngle;\r
+ int plusheight;\r
+ GLuint carpet;\r
+ GLuint block;\r
+ NODE *blockNodeRoot;\r
+ NODE *blockNodeFollow;\r
+ GLfloat rotation;\r
+} topBlockSTATE;\r
+\r
+/* parameter vars */\r
+Bool override;\r
+Bool rotate;\r
+Bool follow;\r
+Bool drawCarpet;\r
+Bool drawBlob;\r
+Bool drawNipples;\r
+GLfloat rotateSpeed;\r
+GLfloat camX;\r
+GLfloat camY;\r
+GLfloat camZ;\r
+GLfloat dropSpeed;\r
+int maxFalling;\r
+int maxColors;\r
+int size;\r
+int spawn;\r
+int resolution;\r
+\r
+static XrmOptionDescRec opts[] = {\r
+ { "-size", ".size", XrmoptionSepArg, 0 },\r
+ { "-spawn", ".spawn", XrmoptionSepArg, 0 },\r
+ { "-camX", ".camX", XrmoptionSepArg, 0 },\r
+ { "-camY", ".camY", XrmoptionSepArg, 0 },\r
+ { "-camZ", ".camZ", XrmoptionSepArg, 0 },\r
+ { "+rotate", ".rotate", XrmoptionNoArg, "False" },\r
+ { "-rotate", ".rotate", XrmoptionNoArg, "True" },\r
+ { "+carpet", ".carpet", XrmoptionNoArg, "False" },\r
+ { "+nipples", ".nipples", XrmoptionNoArg, "False" },\r
+ { "-blob", ".blob", XrmoptionNoArg, "True" },\r
+ { "-rotateSpeed", ".rotateSpeed", XrmoptionSepArg, 0 },\r
+ { "-follow", ".follow", XrmoptionNoArg, "True" },\r
+ { "-maxFalling", ".maxFalling", XrmoptionSepArg, 0 },\r
+ { "-resolution", ".resolution", XrmoptionSepArg, 0 },\r
+ { "-maxColors", ".maxColors", XrmoptionSepArg, 0 },\r
+ { "-dropSpeed", ".dropSpeed", XrmoptionSepArg, 0 },\r
+ { "-override", ".override", XrmoptionNoArg, "True" },\r
+};\r
+\r
+#define DEF_override "False"\r
+#define DEF_rotate "True"\r
+#define DEF_follow "False"\r
+#define DEF_drawCarpet "True"\r
+#define DEF_drawBlob "False"\r
+#define DEF_drawNipples "True"\r
+#define DEF_rotateSpeed "10"\r
+#define DEF_maxFalling "500"\r
+#define DEF_maxColors "7"\r
+#define DEF_size "2"\r
+#define DEF_spawn "50"\r
+#define DEF_resolution "4"\r
+#define DEF_camX "1"\r
+#define DEF_camY "20"\r
+#define DEF_camZ "25"\r
+#define DEF_dropSpeed "4"\r
+\r
+static argtype vars[] = {\r
+ {&override, "override", "Override", DEF_override, t_Bool},\r
+ {&rotate, "rotate", "Rotate", DEF_rotate, t_Bool},\r
+ {&drawCarpet, "carpet", "Carpet", DEF_drawCarpet, t_Bool},\r
+ {&drawNipples, "nipples", "Nipples", DEF_drawNipples, t_Bool},\r
+ {&drawBlob, "blob", "Blob", DEF_drawBlob, t_Bool},\r
+ {&rotateSpeed, "rotateSpeed", "RotateSpeed", DEF_rotateSpeed, t_Float},\r
+ {&follow, "follow", "Follow", DEF_follow, t_Bool},\r
+ {&camX, "camX", "camX", DEF_camX, t_Float},\r
+ {&camY, "camY", "camY", DEF_camY, t_Float},\r
+ {&camZ, "camZ", "camZ", DEF_camZ, t_Float},\r
+ {&size, "size", "size", DEF_size, t_Int},\r
+ {&spawn, "spawn", "spawn", DEF_spawn, t_Int},\r
+ {&maxFalling, "maxFalling", "maxFalling", DEF_maxFalling, t_Int},\r
+ {&resolution, "resolution", "resolution", DEF_resolution, t_Int},\r
+ {&maxColors, "maxColors", "maxColors", DEF_maxColors, t_Int},\r
+ {&dropSpeed, "dropSpeed", "DropSpeed", DEF_dropSpeed, t_Float},\r
+};\r
+\r
+static topBlockSTATE *tbs = NULL;\r
+\r
+ModeSpecOpt topBlock_opts = {countof(opts), opts, countof(vars), vars, NULL};\r
+\r
+/* Window management, etc */\r
+ENTRYPOINT void\r
+reshape_topBlock (ModeInfo *mi, int width, int height) {\r
+ GLfloat h = (GLfloat) height / (GLfloat) width;\r
+ glViewport (0, 0, (GLint) width, (GLint) height);\r
+ glMatrixMode(GL_PROJECTION);\r
+ glLoadIdentity();\r
+ gluPerspective (60.0, 1/h, 1.0, 1000.0);\r
+ glMatrixMode(GL_MODELVIEW);\r
+ glLoadIdentity();\r
+ glClear(GL_COLOR_BUFFER_BIT);\r
+}\r
+\r
+/* clean up on exit, not required ... */\r
+ENTRYPOINT void\r
+ release_topBlock(ModeInfo *mi) {\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ NODE *llCurrent, *llOld;\r
+ llCurrent = tb->blockNodeRoot;\r
+ while (llCurrent != NULL) {\r
+ llOld = llCurrent;\r
+ llCurrent = llCurrent->next;\r
+ free(llOld);\r
+ }\r
+}\r
+\r
+/* setup */\r
+ENTRYPOINT void \r
+init_topBlock (ModeInfo *mi)\r
+{\r
+ topBlockSTATE *tb;\r
+ int wire = MI_IS_WIREFRAME(mi);\r
+\r
+ if (!tbs) {\r
+ tbs = (topBlockSTATE *)\r
+ calloc (MI_NUM_SCREENS(mi), sizeof (topBlockSTATE));\r
+ if (!tbs) {\r
+ fprintf(stderr, "%s: out of memory\n", progname);\r
+ exit(1);\r
+ }\r
+ }\r
+\r
+ tb = &tbs[MI_SCREEN(mi)];\r
+\r
+ tb->glx_context = init_GL(mi);\r
+\r
+ reshape_topBlock (mi, MI_WIDTH(mi), MI_HEIGHT(mi));\r
+\r
+ if (wire) { drawNipples=False; }\r
+ tb->numFallingBlocks=0;\r
+\r
+ if (size>10) { size = 10; }\r
+ if (size<1) { size = 2; }\r
+ tb->carpetWidth = 8 * size;\r
+ tb->carpetLength = tb->carpetWidth;\r
+ \r
+ maxFalling=maxFalling*size;\r
+\r
+ if (spawn<4) { spawn=4; }\r
+ if (spawn>1000) { spawn=1000; }\r
+\r
+ if (rotateSpeed<1) {rotateSpeed=1; }\r
+ if (rotateSpeed>1000) {rotateSpeed=1000;}\r
+ rotateSpeed = rotateSpeed / 100;\r
+\r
+ if (resolution<4) {resolution=4;}\r
+ if (resolution>20) {resolution=20;}\r
+ resolution=resolution*2;\r
+\r
+ if (maxColors<1) {maxColors=1;}\r
+ if (maxColors>8) {maxColors=8;}\r
+\r
+ if (dropSpeed<1) {dropSpeed=1;}\r
+ if (dropSpeed>9) {dropSpeed=9;} /* 10+ produces blocks that can pass through each other */\r
+ \r
+ dropSpeed = 80/dropSpeed;\r
+ dropSpeed = (blockHeight/dropSpeed); \r
+\r
+ tb->glx_context = init_GL(mi);\r
+\r
+ reshape_topBlock (mi, MI_WIDTH(mi), MI_HEIGHT(mi));\r
+ if (0==1) {\r
+ glClearColor(1.0f, 1.0f, 1.0f, 0.5f);\r
+ } else {\r
+ glClearColor(0.0f, 0.0f, 0.0f, 0.5f);\r
+ }\r
+ glClearDepth(1.0f);\r
+ if (!wire) {\r
+ GLfloat pos[4] = {10.0, 10.0, 1.0, 0.0};\r
+ GLfloat amb[4] = {0.1, 0.1, 0.1, 1.0};\r
+ GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};\r
+ GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};\r
+\r
+ glEnable(GL_LIGHTING);\r
+ glEnable(GL_LIGHT0);\r
+ glLightfv(GL_LIGHT0, GL_POSITION, pos);\r
+ glLightfv(GL_LIGHT0, GL_AMBIENT, amb); \r
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);\r
+ glLightfv(GL_LIGHT0, GL_SPECULAR, spc); \r
+ }\r
+ glDepthFunc(GL_LEQUAL);\r
+ glEnable(GL_DEPTH_TEST);\r
+ glDisable(GL_CULL_FACE); /* all objects exhibit a reverse side */\r
+ glCullFace(GL_BACK); \r
+\r
+ if (drawBlob) {\r
+ buildBlobBlock(mi); \r
+ } else {\r
+ buildBlock(mi); /* build the display list holding the simple block */\r
+ }\r
+ buildCarpet(mi); /* build the base */\r
+ tb->highest=0;\r
+ tb->highestFalling=0;\r
+ tb->eyeLine=tb->highest;\r
+ tb->eyeX=0;\r
+ tb->eyeY=0;\r
+ tb->eyeZ=0;\r
+ tb->followMode=0;\r
+ if (follow) {\r
+ tb->plusheight=100;\r
+ camZ=camZ-60;\r
+ } else {\r
+ tb->rotation=random() % 360;\r
+ tb->eyeY=10;\r
+ tb->plusheight=30;\r
+ }\r
+ tb->followRadius=0;\r
+ /* override camera settings */\r
+ if (override) {\r
+ tb->plusheight=100;\r
+ drawCarpet=False;\r
+ camX=0;\r
+ camY=1;\r
+ camZ=0;\r
+ tb->eyeX=-1;\r
+ tb->eyeY=20;\r
+ tb->eyeZ=0;\r
+ }\r
+}\r
+\r
+/* provides the per frame entertainment */\r
+ENTRYPOINT void\r
+draw_topBlock (ModeInfo *mi) {\r
+ Display *dpy = MI_DISPLAY(mi);\r
+ Window window = MI_WINDOW(mi);\r
+ NODE *llCurrent;\r
+ NODE *llNode;\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ GLfloat spcN1x,spcN1y,spcN2x,spcN2y;\r
+ GLfloat spcC1x,spcC1y,spcC2x,spcC2y;\r
+ int wire = MI_IS_WIREFRAME(mi);\r
+ GLfloat color[4]; \r
+\r
+ if (!tb->glx_context)\r
+ return;\r
+ glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(tb->glx_context));\r
+\r
+ generateNewBlock(mi);\r
+\r
+ if (rotate) { tb->rotation += rotateSpeed; } \r
+ if (tb->rotation>=360) { tb->rotation=tb->rotation-360; } \r
+\r
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* clear current */\r
+ glLoadIdentity(); /* resets directions, do it every time ! */\r
+\r
+ if (!follow) {\r
+ if (tb->highest>tb->eyeLine) { tb->eyeLine=tb->eyeLine + ((tb->highest-tb->eyeLine)/100); } /* creates a smooth camera transition */\r
+ gluLookAt(camX, camY+tb->eyeLine, camZ, tb->eyeX, tb->eyeY+tb->eyeLine, tb->eyeZ, 0.0, 1.0, 0.0); /* setup viewer, xyz cam, xyz looking at and where is up normaly 0,1,0 */\r
+ glRotatef(90, 1.0, 0.0, 0.0); /* x axis */\r
+ } else {\r
+ glRotatef(90, 0.0, 0.0, 1.0); /* z axis */\r
+ followBlock(mi);\r
+ }\r
+ /* rotate the world */\r
+ glRotatef(tb->rotation, 0.0, 0.0, 1.0); \r
+\r
+ llCurrent = tb->blockNodeRoot;\r
+ if (drawCarpet) {\r
+ /* center carpet */\r
+ glTranslatef(0.0-(tb->carpetWidth/2),0.0-(tb->carpetLength/2),0.0);\r
+ glCallList(tb->carpet);\r
+ glTranslatef(0.0+(tb->carpetWidth/2),0.0+(tb->carpetLength/2),0.0);\r
+ glTranslatef(0.0,0.0,-0.55);\r
+ }\r
+ tb->highestFalling=0;\r
+ while (llCurrent != NULL) { /* for each block */\r
+ glPushMatrix(); /* save state */\r
+ /* set color */\r
+ switch (llCurrent->color) { \r
+ case 0:\r
+ color[0] = 1.0f; \r
+ color[1] = 0.0f; \r
+ color[2] = 0.0f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 1:\r
+ color[0] = 0.0f; \r
+ color[1] = 1.0f; \r
+ color[2] = 0.0f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 2:\r
+ color[0] = 0.0f; \r
+ color[1] = 0.0f; \r
+ color[2] = 1.0f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 3:\r
+ color[0] = 0.95f; \r
+ color[1] = 0.95f; \r
+ color[2] = 0.95f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 4:\r
+ color[0] = 1.0f; \r
+ color[1] = 0.5f; \r
+ color[2] = 0.0f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 5:\r
+ color[0] = 1.0f; \r
+ color[1] = 1.0f; \r
+ color[2] = 0.0f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 6: \r
+ color[0] = 0.5f; \r
+ color[1] = 0.5f; \r
+ color[2] = 0.5f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ case 7:\r
+ color[0] = 0.05f; \r
+ color[1] = 0.05f; \r
+ color[2] = 0.05f; \r
+ color[3] = 1.0f; \r
+ break;\r
+ } \r
+ if (wire) { glColor3fv(color); }\r
+ else { glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); }\r
+\r
+ if (llCurrent->falling==1) {\r
+ spcC2x = 0;\r
+ spcC2y = 0;\r
+ spcN2x = 0;\r
+ spcN2y = 0;\r
+ if (llCurrent->height>tb->highestFalling) {tb->highestFalling=llCurrent->height;}\r
+ /* all blocks fall at the same rate to avoid mid air collisions */\r
+ llCurrent->height=llCurrent->height-dropSpeed;\r
+ if (llCurrent->height<=0) {\r
+ llCurrent->falling=0;\r
+ if (tb->highest==0) { \r
+ tb->highest=tb->highest+blockHeight; \r
+ }\r
+ } \r
+ if ( (llCurrent->height<=tb->highest+1) && (llCurrent->falling==1) ) {\r
+ /* check for collision */\r
+ llNode = tb->blockNodeRoot;\r
+ spcC1x = llCurrent->x;\r
+ spcC1y = llCurrent->y;\r
+ switch(llCurrent->rotation) {\r
+ case getOrientation(0):\r
+ spcC2x = spcC1x;\r
+ spcC2y = spcC1y-2;\r
+ break;\r
+ case getOrientation(1):\r
+ spcC2x = spcC1x+2;\r
+ spcC2y = spcC1y;\r
+ break;\r
+ case getOrientation(2):\r
+ spcC2x = spcC1x;\r
+ spcC2y = spcC1y+2;\r
+ break;\r
+ case getOrientation(3):\r
+ spcC2x = spcC1x-2;\r
+ spcC2y = spcC1y;\r
+ break;\r
+ }\r
+ while (llNode != NULL) {\r
+ if ( (llNode->falling==0) && (llCurrent->falling==1) ) {\r
+ spcN1x = llNode->x;\r
+ spcN1y = llNode->y;\r
+ switch(llNode->rotation) {\r
+ case getOrientation(0):\r
+ spcN2x = spcN1x;\r
+ spcN2y = spcN1y-2;\r
+ break;\r
+ case getOrientation(1):\r
+ spcN2x = spcN1x+2;\r
+ spcN2y = spcN1y;\r
+ break;\r
+ case getOrientation(2):\r
+ spcN2x = spcN1x;\r
+ spcN2y = spcN1y+2;\r
+ break;\r
+ case getOrientation(3):\r
+ spcN2x = spcN1x-2;\r
+ spcN2y = spcN1y;\r
+ break;\r
+ }\r
+ if ( \r
+ ( (spcC1x==spcN1x) && (spcC1y==spcN1y) ) ||\r
+ ( (spcC1x==spcN2x) && (spcC1y==spcN2y) ) ||\r
+ ( (spcC2x==spcN2x) && (spcC2y==spcN2y) ) ||\r
+ ( (spcC2x==spcN1x) && (spcC2y==spcN1y) )\r
+ ){\r
+ if ( (llCurrent->height<=llNode->height+blockHeight+TOLLERANCE) && (llCurrent->height>=llNode->height+blockHeight-TOLLERANCE) ) {\r
+ llCurrent->falling=0;\r
+ llCurrent->height=llNode->height+blockHeight; /* if this is missing then small errors build up until the model fails */\r
+ if ( (llCurrent->height<=tb->highest+TOLLERANCE) && (llCurrent->height>=tb->highest-TOLLERANCE) ) { \r
+ tb->highest=tb->highest+blockHeight; \r
+ }\r
+ }\r
+ }\r
+ }\r
+ llNode=llNode->next;\r
+ } \r
+ }\r
+ } \r
+ /* set location in space */\r
+ glTranslatef(llCurrent->x,llCurrent->y,-llCurrent->height);\r
+ /* rotate */\r
+ glRotatef(llCurrent->rotation, 0.0f, 0.0f, 1.0f);\r
+ if ((tb->followMode==0) && (llCurrent->next==NULL)) {\r
+ tb->blockNodeFollow = llCurrent;\r
+ tb->followMode=1;\r
+ } \r
+ llCurrent = llCurrent->next;\r
+ /* draw */\r
+ glCallList(tb->block); \r
+ glPopMatrix(); /* restore state */\r
+ } \r
+ if (mi->fps_p) do_fps (mi);\r
+ glFinish();\r
+\r
+ if (tb->highest>(5*maxFalling)) { drawCarpet=False; }\r
+ glXSwapBuffers(dpy, window);\r
+}\r
+\r
+\r
+\r
+/* camera is in follow mode, work out where we should be looking */\r
+static void followBlock(ModeInfo *mi) {\r
+ GLfloat xLen,yLen,cx,cy,rangle,xTarget,yTarget;\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ cx=0;cy=0;\r
+ if ((tb->blockNodeFollow!=NULL) && (tb->followMode==1)){\r
+\r
+ if (tb->highest>tb->eyeLine) { tb->eyeLine=tb->eyeLine + ((tb->highest-tb->eyeLine)/100); } \r
+ /*tb->blockNodeFollow->color=1; only noticable if you set the colors to 1 */\r
+ \r
+ if (tb->blockNodeFollow->height > tb->eyeZ) { tb->eyeZ=tb->eyeZ + ((tb->blockNodeFollow->height - tb->eyeZ)/100); } \r
+ if (tb->blockNodeFollow->height < tb->eyeZ) { tb->eyeZ=tb->eyeZ - ((tb->eyeZ - tb->blockNodeFollow->height)/100); } \r
+ \r
+\r
+ /* when the scene is rotated we need to know where the block is in the 2 dimensional coordinates of the carpet area\r
+ (see http://www.jumpstation.co.uk/rotation/)\r
+ */\r
+\r
+ if (tb->followRadius==0) { \r
+ xLen = tb->blockNodeFollow->x-cx;\r
+ yLen = tb->blockNodeFollow->y-cy;\r
+ tb->followRadius=sqrt( (xLen*xLen) + (yLen*yLen) ); \r
+ tb->followAngle = (180/M_PI) * asin(xLen/tb->followRadius); \r
+ tb->followAngle = quadrantCorrection(tb->followAngle,(int)cx,(int)cy,(int)tb->blockNodeFollow->x,(int)tb->blockNodeFollow->y);\r
+ }\r
+ rangle = (tb->followAngle+tb->rotation) * M_PI /180;\r
+ xTarget = cos(rangle) * tb->followRadius + cx;\r
+ yTarget = sin(rangle) * tb->followRadius + cy;\r
+ if (tb->followAngle>360) { tb->followAngle=tb->followAngle-360; }\r
+\r
+ if (xTarget < tb->eyeX) { tb->eyeX=tb->eyeX - ((tb->eyeX - xTarget)/100); }\r
+ if (xTarget > tb->eyeX) { tb->eyeX=tb->eyeX + ((xTarget - tb->eyeX)/100); }\r
+\r
+ if (yTarget < tb->eyeY) { tb->eyeY=tb->eyeY - ((tb->eyeY - yTarget)/100); }\r
+ if (yTarget > tb->eyeY) { tb->eyeY=tb->eyeY + ((yTarget - tb->eyeY)/100); }\r
+ /*\r
+ tb->eyeX = xTarget;\r
+ tb->eyeY = yTarget;\r
+***************************************************************************\r
+*/\r
+ if (!tb->blockNodeFollow->falling) { \r
+ tb->followMode=0; \r
+ /*tb->blockNodeFollow->color=2; only noticable if you set the colors to 1 */\r
+ tb->followRadius=0;\r
+ } \r
+ }\r
+ gluLookAt(camX, camY, camZ-tb->eyeLine, tb->eyeX, tb->eyeY, -tb->eyeZ,-1.0,0.0,0.0);\r
+}\r
+\r
+/* each quater of the circle has to be adjusted for */\r
+static double quadrantCorrection(double angle,int cx,int cy,int x,int y) {\r
+ if ((x>=cx) && (y>=cy)) {\r
+ angle = angle + (90-(angle-90) * 2); \r
+ } else if ((x>=cx) && (y<=cy)) {\r
+ angle = angle + 90; \r
+ } else if ((x<=cx) && (y<=cy)) {\r
+ angle = angle + 90; \r
+ } else if ((x<=cx) && (y>=cy)) {\r
+ angle = angle + (90-(angle-90) * 2); \r
+ }\r
+ return(angle-180);\r
+}\r
+\r
+/* if random chance then create a new falling block */\r
+static void generateNewBlock(ModeInfo *mi) {\r
+ NODE *llCurrent, *llTail;\r
+ GLfloat startOffx, startOffy;\r
+ int endOffx, endOffy;\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ if ( ((random() % spawn) == 1) && (tb->highestFalling<getHeight((tb->plusheight-blockHeight)+tb->highest)) ) { \r
+ startOffx=0;\r
+ endOffx=0;\r
+ startOffy=0;\r
+ endOffy=0;\r
+ tb->numFallingBlocks++;\r
+ llTail = tb->blockNodeRoot; \r
+ if (llTail == NULL) {\r
+ if ((llCurrent = ((NODE*) malloc(sizeof(NODE)))) == NULL) { fprintf(stderr, "%s: out of memory.\n", progname); }\r
+ llTail = llCurrent;\r
+ tb->blockNodeRoot = llCurrent; \r
+ } else {\r
+ if (tb->numFallingBlocks>=maxFalling) {\r
+ /* recycle */\r
+ llCurrent=llTail->next;\r
+ tb->blockNodeRoot=llCurrent->next;\r
+ } else {\r
+ if ((llCurrent = ((NODE*) malloc(sizeof(NODE)))) == NULL) { fprintf(stderr, "%s: out of memory..\n", progname); }\r
+ }\r
+ while (llTail->next != NULL) { llTail = llTail->next; } /* find last item in list */\r
+ }\r
+ llCurrent->falling=1;\r
+ llCurrent->rotation=getOrientation(random() % 4); \r
+ if (llCurrent->rotation==getOrientation(0)) {\r
+ startOffx=1.0;\r
+ endOffx=0;\r
+ startOffy=3.0;\r
+ endOffy=-1;\r
+ } else if (llCurrent->rotation==getOrientation(1)) {\r
+ startOffx=1.0;\r
+ endOffx=-1;\r
+ startOffy=1.0;\r
+ endOffy=0; \r
+ } else if (llCurrent->rotation==getOrientation(2)) {\r
+ startOffx=1.0;\r
+ endOffx=0;\r
+ startOffy=3.0;\r
+ endOffy=-1; \r
+ } else if (llCurrent->rotation==getOrientation(3)) { \r
+ startOffx=5.0;\r
+ endOffx=-1;\r
+ startOffy=1.0;\r
+ endOffy=0; \r
+ }\r
+\r
+ llCurrent->x=(startOffx-(tb->carpetLength/2)) + getLocation(random() % ((tb->carpetLength/2)+endOffx) );\r
+ llCurrent->y=(startOffy-(tb->carpetLength/2)) + getLocation(random() % ((tb->carpetLength/2)+endOffy) );\r
+ llCurrent->color=(random() % maxColors);\r
+ llCurrent->height=getHeight(tb->plusheight+tb->highest); \r
+ if (tb->numFallingBlocks>=maxFalling) {\r
+ tb->numFallingBlocks--;\r
+ tb->numFallingBlocks--;\r
+ } \r
+ llTail->next = llCurrent;\r
+ llTail = llCurrent;\r
+ llTail->next = NULL;\r
+\r
+ }\r
+}\r
+\r
+/* called at init this creates the 'carpet' display list item */\r
+static void buildCarpet(ModeInfo *mi) {\r
+ int i,c,x,y;\r
+ GLfloat color[4];\r
+ int wire = MI_IS_WIREFRAME(mi);\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ GLUquadricObj *quadratic;\r
+ color[0] = 0.0f; \r
+ color[1] = 1.0f; \r
+ color[2] = 0.0f; \r
+ color[3] = 1.0f; \r
+ tb->carpet=glGenLists(1); /* only one */\r
+ glNewList(tb->carpet,GL_COMPILE);\r
+ glPushMatrix(); /* save state */\r
+ x=tb->carpetWidth;\r
+ y=tb->carpetLength;\r
+ if (wire) { glColor3fv(color); }\r
+ else { glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); }\r
+ /* draw carpet plane */\r
+ glBegin( wire ? GL_LINE_LOOP : GL_QUADS );\r
+ /* draw top */\r
+ glNormal3f( 0, 0, -1 );\r
+ glVertex3f(0.0,0.0,0.0);\r
+ glVertex3f(x,0.0,0.0);\r
+ glVertex3f(x,y,0.0);\r
+ glVertex3f(0.0,y,0.0);\r
+ if (wire) { glEnd(); } \r
+ else {\r
+ /* add edge pieces */\r
+ /* side 1 */\r
+ glNormal3f( 0, -1, 0 );\r
+ glVertex3f(0.0,0.0,0.0);\r
+ glVertex3f(x,0.0,0.0);\r
+ glVertex3f(x,0,singleThick);\r
+ glVertex3f(0.0,0,singleThick);\r
+ /* side 2 */\r
+ glNormal3f( -1, 0, 0 );\r
+ glVertex3f(0.0,0.0,0.0);\r
+ glVertex3f(0,y,0.0);\r
+ glVertex3f(0,y,singleThick);\r
+ glVertex3f(0.0,0,singleThick);\r
+ /* side 3 */\r
+ glNormal3f( 1, 0, 0 );\r
+ glVertex3f(x,0.0,0.0);\r
+ glVertex3f(x,y,0.0);\r
+ glVertex3f(x,y,singleThick);\r
+ glVertex3f(x,0,singleThick);\r
+ /* side 4 */\r
+ glNormal3f( 0, 1, 0 );\r
+ glVertex3f(0,y,0.0);\r
+ glVertex3f(x,y,0.0);\r
+ glVertex3f(x,y,singleThick);\r
+ glVertex3f(0,y,singleThick);\r
+ }\r
+ glEnd();\r
+ /* nipples */\r
+ if (drawNipples) {\r
+ quadratic=gluNewQuadric(); /* Create A Pointer To The Quadric Object */\r
+ gluQuadricNormals(quadratic, GLU_SMOOTH); /* Create Smooth Normals */\r
+ gluQuadricTexture(quadratic, GL_TRUE); /* Create Texture Coords */\r
+ glTranslatef(0.5f,0.5f,-.25); /* move to the cylinder center */\r
+ for (c=0;c<x;c++) {\r
+ glPushMatrix(); /* save state */\r
+ for (i=0;i<y;i++) {\r
+ gluCylinder(quadratic, cylSize, cylSize, 0.25f, resolution, resolution); /* quad, radius(bottom, radius(top), height, subdivisions (around Z), subdevisions (along Z) */\r
+ glRotatef(180, 0.0f, 1.0f, 0.0f); /* they are upside down */\r
+ gluDisk(quadratic, 0.0f, cylSize, resolution, resolution ); /* inner size (cd hole), outer size (radius), subdivisions radial, subdivisions circular */\r
+ glRotatef(180, 0.0f, 1.0f, 0.0f); /* recover */\r
+ glTranslatef(0.0f,1.0f,0.0f); /* move to the next cylinder center (backward) */\r
+ }\r
+ glPopMatrix(); /* save state */\r
+ glTranslatef(1.0f,0.0f,0.0f); /* reset */\r
+ }\r
+ }\r
+ glPopMatrix(); /* restore state */\r
+ glEndList(); \r
+}\r
+\r
+/* using the verticies arrays builds the plane, now with normals */\r
+static void polygonPlane(int wire, int a, int b, int c , int d, int i)\r
+{\r
+ GLfloat topBlockNormals[5][3] = { {0,0,-1}, {0,1,0}, {1,0,0}, {0,0,1}, {0,-1,0} };\r
+ GLfloat topBlockVertices[8][3] = { {-0.49,-2.97,-0.99}, {0.99,-2.97,-0.99}, {0.99,0.99,-0.99} , {-0.49,0.99,-0.99}, {-0.49,-2.97,0.99} , {0.99,-2.97,0.99}, {0.99,0.99,0.99} , {-0.49,0.99,0.99} };\r
+ glBegin( wire ? GL_LINE_LOOP : GL_POLYGON);\r
+ glNormal3fv(topBlockNormals[i] );\r
+ glVertex3fv(topBlockVertices[a]);\r
+ glVertex3fv(topBlockVertices[b]);\r
+ glVertex3fv(topBlockVertices[c]);\r
+ glVertex3fv(topBlockVertices[d]);\r
+ glEnd();\r
+}\r
+\r
+/* called at init this creates the 'block' display list item */\r
+/* the spheres came about originaly as quick way to test the directional lighting/normals */\r
+static void buildBlock(ModeInfo *mi) {\r
+ int i,c;\r
+ int wire = MI_IS_WIREFRAME(mi);\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ GLUquadricObj *quadratic;\r
+ tb->block=glGenLists(1); /* only one */\r
+ glNewList(tb->block,GL_COMPILE);\r
+ glPushMatrix(); /* save state */\r
+ glRotatef(90, 0.0f, 1.0f, 0.0f);\r
+ /* base */\r
+ polygonPlane(wire, 0,3,2,1,0);\r
+ polygonPlane(wire, 2,3,7,6,1);\r
+ polygonPlane(wire, 1,2,6,5,2); \r
+ polygonPlane(wire, 4,5,6,7,3); \r
+ polygonPlane(wire, 0,1,5,4,4);\r
+ if (drawNipples) {\r
+ /* nipples */\r
+ /* draw 8 cylinders each with a disk cap */\r
+ quadratic=gluNewQuadric(); /* Create A Pointer To The Quadric Object */\r
+ gluQuadricNormals(quadratic, GLU_SMOOTH); /* Create Smooth Normals */\r
+ glRotatef(90, 0.0f, 1.0f, 0.0f); /* 'aim' the pointer ready for the cylinder */\r
+ glTranslatef(0.5f,0.5f,0.99f); /* move to the cylinder center */\r
+ for (c=0;c<2;c++) {\r
+ for (i=0;i<4;i++) {\r
+ gluCylinder(quadratic, cylSize, cylSize, 0.25f, resolution, resolution); /* quad, radius(bottom, radius(top), height, subdivisions (around Z), subdevisions (along Z) */\r
+ glTranslatef(0.0f,0.0f,0.25f); /* move to the cylinder cap */\r
+ gluDisk(quadratic, 0.0f, cylSize, resolution, resolution ); /* inner size (cd hole), outer size (radius), subdivisions radial, subdivisions circular */\r
+ glTranslatef(0.0f,0.0f,-0.25f); /* move back from the cylinder cap */\r
+ if (c==0) { \r
+ glTranslatef(0.0f,-1.0f,0.0f); /* move to the next cylinder center (forward) */\r
+ } else {\r
+ glTranslatef(0.0f,1.0f,0.0f); /* move to the next cylinder center (backward) */\r
+ }\r
+ }\r
+ glTranslatef(-1.0f,1.0f,0.0f); /* move to the cylinder center */\r
+ }\r
+ /* udders */\r
+ /* 3 cylinders on the underside */\r
+ glTranslatef(1.5f,-2.5f,-1.5f); /* move to the center, under the top of the brick */\r
+ for (c=0;c<3;c++) {\r
+ gluCylinder(quadratic, uddSize, uddSize, 1.5f, resolution, resolution); /* quad, radius(bottom, radius(top), height, subdivisions (around Z), subdevisions (along Z) */\r
+ glTranslatef(0.0f,-1.0f,0.0f); /* move to the center */ \r
+ }\r
+ }\r
+ glPopMatrix(); /* restore state */\r
+ glEndList(); \r
+}\r
+\r
+/* \r
+ rip off of the builBlock() function creating the GL compilied pointer "block" but only creates two spheres.\r
+ spheres are created with unit_sphere from spheres.h to allow wire frame \r
+*/\r
+static void buildBlobBlock(ModeInfo *mi) {\r
+ int wire = MI_IS_WIREFRAME(mi);\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ tb->block=glGenLists(1); /* only one */\r
+ glNewList(tb->block,GL_COMPILE);\r
+ glPushMatrix();\r
+ glScalef(1.4,1.4,1.4);\r
+ unit_sphere (resolution/2,resolution, wire);\r
+ glPopMatrix();\r
+ glTranslatef(0.0f,-2.0f,0.0f);\r
+ glScalef(1.4,1.4,1.4);\r
+ unit_sphere (resolution/2,resolution, wire);\r
+ glEndList(); \r
+}\r
+\r
+\r
+/* handle input events or not if daemon running the show */\r
+ENTRYPOINT Bool \r
+topBlock_handle_event (ModeInfo *mi, XEvent *event) {\r
+ topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];\r
+ if (event->xany.type == KeyPress) {\r
+ KeySym keysym;\r
+ char c = 0;\r
+ XLookupString (&event->xkey, &c, 1, &keysym, 0);\r
+ if (c == 'a') {\r
+ tb->eyeX=tb->eyeX+1;\r
+ return True;\r
+ } else if (c == 'z') {\r
+ tb->eyeX=tb->eyeX-1;\r
+ return True;\r
+ } else if (c == 's') {\r
+ tb->eyeY=tb->eyeY+1;\r
+ return True;\r
+ } else if (c == 'x') {\r
+ tb->eyeY=tb->eyeY-1;\r
+ return True;\r
+ } else if (c == 'd') {\r
+ tb->eyeZ=tb->eyeZ+1;\r
+ return True;\r
+ } else if (c == 'c') {\r
+ tb->eyeZ=tb->eyeZ-1;\r
+ return True;\r
+ } else if (c == 'f') {\r
+ camX=camX+1;\r
+ return True;\r
+ } else if (c == 'v') {\r
+ camX=camX-1;\r
+ return True;\r
+ } else if (c == 'g') {\r
+ camY=camY+1;\r
+ return True;\r
+ } else if (c == 'b') {\r
+ camY=camY-1;\r
+ return True;\r
+ } else if (c == 'h') {\r
+ camZ=camZ+1;\r
+ return True;\r
+ } else if (c == 'n') {\r
+ camZ=camZ-1;\r
+ return True;\r
+ } else if (c == 'r') {\r
+ tb->rotation += 1;\r
+ return True;\r
+ }\r
+ }\r
+ return False;\r
+}\r
+\r
+/* this is tha main change for v5 compatability and acompanying ENTRYPOINTS */\r
+XSCREENSAVER_MODULE_2 ("topBlock", topblock, topBlock)\r
+\r
+#endif /* USE_GL */\r