~strahinja/lav-sdl

538f50e5d4a2b9263048f0232e55a3d360d2739c — Страхиња Радић 6 months ago 14c2f39
Cleanup, optimization

Signed-off-by: Страхиња Радић <contact@strahinja.org>
1 files changed, 72 insertions(+), 63 deletions(-)

M lav-sdl.c
M lav-sdl.c => lav-sdl.c +72 -63
@@ 116,6 116,8 @@ void assign_point_xyIF(const int x, const int y, SDL_FPoint* to);
void calculate_delta(void);
void cleanup(void);
void draw(void);
void draw_background(SDL_Rect* bg_rect, SDL_Rect* cell_rect,
	SDL_FRect* dest_rect);
void draw_help_dialog(void);
void draw_text(const int x, const int y, const int font_width,
	const int font_height, SDL_Texture* font_tex, const char* text, ...);


@@ 135,6 137,7 @@ int passable(const int x, const int y);
void print_debug(const char* fmt, ...);
void print_error(const char* fmt, ...);
void reset(void);
void step_animation(void);
void update_size(void);

void


@@ 194,7 197,6 @@ draw(void)
{
	SDL_Rect bg_rect, cell_rect;
	SDL_FRect dest_rect;
	SDL_DisplayMode mode;
	int x, y;
	int cx, cy;
	float startx, starty;


@@ 218,48 220,7 @@ draw(void)

	/* Render background */
	if (!background)
	{
		if (SDL_GetCurrentDisplayMode(0, &mode) < 0)
		{
			print_error("SDL_GetCurrentDisplayMode failed: %s",
				SDL_GetError());
			exit(1);
		}

		background = SDL_CreateTexture(renderer, mode.format,
			SDL_TEXTUREACCESS_TARGET, bg_rect.w, bg_rect.h);
		if (!background)
		{
			print_error("SDL_CreateTextureFromSurface failed: %s",
				SDL_GetError());
			exit(1);
		}
		if (SDL_SetRenderTarget(renderer, background) < 0)
		{
			print_error("SDL_SetRenderTarget failed: %s",
				SDL_GetError());
			exit(1);
		}

		SDL_SetRenderDrawColor(renderer, 0x22, 0x22, 0x22,
			SDL_ALPHA_OPAQUE);
		SDL_RenderFillRect(renderer, &bg_rect);

		dest_rect.w = CELL_SIZE * scale_factor;
		dest_rect.h = CELL_SIZE * scale_factor;
		for (y = 0; y < labyrinth.height; y++)
			for (x = 0; x < labyrinth.width; x++)
			{
				get_sprite_xy(labyrinth.cells[y][x],
					&cell_rect.x, &cell_rect.y);
				labyrinth_coord_to_screen_coord(x, y, 0.0f,
					0.0f, &dest_rect.x, &dest_rect.y);
				SDL_RenderCopyF(renderer, sprites, &cell_rect,
					&dest_rect);
			}

		SDL_SetRenderTarget(renderer, NULL);
	}
		draw_background(&bg_rect, &cell_rect, &dest_rect);

	/* Draw buffered copy of background */
	dest_rect.x = startx;


@@ 288,13 249,9 @@ draw(void)
				&dest_rect);
		}

	/* Render hero */
	cell_rect.w = CELL_SIZE;
	cell_rect.h = CELL_SIZE;

	/* Draw hero */
	dest_rect.w = CELL_SIZE * scale_factor;
	dest_rect.h = CELL_SIZE * scale_factor;

	get_hero_sprite_xy(&cell_rect.x, &cell_rect.y);
	if (animation.active)
		labyrinth_coord_to_screen_coordF(animation.position.x,


@@ 303,22 260,8 @@ draw(void)
	else
		labyrinth_coord_to_screen_coord(hero.x, hero.y, startx, starty,
			&dest_rect.x, &dest_rect.y);

	SDL_RenderCopyF(renderer, sprites, &cell_rect, &dest_rect);

	if (animation.active)
	{
		animation.position.x += animation.delta.x;
		animation.position.y += animation.delta.y;
		animation.step++;
		if (animation.step == animation.final_step)
		{
			animation.active = 0;
			hero.x		 = (int)animation.destination.x;
			hero.y		 = (int)animation.destination.y;
		}
	}

	/* Render status */
	draw_text(startx, starty + bg_rect.h, TER16_WIDTH, TER16_HEIGHT,
		ter16_texture,


@@ 329,7 272,56 @@ draw(void)
		draw_help_dialog();

	SDL_RenderPresent(renderer);
	// SDL_RenderFlush(renderer);
}

void
draw_background(SDL_Rect* bg_rect, SDL_Rect* cell_rect, SDL_FRect* dest_rect)
{
	SDL_DisplayMode mode;

	if (SDL_GetCurrentDisplayMode(0, &mode) < 0)
	{
		print_error("SDL_GetCurrentDisplayMode failed: %s",
			SDL_GetError());
		exit(1);
	}

	background = SDL_CreateTexture(renderer, mode.format,
		SDL_TEXTUREACCESS_TARGET, bg_rect->w, bg_rect->h);
	if (!background)
	{
		print_error("SDL_CreateTextureFromSurface failed: %s",
			SDL_GetError());
		exit(1);
	}
	if (SDL_SetRenderTarget(renderer, background) < 0)
	{
		print_error("SDL_SetRenderTarget failed: %s", SDL_GetError());
		exit(1);
	}

	SDL_SetRenderDrawColor(renderer, 0x22, 0x22, 0x22, SDL_ALPHA_OPAQUE);
	SDL_RenderFillRect(renderer, bg_rect);

	dest_rect->w = CELL_SIZE * scale_factor;
	dest_rect->h = CELL_SIZE * scale_factor;
	for (int y = 0; y < labyrinth.height; y++)
		for (int x = 0; x < labyrinth.width; x++)
		{
			if (!((labyrinth.cells[y][x] != CELL_OK
				      && labyrinth.cells[y][x] != CELL_FAIL)
				    || (x == start.x && y == start.y)
				    || (x == end.x && y == end.y)))
				continue;

			get_sprite_xy(labyrinth.cells[y][x], &cell_rect->x,
				&cell_rect->y);
			labyrinth_coord_to_screen_coord(x, y, 0.0f, 0.0f,
				&dest_rect->x, &dest_rect->y);
			SDL_RenderCopyF(renderer, sprites, cell_rect, dest_rect);
		}

	SDL_SetRenderTarget(renderer, NULL);
}

void


@@ 867,6 859,20 @@ reset(void)
}

void
step_animation(void)
{
	animation.position.x += animation.delta.x;
	animation.position.y += animation.delta.y;
	animation.step++;
	if (animation.step == animation.final_step)
	{
		animation.active = 0;
		hero.x		 = (int)animation.destination.x;
		hero.y		 = (int)animation.destination.y;
	}
}

void
update_size(void)
{
	float factor1;


@@ 933,7 939,10 @@ main(int argc, char** argv)
			handle_event(&event);

		if (animation.active)
		{
			draw();
			step_animation();
		}
		else
		{
			if (state == STATE_SEARCHING)