cursor: Fixed cursot position set to require render coordinate not
authorLukas Krickl <lukas@krickl.dev>
Thu, 5 Mar 2026 11:08:33 +0000 (12:08 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 5 Mar 2026 11:08:33 +0000 (12:08 +0100)
*window* cooridnate

Added test highlight for tile mouse is hovering over.

src/i_input.c
src/i_input.h
src/p_r_sdl/p_window.c
src/r_assets.c
src/t_camera.c
src/t_camera.h

index a7cd748406d32b8a1959309e45ca268f46553dd0..e7e179f9b3b01fec234dcfd7c2cd7d2fb0eb077f 100644 (file)
@@ -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;
+}
index bb8d97501a0c43a9f70a6947fd52c279b5e656f7..890e4a78b2baefb0e1692cacd23e218ab1d48a5d 100644 (file)
@@ -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, 
index 27e0ab912f3ab5b06b056d7204d49a23ae87ba62..484419bdaf9045b082cf54b0dccced1012c6beeb 100644 (file)
@@ -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;
 }
index ce0d9a5b09ac0b0527be1611a36244a3b9d901e1..6c2f6aa24629dc8ab54fe72ef655402fa13ddf2f 100644 (file)
@@ -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);
+
                }
        }
 
index f9a34a889488c5f03df3acbb22f29e409dfeecac..69822a9d6543d038e61199f3f06dce3625c7214a 100644 (file)
@@ -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;
+}
index 5763f642b964712ae8cf67c8d74d300c14fd9c23..3cc33956f7d0864c1a135f590d1b5ecc3bc6baba 100644 (file)
@@ -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);