Skip to content

Commit

Permalink
feat: show lights out of ingame box
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Jun 7, 2024
1 parent 71ecb21 commit 027d455
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.
6 changes: 2 additions & 4 deletions data/menubar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@
<item name="Show $all floors" hotkey="Ctrl+W" action="SHOW_ALL_FLOORS" help="If not checked other floors are hidden."/>
<item name="Ghost $loose items" hotkey="G" action="GHOST_ITEMS" help="Ghost items (except ground)."/>
<item name="Ghost $higher floors" hotkey="Ctrl+L" action="GHOST_HIGHER_FLOORS" help="Ghost floors."/>
<menu name="$Ingame Box">
<item name="Show $client box" hotkey="Shift+I" action="SHOW_INGAME_BOX" help="Shadows out areas not visible ingame (from the center of the screen)."/>
<item name="Show $lights" hotkey="Shift+I" action="SHOW_LIGHTS" help="Show lights."/>
</menu>
<item name="Show $client box" hotkey="Shift+I" action="SHOW_INGAME_BOX" help="Shadows out areas not visible ingame (from the center of the screen)."/>
<item name="Show $lights" hotkey="Shift+I" action="SHOW_LIGHTS" help="Show lights."/>
<item name="Show $Grid" hotkey="Shift+G" action="SHOW_GRID" help="Shows a grid over all items."/>
<item name="H$ighlight items" hotkey="V" action="HIGHLIGHT_ITEMS" help="Highlight tiles with items on them."/>
<separator/>
Expand Down
40 changes: 27 additions & 13 deletions source/light_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@ LightDrawer::~LightDrawer() {
lights.clear();
}

