Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Drawpool 3.0 #641

Merged
merged 73 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
c26a5d7
init
mehah Nov 23, 2023
7af246a
new way of drawing
mehah Nov 23, 2023
4c2cf27
Update lightview.cpp
mehah Nov 23, 2023
5e4c299
improve
mehah Nov 23, 2023
d13310e
Update drawpoolmanager.cpp
mehah Nov 23, 2023
335e071
improve
mehah Nov 23, 2023
661e339
improve
mehah Nov 23, 2023
fa63a0b
Update drawpoolmanager.cpp
mehah Nov 23, 2023
06e1e0e
improve
mehah Nov 23, 2023
47dc1e9
update
mehah Nov 23, 2023
8754f25
fixes
mehah Nov 23, 2023
a333637
Update drawpoolmanager.cpp
mehah Nov 24, 2023
8610031
m_repaint
mehah Nov 24, 2023
36c25b8
Update lightview.cpp
mehah Nov 24, 2023
db618ac
improve
mehah Nov 24, 2023
088aac9
Update graphicalapplication.cpp
mehah Nov 24, 2023
5eab35b
cleanup
mehah Nov 24, 2023
ea07201
improve
mehah Nov 24, 2023
295eeaa
improve
mehah Nov 24, 2023
c344b8d
Update asyncdispatcher.cpp
mehah Nov 24, 2023
faf660b
improve
mehah Nov 25, 2023
ccd2beb
improve
mehah Nov 25, 2023
a7866a4
Revert "improve"
mehah Nov 25, 2023
4489125
Update drawpool.h
mehah Nov 25, 2023
6b6e562
Update framebuffer.h
mehah Nov 25, 2023
452977b
improve
mehah Nov 25, 2023
f23f892
imrprove
mehah Nov 25, 2023
f94ecbb
improve
mehah Nov 25, 2023
d8192ec
Update drawpoolmanager.cpp
mehah Nov 25, 2023
83de1b0
improve
mehah Nov 25, 2023
67720a5
fixes
mehah Nov 25, 2023
bdb5ec4
Update graphicalapplication.cpp
mehah Nov 25, 2023
4ca45f2
real fps
mehah Nov 25, 2023
49db957
fix fps
mehah Nov 25, 2023
d64586b
Update graphicalapplication.cpp
mehah Nov 25, 2023
89ff7aa
Update drawpoolmanager.cpp
mehah Nov 25, 2023
5af088e
Update win32window.cpp
mehah Nov 25, 2023
bcf163a
fix vsync
mehah Nov 25, 2023
8ae69c2
vsync rollback
mehah Nov 25, 2023
6047a5c
removed mutex
mehah Nov 26, 2023
e5052e4
Update drawpoolmanager.cpp
mehah Nov 26, 2023
ad880ab
Update drawpoolmanager.cpp
mehah Nov 26, 2023
0eec1a1
Merge branch 'main' into optimization
mehah Nov 26, 2023
43862c6
update
mehah Nov 26, 2023
ac890ad
fix light
mehah Nov 26, 2023
fd29226
fix draws bugs
mehah Nov 26, 2023
566de76
fix shader draw
mehah Nov 26, 2023
8e65429
fix draws
mehah Nov 26, 2023
dadb33c
foreground map review
mehah Nov 26, 2023
18f49dd
improve
mehah Nov 26, 2023
069f934
improve
mehah Nov 26, 2023
3c3de96
fix crash
mehah Nov 26, 2023
988b678
fix memory leak
mehah Nov 26, 2023
09ce173
min threads
mehah Nov 26, 2023
3f4212c
improve fps counter
mehah Nov 26, 2023
15bad91
Update uiwidget.cpp
mehah Nov 26, 2023
f2e0ec5
fix lighting crash on resize
mehah Nov 26, 2023
50121ff
fix lighting draw
mehah Nov 26, 2023
bd886cb
Update asyncdispatcher.cpp
mehah Nov 26, 2023
0dbc7c6
fix crash
mehah Nov 27, 2023
f39bb6e
Update lightview.cpp
mehah Nov 27, 2023
d3cb286
Update drawpoolmanager.cpp
mehah Nov 27, 2023
2767ad2
Update drawpoolmanager.cpp
mehah Nov 27, 2023
78c8fbc
Revert "Update drawpoolmanager.cpp"
mehah Nov 27, 2023
718124a
Update lightview.cpp
mehah Nov 27, 2023
6708e0f
Update lightview.cpp
mehah Nov 27, 2023
c249d2b
Update graphicalapplication.cpp
mehah Nov 27, 2023
92ba14e
improve
mehah Nov 27, 2023
28f206e
Update graphicalapplication.cpp
mehah Nov 27, 2023
c117a8b
improve frame counter
mehah Nov 27, 2023
d16c7ce
Update application.h
mehah Nov 27, 2023
a9e10be
Update init.lua
mehah Nov 27, 2023
07fe126
update
mehah Nov 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Services = {

g_app.setName("OTClient - Redemption");
g_app.setCompactName("otclient");
g_app.setOrganizationName("otbr");
g_app.setOrganizationName("otcr");

g_app.hasUpdater = function()
return (Services.updater and Services.updater ~= "" and g_modules.getModule("updater"))
Expand Down
40 changes: 19 additions & 21 deletions modules/game_battle/battle.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Global Tables
local binaryTree = {} -- BST
local binaryTree = {} -- BST
local battleButtons = {} -- map of creature id

-- Global variables that will inherit from init
Expand Down Expand Up @@ -64,7 +64,7 @@ end
function init() -- Initiating the module (load)
g_ui.importStyle('battlebutton')
battleButton = modules.client_topmenu.addRightGameToggleButton('battleButton', tr('Battle') .. ' (Ctrl+B)',
'/images/topbuttons/battle', toggle)
'/images/topbuttons/battle', toggle)
battleButton:setOn(true)
battleWindow = g_ui.loadUI('battle')

