~srivathsan/dave

3d982875c83621204a2ed11e446b5162178a3540 — Srivathsan Murali 8 months ago 8d12460
abstrating initialization
2 files changed, 56 insertions(+), 31 deletions(-)

M dave.c
M dave.h
M dave.c => dave.c +40 -30
@@ 5,50 5,60 @@

int main(int argc, char* argv[])
{
	// Load game assets
	struct game_assets *assets;
	assets = malloc(sizeof(struct game_assets));

	load_assets(assets);
	struct dave *game;
	game = malloc(sizeof(struct dave));
	
	// Initialize the game state
	struct game_state *state;
	state = malloc(sizeof(struct game_state));
	// initialize the dave struct
	initialize_dave(game);
	
	initialize_game_state(state);
	// intialize the game graphics
	initialize_graphics(game);
	
	// Run the game loop
	printf("Starting Dave...\n");
	while(!game->state->quit)
	{
		// Do a single game tick.
		tick(game);
	}
	printf("Stopping Dave...\n");

	free(game->state);
	free(game->assets);
	free(game);
	return 0;
}

void initialize_dave(struct dave *game)
{
	// Load game assets
	game->assets = malloc(sizeof(struct game_assets));
	load_assets(game->assets);
	
	// Initialize the game state
	game->state = malloc(sizeof(struct game_state));
	initialize_game_state(game->state);
}

void initialize_graphics(struct dave *game)
{
	// Initialize the UI
	SDL_Window *window;
	SDL_Renderer *renderer;
	const uint8_t DISPLAY_SCALE = 3;

	if (SDL_Init(SDL_INIT_VIDEO))
		SDL_Log("SDL error %s", SDL_GetError());

	if (SDL_CreateWindowAndRenderer(320*DISPLAY_SCALE, 200*DISPLAY_SCALE, 
									0, &window, &renderer))
									0, &game->window, &game->renderer))
		SDL_Log("Window/Renderer error %s", SDL_GetError());

	SDL_RenderSetScale(renderer, DISPLAY_SCALE, DISPLAY_SCALE);

	SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
	SDL_RenderClear(renderer);
	
	// Run the game loop
	printf("Starting Dave...\n");
	while(!state->quit)
	{
		// Do a single game tick.
		dave_tick(state, assets);
	}
	printf("Stopping Dave...\n");
	SDL_RenderSetScale(game->renderer, DISPLAY_SCALE, DISPLAY_SCALE);

	free(state);
	free(assets);
	return 0;
	SDL_SetRenderDrawColor(game->renderer, 0x00, 0x00, 0x00, 0x00);
	SDL_RenderClear(game->renderer);
}

void dave_tick(struct game_state *state, struct game_assets *assets)
void tick(struct dave *game)
{
	state->quit = 1;
	game->state->quit = 1;
}

M dave.h => dave.h +16 -1
@@ 7,7 7,22 @@

#include <stdint.h>

struct dave
{
	struct game_assets *assets;
	struct game_state *state;
	SDL_Window *window;
	SDL_Renderer *renderer;

};

// initialize game assets and state
void initialize_dave(struct dave *);

// initialize game graphics
void initialize_graphics(struct dave *);

// the main game loop.
void dave_tick(struct game_state *state, struct game_assets *assets);
void tick(struct dave *);

#endif // __DAVE_DAVE_H__