Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed May 20, 2023
1 parent c20aa0f commit 812b3e6
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 166 deletions.
11 changes: 6 additions & 5 deletions modules/game_things/things.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ function load(version)
local errorMessage = ''

if version >= 1281 and not g_game.getFeature(GameLoadSprInsteadProtobuf) then
if not g_things.loadAppearances(resolvepath(string.format('/things/%d/catalog-content', version))) then
if not g_things.loadAppearances(resolvepath(string.format('/things/%d/', version))) then
errorMessage = errorMessage .. 'Couldn\'t load assets'
end
else
if g_game.getFeature(GameLoadSprInsteadProtobuf) then
local warningBox = displayErrorBox(tr('Warning'), 'Load spr instead protobuf it\'s unstable, use by yours risk!')
local warningBox = displayErrorBox(tr('Warning'),
'Load spr instead protobuf it\'s unstable, use by yours risk!')
addEvent(function()
warningBox:raise()
warningBox:focus()
end)
end)
end
local datPath, sprPath
if filename then
Expand All @@ -47,11 +48,11 @@ function load(version)

if not g_things.loadDat(datPath) then
errorMessage = errorMessage ..
tr('Unable to load dat file, please place a valid dat in \'%s.dat\'', datPath) .. '\n'
tr('Unable to load dat file, please place a valid dat in \'%s.dat\'', datPath) .. '\n'
end
if not g_sprites.loadSpr(sprPath) then
errorMessage = errorMessage ..
tr('Unable to load spr file, please place a valid spr in \'%s.spr\'', sprPath)
tr('Unable to load spr file, please place a valid spr in \'%s.spr\'', sprPath)
end
end

Expand Down
20 changes: 10 additions & 10 deletions src/client/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ void Creature::draw(const Point& dest, uint32_t flags, LightView* lightView)