Expand All @@ -88,14 +88,14 @@ function init() -- Initiating the module (load)
end

-- Adding Filter options
local options = {'hidePlayers', 'hideNPCs', 'hideMonsters', 'hideSkulls', 'hideParty'}
local options = { 'hidePlayers', 'hideNPCs', 'hideMonsters', 'hideSkulls', 'hideParty' }
for i, v in ipairs(options) do
hideButtons[v] = battleWindow:recursiveGetChildById(v)
end

-- Adding SortType and SortOrder options
local sortTypeOptions = {'Name', 'Distance', 'Age', 'Health'}
local sortOrderOptions = {'Asc.', 'Desc.'}
local sortTypeOptions = { 'Name', 'Distance', 'Age', 'Health' }
local sortOrderOptions = { 'Asc.', 'Desc.' }

local sortTypeBox = battleWindow:recursiveGetChildById('sortTypeBox')
for i, v in ipairs(sortTypeOptions) do
Expand Down Expand Up @@ -134,7 +134,6 @@ end

-- Binary Search, Insertion and Resort functions
local function debugTables(sortType) -- Print both battlebutton and binarytree tables

local function getInfo(v, sortType)
local returnedInfo = v.id
if sortType then
Expand Down Expand Up @@ -464,10 +463,14 @@ end

local function canBeSeen(creature)
return creature and creature:canBeSeen() and creature:getPosition() and
modules.game_interface.getMapPanel():isInRange(creature:getPosition())
modules.game_interface.getMapPanel():isInRange(creature:getPosition())
end

local function getDistanceBetween(p1, p2) -- Calculate distance
if p2 == nil then
p2 = { x = 0, y = 0 }
end

local xd = math.abs(p1.x - p2.x);
local yd = math.abs(p1.y - p2.y);

Expand Down Expand Up @@ -622,8 +625,8 @@ function removeCreature(creature, all) -- Remove a single creature or all
msg = msg .. p
end
assert(index ~= nil,
'Not able to remove creature: id ' .. creatureId .. ' not found in binary search using ' .. sortType ..
' to find value ' .. msg .. '.')
'Not able to remove creature: id ' .. creatureId .. ' not found in binary search using ' .. sortType ..
' to find value ' .. msg .. '.')
end
end
return false
Expand Down Expand Up @@ -688,10 +691,8 @@ function attackNext(previous)

if battleButton.isTarget then
foundTarget = true

elseif foundTarget and not nextElement then
nextElement = battleButton

elseif not foundTarget then
prevElement = battleButton
end
Expand All @@ -712,7 +713,6 @@ function attackNext(previous)
g_game.attack(firstElement.creature)
end
end

