diff options
author | cflip <cflip@cflip.net> | 2022-02-27 13:42:13 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-02-27 13:42:54 -0700 |
commit | acc2836e12b021283b95d4db4337f4413c304be2 (patch) | |
tree | 60af5ab8b7b5fb89dd3a1b800e29b68112ee6206 | |
parent | 5e35269921281f38f77fcfe2150c937290cafe46 (diff) |
Add highlight on hovered tiles
-rw-r--r-- | src/level.cpp | 6 | ||||
-rw-r--r-- | src/level.h | 4 | ||||
-rw-r--r-- | src/main.cpp | 11 |
3 files changed, 15 insertions, 6 deletions
diff --git a/src/level.cpp b/src/level.cpp index 1e71799..54cf7f0 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -1,8 +1,8 @@ #include "level.h" #include "util.h" -Level::Level(int width, int height) - : m_width(width), m_height(height), m_tileSprites("../res/tiles.png") +Level::Level(int width, int height, Bitmap& tileSprites) + : m_width(width), m_height(height), m_tileSprites(tileSprites) { m_tiles = new uint8_t[width * height]; memset(m_tiles, 0, width * height); @@ -55,7 +55,7 @@ void Level::draw(Bitmap& bitmap, int xo, int yo) ty = 3; } - bitmap.blit(m_tileSprites, xx, yy, tx * 24, ty * 24, TileSize, TileSize); + bitmap.blit(m_tileSprites, xx, yy, tx * TileSize, ty * TileSize, TileSize, TileSize); } } } diff --git a/src/level.h b/src/level.h index 948dfcb..897daa5 100644 --- a/src/level.h +++ b/src/level.h @@ -6,7 +6,7 @@ class Level { public: - Level(int width, int height); + Level(int width, int height, Bitmap& tileSprites); ~Level() { delete[] m_tiles; } uint8_t get(int x, int y); @@ -17,7 +17,7 @@ public: void toggleTile(int x, int y); private: - Bitmap m_tileSprites; + Bitmap& m_tileSprites; int m_width, m_height; uint8_t* m_tiles; }; diff --git a/src/main.cpp b/src/main.cpp index f2099d5..c19d4e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,13 +11,15 @@ int main(int argc, char** argv) constexpr int Scale = 3; Window window("Nonortho", Width, Height, Scale); + Bitmap tiles("../res/tiles.png"); - Level level(32, 32); + Level level(32, 32, tiles); Bitmap bitmap(Width, Height); Train car(level); int xOffs = 0, yOffs = 0; int xDrag, yDrag; + Point2D hoveredTile = { 0, 0 }; bool isDragging = false; window.onMouseDown([&](int button, int x, int y) { @@ -51,6 +53,9 @@ int main(int argc, char** argv) xDrag = x; yDrag = y; } + int mx = x / Scale + xOffs; + int my = y / Scale + yOffs; + hoveredTile = ScreenToTile({ mx, my }); }); while (!window.shouldClose()) { @@ -61,6 +66,10 @@ int main(int argc, char** argv) car.update(level); car.draw(bitmap, xOffs, yOffs); + int xx = (hoveredTile.x - hoveredTile.y) * (TileSize / 2) - xOffs; + int yy = (hoveredTile.x + hoveredTile.y) * (TileSize / 4) - yOffs; + bitmap.blit(tiles, xx, yy, 0, TileSize, TileSize, TileSize); + window.draw(bitmap); } |