summary refs log tree commit diff stats
path: root/gamestate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gamestate.cpp')
-rw-r--r--gamestate.cpp66
1 files changed, 63 insertions, 3 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
22void 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
45void 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
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 }
16} 70}
17 71
18void GameState::render(SDL_Surface* screen) 72void 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}