summary refs log tree commit diff stats
path: root/gamestate.cpp
blob: 0bcf3d75cacbd95455c7ed958b9a93db070dd792 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "includes.h"

GameState::GameState(SDL_PixelFormat* fmt)
{
	int x,y;
	for (y=0;y<HEIGHT;y++)
	{
		for (x=0;x<WIDTH;x++)
		{
			blocks[x][y] = false;
		}
	}

	on = SDL_MapRGB(fmt, 0, 0, 0);
	off = SDL_MapRGB(fmt, 255, 255, 255);
	player_color = SDL_MapRGB(fmt, 255, 255, 0);

	playerx = 1;
	playery = 1;
}

void GameState::input(SDLKey key)
{
	switch (key)
	{
		case SDLK_LEFT:
			move(playerx-1, playery);

			break;
		case SDLK_RIGHT:
			move(playerx+1, playery);

			break;
		case SDLK_UP:
			move(playerx, playery-1);

			break;
		case SDLK_DOWN:
			move(playerx, playery+1);

			break;
	}
}

void GameState::move(int x, int y)
{
	wrap(&x, &y);

	if (blocks[x][y]) return;

	playerx = x;
	playery = y;
}

void GameState::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;
	}
}

void GameState::render(SDL_Surface* screen)
{
	SDL_Rect block;
	block.w = 16;
	block.h = 16;

	int x,y;

	for (y=0;y<HEIGHT;y++)
	{
		for (x=0;x<WIDTH;x++)
		{
			block.x = x*16;
			block.y = y*16;

			SDL_FillRect(screen, &block, (blocks[x][y] ? on : off));
		}
	}

	block.x = playerx*16;
	block.y = playery*16;

	SDL_FillRect(screen, &block, player_color);
}