~strahinja/lav-sdl

5b33866431aea8c3c2c0aca4496c7bfc9f23f204 — Страхиња Радић 6 months ago a91fd29
WIP mouse support

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

M lav-sdl.c
M lav-sdl.c => lav-sdl.c +52 -9
@@ 95,10 95,12 @@ struct Labyrinth {
static struct AnimationState animation;
static SDL_Point hero, start, end;
static struct Labyrinth labyrinth;
static int location_pointer	  = 0;
static SDL_Point* location_stack  = NULL;
static int location_pointer	 = 0;
static SDL_Point* location_stack = NULL;
static SDL_FPoint pointer;
static int help_shown		  = 0;
static int redraw		  = 1;
static int draw_pointer		  = 1;
static SDL_Renderer* renderer	  = NULL;
static int running		  = 1;
static float scale_factor	  = 1.0f;


@@ 274,7 276,7 @@ draw(void)
		for (x = 0; x < labyrinth.width; x++)
		{
			if ((labyrinth.cells[y][x] != CELL_OK
				&& labyrinth.cells[y][x] != CELL_FAIL)
				    && labyrinth.cells[y][x] != CELL_FAIL)
				|| (x == start.x && y == start.y)
				|| (x == end.x && y == end.y))
				continue;


@@ 297,11 299,11 @@ draw(void)
	get_hero_sprite_xy(&cell_rect.x, &cell_rect.y);
	if (animation.active)
		labyrinth_coord_to_screen_coordF(animation.position.x,
			animation.position.y, startx, starty,
			&dest_rect.x, &dest_rect.y);
			animation.position.y, startx, starty, &dest_rect.x,
			&dest_rect.y);
	else
		labyrinth_coord_to_screen_coord(hero.x, hero.y, startx,
			starty, &dest_rect.x, &dest_rect.y);
		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);



@@ 319,14 321,28 @@ draw(void)
	}

	/* Render status */
	draw_text(startx, starty + bg_rect.h, TER16_WIDTH,
		TER16_HEIGHT, ter16_texture,
	draw_text(startx, starty + bg_rect.h, TER16_WIDTH, TER16_HEIGHT,
		ter16_texture,
		"Column: %2d,  Row: %2d    F1 = Help    Status: %s", hero.x + 1,
		hero.y + 1, state_descriptions[state]);

	if (help_shown)
		draw_help_dialog();

	if (draw_pointer)
	{
		SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
		SDL_RenderDrawLineF(renderer, pointer.x - 9, pointer.y + 1,
			pointer.x + 11, pointer.y + 1);
		SDL_RenderDrawLineF(renderer, pointer.x + 1, pointer.y - 9,
			pointer.x + 1, pointer.y + 11);
		SDL_SetRenderDrawColor(renderer, 255, 255, 255,
			SDL_ALPHA_OPAQUE);
		SDL_RenderDrawLineF(renderer, pointer.x - 10, pointer.y,
			pointer.x + 10, pointer.y);
		SDL_RenderDrawLineF(renderer, pointer.x, pointer.y - 10,
			pointer.x, pointer.y + 10);
	}
	SDL_RenderPresent(renderer);
	// SDL_RenderFlush(renderer);
}


@@ 552,6 568,26 @@ handle_event(SDL_Event* event)
		default:
			print_debug("switch fallthrough");
		}
		break;
	case SDL_MOUSEMOTION:
		pointer.x = event->motion.x;
		pointer.y = event->motion.y;
		if (pointer.x <= 0 || pointer.y <= 0
			|| pointer.x + 1 >= screen_width
			|| pointer.y + 1 >= screen_height)
		{
			// SDL_SetRelativeMouseMode(SDL_FALSE);
			draw_pointer = 0;
			SDL_ShowCursor(1);
		}
		else
		{
			// SDL_SetRelativeMouseMode(SDL_TRUE);
			draw_pointer = 1;
			SDL_ShowCursor(0);
		}
		redraw = 1;
		break;
	}
}



@@ 910,6 946,13 @@ main(int argc, char** argv)
	update_size();
	animation.active = 0;

	// SDL_SetWindowGrab(window, SDL_TRUE);
	// SDL_SetRelativeMouseMode(SDL_TRUE);
	SDL_ShowCursor(0);
	int px, py;
	SDL_GetMouseState(&px, &py);
	pointer.x = (float)px;
	pointer.y = (float)py;
	while (running)
	{
		while (SDL_PollEvent(&event))