Rendr function

Could someone please explain what's going on in this rendered background function? Really appreciate it. What are all the variables for?? Seems random to me. The only main point in this function is the fillrect() and the brush right?
But if someone could explain the purpose of the other variables i would appreciate it. Eg. What's dx, dy, wf, wh, rowData for?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void RenderredBackgrnd::rendr() const
{
    const unsigned int width = this->width();
    const unsigned int height = this->height();
    const int rowData = BYTES_PER_PIXEL * width;
    const float dx = 1.0f / static_cast<float>(width);
    const float dy = 1.0f / static_cast<float>(height);
    for (unsigned int h = 0; h < height; ++h)
    {
        for (unsigned int w = 0; w < width; ++w)
        {
            const float wf = static_cast<float>(w);
            const float hf = static_cast<float>(h);
            const Object::PointType ul(wf * dx, (hf + 1.0f) * dy);
            const Object::PointType lr((wf + 1.0f) * dx, hf * dy);
            const POINT pul = getFactory().toDevice(ul);
            const POINT plr = getFactory().toDevice(lr);
            const RECT rect = {pul.x, pul.y, plr.x, plr.y};
            const unsigned int index = h * rowData + BYTES_PER_PIXEL * w;
            const char* data = pixelData();
            HBRUSH brush = CreateSolidBrush(RGB(
                data[index + 2],
                data[index + 1],
                data[index + 0]
            ));
            FillRect(getFactory().getContext(), &rect, brush);
            DeleteObject(brush);
        }
    }
}

Last edited on
dx is 1 / width. this is a common technique for iteration over something in increments, eg if you want to do 10% of something per loop, you do it with this kind of percentage.

so if you look at the for loop on 10, w is 0- width, and now it multiplies by 1/width * increment... do you see what that does?

it does look like whoever coded this has too many redundant variables, and I can't answer what they were thinking. But dx and dy are probably necessary to do that kind of iteration, but that kind of iteration may not be necessary, not 100% sure but it looks... overcooked ... to me.
Ok. Thanks jonnin. I think I understand. What would you have done to simplify it since this is overcooked. I'm trying to simplify it hopefully. But do not know how to since this is such a complicated function. Do not know what to remove or add. Also, why do you think the person who coded this did this kind of incrementation. Why don't he just do a normal iteration without this kind of incrementation involved.
Last edited on
Looks like it's drawing some kind of pixel-art background. Is this correct?
I would do what I understand easily and work it up from there.
lets start with wf and wh. is there any reason for them?

also, can you update a 'brush' instead of create/destroy rapidly in a loop? That looks like a performance hit, but that depends on what the things really are.
Topic archived. No new replies allowed.