void Creature::internalDraw(Point dest, bool isMarked, const Color& color, LightView* lightView)
{
int animationPhase = 0;

if (isMarked)
g_drawPool.setShaderProgram(g_painter->getReplaceColorShader());
else
Expand All @@ -113,34 +111,32 @@ void Creature::internalDraw(Point dest, bool isMarked, const Color& color, Light
// outfit is a real creature
if (m_outfit.isCreature()) {
if (m_outfit.hasMount()) {
animationPhase = getCurrentAnimationPhase(true);

dest -= m_mountType->getDisplacement() * g_drawPool.getScaleFactor();

if (!isMarked && m_mountShader)
g_drawPool.setShaderProgram(m_mountShader, true, m_mountShaderAction);
m_mountType->draw(dest, 0, m_numPatternX, 0, 0, animationPhase, Otc::DrawThingsAndLights, color);
m_mountType->draw(dest, 0, m_numPatternX, 0, 0, getCurrentAnimationPhase(true), Otc::DrawThingsAndLights, color);

dest += getDisplacement() * g_drawPool.getScaleFactor();
}

animationPhase = getCurrentAnimationPhase();

if (!m_jumpOffset.isNull()) {
const auto& jumpOffset = m_jumpOffset * g_drawPool.getScaleFactor();
dest -= Point(std::round(jumpOffset.x), std::round(jumpOffset.y));
}

const auto& datType = getThingType();
const int animationPhase = getCurrentAnimationPhase();

if (!isMarked && m_shader)
g_drawPool.setShaderProgram(m_shader, m_shaderAction);

// yPattern => creature addon
for (int yPattern = 0; yPattern < getNumPatternY(); ++yPattern) {
// continue if we dont have this addon
if (yPattern > 0 && !(m_outfit.getAddons() & (1 << (yPattern - 1))))
continue;

if (!isMarked && m_shader)
g_drawPool.setShaderProgram(m_shader, true, m_shaderAction);

datType->draw(dest, 0, m_numPatternX, yPattern, m_numPatternZ, animationPhase, Otc::DrawThingsAndLights, color);

if (m_drawOutfitColor && !isMarked && getLayers() > 1) {
Expand All @@ -153,6 +149,9 @@ void Creature::internalDraw(Point dest, bool isMarked, const Color& color, Light
}
}

if (!isMarked && m_shader)
g_drawPool.resetShaderProgram();

// outfit is a creature imitating an item or the invisible effect
} else {
int animationPhases = m_thingType->getAnimationPhases();
Expand All @@ -165,6 +164,7 @@ void Creature::internalDraw(Point dest, bool isMarked, const Color& color, Light
animateTicks = g_gameConfig.getInvisibleTicksPerFrame();
}

int animationPhase = 0;
if (animationPhases > 1) {
animationPhase = (g_clock.millis() % (static_cast<long long>(animateTicks) * animationPhases)) / animateTicks;
}
Expand Down
18 changes: 11 additions & 7 deletions src/client/spriteappearances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ bool SpriteAppearances::loadSpriteSheet(const SpriteSheetPtr& sheet) const
std::scoped_lock lock(sheet->m_mutex);

try {
const auto& fin = g_resources.openFile(stdext::format("/things/%d/%s", g_game.getClientVersion(), sheet->file));
const auto& path = stdext::format("%s%s", g_spriteAppearances.getPath(), sheet->file);
if (!g_resources.fileExists(path))
return false;

const auto& fin = g_resources.openFile(path);
fin->cache();

const auto decompressed = std::make_unique<uint8_t[]>(LZMA_UNCOMPRESSED_SIZE); // uncompressed size, bmp file + 122 bytes header
Expand Down Expand Up @@ -132,7 +136,7 @@ bool SpriteAppearances::loadSpriteSheet(const SpriteSheetPtr& sheet) const
// flip vertically
for (int y = 0; y < 192; ++y) {
uint8_t* itr1 = &bufferStart[y * SPRITE_SHEET_WIDTH_BYTES];
uint8_t* itr2 = &bufferStart[(384 - y - 1) * SPRITE_SHEET_WIDTH_BYTES];
uint8_t* itr2 = &bufferStart[(SpriteSheet::SIZE - y - 1) * SPRITE_SHEET_WIDTH_BYTES];

for (std::size_t x = 0; x < SPRITE_SHEET_WIDTH_BYTES; ++x) {
std::swap(*(itr1 + x), *(itr2 + x));
Expand Down Expand Up @@ -179,8 +183,8 @@ SpriteSheetPtr SpriteAppearances::getSheetBySpriteId(int id, bool load /* = true

const auto& sheet = *sheetIt;

if (load)
loadSpriteSheet(sheet);
if (load && !loadSpriteSheet(sheet))
return nullptr;

return sheet;
}
Expand All @@ -199,7 +203,7 @@ ImagePtr SpriteAppearances::getSpriteImage(int id)
uint8_t* pixelData = image->getPixelData();

const int spriteOffset = id - sheet->firstId;
const int allColumns = size.width() == 32 ? 12 : 6; // 64 pixel width == 6 columns each 64x or 32 pixels, 12 columns
const int allColumns = sheet->getColumns();
const int spriteRow = std::floor(static_cast<float>(spriteOffset) / static_cast<float>(allColumns));
const int spriteColumn = spriteOffset % allColumns;

Expand Down Expand Up @@ -237,13 +241,13 @@ void SpriteAppearances::saveSpriteToFile(int id, const std::string& file)
void SpriteAppearances::saveSheetToFileBySprite(int id, const std::string& file)
{
if (const auto& sheet = getSheetBySpriteId(id)) {
Image image({ 384 }, 4, sheet->data.get());
Image image({ SpriteSheet::SIZE }, 4, sheet->data.get());
image.savePNG(file);
}
}

void SpriteAppearances::saveSheetToFile(const SpriteSheetPtr& sheet, const std::string& file)
{
Image image({ 384 }, 4, sheet->data.get());
Image image({ SpriteSheet::SIZE }, 4, sheet->data.get());
image.savePNG(file);
}
9 changes: 9 additions & 0 deletions src/client/spriteappearances.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ enum class SpriteLayout
class SpriteSheet
{
public:
static constexpr uint16_t SIZE = 384;

SpriteSheet(int firstId, int lastId, SpriteLayout spriteLayout, const std::string& file) : firstId(firstId), lastId(lastId), spriteLayout(spriteLayout), file(file) {}

Size getSpriteSize() const
Expand All @@ -55,6 +57,9 @@ class SpriteSheet
return size;
}

// 64 pixel width == 6 columns each 64x or 32 pixels, 12 columns
int getColumns() const { return SIZE / getSpriteSize().width(); }

int firstId = 0;
int lastId = 0;

Expand All @@ -76,6 +81,9 @@ class SpriteAppearances
void setSpritesCount(int count) { m_spritesCount = count; }
int getSpritesCount() const { return m_spritesCount; }

void setPath(const std::string& path) { m_path = path; }
std::string getPath() const { return m_path; }

bool loadSpriteSheet(const SpriteSheetPtr& sheet) const;
void saveSheetToFileBySprite(int id, const std::string& file);
void saveSheetToFile(const SpriteSheetPtr& sheet, const std::string& file);
Expand All @@ -89,6 +97,7 @@ class SpriteAppearances
private:
uint32_t m_spritesCount{ 0 };
std::vector<SpriteSheetPtr> m_sheets;
std::string m_path;
};

extern SpriteAppearances g_spriteAppearances;
Loading

0 comments on commit 812b3e6

Please sign in to comment.