Skip to content

Commit

Permalink
Use the drawing context provided by rendering engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Jan 24, 2023
1 parent cef2539 commit 63445f6
Show file tree
Hide file tree
Showing 99 changed files with 1,234 additions and 776 deletions.
7 changes: 5 additions & 2 deletions src/OpenLoco/src/Drawing/FPSCounter.cpp
@@ -1,4 +1,5 @@
#include "FPSCounter.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Localisation/StringManager.h"
Expand Down Expand Up @@ -35,6 +36,8 @@ namespace OpenLoco::Drawing

void drawFPS()
{
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();

// Measure FPS
const float fps = measureFPS();

Expand All @@ -50,10 +53,10 @@ namespace OpenLoco::Drawing
auto& rt = Gfx::getScreenRT();

// Draw text
const int stringWidth = Gfx::getStringWidth(buffer);
const int stringWidth = drawingCtx.getStringWidth(buffer);
const auto x = Ui::width() / 2 - (stringWidth / 2);
const auto y = 2;
Gfx::drawString(rt, x, y, Colour::black, buffer);
drawingCtx.drawString(rt, x, y, Colour::black, buffer);

// Make area dirty so the text doesn't get drawn over the last
Gfx::invalidateRegion(x - 16, y - 4, x + 16, 16);
Expand Down
56 changes: 29 additions & 27 deletions src/OpenLoco/src/Drawing/SoftwareDrawingContext.cpp
Expand Up @@ -51,7 +51,9 @@ namespace OpenLoco::Drawing
}

static uint16_t getStringWidth(const char* buffer);

static std::pair<uint16_t, uint16_t> wrapString(char* buffer, uint16_t stringWidth);
static void drawRect(Gfx::RenderTarget& rt, int16_t x, int16_t y, uint16_t dx, uint16_t dy, uint32_t colour);

// 0x0112C876
static int16_t getCurrentFontSpriteBase()
{
Expand Down Expand Up @@ -488,13 +490,7 @@ namespace OpenLoco::Drawing
}

// 0x00448C79
void drawImage(Gfx::RenderTarget* rt, int16_t x, int16_t y, uint32_t image)
{
drawImage(*rt, { x, y }, ImageId::fromUInt32(image));
}

// 0x00448C79
void drawImage(Gfx::RenderTarget& rt, const Ui::Point& pos, const ImageId& image)
static void drawImage(Gfx::RenderTarget& rt, const Ui::Point& pos, const ImageId& image)
{
const auto* noiseImage = getNoiseMaskImageFromImage(image);
const auto palette = PaletteMap::getForImage(image);
Expand All @@ -509,6 +505,12 @@ namespace OpenLoco::Drawing
}
}

// 0x00448C79
static void drawImage(Gfx::RenderTarget* rt, int16_t x, int16_t y, uint32_t image)
{
drawImage(*rt, { x, y }, ImageId::fromUInt32(image));
}

void drawImageSolid(Gfx::RenderTarget& rt, const Ui::Point& pos, const ImageId& image, PaletteIndex_t paletteIndex)
{
PaletteMap::Buffer<PaletteMap::kDefaultSize> palette;
Expand Down Expand Up @@ -1056,25 +1058,6 @@ namespace OpenLoco::Drawing
return point.y;
}

// 0x00494B3F
// al: colour
// bx: string id
// cx: x
// dx: y
// esi: args
// edi: rt
static void drawStringLeft(
RenderTarget& rt,
int16_t x,
int16_t y,
AdvancedColour colour,
string_id stringId,
const void* args)
{
Point origin = { x, y };
drawStringLeft(rt, &origin, colour, stringId, args);
}

/**
*
* @param rt @<edi>
Expand All @@ -1100,6 +1083,25 @@ namespace OpenLoco::Drawing
origin->y = point.y;
}

// 0x00494B3F
// al: colour
// bx: string id
// cx: x
// dx: y
// esi: args
// edi: rt
static void drawStringLeft(
RenderTarget& rt,
int16_t x,
int16_t y,
AdvancedColour colour,
string_id stringId,
const void* args)
{
Point origin = { x, y };
drawStringLeft(rt, &origin, colour, stringId, args);
}

// 0x00494BBF
// al: colour
// bx: string id
Expand Down
4 changes: 2 additions & 2 deletions src/OpenLoco/src/Drawing/SoftwareDrawingContext.h
@@ -1,15 +1,15 @@
#pragma once

#include "Graphics/PaletteMap.h"
#include "Graphics/Gfx.h"
#include "Graphics/PaletteMap.h"
#include "Types.hpp"
#include "Ui/Rect.h"
#include <cstdint>

namespace OpenLoco::Drawing
{
class SoftwareDrawingContext
{
{
public:
void clear(Gfx::RenderTarget& rt, uint32_t fill);
void clearSingle(Gfx::RenderTarget& rt, uint8_t paletteId);
Expand Down
6 changes: 4 additions & 2 deletions src/OpenLoco/src/Entities/Misc.cpp
@@ -1,4 +1,5 @@
#include "Misc.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "EntityManager.h"
#include "Localisation/FormatArguments.hpp"
#include "Localisation/StringIds.h"
Expand Down Expand Up @@ -341,8 +342,9 @@ namespace OpenLoco
char buffer[255] = {};
auto args = FormatArguments::common(amount);
StringManager::formatString(buffer, strFormat, &args);
Gfx::setCurrentFontSpriteBase(Font::medium_bold);
m->offsetX = -Gfx::getStringWidth(buffer) / 2;
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.setCurrentFontSpriteBase(Font::medium_bold);
m->offsetX = -drawingCtx.getStringWidth(buffer) / 2;
m->wiggle = 0;
}
return m;
Expand Down
6 changes: 4 additions & 2 deletions src/OpenLoco/src/Graphics/Gfx.cpp
Expand Up @@ -341,7 +341,7 @@ namespace OpenLoco::Gfx
}

loco_global<uint8_t*, 0x0050B860> _50B860;

G1Element* getG1Element(uint32_t imageId)
{
const auto id = getImageIndex(imageId);
Expand Down Expand Up @@ -389,10 +389,12 @@ namespace OpenLoco::Gfx
/*.zoom_level = */ 0,
};

auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();

// Draw all the images ontop of the one bitmap
for (size_t i = 0; i < numImages; ++i)
{
drawImage(rt, { 0, 0 }, baseImageId.withIndexOffset(i));
drawingCtx.drawImage(rt, { 0, 0 }, baseImageId.withIndexOffset(i));
}

// Explore the bitmap to find the extents of the images drawn
Expand Down
4 changes: 0 additions & 4 deletions src/OpenLoco/src/Graphics/Gfx.h
Expand Up @@ -235,10 +235,6 @@ namespace OpenLoco::Gfx

G1Element* getG1Element(uint32_t id);

// 0x0112C876
int16_t getCurrentFontSpriteBase();
void setCurrentFontSpriteBase(int16_t base);

Drawing::SoftwareDrawingEngine& getDrawingEngine();

void loadCurrency();
Expand Down
7 changes: 5 additions & 2 deletions src/OpenLoco/src/Interop/Hooks.cpp
Expand Up @@ -9,6 +9,7 @@
#include "Audio/Audio.h"
#include "Config.h"
#include "Console.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Entities/EntityManager.h"
#include "Environment.h"
#include "Game.h"
Expand Down Expand Up @@ -682,7 +683,8 @@ void OpenLoco::Interop::registerHooks()
0x00451025,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
auto pos = Gfx::drawString(*X86Pointer<Gfx::RenderTarget>(regs.edi), regs.cx, regs.dx, static_cast<Colour>(regs.al), X86Pointer<uint8_t>(regs.esi));
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
auto pos = drawingCtx.drawString(*X86Pointer<Gfx::RenderTarget>(regs.edi), regs.cx, regs.dx, static_cast<Colour>(regs.al), X86Pointer<uint8_t>(regs.esi));
regs = backup;
regs.cx = pos.x;
regs.dx = pos.y;
Expand Down Expand Up @@ -906,7 +908,8 @@ void OpenLoco::Interop::registerHooks()
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Gfx::RenderTarget* rt = X86Pointer<Gfx::RenderTarget>(regs.edi);
Gfx::drawImage(*rt, { regs.cx, regs.dx }, ImageId::fromUInt32(regs.ebx));
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(*rt, { regs.cx, regs.dx }, ImageId::fromUInt32(regs.ebx));

regs = backup;
return 0;
Expand Down
4 changes: 3 additions & 1 deletion src/OpenLoco/src/Objects/AirportObject.cpp
@@ -1,4 +1,5 @@
#include "AirportObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
Expand All @@ -13,7 +14,8 @@ namespace OpenLoco
{
auto colourImage = Gfx::recolour(image, Colour::mutedDarkRed);

Gfx::drawImage(&rt, x - 34, y - 34, colourImage);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(&rt, x - 34, y - 34, colourImage);
}

// 0x00490DE7
Expand Down
4 changes: 3 additions & 1 deletion src/OpenLoco/src/Objects/BridgeObject.cpp
@@ -1,4 +1,5 @@
#include "BridgeObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
Expand All @@ -13,7 +14,8 @@ namespace OpenLoco
{
auto colourImage = Gfx::recolour(image, Colour::mutedDarkRed);

Gfx::drawImage(&rt, x - 21, y - 9, colourImage);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(&rt, x - 21, y - 9, colourImage);
}

