diff options
-rw-r--r-- | gamestate.cpp | 66 | ||||
-rw-r--r-- | gamestate.h | 5 | ||||
-rw-r--r-- | mazeoflife.cpp | 7 | ||||
-rw-r--r-- | state.h | 1 |
4 files changed, 75 insertions, 4 deletions
diff --git a/gamestate.cpp b/gamestate.cpp index dac5272..0bcf3d7 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
@@ -13,23 +13,83 @@ GameState::GameState(SDL_PixelFormat* fmt) | |||
13 | 13 | ||
14 | on = SDL_MapRGB(fmt, 0, 0, 0); | 14 | on = SDL_MapRGB(fmt, 0, 0, 0); |
15 | off = SDL_MapRGB(fmt, 255, 255, 255); | 15 | off = SDL_MapRGB(fmt, 255, 255, 255); |
16 | player_color = SDL_MapRGB(fmt, 255, 255, 0); | ||
17 | |||
18 | playerx = 1; | ||
19 | playery = 1; | ||
20 | } | ||
21 | |||
22 | void GameState::input(SDLKey key) | ||
23 | { | ||
24 | switch (key) | ||
25 | { | ||
26 | case SDLK_LEFT: | ||
27 | move(playerx-1, playery); | ||
28 | |||
29 | break; | ||
30 | case SDLK_RIGHT: | ||
31 | move(playerx+1, playery); | ||
32 | |||
33 | break; | ||
34 | case SDLK_UP: | ||
35 | move(playerx, playery-1); | ||
36 | |||
37 | break; | ||
38 | case SDLK_DOWN: | ||
39 | move(playerx, playery+1); | ||
40 | |||
41 | break; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | void GameState::move(int x, int y) | ||
46 | { | ||
47 | wrap(&x, &y); | ||
48 | |||
49 | if (blocks[x][y]) return; | ||
50 | |||
51 | playerx = x; | ||
52 | playery = y; | ||
53 | } | ||
54 | |||
55 | void 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 | } | ||
16 | } | 70 | } |
17 | 71 | ||
18 | void GameState::render(SDL_Surface* screen) | 72 | void GameState::render(SDL_Surface* screen) |
19 | { | 73 | { |
74 | SDL_Rect block; | ||
75 | block.w = 16; | ||
76 | block.h = 16; | ||
77 | |||
20 | int x,y; | 78 | int x,y; |
21 | 79 | ||
22 | for (y=0;y<HEIGHT;y++) | 80 | for (y=0;y<HEIGHT;y++) |
23 | { | 81 | { |
24 | for (x=0;x<WIDTH;x++) | 82 | for (x=0;x<WIDTH;x++) |
25 | { | 83 | { |
26 | SDL_Rect block; | ||
27 | block.x = x*16; | 84 | block.x = x*16; |
28 | block.y = y*16; | 85 | block.y = y*16; |
29 | block.w = 16; | ||
30 | block.h = 16; | ||
31 | 86 | ||
32 | SDL_FillRect(screen, &block, (blocks[x][y] ? on : off)); | 87 | SDL_FillRect(screen, &block, (blocks[x][y] ? on : off)); |
33 | } | 88 | } |
34 | } | 89 | } |
90 | |||
91 | block.x = playerx*16; | ||
92 | block.y = playery*16; | ||
93 | |||
94 | SDL_FillRect(screen, &block, player_color); | ||
35 | } | 95 | } |
diff --git a/gamestate.h b/gamestate.h index f906bf6..36de42e 100644 --- a/gamestate.h +++ b/gamestate.h | |||
@@ -6,9 +6,14 @@ class GameState : public State { | |||
6 | bool blocks[WIDTH][HEIGHT]; | 6 | bool blocks[WIDTH][HEIGHT]; |
7 | Uint32 on; | 7 | Uint32 on; |
8 | Uint32 off; | 8 | Uint32 off; |
9 | Uint32 player_color; | ||
10 | int playerx, playery; | ||
11 | void move(int x, int y); | ||
12 | void wrap(int* x, int* y); | ||
9 | 13 | ||
10 | public: | 14 | public: |
11 | GameState(SDL_PixelFormat* fmt); | 15 | GameState(SDL_PixelFormat* fmt); |
16 | void input(SDLKey key); | ||
12 | void render(SDL_Surface* screen); | 17 | void render(SDL_Surface* screen); |
13 | }; | 18 | }; |
14 | 19 | ||
diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 0e3464a..20bee1d 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp | |||
@@ -24,6 +24,7 @@ int main(int argc, char *argv[]) | |||
24 | } | 24 | } |
25 | 25 | ||
26 | SDL_WM_SetCaption("Maze Of Life", NULL); | 26 | SDL_WM_SetCaption("Maze Of Life", NULL); |
27 | SDL_EnableKeyRepeat(100, 50); | ||
27 | 28 | ||
28 | State* state = new GameState(screen->format); | 29 | State* state = new GameState(screen->format); |
29 | 30 | ||
@@ -37,7 +38,7 @@ int main(int argc, char *argv[]) | |||
37 | case SDL_ACTIVEEVENT: | 38 | case SDL_ACTIVEEVENT: |
38 | if (anEvent.active.state == SDL_APPINPUTFOCUS) | 39 | if (anEvent.active.state == SDL_APPINPUTFOCUS) |
39 | { | 40 | { |
40 | // gameSleep = !anEvent.active.gain; | 41 | gameSleep = !anEvent.active.gain; |
41 | } | 42 | } |
42 | 43 | ||
43 | break; | 44 | break; |
@@ -45,6 +46,10 @@ int main(int argc, char *argv[]) | |||
45 | exit(0); | 46 | exit(0); |
46 | 47 | ||
47 | break; | 48 | break; |
49 | case SDL_KEYDOWN: | ||
50 | state->input(anEvent.key.keysym.sym); | ||
51 | |||
52 | break; | ||
48 | } | 53 | } |
49 | } | 54 | } |
50 | 55 | ||
diff --git a/state.h b/state.h index dd5e52b..e070858 100644 --- a/state.h +++ b/state.h | |||
@@ -4,6 +4,7 @@ | |||
4 | class State | 4 | class State |
5 | { | 5 | { |
6 | public: | 6 | public: |
7 | virtual void input(SDLKey key) = 0; | ||
7 | virtual void render(SDL_Surface* screen) = 0; | 8 | virtual void render(SDL_Surface* screen) = 0; |
8 | }; | 9 | }; |
9 | 10 | ||