summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--board.cpp46
-rw-r--r--board.h17
-rw-r--r--gamestate.cpp92
-rw-r--r--gamestate.h7
-rw-r--r--includes.h2
-rw-r--r--mazeoflife.cpp28
-rw-r--r--mazeoflife.h3
8 files changed, 130 insertions, 67 deletions
diff --git a/Makefile b/Makefile index 0eb1738..3361044 100644 --- a/Makefile +++ b/Makefile
@@ -1,4 +1,4 @@
1OBJS = mazeoflife.o gamestate.o 1OBJS = mazeoflife.o gamestate.o board.o
2CC = g++ 2CC = g++
3CFLAGS = `pkg-config sdl --cflags` 3CFLAGS = `pkg-config sdl --cflags`
4LIBS = `pkg-config sdl --libs` 4LIBS = `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 @@
1#include "includes.h"
2
3Board::Board()
4{
5 int x,y;
6 for (y=0;y<HEIGHT;y++)
7 {
8 for (x=0;x<WIDTH;x++)
9 {
10 if (x > 10 && x < 20 && y > 10 && y < 20)
11 {
12 blocks[x][y] = rand() % 2;
13 } else {
14 blocks[x][y] = false;
15 }
16 }
17 }
18
19 on = getColor(0, 0, 0);
20 off = getColor(255, 255, 255);
21}
22
23bool Board::isObstructed(int x, int y)
24{
25 return blocks[x][y];
26}
27
28void Board::render(SDL_Surface* screen)
29{
30 SDL_Rect block;
31 block.w = 16;
32 block.h = 16;
33
34 int x,y;
35
36 for (y=0;y<HEIGHT;y++)
37 {
38 for (x=0;x<WIDTH;x++)
39 {
40 block.x = x*16;
41 block.y = y*16;
42
43 SDL_FillRect(screen, &block, (blocks[x][y] ? on : off));
44 }
45 }
46}
diff --git a/board.h b/board.h new file mode 100644 index 0000000..e2dcd8d --- /dev/null +++ b/board.h
@@ -0,0 +1,17 @@
1#ifndef BOARD_H
2#define BOARD_H
3
4class Board
5{
6 private:
7 Uint32 on;
8 Uint32 off;
9 bool blocks[WIDTH][HEIGHT];
10
11 public:
12 Board();
13 bool isObstructed(int x, int y);
14 void render(SDL_Surface* screen);
15};
16
17#endif
diff --git a/gamestate.cpp b/gamestate.cpp index 0bcf3d7..fd115f8 100644 --- a/gamestate.cpp +++ b/gamestate.cpp
@@ -2,43 +2,44 @@
2 2
3GameState::GameState(SDL_PixelFormat* fmt) 3GameState::GameState(SDL_PixelFormat* fmt)
4{ 4{
5 int x,y;
6 for (y=0;y<HEIGHT;y++)
7 {
8 for (x=0;x<WIDTH;x++)
9 {
10 blocks[x][y] = false;
11 }
12 }
13
14 on = SDL_MapRGB(fmt, 0, 0, 0);
15 off = SDL_MapRGB(fmt, 255, 255, 255);
16 player_color = SDL_MapRGB(fmt, 255, 255, 0); 5 player_color = SDL_MapRGB(fmt, 255, 255, 0);
17 6
18 playerx = 1; 7 newGame = true;
19 playery = 1; 8 doneMaking = false;
20} 9}
21 10
22void GameState::input(SDLKey key) 11void GameState::input(SDLKey key)
23{ 12{
24 switch (key) 13 if (doneMaking)
25 { 14 {
26 case SDLK_LEFT: 15 switch (key)
27 move(playerx-1, playery); 16 {
17 case SDLK_LEFT:
18 move(playerx-1, playery);
28 19
29 break; 20 break;
30 case SDLK_RIGHT: 21 case SDLK_RIGHT:
31 move(playerx+1, playery); 22 move(playerx+1, playery);
32 23
33 break; 24 break;
34 case SDLK_UP: 25 case SDLK_UP:
35 move(playerx, playery-1); 26 move(playerx, playery-1);
36 27
37 break; 28 break;
38 case SDLK_DOWN: 29 case SDLK_DOWN:
39 move(playerx, playery+1); 30 move(playerx, playery+1);
31
32 break;
33 }
34 }
40 35
41 break; 36 if (newGame)
37 {
38 playerx = 1;
39 playery = 1;
40 board = Board();
41 newGame = false;
42 doneMaking = false;
42 } 43 }
43} 44}
44 45
@@ -46,50 +47,21 @@ void GameState::move(int x, int y)
46{ 47{
47 wrap(&x, &y); 48 wrap(&x, &y);
48 49
49 if (blocks[x][y]) return; 50 if (board.isObstructed(x,y)) return;
50 51
51 playerx = x; 52 playerx = x;
52 playery = y; 53 playery = y;
53} 54}
54 55
55void GameState::wrap(int* x, int* y)
56{
57 if (*x < 0)
58 {
59 *x = WIDTH-(0-*x);
60 } else if (*y < 0)
61 {
62 *y = HEIGHT-(0-*y);
63 } else if (*x >= WIDTH)
64 {
65 *x = *x-WIDTH;
66 } else if (*y >= HEIGHT)
67 {
68 *y = *y-HEIGHT;
69 }
70}
71
72void GameState::render(SDL_Surface* screen) 56void GameState::render(SDL_Surface* screen)
73{ 57{
74 SDL_Rect block; 58 board.render(screen);
75 block.w = 16;
76 block.h = 16;
77
78 int x,y;
79
80 for (y=0;y<HEIGHT;y++)
81 {
82 for (x=0;x<WIDTH;x++)
83 {
84 block.x = x*16;
85 block.y = y*16;
86
87 SDL_FillRect(screen, &block, (blocks[x][y] ? on : off));
88 }
89 }
90 59
60 SDL_Rect block;
91 block.x = playerx*16; 61 block.x = playerx*16;
92 block.y = playery*16; 62 block.y = playery*16;
63 block.w = 16;
64 block.h = 16;
93 65
94 SDL_FillRect(screen, &block, player_color); 66 SDL_FillRect(screen, &block, player_color);
95} 67}
diff --git a/gamestate.h b/gamestate.h index 36de42e..1d66a9e 100644 --- a/gamestate.h +++ b/gamestate.h
@@ -3,13 +3,12 @@
3 3
4class GameState : public State { 4class GameState : public State {
5 private: 5 private:
6 bool blocks[WIDTH][HEIGHT];
7 Uint32 on;
8 Uint32 off;
9 Uint32 player_color; 6 Uint32 player_color;
10 int playerx, playery; 7 int playerx, playery;
8 bool newGame;
9 bool doneMaking;
10 Board board;
11 void move(int x, int y); 11 void move(int x, int y);
12 void wrap(int* x, int* y);
13 12
14 public: 13 public:
15 GameState(SDL_PixelFormat* fmt); 14 GameState(SDL_PixelFormat* fmt);
diff --git a/includes.h b/includes.h index 83f3619..5b70420 100644 --- a/includes.h +++ b/includes.h
@@ -1,5 +1,7 @@
1#include <SDL.h> 1#include <SDL.h>
2#include <stdio.h> 2#include <stdio.h>
3#include <time.h>
3#include "mazeoflife.h" 4#include "mazeoflife.h"
5#include "board.h"
4#include "state.h" 6#include "state.h"
5#include "gamestate.h" 7#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 @@
1#include "includes.h" 1#include "includes.h"
2 2
3SDL_Surface *screen;
3bool gameSleep = false; 4bool gameSleep = false;
4 5
5int main(int argc, char *argv[]) 6int main(int argc, char *argv[])
6{ 7{
8 srand(time(NULL));
9
7 /* Initialize defaults, Video and Audio */ 10 /* Initialize defaults, Video and Audio */
8 if((SDL_Init(SDL_INIT_VIDEO)==-1)) { 11 if((SDL_Init(SDL_INIT_VIDEO)==-1)) {
9 printf("Could not initialize SDL: %s.\n", SDL_GetError()); 12 printf("Could not initialize SDL: %s.\n", SDL_GetError());
@@ -17,7 +20,7 @@ int main(int argc, char *argv[])
17 * Initialize the display in a 640x480 8-bit palettized mode, 20 * Initialize the display in a 640x480 8-bit palettized mode,
18 * requesting a software surface 21 * requesting a software surface
19 */ 22 */
20 SDL_Surface *screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF); 23 screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF);
21 if ( screen == NULL ) { 24 if ( screen == NULL ) {
22 fprintf(stderr, "Couldn't set %dx%dx8 video mode: %s\n", WIDTH*16, WIDTH*16, SDL_GetError()); 25 fprintf(stderr, "Couldn't set %dx%dx8 video mode: %s\n", WIDTH*16, WIDTH*16, SDL_GetError());
23 exit(1); 26 exit(1);
@@ -61,3 +64,24 @@ int main(int argc, char *argv[])
61 exit(0); 64 exit(0);
62} 65}
63 66
67void wrap(int* x, int* y)
68{
69 if (*x < 0)
70 {
71 *x = WIDTH-(0-*x);
72 } else if (*y < 0)
73 {
74 *y = HEIGHT-(0-*y);
75 } else if (*x >= WIDTH)
76 {
77 *x = *x-WIDTH;
78 } else if (*y >= HEIGHT)
79 {
80 *y = *y-HEIGHT;
81 }
82}
83
84Uint32 getColor(int r, int g, int b)
85{
86 return SDL_MapRGB(screen->format, r, g, b);
87}
diff --git a/mazeoflife.h b/mazeoflife.h index 3cc6d6d..6af0cf4 100644 --- a/mazeoflife.h +++ b/mazeoflife.h
@@ -4,4 +4,7 @@
4const int WIDTH = 30; 4const int WIDTH = 30;
5const int HEIGHT = 30; 5const int HEIGHT = 30;
6 6
7void wrap(int* x, int* y);
8Uint32 getColor(int r, int g, int b);
9
7#endif 10#endif