From: Lukas Krickl Date: Thu, 5 Mar 2026 11:08:33 +0000 (+0100) Subject: cursor: Fixed cursot position set to require render coordinate not X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=abc2c125ac5e26fe35968c8a65409f31cd6ac5df;p=lrts%2F.git cursor: Fixed cursot position set to require render coordinate not *window* cooridnate Added test highlight for tile mouse is hovering over. --- diff --git a/src/i_input.c b/src/i_input.c index a7cd748..e7e179f 100644 --- a/src/i_input.c +++ b/src/i_input.c @@ -123,3 +123,10 @@ void i_input_poll(void) { } } } + +struct u_vec2 i_cursor_pos(void) { + struct u_vec2 r; + r.x = i_input_map.cursor_x; + r.y = i_input_map.cursor_y; + return r; +} diff --git a/src/i_input.h b/src/i_input.h index bb8d975..890e4a7 100644 --- a/src/i_input.h +++ b/src/i_input.h @@ -76,6 +76,8 @@ void i_input_update(void); /* sets cursor positon * should be called by p_poll_events + * this should be the position of the cursor + * in render space *not* window space */ void i_input_cursor_set(i32 x, i32 y); @@ -100,6 +102,8 @@ void i_input_delay_ticks(enum i_input_actions action, u32 tick_count); /* tests if the input is delayed */ lrts_bool i_input_is_delayed(enum i_input_actions action); +struct u_vec2 i_cursor_pos(void); + /* inits the default for a keybind */ void i_input_map_init_default(enum i_input_actions action, u16 scan_code, diff --git a/src/p_r_sdl/p_window.c b/src/p_r_sdl/p_window.c index 27e0ab9..484419b 100644 --- a/src/p_r_sdl/p_window.c +++ b/src/p_r_sdl/p_window.c @@ -103,8 +103,12 @@ lrts_bool p_input_poll_action(struct i_input_map_ent *ent) { } int p_poll_events() { + struct u_vec2 render_res = lrts_get_render_res(); + struct u_vec2 screen_res = lrts_get_screen_res(); SDL_Event e; float x, y; + float scale_x = (float)screen_res.x / (float)render_res.x; + float scale_y = (float)screen_res.y / (float)render_res.y; while (SDL_PollEvent(&e)) { switch (e.type) { @@ -117,7 +121,8 @@ int p_poll_events() { mouse_state = SDL_GetMouseState(&x, &y); - i_input_cursor_set(x, y); + i_input_cursor_set((float)x / scale_x, (float)y / scale_y); + return 0; } diff --git a/src/r_assets.c b/src/r_assets.c index ce0d9a5..6c2f6aa 100644 --- a/src/r_assets.c +++ b/src/r_assets.c @@ -51,10 +51,19 @@ void r_draw_solid_isometric_tile(i32 x, i32 y, r_color color) { u32 i = 0; u32 j = 0; r_color final_color; + u32 hover_color_shift = 0; struct u_vec2 t = u_tile_to_screen(x, y); struct u_vec2 tt = u_tile_to_screen(x+1, y+1); struct u_vec2 tl = u_tile_to_screen(x+2, y); struct u_vec2 tb = u_tile_to_screen(x+2, y-2); + + /* get mouse cursor and move by camera */ + struct u_vec2 cursor_pos = u_world_to_camera(&t_main_camera, i_cursor_pos()); + if (u_point_in_rect(cursor_pos.x, cursor_pos.y, + t.x, t.y, + R_TILE_W, R_TILE_H)) { + hover_color_shift = 1; + } /* do not draw if tile is not visible at all * also include a few points outside the camera to avoid tiles @@ -74,7 +83,9 @@ void r_draw_solid_isometric_tile(i32 x, i32 y, r_color color) { for (i = 0; i < R_TILE_W; i++) { for (j = 0; j < R_TILE_H; j++) { final_color = r_asset_fallback_tile[i + j * R_TILE_W] & color; + final_color <<= hover_color_shift; r_draw_pixel(&r_framebuffer, t.x + i, t.y + j, final_color); + } } diff --git a/src/t_camera.c b/src/t_camera.c index f9a34a8..69822a9 100644 --- a/src/t_camera.c +++ b/src/t_camera.c @@ -30,3 +30,10 @@ struct u_vec2 u_screen_to_camera(struct t_camera *c, struct u_vec2 v) { return v; } + +struct u_vec2 u_world_to_camera(struct t_camera *c, struct u_vec2 v) { + v.x += c->x; + v.y += c->y; + + return v; +} diff --git a/src/t_camera.h b/src/t_camera.h index 5763f64..3cc3395 100644 --- a/src/t_camera.h +++ b/src/t_camera.h @@ -17,9 +17,12 @@ struct t_camera t_camera_init(i32 viewport_x, i32 viewport_y); void t_camera_scroll(struct t_camera *c, i32 by_x, i32 by_y); -/* adjusts a screen coodrinate by the camera offset */ +/* adjusts a screen coodrinate (isometric) by the camera offset */ struct u_vec2 u_screen_to_camera(struct t_camera *c, struct u_vec2 v); +/* converts a world coordinate (not isometric) to camera */ +struct u_vec2 u_world_to_camera(struct t_camera *c, struct u_vec2 v); + /* tests if a point is visible in screen sapce */ lrts_bool t_camera_is_visible_screen(struct t_camera *c, i32 x, i32 y);