~srivathsan/dave

cdd9cc771d9d8ad156a5852015e8a61b758792db — Srivathsan Murali 8 months ago 962ef93 master
init render
4 files changed, 78 insertions(+), 2 deletions(-)

M dave.c
M dave.h
M state.c
M state.h
M dave.c => dave.c +67 -1
@@ 13,13 13,23 @@ int main(int argc, char* argv[])
	
	// initialize the dave struct
	initialize_dave(game);

	uint32_t timer_start;
	uint32_t timer_end;
	uint32_t delay;
	
	// Run the game loop
	printf("Starting Dave...\n");
	while(!game->state->quit)
	{
		timer_start = SDL_GetTicks();
		// Do a single game tick.
		tick(game);
		timer_end = SDL_GetTicks();
		delay = 33 - (timer_end - timer_start);
		delay = delay > 33 ? 0 : delay;

		SDL_Delay(delay);
	}
	printf("Stopping Dave...\n");



@@ 60,5 70,61 @@ void initialize_graphics(struct dave *game)

void tick(struct dave *game)
{
	game->state->quit = 1;
	check_input(game);
	update_game(game);
	render(game);
}

void check_input(struct dave *game)
{
	SDL_Event event;
	SDL_PollEvent(&event);

	if (event.type == SDL_QUIT)
		game->state->quit = 1;

	if (event.type == SDL_KEYDOWN)
	{
		switch(event.key.keysym.sym)
		{
			case SDLK_UP:
				game->state->current_level++;
				break;
			case SDLK_DOWN:
				game->state->current_level--;
				break;
		}
	}
}

void update_game(struct dave *game)
{
	if (game->state->current_level == 0xff)
		game->state->current_level = 0;
	if (game->state->current_level > 9)
		game->state->current_level = 9;
}

void render(struct dave *game)
{
	SDL_Rect dest;
	uint8_t tile_index;

	for (uint8_t y = 0; y < 10; y++)
	{
		dest.y = TILE_SIZE + y * TILE_SIZE;
		dest.w = TILE_SIZE;
		dest.h = TILE_SIZE;

		for (uint8_t x = 0; x < 20; x++)
		{
			dest.x = x * TILE_SIZE;
			tile_index = game->assets->levels[game->state->current_level]
							.tiles[y*100+game->state->view_x+x];
			SDL_RenderCopy(
				game->renderer, game->assets->tiles[tile_index], NULL, &dest);
		}
	}

	SDL_RenderPresent(game->renderer);
}

M dave.h => dave.h +9 -0
@@ 7,6 7,8 @@

#include <stdint.h>

const uint8_t TILE_SIZE = 16;

struct dave
{
	struct game_assets *assets;


@@ 25,4 27,11 @@ void initialize_graphics(struct dave *);
// the main game loop.
void tick(struct dave *);

// check user input from keystrokes
void check_input(struct dave *);

void update_game(struct dave *);

// render the graphics
void render(struct dave *);
#endif // __DAVE_DAVE_H__

M state.c => state.c +1 -0
@@ 5,4 5,5 @@ void initialize_game_state(struct game_state *state)
{
	state->quit = 0;
	state->current_level = 0;
	state->view_x  = 0;
}

M state.h => state.h +1 -1
@@ 9,7 9,7 @@ struct game_state
{
	uint8_t quit;
	uint8_t current_level;
	struct game_level levels[10];
	uint8_t view_x;
};

void initialize_game_state(struct game_state *state);