I've created a basic program that renders sprites in a windows console using the SetPixel() method, and it works fine, but there is massive overhead.
I've made some optimizations to this, which helped but it's still too slow.
Currently my program uses two buffers of COLORREF draws the newer to the screen, swaps them and starts over. However it only redraws a pixel if said pixel has changed. This improved performance massively but it's still slow.
Buffer swapping isn't done with pointers as of yet, but the real overhead is SetPixel() so, i'm looking for an alternative way of creating pixel level graphics with GDI, which is faster than SetPixel()
(ignore anim_frame and the first dimension of the img_data vector, they are just there for the future if i decide to add animated objects)
void graphics_context::update_screen()
{
update_buffer();
for (int x = 0; x < this->width; x++)
{
for (int y = 0; y < this->height; y++)
{
if (this->buffer.at(x).at(y) != this->buffer_past.at(x).at(y))
{
for (int i = 0; i < this->scale_factor; i++)
{
for (int j = 0; j < this->scale_factor; j++)
{
int posX = i + (this->scale_factor * x) + this->width_offset;
int posY = j + (this->scale_factor * y) + this->height_offset;
SetPixel(this->target_dc, posX, posY, this->buffer.at(x).at(y));
}
}
}
}
}
buffer_past = buffer;
}
And this is the update_buffer() method:
void graphics_context::update_buffer()
{
for (int x = 0; x < this->width; x++)
{
for (int y = 0; y < this->height; y++)
{
buffer.at(x).at(y) = RGB(0, 0, 0);
}
}
//this->layers.at(1)->sprite; <- pointer to member gfx_obj pointer
for (int i = 0; i < this->layers.size(); i++)
{
gfx_object tmp_gfx = *this->layers.at(i)->sprite;
for (int x = 0; x < tmp_gfx.img_data.at(0).size(); x++)
{
for (int y = 0; y < tmp_gfx.img_data.at(tmp_gfx.anim_frame).at(0).size(); y++)
{
if(tmp_gfx.img_data.at(tmp_gfx.anim_frame).at(x).at(y) != RGB(0,255,0))
buffer.at(x + this->layers.at(i)->locX).at(y + this->layers.at(i)->locY) = tmp_gfx.img_data.at(tmp_gfx.anim_frame).at(x).at(y);
}
}
}
}