Skip to content

Commit

Permalink
fix pixel to map tile lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed Jan 24, 2025
1 parent ccfca03 commit 92d7012
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void game_load_config(class Game *g) { g->load_config(); }

class HiScores *game_hiscores_get(class Game *g) { return &g->config.hiscores; }

void game_visible_map_get(class Game *g, int *visible_map_tl_x, int *visible_map_tl_y, int *visible_map_br_x,
void game_visible_map_pix_get(class Game *g, int *visible_map_tl_x, int *visible_map_tl_y, int *visible_map_br_x,
int *visible_map_br_y)
{
*visible_map_tl_x = g->visible_map_tl_x;
Expand All @@ -516,7 +516,7 @@ void game_visible_map_get(class Game *g, int *visible_map_tl_x, int *visible_map
*visible_map_br_y = g->visible_map_br_y;
}

void game_visible_map_set(class Game *g, int visible_map_tl_x, int visible_map_tl_y, int visible_map_br_x,
void game_visible_map_pix_set(class Game *g, int visible_map_tl_x, int visible_map_tl_y, int visible_map_br_x,
int visible_map_br_y)
{
g->visible_map_tl_x = visible_map_tl_x;
Expand Down
1 change: 0 additions & 1 deletion src/level_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ static void level_display_obj(Levelp l, point p, Tpp tp, Thingp t, bool deco)

if (deco) {
return;
tile_index += 47;
}

level_display_tile_index(l, tp, tile_index, tl, br, point(0, 0));
Expand Down
20 changes: 11 additions & 9 deletions src/level_mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "my_dmap.hpp"
#include "my_game.hpp"
#include "my_level.hpp"
#include "my_main.hpp"
#include "my_point.hpp"
#include "my_sdl_event.hpp"
#include "my_tp.hpp"
Expand All @@ -27,19 +28,20 @@ void level_mouse_position_get(Levelp l)
int visible_map_tl_y;
int visible_map_br_x;
int visible_map_br_y;
int visible_map_mouse_x;
int visible_map_mouse_y;
game_visible_map_get(game, &visible_map_tl_x, &visible_map_tl_y, &visible_map_br_x, &visible_map_br_y);
game_visible_map_pix_get(game, &visible_map_tl_x, &visible_map_tl_y, &visible_map_br_x, &visible_map_br_y);

//
// Find out what pixel on the map the mouse is over
//
visible_map_mouse_x = sdl.mouse_x - visible_map_tl_x;
visible_map_mouse_y = sdl.mouse_y - visible_map_tl_y;
float scale_x = (float) game_pix_width_get(game) / (float) game_window_pix_width_get(game);
float scale_y = (float) game_pix_height_get(game) / (float) game_window_pix_height_get(game);
visible_map_mouse_x = (int) ((float) visible_map_mouse_x * scale_x);
visible_map_mouse_y = (int) ((float) visible_map_mouse_y * scale_y);
int rel_map_mouse_x = sdl.mouse_x - visible_map_tl_x;
int rel_map_mouse_y = sdl.mouse_y - visible_map_tl_y;
int map_pix_width = visible_map_br_x - visible_map_tl_x;
int map_pix_height = visible_map_br_y - visible_map_tl_y;

float scale_x = (float) map_pix_width / (float) game_pix_width_get(game);
float scale_y = (float) map_pix_height / (float) game_pix_height_get(game);
int visible_map_mouse_x = (int) ((float) rel_map_mouse_x / scale_x);
int visible_map_mouse_y = (int) ((float) rel_map_mouse_y / scale_y);

//
// Now we wait for level_display to find the cursor
Expand Down
4 changes: 2 additions & 2 deletions src/my_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ extern class Game *game;

class HiScores *game_hiscores_get(class Game *);

void game_visible_map_get(class Game *, int *visible_map_tl_x, int *visible_map_tl_y, int *visible_map_br_x,
void game_visible_map_pix_get(class Game *, int *visible_map_tl_x, int *visible_map_tl_y, int *visible_map_br_x,
int *visible_map_br_y);
void game_visible_map_set(class Game *, int visible_map_tl_x, int visible_map_tl_y, int visible_map_br_x,
void game_visible_map_pix_set(class Game *, int visible_map_tl_x, int visible_map_tl_y, int visible_map_br_x,
int visible_map_br_y);

void game_visible_map_mouse_get(class Game *game, int *visible_map_mouse_x, int *visible_map_mouse_y);
Expand Down
17 changes: 10 additions & 7 deletions src/sdl_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ void sdl_display(class Game *g)
if (g) {
auto level = game_level_get(g);
if (level) {
auto w = game_ascii_pix_width_get(game);
auto h = game_ascii_pix_height_get(game);

int visible_map_tl_x = w * UI_LEFTBAR_WIDTH;
int visible_map_tl_y = h * UI_TOPCON_HEIGHT;
int visible_map_br_x = (TERM_WIDTH - UI_RIGHTBAR_WIDTH) * w;
int visible_map_br_y = (TERM_HEIGHT - 2) * h;
//
// Get the pixel extents of the map on screen
//
auto w = game_ascii_pix_width_get(game);
auto h = game_ascii_pix_height_get(game);
int visible_map_tl_x = w * UI_LEFTBAR_WIDTH;
int visible_map_tl_y = h * UI_TOPCON_HEIGHT;
int visible_map_br_x = (TERM_WIDTH - UI_RIGHTBAR_WIDTH) * w;
int visible_map_br_y = (TERM_HEIGHT - 2) * h;
game_visible_map_pix_set(g, visible_map_tl_x, visible_map_tl_y, visible_map_br_x, visible_map_br_y);

blit_init();
blit(g_fbo_tex_id[ FBO_MAP ], 0.0, 1.0, 1.0, 0.0, visible_map_tl_x, visible_map_tl_y, visible_map_br_x,
Expand Down

0 comments on commit 92d7012

Please sign in to comment.