elseif firstElement then
g_game.attack(firstElement.creature)
else
Expand Down Expand Up @@ -896,9 +896,9 @@ function onCreaturePositionChange(creature, newPos, oldPos) -- Update battleButt
correctBattleButtons()
else
assert(index ~= nil,
'Not able to update Position Change. Creature: ' .. creature:getName() .. ' id ' ..
creatureId .. ' not found in binary search using ' .. sortType ..
' to find value ' .. oldDistance .. '.\n')
'Not able to update Position Change. Creature: ' .. creature:getName() .. ' id ' ..
creatureId .. ' not found in binary search using ' .. sortType ..
' to find value ' .. oldDistance .. '.\n')
end
end
end
Expand Down Expand Up @@ -952,11 +952,10 @@ function onCreatureHealthPercentChange(creature, healthPercent, oldHealthPercent
correctBattleButtons()
else
assert(index ~= nil,
'Not able to update HealthPercent Change. Creature: id ' .. creatureId ..
' not found in binary search using ' .. sortType .. ' to find value ' .. oldHealthPercent ..
'.')
'Not able to update HealthPercent Change. Creature: id ' .. creatureId ..
' not found in binary search using ' .. sortType .. ' to find value ' .. oldHealthPercent ..
'.')
end

end
battleButton:setLifeBarPercent(healthPercent)
end
Expand Down Expand Up @@ -986,7 +985,7 @@ function onBattleButtonMouseRelease(self, mousePosition, mouseButton) -- Interac
end

if ((g_mouse.isPressed(MouseLeftButton) and mouseButton == MouseRightButton) or
(g_mouse.isPressed(MouseRightButton) and mouseButton == MouseLeftButton)) then
(g_mouse.isPressed(MouseRightButton) and mouseButton == MouseLeftButton)) then
mouseWidget.cancelNextRelease = true
g_game.look(self.creature, true)
return true
Expand Down Expand Up @@ -1080,5 +1079,4 @@ function terminate() -- Terminating the Module (unload)
onGameStart = onGameStart
})
disconnecting()

end
3 changes: 2 additions & 1 deletion modules/game_features/features.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
controller = Controller:new()
controller:registerEvents(g_game, {
onClientVersionChange = function(version)
g_game.enableFeature(GameFormatCreatureName);
-- g_game.enableFeature(GameKeepUnawareTiles)
g_game.enableFeature(GameFormatCreatureName)

if version >= 750 then
g_game.enableFeature(GameSoul);
Expand Down
49 changes: 25 additions & 24 deletions src/client/lightview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,13 @@ LightView::LightView(const Size& size, const uint16_t tileSize) : m_pool(g_drawP
m_texture = std::make_shared<Texture>(size);
m_texture->setSmooth(true);
});

g_drawPool.use(DrawPoolType::LIGHT);
g_drawPool.addAction([this] {
{
std::scoped_lock l(m_pool->getMutex());
m_texture->updatePixels(m_pixels.data());
}
g_painter->resetColor();
g_painter->resetTransformMatrix();
g_painter->setTexture(m_texture.get());
g_painter->setCompositionMode(CompositionMode::MULTIPLY);
g_painter->drawCoords(m_coords);
});
}

