2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
conway.cpp
|
|
|
|
Conway's Game of Life.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2012-09-22 15:19:43 -04:00
|
|
|
#include <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
|
|
|
|
2012-12-15 18:18:28 -05:00
|
|
|
#include <dispd.h>
|
|
|
|
|
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
|
|
|
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
|
|
|
int Init()
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-09-08 14:55:43 -04:00
|
|
|
int usage(int /*argc*/, char* argv[])
|
2011-11-09 17:54:35 -05:00
|
|
|
{
|
|
|
|
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[])
|
|
|
|
{
|
2012-12-15 18:18:28 -05:00
|
|
|
if ( !dispd_initialize(&argc, &argv) )
|
|
|
|
error(1, 0, "couldn't initialize dispd library");
|
|
|
|
|
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; }
|
|
|
|
|
2012-12-15 18:18:28 -05:00
|
|
|
struct dispd_session* session = dispd_attach_default_session();
|
|
|
|
if ( !session )
|
|
|
|
error(1, 0, "couldn't attach to dispd default session");
|
|
|
|
if ( !dispd_session_setup_game_vga(session) )
|
|
|
|
error(1, 0, "couldn't setup dispd vga session");
|
|
|
|
struct dispd_window* window = dispd_create_window_game_vga(session);
|
|
|
|
if ( !window )
|
|
|
|
error(1, 0, "couldn't create dispd vga window");
|
|
|
|
|
2011-08-28 18:09:37 -04:00
|
|
|
// 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();
|
2012-12-15 18:18:28 -05:00
|
|
|
struct dispd_framebuffer* fb = dispd_begin_render(window);
|
|
|
|
if ( !fb )
|
|
|
|
error(1, 0, "unable to begin rendering dispd window");
|
|
|
|
memcpy(dispd_get_framebuffer_data(fb), frame, sizeof(frame));
|
|
|
|
dispd_finish_render(fb);
|
2011-08-28 18:09:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|