From 71045152800ab0cc0dce6ec70dba9d7f9bb9dab5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 18 Jun 2009 17:20:59 -0400 Subject: Started Board class --- Makefile | 2 +- board.cpp | 46 +++++++++++++++++++++++++++++ board.h | 17 +++++++++++ gamestate.cpp | 92 ++++++++++++++++++++-------------------------------------- gamestate.h | 7 ++--- includes.h | 2 ++ mazeoflife.cpp | 28 ++++++++++++++++-- mazeoflife.h | 3 ++ 8 files changed, 130 insertions(+), 67 deletions(-) create mode 100644 board.cpp create mode 100644 board.h diff --git a/Makefile b/Makefile index 0eb1738..3361044 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS = mazeoflife.o gamestate.o +OBJS = mazeoflife.o gamestate.o board.o CC = g++ CFLAGS = `pkg-config sdl --cflags` LIBS = `pkg-config sdl --libs` diff --git a/board.cpp b/board.cpp new file mode 100644 index 0000000..ca0af83 --- /dev/null +++ b/board.cpp @@ -0,0 +1,46 @@ +#include "includes.h" + +Board::Board() +{ + int x,y; + for (y=0;y 10 && x < 20 && y > 10 && y < 20) + { + blocks[x][y] = rand() % 2; + } else { + blocks[x][y] = false; + } + } + } + + on = getColor(0, 0, 0); + off = getColor(255, 255, 255); +} + +bool Board::isObstructed(int x, int y) +{ + return blocks[x][y]; +} + +void Board::render(SDL_Surface* screen) +{ + SDL_Rect block; + block.w = 16; + block.h = 16; + + int x,y; + + for (y=0;y= WIDTH) - { - *x = *x-WIDTH; - } else if (*y >= HEIGHT) - { - *y = *y-HEIGHT; - } -} - void GameState::render(SDL_Surface* screen) { - SDL_Rect block; - block.w = 16; - block.h = 16; - - int x,y; - - for (y=0;y #include +#include #include "mazeoflife.h" +#include "board.h" #include "state.h" #include "gamestate.h" diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 20bee1d..06a2ca2 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp @@ -1,9 +1,12 @@ #include "includes.h" +SDL_Surface *screen; bool gameSleep = false; int main(int argc, char *argv[]) -{ +{ + srand(time(NULL)); + /* Initialize defaults, Video and Audio */ if((SDL_Init(SDL_INIT_VIDEO)==-1)) { printf("Could not initialize SDL: %s.\n", SDL_GetError()); @@ -17,7 +20,7 @@ int main(int argc, char *argv[]) * Initialize the display in a 640x480 8-bit palettized mode, * requesting a software surface */ - SDL_Surface *screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF); + screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF); if ( screen == NULL ) { fprintf(stderr, "Couldn't set %dx%dx8 video mode: %s\n", WIDTH*16, WIDTH*16, SDL_GetError()); exit(1); @@ -61,3 +64,24 @@ int main(int argc, char *argv[]) exit(0); } +void wrap(int* x, int* y) +{ + if (*x < 0) + { + *x = WIDTH-(0-*x); + } else if (*y < 0) + { + *y = HEIGHT-(0-*y); + } else if (*x >= WIDTH) + { + *x = *x-WIDTH; + } else if (*y >= HEIGHT) + { + *y = *y-HEIGHT; + } +} + +Uint32 getColor(int r, int g, int b) +{ + return SDL_MapRGB(screen->format, r, g, b); +} diff --git a/mazeoflife.h b/mazeoflife.h index 3cc6d6d..6af0cf4 100644 --- a/mazeoflife.h +++ b/mazeoflife.h @@ -4,4 +4,7 @@ const int WIDTH = 30; const int HEIGHT = 30; +void wrap(int* x, int* y); +Uint32 getColor(int r, int g, int b); + #endif -- cgit 1.4.1