Here are some images to display what I am describing:
The below depicts the ideal drawing:
https://imgur.com/a/U447hEB
This was done by simply creating assets from the top left to the bottom right, so the bitmaps would naturally overlap.
The below depicts the problem using an unsorted approach:
https://imgur.com/a/xMXHEh2
The below depicts the ideal drawing using a std::stable_sort:
https://imgur.com/a/sbS9LBz
Drawing assets from the top left to the bottom right results in no issues, the image confusion to come is not present.
The below depicts the issue present using std::stable_sort before drawing each asset's bitmap:
https://imgur.com/a/uUUZ3aU
Though an entirely different asset has been selected, it seems that drawing different patterns, i.e. not from the top left to the bottom right, results in a certain strangeness in which the bitmaps of assets become jumbled.
I am not sure what could be the cause of this issue. I have stepped through the paint event handler to ensure that created assets are sorted correctly according the the comparator:
Code: Select all
[](MapTile m1, MapTile m2){return m1.getBounds().x < m2.getBounds().x || m1.getBounds().y < m2.getBounds().y;}
Here I will include the relevant code within the paint event handler:
Code: Select all
vector<Layer*> layer_vector
{
&draw_boxes_background,
&draw_boxes_layer1,
&draw_boxes_layer2,
&draw_boxes_layer3,
&draw_boxes_layer4
};
if(Replot())
{
for(Layer* l : layer_vector)
{
const int layer_number = l->getLayer();
vector<MapTile>& v = *l->getLayerVector();
if(v.size() == 0){continue;}
switch(layer_number)
{
case(0) :
{
for(int y{}, index{}; y < squares_per_column; ++y)
{
for(int x{}; x < squares_per_row; ++x)
{
lock_guard g{m};
MapTile& t = v[index];
/* How are the coordinates to be calculated with "free" assets? An "offset" member perhaps? */
int x_coord = static_cast<float>(draw_origin.m_x + x) * static_cast<float>(scaled_tile_size);
int y_coord = static_cast<float>(draw_origin.m_y + y) * static_cast<float>(scaled_tile_size);
/* RANDOMIZATION OF BACKGROUND TILES */
if(t == MapTile{}){v[index] = move(MapTile{1});}
/* This can be simplified when compared to the bounds assignment in the MouseUp event. */
if(!t.ImageInitialized()){t.InitializeImage(*gc);}
t.UpdateCoordinates(x_coord, y_coord,
scaled_tile_size * t.getOccupiedWidth(),
scaled_tile_size * t.getOccupiedHeight(),
*gc);
++index;
}
}
break;
}
case(1) :
{
for(int y{}, index{}; y < squares_per_column; ++y)
{
for(int x{}; x < squares_per_row; ++x)
{
lock_guard g{m};
MapTile& t = v[index];
int x_coord = static_cast<float>(draw_origin.m_x + x) * static_cast<float>(scaled_tile_size);
int y_coord = static_cast<float>(draw_origin.m_y + y) * static_cast<float>(scaled_tile_size);
/* An inequality operator for class MapTile needs to be defined. */
if(!(t == MapTile{}) && !t.ImageInitialized()){t.InitializeImage(*gc);}
t.UpdateCoordinates(x_coord, y_coord,
scaled_tile_size * t.getOccupiedWidth(),
scaled_tile_size * t.getOccupiedHeight(),
*gc
);
++index;
}
}
break;
}
case(2) :
{
if(layer2_sorted == false)
{
/* Remember constant and mutable may be used here (a std::vector of const MapTiles). */
stable_sort(v.begin(), v.end(), [](MapTile m1, MapTile m2)
{
return m1.getBounds().x < m2.getBounds().x ||
m1.getBounds().y < m2.getBounds().y;
}
);
layer2_sorted = true;
for(MapTile& m : v){m.InitializeImage(*gc);}
}
for(MapTile& m : v)
{
wxRect& r = m.getBounds();
r.x = m.getNormalizedX() * map_width;
r.y = m.getNormalizedY() * map_height;
m.UpdateCoordinates(r.x, r.y,
scaled_tile_size * m.getOccupiedWidth(),
scaled_tile_size * m.getOccupiedHeight(),
*gc
);
if(!(m == MapTile{}) && !m.ImageInitialized()){m.InitializeImage(*gc);}
}
break;
}
}
}
replot = false;
redraw = true;
}
if(Redraw())
{
for(Layer* l : layer_vector)
{
const int layer_number = l->getLayer();
vector<MapTile>& v = *l->getLayerVector();
if(v.size() == 0){continue;}
switch(layer_number)
{
case(0) : case(1) :
{
for(MapTile& m : v)
{
wxRect& r = m.getBounds();
if(m.getValue() != -1 && m.isMainTile()) /* Invalidation may be added here, e.g. "&& t.update". */
{
gc->DrawBitmap(
m.getGraphicsBitmap(),
r.x,
r.y,
r.width,
r.height
);
}
else
{
}
}
break;
}
case(2) :
{
/* The sorted elements from the plot section are somehow reversed by this point. */
for(MapTile& m : v)
{
//t.UpdateImage();
//t.InitializeImage(*gc);
wxRect& r = m.getBounds();
if(m.getValue() != -1 && m.isMainTile()) /* Invalidation may be added here, e.g. "&& t.update". */
{
gc->DrawBitmap(
m.getGraphicsBitmap(),
r.x,
r.y,
r.width,
r.height
);
}
else
{
}
}
break;
}
}
}
redraw = false;
}
}
delete gc;
return;
}
To finish, does anyone know of a possible source of this issue or have experience solving similar problems? For now I return to experimentation to see what may be the cause of the problem, I am sure it is something rather simple.