Browse Source

♻️ engine.h: Minor refactoring

1. Move `SetPixel` definition to the header to make it easier for the
   compiler to inline (make it inlinable even without LTO).
2. Add an `operator[](Point)` overload to `CelOutputBuffer`.
pull/1868/head
Gleb Mazovetskiy 5 years ago committed by Anders Jenbo
parent
commit
e7a9293153
  1. 12
      Source/engine.cpp
  2. 15
      Source/engine.h

12
Source/engine.cpp

@ -551,14 +551,6 @@ void CelBlitOutlineTo(const CelOutputBuffer &out, BYTE col, int sx, int sy, cons
}
}
void SetPixel(const CelOutputBuffer &out, Point position, BYTE col)
{
if (!out.in_bounds(position))
return;
*out.at(position.x, position.y) = col;
}
void DrawHorizontalLine(const CelOutputBuffer &out, Point from, int width, std::uint8_t colorIndex)
{
if (from.y < 0 || from.y >= out.h() || from.x >= out.w() || width <= 0 || from.x + width <= 0)
@ -574,7 +566,7 @@ void DrawHorizontalLine(const CelOutputBuffer &out, Point from, int width, std::
void UnsafeDrawHorizontalLine(const CelOutputBuffer &out, Point from, int width, std::uint8_t colorIndex)
{
std::memset(out.at(from.x, from.y), colorIndex, width);
std::memset(&out[from], colorIndex, width);
}
void DrawVerticalLine(const CelOutputBuffer &out, Point from, int height, std::uint8_t colorIndex)
@ -592,7 +584,7 @@ void DrawVerticalLine(const CelOutputBuffer &out, Point from, int height, std::u
void UnsafeDrawVerticalLine(const CelOutputBuffer &out, Point from, int height, std::uint8_t colorIndex)
{
auto *dst = out.at(from.x, from.y);
auto *dst = &out[from];
const auto pitch = out.pitch();
while (height-- > 0) {
*dst = colorIndex;

15
Source/engine.h

@ -260,6 +260,11 @@ struct CelOutputBuffer {
return region.h;
}
std::uint8_t &operator[](Point p) const
{
return *at(p.x, p.y);
}
BYTE *at(int x, int y) const
{
return static_cast<BYTE *>(surface->pixels) + region.x + x + surface->pitch * (region.y + y);
@ -283,7 +288,7 @@ struct CelOutputBuffer {
return surface->pitch;
}
bool in_bounds(Point position) const
bool InBounds(Point position) const
{
return position.x >= 0 && position.y >= 0 && position.x < region.w && position.y < region.h;
}
@ -520,7 +525,13 @@ void CelBlitOutlineTo(const CelOutputBuffer &out, BYTE col, int sx, int sy, cons
* @param point Target buffer coordinate
* @param col Color index from current palette
*/
void SetPixel(const CelOutputBuffer &out, Point position, BYTE col);
inline void SetPixel(const CelOutputBuffer &out, Point position, std::uint8_t col)
{
if (!out.InBounds(position))
return;
out[position] = col;
}
/**
* @brief Blit CL2 sprite, to the back buffer at the given coordianates

Loading…
Cancel
Save