void LightDrawer::draw(int map_x, int map_y, int scroll_x, int scroll_y) {
constexpr int half_tile_size = rme::TileSize / 2;
void LightDrawer::draw(int map_x, int map_y, int end_x, int end_y, int scroll_x, int scroll_y) {
if (texture == 0) {
createGLTexture();
}

int w = end_x - map_x;
int h = end_y - map_y;

for (int x = 0; x < rme::ClientMapWidth; ++x) {
for (int y = 0; y < rme::ClientMapHeight; ++y) {
buffer.resize(static_cast<size_t>(w * h * rme::PixelFormatRGBA));

for (int x = 0; x < w; ++x) {
for (int y = 0; y < h; ++y) {
int mx = (map_x + x);
int my = (map_y + y);
int px = (mx * rme::TileSize + half_tile_size);
int py = (my * rme::TileSize + half_tile_size);
int index = (y * rme::ClientMapWidth + x);
int index = (y * w + x);
int color_index = index * rme::PixelFormatRGBA;

buffer[color_index] = global_color.Red();
Expand All @@ -67,18 +72,19 @@ void LightDrawer::draw(int map_x, int map_y, int scroll_x, int scroll_y) {

const int draw_x = map_x * rme::TileSize - scroll_x;
const int draw_y = map_y * rme::TileSize - scroll_y;
constexpr int draw_width = rme::ClientMapWidth * rme::TileSize;
constexpr int draw_height = rme::ClientMapHeight * rme::TileSize;
int draw_width = w * rme::TileSize;
int draw_height = h * rme::TileSize;

glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 0x812F);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 0x812F);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rme::ClientMapWidth, rme::ClientMapHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data());

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data());
glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);

glColor4ub(255, 255, 255, 255); // reset color
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
Expand All @@ -99,7 +105,12 @@ void LightDrawer::setGlobalLightColor(uint8_t color) {
global_color = colorFromEightBit(color);
}

void LightDrawer::addLight(int map_x, int map_y, const SpriteLight &light) {
void LightDrawer::addLight(int map_x, int map_y, int map_z, const SpriteLight &light) {
if (map_z <= rme::MapGroundLayer) {
map_x -= (rme::MapGroundLayer - map_z);
map_y -= (rme::MapGroundLayer - map_z);
}

if (map_x <= 0 || map_x >= rme::MapMaxWidth || map_y <= 0 || map_y >= rme::MapMaxHeight) {
return;
}
Expand All @@ -123,8 +134,11 @@ void LightDrawer::clear() noexcept {

void LightDrawer::createGLTexture() {
glGenTextures(1, &texture);
ASSERT(texture == 0);
}

void LightDrawer::unloadGLTexture() {
glDeleteTextures(1, &texture);
if (texture != 0) {
glDeleteTextures(1, &texture);
}
}
4 changes: 2 additions & 2 deletions source/light_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class LightDrawer {
LightDrawer();
virtual ~LightDrawer();

void draw(int map_x, int map_y, int scroll_x, int scroll_y);
void draw(int map_x, int map_y, int end_x, int end_y, int scroll_x, int scroll_y);

void setGlobalLightColor(uint8_t color);
void addLight(int map_x, int map_y, const SpriteLight &light);
void addLight(int map_x, int map_y, int map_z, const SpriteLight &light);
void clear() noexcept;

private:
Expand Down
29 changes: 14 additions & 15 deletions source/map_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ bool DrawingOptions::isTooltips() const noexcept {
return show_tooltips && !isOnlyColors();
}

bool DrawingOptions::isDrawLight() const noexcept {
return show_ingame_box && show_lights;
}

MapDrawer::MapDrawer(MapCanvas* canvas) :
canvas(canvas), editor(canvas->editor) {
light_drawer = std::make_shared<LightDrawer>();
Expand Down Expand Up @@ -216,6 +212,9 @@ void MapDrawer::Release() {
void MapDrawer::Draw() {
DrawBackground();
DrawMap();
if (options.show_lights) {
light_drawer->draw(start_x, start_y, end_x, end_y, view_scroll_x, view_scroll_y);
}
DrawDraggingShadow();
DrawHigherFloors();
if (options.dragging) {
Expand Down Expand Up @@ -336,11 +335,10 @@ void MapDrawer::DrawMap() {
for (int map_y = 0; map_y < 4; ++map_y) {
TileLocation* location = nd->getTile(map_x, map_y, map_z);
DrawTile(location);
if (location && options.isDrawLight()) {
// draw light, but only if not zoomed too far
if (location && options.show_lights && zoom <= 10) {
auto &position = location->getPosition();
if (position.x >= box_start_map_x && position.x <= box_end_map_x && position.y >= box_start_map_y && position.y <= box_end_map_y) {
AddLight(location);
}
AddLight(location);
}
}
}
Expand Down Expand Up @@ -509,10 +507,6 @@ void MapDrawer::DrawIngameBox() {
int box_end_x = box_end_map_x * rme::TileSize - view_scroll_x;
int box_end_y = box_end_map_y * rme::TileSize - view_scroll_y;

if (options.isDrawLight()) {
light_drawer->draw(box_start_map_x, box_start_map_y, view_scroll_x, view_scroll_y);
}

static wxColor side_color(0, 0, 0, 200);

glDisable(GL_TEXTURE_2D);
Expand Down Expand Up @@ -1913,6 +1907,11 @@ void MapDrawer::DrawTooltips() {
#endif
}

void MapDrawer::DrawLight() {
// draw in-game light
light_drawer->draw(start_x, start_y, end_x, end_y, view_scroll_x, view_scroll_y);
}

void MapDrawer::MakeTooltip(int screenx, int screeny, const std::string &text, uint8_t r, uint8_t g, uint8_t b) {
if (text.empty()) {
return;
Expand All @@ -1924,7 +1923,7 @@ void MapDrawer::MakeTooltip(int screenx, int screeny, const std::string &text, u
}

void MapDrawer::AddLight(TileLocation* location) {
if (!options.isDrawLight() || !location) {
if (!options.show_lights || !location) {
return;
}

Expand All @@ -1937,15 +1936,15 @@ void MapDrawer::AddLight(TileLocation* location) {

if (tile->ground) {
if (tile->ground->hasLight()) {
light_drawer->addLight(position.x, position.y, tile->ground->getLight());
light_drawer->addLight(position.x, position.y, position.z, tile->ground->getLight());
}
}

bool hidden = options.hide_items_when_zoomed && zoom > 10.f;
if (!hidden && !tile->items.empty()) {
for (auto item : tile->items) {
if (item->hasLight()) {
light_drawer->addLight(position.x, position.y, item->getLight());
light_drawer->addLight(position.x, position.y, position.z, item->getLight());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/map_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ struct DrawingOptions {
bool isOnlyColors() const noexcept;
bool isTileIndicators() const noexcept;
bool isTooltips() const noexcept;
bool isDrawLight() const noexcept;

bool transparent_floors;
bool transparent_items;
Expand Down Expand Up @@ -162,6 +161,7 @@ class MapDrawer {
void DrawTileIndicators(TileLocation* location);
void DrawIndicator(int x, int y, int indicator, uint8_t r = 255, uint8_t g = 255, uint8_t b = 255, uint8_t a = 255);
void DrawPositionIndicator(int z);
void DrawLight();
void WriteTooltip(const Item* item, std::ostringstream &stream);
void WriteTooltip(const Waypoint* item, std::ostringstream &stream);
void MakeTooltip(int screenx, int screeny, const std::string &text, uint8_t r = 255, uint8_t g = 255, uint8_t b = 255);
Expand Down

0 comments on commit 027d455

Please sign in to comment.