void LightView::resize(const Size& size, const uint16_t tileSize) {
if (!m_texture || m_mapSize == size && m_tileSize == tileSize)
return;

std::scoped_lock l(m_pool->getMutex());
std::scoped_lock l(m_pool->getMutex(), m_pool->getMutexPreDraw());

m_mapSize = size;
m_tileSize = tileSize;
Expand All @@ -71,10 +58,10 @@ void LightView::resize(const Size& size, const uint16_t tileSize) {

void LightView::addLightSource(const Point& pos, const Light& light, float brightness)
{
if (light.intensity == 0)
if (!isDark() || light.intensity == 0)
return;

auto& lightData = m_lightData[m_currentLightData];
auto& lightData = m_lightData[0];

if (!lightData.lights.empty()) {
auto& prevLight = lightData.lights.back();
Expand All @@ -95,7 +82,7 @@ void LightView::addLightSource(const Point& pos, const Light& light, float brigh

void LightView::resetShade(const Point& pos)
{
auto& lightData = m_lightData[m_currentLightData];
auto& lightData = m_lightData[0];

size_t index = (pos.y / m_tileSize) * m_mapSize.width() + (pos.x / m_tileSize);
if (index >= lightData.tiles.size()) return;
Expand All @@ -104,20 +91,34 @@ void LightView::resetShade(const Point& pos)

void LightView::draw(const Rect& dest, const Rect& src)
{
updateCoords(dest, src);

if (m_updatedHash != m_hash) {
m_hash = m_updatedHash;
m_updatedHash = 0;

std::scoped_lock l(m_pool->getMutex());
if (++m_currentLightData > 1) m_currentLightData = 0;
std::scoped_lock l(m_pool->getMutexPreDraw());
std::swap(m_lightData[0], m_lightData[1]);
g_asyncDispatcher.dispatch([this] {
updatePixels();
});
}

auto& lightData = m_lightData[m_currentLightData];
g_drawPool.preDraw(DrawPoolType::LIGHT, [this, &dest, &src] {
g_drawPool.addAction([=, this] {
{
std::scoped_lock l(m_pool->getMutexPreDraw());
m_texture->updatePixels(m_pixels.data());
}

updateCoords(dest, src);
g_painter->setCompositionMode(CompositionMode::MULTIPLY);
g_painter->resetTransformMatrix();
g_painter->resetColor();
g_painter->setTexture(m_texture.get());
g_painter->drawCoords(m_coords);
});
});

auto& lightData = m_lightData[0];
lightData.lights.clear();
lightData.tiles.assign(m_mapSize.area(), {});
}
Expand All @@ -139,9 +140,9 @@ void LightView::updateCoords(const Rect& dest, const Rect& src) {
}

void LightView::updatePixels() {
std::scoped_lock l(m_pool->getMutex());
std::scoped_lock l(m_pool->getMutexPreDraw());

const auto& lightData = m_lightData[m_currentLightData ? 0 : 1];
const auto& lightData = m_lightData[1];

const size_t lightSize = lightData.lights.size();

Expand Down
1 change: 0 additions & 1 deletion src/client/lightview.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,5 @@ class LightView : public LuaObject
CoordsBuffer m_coords;
TexturePtr m_texture;
LightData m_lightData[2];
std::atomic_uint8_t m_currentLightData{ 0 };
std::vector<uint8_t> m_pixels;
};
66 changes: 35 additions & 31 deletions src/client/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,51 +152,55 @@ void Map::addStaticText(const StaticTextPtr& txt, const Position& pos) {
if (!g_app.isDrawingTexts())
return;

for (const auto& other : m_staticTexts) {
// try to combine messages
if (other->getPosition() == pos && other->addMessage(txt->getName(), txt->getMessageMode(), txt->getFirstMessage())) {
return;
g_textDispatcher.addEvent([=, this] {
for (const auto& other : m_staticTexts) {
// try to combine messages
if (other->getPosition() == pos && other->addMessage(txt->getName(), txt->getMessageMode(), txt->getFirstMessage())) {
return;
}
}
}

txt->setPosition(pos);
m_staticTexts.emplace_back(txt);
txt->setPosition(pos);
m_staticTexts.emplace_back(txt);
});
}

void Map::addAnimatedText(const AnimatedTextPtr& txt, const Position& pos) {
if (!g_app.isDrawingTexts())
return;

// this code will stack animated texts of the same color
AnimatedTextPtr prevAnimatedText;

bool merged = false;
for (const auto& other : m_animatedTexts) {
if (other->getPosition() == pos) {
prevAnimatedText = other;
if (other->merge(txt)) {
merged = true;
break;
g_textDispatcher.addEvent([=, this] {
// this code will stack animated texts of the same color
AnimatedTextPtr prevAnimatedText;

bool merged = false;
for (const auto& other : m_animatedTexts) {
if (other->getPosition() == pos) {
prevAnimatedText = other;
if (other->merge(txt)) {
merged = true;
break;
}
}
}
}

if (!merged) {
if (prevAnimatedText) {
Point offset = prevAnimatedText->getOffset();
if (const float t = prevAnimatedText->getTimer().ticksElapsed();
t < g_gameConfig.getAnimatedTextDuration() / 4.0) { // didnt move 12 pixels
const int32_t y = 12 - 48 * t / static_cast<float>(g_gameConfig.getAnimatedTextDuration());
offset += Point(0, y);
if (!merged) {
if (prevAnimatedText) {
Point offset = prevAnimatedText->getOffset();
if (const float t = prevAnimatedText->getTimer().ticksElapsed();
t < g_gameConfig.getAnimatedTextDuration() / 4.0) { // didnt move 12 pixels
const int32_t y = 12 - 48 * t / static_cast<float>(g_gameConfig.getAnimatedTextDuration());
offset += Point(0, y);
}
offset.y = std::min<int32_t>(offset.y, 12);
txt->setOffset(offset);
}
offset.y = std::min<int32_t>(offset.y, 12);
txt->setOffset(offset);
m_animatedTexts.emplace_back(txt);
}
m_animatedTexts.emplace_back(txt);
}

txt->setPosition(pos);
txt->onAppear();
txt->setPosition(pos);
txt->onAppear();
});
}

ThingPtr Map::getThing(const Position& pos, int16_t stackPos)
Expand Down
Loading
Loading