Port snake to dispd.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-12-16 00:18:20 +01:00
parent b99fd83d9a
commit 872954b364
1 changed files with 19 additions and 34 deletions

View File

@ -32,12 +32,13 @@
#include <error.h>
#include <string.h>
#include <dispd.h>
const int width = 80;
const int height = 25;
const int buffersize = height * width;
int vgafd;
uint16_t frame[width*height];
int posx;
@ -93,41 +94,9 @@ void Reset()
speed = defaultspeed;
}
bool FlushVGA()
{
if ( lseek(vgafd, 0, SEEK_SET) < 0)
return false;
return writeall(vgafd, frame, sizeof(frame)) == sizeof(frame);
}
int Init()
{
bool has_vga_mode_set = true;
FILE* modefp = fopen("/dev/video/mode", "r");
if ( modefp )
{
char* mode = NULL;
size_t modesize;
if ( 0 <= getline(&mode, &modesize, modefp) )
{
if ( strcmp(mode, "driver=none\n") != 0 )
has_vga_mode_set = false;
free(mode);
}
fclose(modefp);
}
if ( !has_vga_mode_set )
{
fprintf(stderr, "Sorry, this game only works in VGA mode.\n");
return 1;
}
vgafd = open("/dev/vga", O_RDWR);
if ( vgafd < 0 ) { error(0, errno, "unable to open vga device /dev/vga"); return 1; }
Reset();
return 0;
}
@ -229,6 +198,9 @@ void Update()
int main(int argc, char* argv[])
{
if ( !dispd_initialize(&argc, &argv) )
error(1, 0, "couldn't initialize dispd library");
for ( int i = 1; i < argc; i++ )
{
if ( strcmp("--tabhack", argv[i]) == 0 ) { tabhack = true; }
@ -237,12 +209,25 @@ int main(int argc, char* argv[])
int result = Init();
if ( result != 0 ) { return result; }
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");
// Update the game every once in a while.
while ( true )
{
usleep(speed * 1000);
Update();
FlushVGA();
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);
}
return 0;