// Genesaver: copyright 2003 Sam Stafford. #include <stdio.h> #include <stdlib.h> #include <gl/glut.h> #include <time.h> #ifdef WIN32 #include <direct.h> #include <windows.h> #endif //WIN32 #include "globals.h" #include "DNA.h" #include "World.h" #include "Jungle.h" int config(); void display(); void timer( int ); void kdone( unsigned char, int, int ); void mdone( int, int ); void load(); void loadsettings(); int save(); World* world; Settings settings; FSettings fsettings; // mouse tracking for mdone() bool mstart; int mx, my; int main( int argc, char** argv ); #ifdef WIN32 int WINAPI WinMain(HINSTANCE h,HINSTANCE,LPSTR s,int) { //Return 0 if arg isn't "/s". FIXME: "/p" for preview if ( !(*s && *s == '/' ) ) return 0; chdir( getenv( "SystemRoot" ) ); chdir( "system32" ); char a = *(s+1); if ( a == 'c' ) return config(); if ( a != 's' ) return 0; //Just call the real main() function with phony args. char** argv = (char**) malloc( 32 ); argv[0] = "Genesaver"; argv[1] = "foo"; argv[2] = "bar"; return main( 1, argv ); } #endif //WIN32 int main( int argc, char** argv ) { srand( time(NULL ) ); mstart = false; loadsettings(); glutInit( &argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | ( settings.AA ? GLUT_ALPHA : 0 ) ); glutCreateWindow( "Genesaver" ); glutFullScreen(); if ( settings.lizard ) world = new Jungle(); else world = new World(); load(); glutSetCursor( GLUT_CURSOR_NONE ); if ( settings.AA ) { // Thanks to Adrian for making this work! // And thanks to Matt for helping me test the problem that // manifested only on his machine and noplace else. glEnable( GL_BLEND ); glEnable( GL_POLYGON_SMOOTH ); if ( settings.AAline) glEnable( GL_LINE_SMOOTH ); glBlendFunc (GL_SRC_ALPHA, GL_ONE); glHint( GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE ); } glShadeModel( settings.shade ? GL_SMOOTH : GL_FLAT ); glutDisplayFunc( display ); glutKeyboardFunc( kdone ); glutPassiveMotionFunc( mdone ); glutTimerFunc( 1, timer, 0 ); glutMainLoop(); return 0; } void display() { bool drew = true; switch( settings.camera ) { case C_CHASE: world->RenderTagged(); break; case C_WORLD: world->Render(); break; case C_GRAPH: drew = world->RenderGraph(); break; } if ( drew ) { glutSwapBuffers(); glFlush(); } } void timer( int val ) { world->Step(); glutPostRedisplay(); glutTimerFunc( 1, timer, 0 ); } void kdone( unsigned char c, int, int ) { switch ( c ) { case 'c': switch( settings.camera ) { case C_WORLD: settings.camera = C_CHASE; break; case C_GRAPH: settings.camera = C_WORLD; break; case C_CHASE: settings.camera = C_GRAPH; int rc = world->rgcount; world->rgcount = 1; world->RenderGraph(); glutSwapBuffers(); glFlush(); world->rgcount = rc; } break; case 'v': world->ViewChange(); break; case '+': case '=': if ( settings.zoom < 1000 ) settings.zoom += 5; break; case '_': case '-': if ( settings.zoom > 10 ) settings.zoom -= 5; break; default: exit( save() ); } } void mdone( int x, int y ) { if ( !mstart ) { mx = x; my = y; mstart = true; return; } // Exit if the mouse moves more than 100 pixels. if ( (x-mx)*(x-mx)+(y-my)*(y-my) < 10000 ) return; exit( save() ); } void load() { FILE* f = fopen( "Genes.txt", "r" ); if ( !f ) return; fseek( f, 0, SEEK_END ); int size = ftell( f ); fseek( f, 0, SEEK_SET ); if ( !size ) return; char* dfile = new char[size]; for ( int a = 0 ; a < size ; a++ ) dfile[a] = fgetc( f ); fclose( f ); world->Load( dfile ); } int save() { if ( !settings.save ) return 0; FILE* f = fopen( "Genes.tmp", "w" ); if ( !f ) return 1; world->Save( f ); int e = fclose( f ); if ( e ) return e; unlink( "Genes.txt" ); return rename( "Genes.tmp", "Genes.txt" ); } float RandFloat() { return rand() / ( 1.0f * RAND_MAX ); } int RandInt( int foo ) { return rand() % ( foo + 1 ); } void SetColor( Color c ) { float alpha = settings.alpha; switch ( c ) { case Black: glColor4f( 0.0, 0.0, 0.0, alpha ); break; case Blue: glColor4f( 0.0, 0.0, 1.0, alpha ); break; case Cyan: glColor4f( 0.0, 1.0, 1.0, alpha ); break; case Green: glColor4f( 0.0, 1.0, 0.0, alpha ); break; case Grey: glColor4f( 0.5, 0.5, 0.5, alpha ); break; case Magenta: glColor4f( 1.0, 0.0, 1.0, alpha ); break; case Red: glColor4f( 1.0, 0.0, 0.0, alpha ); break; case Yellow: glColor4f( 1.0, 1.0, 0.0, alpha ); break; } } void SetColor21( Color c, float f ) //set color for graph { float r, g, b, inc, dec; inc = dec = 0.0; f *= 1.2; if ( f < 0.0 ) f = 0.0; else if ( f > 1.0 ) f = 1.0; if ( f < 0.5 ) //darken primary dec = ( 1.0 - 2.0 * f ) * 0.64; else //brighten all others inc = ( 2.0 * f - 1.0 ) * 0.64; switch( c ) { case Red: r = 1.0 - dec; g = b = inc; break; case Green: g = 1.0 - dec; r = b = inc; break; case Blue: b = 1.0 - dec; r = g = inc; break; } glColor3f( r, g, b ); } void SetHue( float d ) { if ( d > 1.0 ) d = 1.0; else if ( d < 0.0 ) d = 0.0; float r, g, b; r = g = b = 0.0; d += 1.0 / 6.0; if ( d > 1.0 ) d -= 1.0; if ( d <= 1.0 / 3.0 ) { r = -3.0 * d + 1.0; b = 3.0 * d; } else if ( d <= 2.0 / 3.0 ) { d -= 1.0 / 3.0; b = -3.0 * d + 1.0; g = 3.0 * d; } else { d -= 2.0 / 3.0; g = -3.0 * d + 1.0; r = 3.0 * d; } glColor4f( r, g, b, settings.alpha ); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 5393 | Sam Stafford |
Integrate in Marc's porting changes, and merge my own relevant winmain.cpp changes into main.cpp. (Thanks Marc!!!) |
||
#3 | 3354 | Sam Stafford |
Code refactoring - split "main" functions into main.cpp (non-Windows) and winmain.cpp (Windows), with all shared code going in util.cpp. No functional changes. |
||
#2 | 3353 | Sam Stafford |
Use Windows <scrnsave.h> and OpenGL functions instead of GLUT. This should fix all sorts of bugs involving the screensaver not terminating when it should, spawning new processes, et cetera. Code shuffling to follow, since right now it's a tangled mess. |
||
#1 | 3052 | Sam Stafford |
Add Genesaver to the Public Depot. It's not in any way Perforce-related, but it does share a bit of code with Jamgraph, and it feels strange to have an open-source project that's not in the PD. |