| 1 | /* |
|---|
| 2 | * Copyright (C) 2007 Michael Lewis |
|---|
| 3 | * Author: Mike Lewis <mikelikespie@gmail.com> |
|---|
| 4 | * |
|---|
| 5 | * This work is provide AS IS, and has no warranty. |
|---|
| 6 | * The author is NOT responsible for anything that happens |
|---|
| 7 | * due to use of this code, either using it or running it on |
|---|
| 8 | * your system |
|---|
| 9 | */ |
|---|
| 10 | #ifdef __SSE2__ |
|---|
| 11 | #include <xmmintrin.h> |
|---|
| 12 | #include <emmintrin.h> |
|---|
| 13 | #endif |
|---|
| 14 | |
|---|
| 15 | #include <stdlib.h> |
|---|
| 16 | #include <stdio.h> |
|---|
| 17 | |
|---|
| 18 | #ifdef __APPLE__ |
|---|
| 19 | #include <GLUT/glut.h> |
|---|
| 20 | #include <OpenGL/glu.h> |
|---|
| 21 | #else |
|---|
| 22 | #include <GL/glut.h> |
|---|
| 23 | #include <GL/glu.h> |
|---|
| 24 | #endif |
|---|
| 25 | |
|---|
| 26 | #include "types.h" |
|---|
| 27 | #include "helpers.h" |
|---|
| 28 | #include "block.h" |
|---|
| 29 | |
|---|
| 30 | #ifndef FALSE |
|---|
| 31 | #define FALSE 0 |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #ifndef TRUE |
|---|
| 35 | #define TRUE (!FALSE) |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | llife_world my_world; |
|---|
| 39 | |
|---|
| 40 | void reshape( int width, int height ) { |
|---|
| 41 | height = height <= 0 ? 1 : height; |
|---|
| 42 | float ratio = (float) width / (float) height * 2; |
|---|
| 43 | float diagonal = sqrt( width * width + height * height ); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | glMatrixMode( GL_PROJECTION ); |
|---|
| 47 | glLoadIdentity(); |
|---|
| 48 | glViewport(0, 0, width, height); |
|---|
| 49 | glOrtho( 0, my_world->width * 128, my_world->height * BLOCK_HEIGHT, 0, -1.0, 1.0 ); |
|---|
| 50 | glMatrixMode(GL_MODELVIEW); |
|---|
| 51 | glLoadIdentity(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | void drawRow( __m128i A, int x, int y, int lines ) { |
|---|
| 56 | int i, j; |
|---|
| 57 | for( i = 0; i < 8; i++ ) { |
|---|
| 58 | __m128i fin = unpack( A, i ); |
|---|
| 59 | unsigned char *f = (unsigned char*)&fin; |
|---|
| 60 | for( j = 0; j < 16; j++ ) { |
|---|
| 61 | if( *f ) { |
|---|
| 62 | int xx = x + i * 16 + j; |
|---|
| 63 | if( !lines ) { |
|---|
| 64 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); |
|---|
| 65 | glColor4f(.5, .5, .8, .7 ); |
|---|
| 66 | glBegin( GL_TRIANGLE_STRIP ); |
|---|
| 67 | glVertex3f( 1 + xx, 1 + y, 0 ); |
|---|
| 68 | glVertex3f( 0 + xx, 1 + y, 0 ); |
|---|
| 69 | |
|---|
| 70 | glVertex3f( 1 + xx, 0 + y, 0 ); |
|---|
| 71 | glVertex3f( 0 + xx, 0 + y, 0 ); |
|---|
| 72 | glEnd(); |
|---|
| 73 | } else { |
|---|
| 74 | glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); |
|---|
| 75 | //glBlendFunc(GL_SRC_ALPHA,GL_ONE); |
|---|
| 76 | glColor4f(.40, .40, .40, 1.0 ); |
|---|
| 77 | glBegin( GL_POLYGON ); |
|---|
| 78 | glVertex3f( 1 + xx, 1 + y, .10 ); |
|---|
| 79 | glVertex3f( 0 + xx, 1 + y, .10 ); |
|---|
| 80 | glVertex3f( 0 + xx, 0 + y, .10 ); |
|---|
| 81 | glVertex3f( 1 + xx, 0 + y, .10 ); |
|---|
| 82 | glEnd(); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | f++; |
|---|
| 86 | } |
|---|
| 87 | // printf( " " ); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void renderBlock( llife_block block, int x, int y, int lines ) { |
|---|
| 92 | int i; |
|---|
| 93 | for( i = 0; i < 128; i++ ) { |
|---|
| 94 | drawRow((*block->current)[i], x, y + i, lines ); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | void renderScene() { |
|---|
| 100 | glClear( GL_COLOR_BUFFER_BIT ); |
|---|
| 101 | int i, j; |
|---|
| 102 | |
|---|
| 103 | for( i = 0; i < my_world->width; i++ ) { |
|---|
| 104 | for( j = 0; j < my_world->height; j++ ) { |
|---|
| 105 | //printf( "%x\n", *my_world->blocks ); |
|---|
| 106 | renderBlock( get_life_block( my_world, j, i ) , i*128, j*BLOCK_HEIGHT, FALSE ); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | for( i = 0; i < my_world->width; i++ ) { |
|---|
| 110 | for( j = 0; j < my_world->height; j++ ) { |
|---|
| 111 | //printf( "%x\n", *my_world->blocks ); |
|---|
| 112 | renderBlock( get_life_block( my_world, j, i ) , i*128, j*BLOCK_HEIGHT, TRUE ); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | glFlush(); |
|---|
| 118 | glutSwapBuffers(); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void myTimer(int value) { |
|---|
| 122 | glutTimerFunc(1000/120,myTimer,1); |
|---|
| 123 | |
|---|
| 124 | worldCycle( my_world ); |
|---|
| 125 | glutPostRedisplay(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | int main( int argc, char** argv) { |
|---|
| 129 | int i, j; |
|---|
| 130 | |
|---|
| 131 | int width = 1, height = 1; |
|---|
| 132 | |
|---|
| 133 | int temp_dim; |
|---|
| 134 | for( i = 0; i < argc; i++ ) { |
|---|
| 135 | if( !strcmp( "-h", argv[i] ) && i + 1 < argc ){ |
|---|
| 136 | temp_dim = atoi( argv[i+1] ); |
|---|
| 137 | i++; |
|---|
| 138 | if( temp_dim ) { |
|---|
| 139 | height = temp_dim; |
|---|
| 140 | } |
|---|
| 141 | } else if( !strcmp( "-w", argv[i] ) && i + 1 < argc ){ |
|---|
| 142 | temp_dim = atoi( argv[i+1] ); |
|---|
| 143 | i++; |
|---|
| 144 | if( temp_dim ) { |
|---|
| 145 | width = temp_dim; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | my_world = allocate_world( width, height ); |
|---|
| 152 | *(my_world->blocks) = allocate_block(); |
|---|
| 153 | printf( "%x\n", *my_world->blocks ); |
|---|
| 154 | |
|---|
| 155 | set_bit_in_world( my_world, 100-50, 100-50 ); |
|---|
| 156 | set_bit_in_world( my_world, 100-51, 100-50 ); |
|---|
| 157 | set_bit_in_world( my_world, 100-52, 100-50 ); |
|---|
| 158 | set_bit_in_world( my_world, 100-54, 100-50 ); |
|---|
| 159 | |
|---|
| 160 | set_bit_in_world( my_world, 100-50, 100-51 ); |
|---|
| 161 | |
|---|
| 162 | set_bit_in_world( my_world, 100-53, 100-52 ); |
|---|
| 163 | set_bit_in_world( my_world, 100-54, 100-52 ); |
|---|
| 164 | |
|---|
| 165 | set_bit_in_world( my_world, 100-51, 100-53 ); |
|---|
| 166 | set_bit_in_world( my_world, 100-52, 100-53 ); |
|---|
| 167 | set_bit_in_world( my_world, 100-54, 100-53 ); |
|---|
| 168 | |
|---|
| 169 | set_bit_in_world( my_world, 100-50, 100-54 ); |
|---|
| 170 | set_bit_in_world( my_world, 100-52, 100-54 ); |
|---|
| 171 | set_bit_in_world( my_world, 100-54, 100-54 ); |
|---|
| 172 | |
|---|
| 173 | glutInit( &argc, argv ); |
|---|
| 174 | glutInitWindowPosition( -1, -1 ); |
|---|
| 175 | glutInitWindowSize( 800, 800 ); |
|---|
| 176 | glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); |
|---|
| 177 | glutCreateWindow( "Conway's game of Life" ); |
|---|
| 178 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|---|
| 179 | // glDisable(GL_DEPTH_TEST); |
|---|
| 180 | //glEnable(GL_BLEND); |
|---|
| 181 | glutDisplayFunc( renderScene ); |
|---|
| 182 | glutReshapeFunc( reshape ); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | glutTimerFunc(1000.*5,myTimer,1); |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | glutMainLoop(); |
|---|
| 189 | |
|---|
| 190 | printf( "meow\n"); |
|---|
| 191 | return 0; |
|---|
| 192 | } |
|---|