Skip to content

Commit

Permalink
fix: blit editor sprite texture
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Feb 3, 2024
1 parent cf1d150 commit d9f210c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
4 changes: 3 additions & 1 deletion source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ GraphicManager::~GraphicManager() {
iter->second = nullptr;
}

sprite_space.clear();
image_space.clear();

delete animation_timer;
animation_timer = nullptr;
}
Expand Down Expand Up @@ -416,7 +419,6 @@ bool GraphicManager::loadEditorSprites() {
sprite_space[EDITOR_SPRITE_PICKUPABLE_ITEM] = GameSprite::createFromBitmap(ART_PICKUPABLE);
sprite_space[EDITOR_SPRITE_MOVEABLE_ITEM] = GameSprite::createFromBitmap(ART_MOVEABLE);
sprite_space[EDITOR_SPRITE_PICKUPABLE_MOVEABLE_ITEM] = GameSprite::createFromBitmap(ART_PICKUPABLE_MOVEABLE);

return true;
}

Expand Down
76 changes: 52 additions & 24 deletions source/map_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ void MapDrawer::BlitCreature(int screenx, int screeny, const Outfit &outfit, Dir
auto spriteId = spr->spriteList[0]->getHardwareID();
auto outfitImage = spr->getOutfitImage(spriteId, dir, outfit);
if (outfitImage) {
glBlitTexture(screenx, screeny, outfitImage->getHardwareID(), red, green, blue, alpha, false, outfit);
glBlitTexture(screenx, screeny, outfitImage->getHardwareID(), red, green, blue, alpha, false, false, outfit);
}
}

Expand Down Expand Up @@ -1727,11 +1727,12 @@ void MapDrawer::DrawTileIndicators(TileLocation* location) {
void MapDrawer::DrawIndicator(int x, int y, int indicator, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
GameSprite* sprite = g_gui.gfx.getEditorSprite(indicator);
if (sprite == nullptr) {
spdlog::error("MapDrawer::DrawIndicator: sprite is nullptr");
return;
}

int textureId = sprite->getHardwareID(0, 0, 0, -1, 0, 0);
glBlitTexture(x, y, textureId, r, g, b, a, true);
glBlitTexture(x, y, textureId, r, g, b, a, true, true);
}

void MapDrawer::DrawPositionIndicator(int z) {
Expand Down Expand Up @@ -1940,39 +1941,66 @@ void MapDrawer::ShowPositionIndicator(const Position &position) {
pos_indicator_timer.Start();
}

void MapDrawer::glBlitTexture(int sx, int sy, int texture_number, int red, int green, int blue, int alpha, bool adjustZoom /* = false*/, const Outfit &outfit /* = {}*/) {
if (texture_number != 0) {
SpriteSheetPtr sheet = g_spriteAppearances.getSheetBySpriteId(texture_number);
void MapDrawer::glBlitTexture(int sx, int sy, int textureId, int red, int green, int blue, int alpha, bool adjustZoom, bool isEditorSprite, const Outfit &outfit) {
if (textureId <= 0) {
return;
}

auto width = rme::TileSize;
auto height = rme::TileSize;
// Adjusts the offset of normal sprites
if (!isEditorSprite) {
SpriteSheetPtr sheet = g_spriteAppearances.getSheetBySpriteId(textureId);
if (!sheet) {
return;
}

auto spriteWidth = sheet->getSpriteSize().width;
auto spriteHeight = sheet->getSpriteSize().height;
// Fix the outfit offset 8x8 sprites being drawn offset
if (spriteWidth == 64 && spriteHeight == 64 && (outfit.lookType > 0 || outfit.lookItem > 0)) {
width = sheet->getSpriteSize().width;
height = sheet->getSpriteSize().height;

// If the sprite is an outfit and the size is 64x64, adjust the offset
if (width == 64 && height == 64 && (outfit.lookType > 0 || outfit.lookItem > 0)) {
GameSprite* spr = g_gui.gfx.getCreatureSprite(outfit.lookType);
if (spr && spr->getDrawOffset().x == 8 && spr->getDrawOffset().y == 8) {
sx -= spriteWidth / 2;
sy -= spriteWidth / 2;
sx -= width / 2;
sy -= height / 2;
}
}
}

spdlog::debug("Blitting outfit {} at ({}, {})", outfit.name, sx, sy);
// Adjust zoom if necessary
if (adjustZoom) {
if (zoom < 1.0f) {
float offset = 10 / (10 * zoom);
width = std::max<int>(16, static_cast<int>(width * zoom));
height = std::max<int>(16, static_cast<int>(height * zoom));
sx += offset;
sy += offset;
} else if (zoom > 1.f) {
float offset = (10 * zoom);
width += static_cast<int>(offset);
height += static_cast<int>(offset);
sx -= offset;
sy -= offset;
}
}

glBindTexture(GL_TEXTURE_2D, texture_number);
glColor4ub(uint8_t(red), uint8_t(green), uint8_t(blue), uint8_t(alpha));
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
glVertex2f(sx, sy);
glTexCoord2f(1.f, 0.f);
glVertex2f(sx + spriteWidth, sy);
glTexCoord2f(1.f, 1.f);
glVertex2f(sx + spriteWidth, sy + spriteHeight);
glTexCoord2f(0.f, 1.f);
glVertex2f(sx, sy + spriteHeight);
glEnd();
if (outfit.lookType > 0) {
spdlog::debug("Blitting outfit {} at ({}, {})", outfit.name, sx, sy);
}

glBindTexture(GL_TEXTURE_2D, textureId);
glColor4ub(uint8_t(red), uint8_t(green), uint8_t(blue), uint8_t(alpha));
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
glVertex2f(sx, sy);
glTexCoord2f(1.f, 0.f);
glVertex2f(sx + width, sy);
glTexCoord2f(1.f, 1.f);
glVertex2f(sx + width, sy + height);
glTexCoord2f(0.f, 1.f);
glVertex2f(sx, sy + height);
glEnd();
}

void MapDrawer::glBlitSquare(int x, int y, int red, int green, int blue, int alpha) {
Expand Down
2 changes: 1 addition & 1 deletion source/map_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class MapDrawer {
};

void getColor(Brush* brush, const Position &position, uint8_t &r, uint8_t &g, uint8_t &b);
void glBlitTexture(int sx, int sy, int texture_number, int red, int green, int blue, int alpha, bool adjustZoom = false, const Outfit &outfit = {});
void glBlitTexture(int sx, int sy, int texture_number, int red, int green, int blue, int alpha, bool adjustZoom = false, bool isEditorSprite = false, const Outfit &outfit = {});
void glBlitSquare(int x, int y, int red, int green, int blue, int alpha);
void glBlitSquare(int x, int y, const wxColor &color);
void glColor(const wxColor &color);
Expand Down
8 changes: 0 additions & 8 deletions source/sprites.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
#ifndef RME_SPRITES_H_
#define RME_SPRITES_H_

enum {
SPRITE_FLAME_BLUE = 775,
SPRITE_FLAG_GREY = 2889,
SPRITE_FLAG_GREEN = 2890,
SPRITE_SPAWN = 2138,
SPRITE_SPAWN_NPC = 2133
};

enum {
EDITOR_SPRITE_SELECTION_MARKER = -1000,
EDITOR_SPRITE_BRUSH_CD_1x1,
Expand Down

0 comments on commit d9f210c

Please sign in to comment.