// 0x0042C651
Expand Down
6 changes: 4 additions & 2 deletions src/OpenLoco/src/Objects/CliffEdgeObject.cpp
@@ -1,4 +1,5 @@
#include "CliffEdgeObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
Expand Down Expand Up @@ -37,7 +38,8 @@ namespace OpenLoco
// 0x00469A06
void CliffEdgeObject::drawPreviewImage(Gfx::RenderTarget& rt, const int16_t x, const int16_t y) const
{
Gfx::drawImage(&rt, x - 30, y, image);
Gfx::drawImage(&rt, x - 30, y, image + 16);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(&rt, x - 30, y, image);
drawingCtx.drawImage(&rt, x - 30, y, image + 16);
}
}
14 changes: 9 additions & 5 deletions src/OpenLoco/src/Objects/CompetitorObject.cpp
@@ -1,4 +1,5 @@
#include "CompetitorObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
Expand All @@ -14,38 +15,41 @@ namespace OpenLoco
// 0x00434D5B
void CompetitorObject::drawPreviewImage(Gfx::RenderTarget& rt, const int16_t x, const int16_t y) const
{
Gfx::drawRect(rt, 0, 0, kObjectPreviewSize.width, kObjectPreviewSize.height, AdvancedColour(Colour::mutedOrange).inset().u8());
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();

drawingCtx.drawRect(rt, 0, 0, kObjectPreviewSize.width, kObjectPreviewSize.height, AdvancedColour(Colour::mutedOrange).inset().u8());

auto image = Gfx::recolour(images[0], Colour::mutedSeaGreen);
Gfx::drawImage(&rt, x - 32, y - 32, image);
drawingCtx.drawImage(&rt, x - 32, y - 32, image);
}

// 0x00434DA7
void CompetitorObject::drawDescription(Gfx::RenderTarget& rt, const int16_t x, const int16_t y, [[maybe_unused]] const int16_t width) const
{
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
Ui::Point rowPosition = { x, y };
{
auto args = FormatArguments();
args.push<uint16_t>(intelligence);
args.push(aiRatingToLevel(intelligence));

Gfx::drawStringLeft(rt, rowPosition.x, rowPosition.y, Colour::black, StringIds::company_details_intelligence, &args);
drawingCtx.drawStringLeft(rt, rowPosition.x, rowPosition.y, Colour::black, StringIds::company_details_intelligence, &args);
rowPosition.y += kDescriptionRowHeight;
}
{
auto args = FormatArguments();
args.push<uint16_t>(aggressiveness);
args.push(aiRatingToLevel(aggressiveness));

Gfx::drawStringLeft(rt, rowPosition.x, rowPosition.y, Colour::black, StringIds::company_details_aggressiveness, &args);
drawingCtx.drawStringLeft(rt, rowPosition.x, rowPosition.y, Colour::black, StringIds::company_details_aggressiveness, &args);
rowPosition.y += kDescriptionRowHeight;
}
{
auto args = FormatArguments();
args.push<uint16_t>(competitiveness);
args.push(aiRatingToLevel(competitiveness));

Gfx::drawStringLeft(rt, rowPosition.x, rowPosition.y, Colour::black, StringIds::company_details_competitiveness, &args);
drawingCtx.drawStringLeft(rt, rowPosition.x, rowPosition.y, Colour::black, StringIds::company_details_competitiveness, &args);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/OpenLoco/src/Objects/CurrencyObject.cpp
@@ -1,4 +1,5 @@
#include "CurrencyObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Graphics/ImageIds.h"
Expand Down Expand Up @@ -76,7 +77,8 @@ namespace OpenLoco
auto defaultWidth = _characterWidths[Font::large + 131];
_characterWidths[Font::large + 131] = currencyElement->width + 1;

Gfx::drawStringCentred(rt, x, y - 9, Colour::black, StringIds::object_currency_big_font);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawStringCentred(rt, x, y - 9, Colour::black, StringIds::object_currency_big_font);

_characterWidths[Font::large + 131] = defaultWidth;
*defaultElement = backupElement;
Expand Down
4 changes: 3 additions & 1 deletion src/OpenLoco/src/Objects/DockObject.cpp
@@ -1,4 +1,5 @@
#include "DockObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
Expand All @@ -11,7 +12,8 @@ namespace OpenLoco
{
auto colourImage = Gfx::recolour(image, Colour::mutedDarkRed);

Gfx::drawImage(&rt, x - 34, y - 34, colourImage);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(&rt, x - 34, y - 34, colourImage);
}

// 0x00490F2C
Expand Down
4 changes: 3 additions & 1 deletion src/OpenLoco/src/Objects/HillShapesObject.cpp
@@ -1,4 +1,5 @@
#include "HillShapesObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
#include "ObjectImageTable.h"
Expand All @@ -13,7 +14,8 @@ namespace OpenLoco
{
auto imageId = image + hillHeightMapCount + mountainHeightMapCount;

Gfx::drawImage(&rt, x, y, imageId);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(&rt, x, y, imageId);
}

// 0x00463B70
Expand Down
4 changes: 3 additions & 1 deletion src/OpenLoco/src/Objects/IndustryObject.cpp
@@ -1,5 +1,6 @@
#include "IndustryObject.h"
#include "CargoObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Interop/Interop.hpp"
Expand Down Expand Up @@ -103,10 +104,11 @@ namespace OpenLoco
: Colour::black;
ImageId baseImage(var_12, c);
Ui::Point pos{ x, y };
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
for (const auto part : getBuildingParts(0))
{
auto image = baseImage.withIndexOffset(part * 4 + 1);
Gfx::drawImage(*clipped, pos, image);
drawingCtx.drawImage(*clipped, pos, image);
pos.y -= buildingPartHeight[part];
}
}
Expand Down

0 comments on commit 63445f6

Please sign in to comment.