[SDL] ot - storing game data
Sami Näätänen
sn.ml at bayminer.com
Sat Sep 8 04:22:35 PDT 2007
On Monday 03 September 2007, neil at cloudsprinter.com wrote:
> arrgh everone is throwing cpp at me gain! i can barkey cope with c!
>
> (q the cpp is easier / better arguments.. but i know c i like c, it
> does it for
> me ;) )
>
> anyway just for the record none of this data will be on disk, i'm
> keeping everyhting in .h files to be compiled as part of the binary.
>
> i'm just about to try setting up the stuff ken homebrew dot com
> mailed in so hopefully i can have some sane code soon.
>
So here is some C then. ;)
Makes it very easy to add more boards. You of course want to add some
sanity checking before you use the board data.
One thing that needs checking is the length of the data String of each
boards. And of course you want to check that they contain only valid
chars.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct board {
int width;
int height;
char* data;
} Board;
// "12345678901234567890123456789012"
static struct board boards[] = { {5,5, "here is w*h map chars=25." },
{3,3, "OXO" // Easier to do it like this
"OXX" // So that we see the map layout
"XOX" },
{15,8,"xxxxxxxxxxxxxxx"
"x x"
"x xxxxxxxxx x x"
"x x x x x x"
"x x x x x x"
"x x xxxxxxxxx x"
"x x"
"xxxxxxxxxxxxxxx" } };
// How to access data in a specific board at specific x,y position
int getBoardData( int n, int x, int y ) {
char* data = NULL;
if( x < 0 || y < 0 ||
x >= boards[n].width || y >= boards[n].height )
return -1; // not inside the board
data = boards[n].data;
return ((int) *(data+y*boards[n].width+x));
}
int
main( int argc, char* argv[] ) {
int i,j,k;
int board_count = sizeof(boards)/sizeof(struct board);
// Print out the boards.
for( i=0; i<board_count; ++i ) {
printf( "Board: %d\n", i );
for( j=0; j<boards[i].height; j++ ) {
printf("%c",'\'');
for( k=0; k<boards[i].width; k++ )
printf( "%c", (char)getBoardData( i, k, j ) );
printf( "'%c", '\n' );
}
printf( "%s", "\n\n" );
}
return 0;
}
More information about the SDL
mailing list