2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
conway.cpp
|
|
|
|
Conway's Game of Life.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2011-08-28 18:09:37 -04:00
|
|
|
#include <libmaxsi/platform.h>
|
|
|
|
#include <libmaxsi/sortix-vga.h>
|
2012-01-22 17:46:41 -05:00
|
|
|
#include <sys/keycodes.h>
|
|
|
|
#include <sys/termmode.h>
|
2011-11-09 17:54:35 -05:00
|
|
|
#include <stdio.h>
|
2011-11-25 07:38:31 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-12-04 09:04:06 -05:00
|
|
|
#include <error.h>
|
2011-11-25 07:38:31 -05:00
|
|
|
#include <string.h>
|
2011-11-10 06:27:31 -05:00
|
|
|
|
2011-08-28 18:09:37 -04:00
|
|
|
const int width = 80;
|
|
|
|
const int height = 25;
|
|
|
|
|
|
|
|
const int rowstride = width + 2;
|
|
|
|
const int buffersize = (height+2) * (width+2);
|
|
|
|
|
2011-11-25 07:38:31 -05:00
|
|
|
int vgafd;
|
|
|
|
uint16_t frame[width*height];
|
2011-08-28 18:09:37 -04:00
|
|
|
|
|
|
|
bool running;
|
|
|
|
int posx;
|
|
|
|
int posy;
|
|
|
|
|
|
|
|
char framea[buffersize];
|
|
|
|
char frameb[buffersize];
|
|
|
|
char* currentframe;
|
|
|
|
char* lastframe;
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
// Reset the game data.
|
|
|
|
for ( int i = 0; i < buffersize; i++ ) { framea[i] = 0; frameb[i] = 0; }
|
|
|
|
}
|
|
|
|
|
2011-11-25 07:38:31 -05:00
|
|
|
bool FlushVGA()
|
2011-08-28 18:09:37 -04:00
|
|
|
{
|
2012-03-24 10:34:30 -04:00
|
|
|
return writeall(vgafd, frame, sizeof(frame)) < sizeof(frame);
|
2011-11-25 07:38:31 -05:00
|
|
|
}
|
2011-08-28 18:09:37 -04:00
|
|
|
|
2011-11-25 07:38:31 -05:00
|
|
|
int Init()
|
|
|
|
{
|
|
|
|
vgafd = open("/dev/vga", O_RDWR);
|
2011-12-04 09:04:06 -05:00
|
|
|
if ( vgafd < 0 ) { error(0, errno, "unable to open vga device /dev/vga"); return 1; }
|
2011-08-28 18:09:37 -04:00
|
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
// Initialize variables.
|
|
|
|
currentframe = framea;
|
|
|
|
lastframe = frameb;
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
posy = height / 2 + 1;
|
|
|
|
posx = width / 2 + 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cycle()
|
|
|
|
{
|
|
|
|
// Run a cycle of the game of life.
|
|
|
|
for ( int y = 1; y <= height; y++ )
|
|
|
|
{
|
|
|
|
for ( int x = 1; x <= width; x++ )
|
|
|
|
{
|
|
|
|
int alivearound = 0;
|
|
|
|
int current = lastframe[y * rowstride + x];
|
|
|
|
|
|
|
|
if ( lastframe[(y-1) * rowstride + (x-1)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y-1) * rowstride + (x-0)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y-1) * rowstride + (x+1)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y-0) * rowstride + (x-1)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y-0) * rowstride + (x+1)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y+1) * rowstride + (x-1)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y+1) * rowstride + (x-0)] > 0 ) { alivearound++; }
|
|
|
|
if ( lastframe[(y+1) * rowstride + (x+1)] > 0 ) { alivearound++; }
|
|
|
|
|
|
|
|
currentframe[y * rowstride + x] = ( (current > 0 && (alivearound == 3 || alivearound == 2)) || (current == 0 && alivearound == 3) ) ? 1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap buffers.
|
|
|
|
char* tmp = lastframe; lastframe = currentframe; currentframe = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Render()
|
|
|
|
{
|
2011-11-25 07:38:31 -05:00
|
|
|
uint16_t* dest = frame;
|
2011-08-28 18:09:37 -04:00
|
|
|
|
|
|
|
uint16_t set = 'O' | (COLOR8_LIGHT_GREY << 8);
|
|
|
|
uint16_t unset = ' ' | (COLOR8_LIGHT_GREY << 8);
|
|
|
|
|
|
|
|
// Render each cell directly to the VGA framebuffer!
|
|
|
|
for ( int y = 1; y <= height; y++ )
|
|
|
|
{
|
|
|
|
for ( int x = 1; x <= width; x++ )
|
|
|
|
{
|
|
|
|
dest[(y-1) * width + (x-1)] = (lastframe[y * rowstride + x] > 0) ? set : unset;
|
|
|
|
|
|
|
|
// Render the cursor.
|
|
|
|
if ( !running && x == posx && y == posy )
|
|
|
|
{
|
|
|
|
dest[(y-1) * width + (x-1)] |= (COLOR8_RED << 12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-25 07:38:31 -05:00
|
|
|
|
|
|
|
FlushVGA();
|
2011-08-28 18:09:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
// Read the keyboard input from the user.
|
2012-01-22 17:46:41 -05:00
|
|
|
unsigned termmode = TERMMODE_KBKEY
|
|
|
|
| TERMMODE_UNICODE
|
|
|
|
| TERMMODE_SIGNAL
|
|
|
|
| TERMMODE_NONBLOCK;
|
|
|
|
if ( settermmode(0, termmode) ) { error(1, errno, "settermmode"); }
|
|
|
|
while ( true )
|
2011-08-28 18:09:37 -04:00
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
uint32_t codepoint;
|
|
|
|
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
|
|
|
|
if ( !numbytes ) { break; }
|
|
|
|
if ( numbytes < 0 ) { break; }
|
|
|
|
int kbkey = KBKEY_DECODE(codepoint);
|
|
|
|
if ( kbkey == KBKEY_R ) { running = !running; }
|
|
|
|
if ( running ) { continue; }
|
|
|
|
if ( kbkey == KBKEY_C ) { Clear(); }
|
|
|
|
if ( kbkey == KBKEY_W ) { if ( posy > 1 ) { posy--; } }
|
|
|
|
if ( kbkey == KBKEY_A ) { if ( posx > 1 ) { posx--; } }
|
|
|
|
if ( kbkey == KBKEY_S ) { if ( posy < height ) { posy++; } }
|
|
|
|
if ( kbkey == KBKEY_D ) { if ( posx < width ) { posx++; } }
|
|
|
|
if ( kbkey == KBKEY_SPACE )
|
2011-08-28 18:09:37 -04:00
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
lastframe[posy * rowstride + posx] = 1 - lastframe[posy * rowstride + posx];
|
2011-08-28 18:09:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a cycle.
|
|
|
|
if ( running ) { Cycle(); }
|
|
|
|
|
|
|
|
Render();
|
|
|
|
}
|
|
|
|
|
2011-11-09 17:54:35 -05:00
|
|
|
int usage(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
printf("usage: %s [OPTIONS]\n", argv[0]);
|
|
|
|
printf("Options:\n");
|
|
|
|
printf(" --speed <miliseconds> How many miliseconds between updates\n");
|
|
|
|
printf(" --usage Display this screen\n");
|
|
|
|
printf(" --help Display this screen\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:09:37 -04:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2011-11-09 17:54:35 -05:00
|
|
|
int sleepms = 50;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
if ( strcmp(argv[i], "--help") == 0 ) { return usage(argc, argv); }
|
|
|
|
if ( strcmp(argv[i], "--usage") == 0 ) { return usage(argc, argv); }
|
|
|
|
if ( strcmp(argv[i], "--speed") == 0 && 1 < argc-i )
|
2011-11-09 17:54:35 -05:00
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
sleepms = atoi(argv[++i]);
|
2011-11-09 17:54:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:09:37 -04:00
|
|
|
int result = Init();
|
|
|
|
if ( result != 0 ) { return result; }
|
|
|
|
|
|
|
|
// Update the game every 50th milisecond.
|
|
|
|
while ( true )
|
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
usleep(sleepms * 1000);
|
2011-08-28 18:09:37 -04:00
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|