handmade.network » Forums » Day 008 - the window graphics is not moving anymore
shuravi
3 posts
#13962 Day 008 - the window graphics is not moving anymore
7 months, 1 week ago Edited by shuravi on Jan. 8, 2018, 10:48 p.m. Reason: Initial post

I unzipped the archive of Day 008 (the Day 007 file works fine).
In Casey's tutorial, inside the window the "blocks" were moving,
but when I run this file, the window opens and the sound starts to play,
but the "graphical blocks" doesn't move.

I have a Windows 10.

Does anyone know why this does happen and how to fix this?
ralph_the_mighty
Joshua Donahue
2 posts

None

#13964 Day 008 - the window graphics is not moving anymore
7 months, 1 week ago Edited by Joshua Donahue on Jan. 9, 2018, 2:34 a.m.

I have the exact same issue on my machine. The problem is that the gradient function renders the gradient based on the offset passed in.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
internal void
RenderWeirdGradient(win32_offscreen_buffer *Buffer, int BlueOffset, int GreenOffset)
{
    // TODO(casey): Let's see what the optimizer does

    uint8 *Row = (uint8 *)Buffer->Memory;    
    for(int Y = 0;
        Y < Buffer->Height;
        ++Y)
    {
        uint32 *Pixel = (uint32 *)Row;
        for(int X = 0;
            X < Buffer->Width;
            ++X)
        {
            uint8 Blue = (X + BlueOffset);
            uint8 Green = (Y + GreenOffset);

            *Pixel++ = ((Green << 8) | Blue);
        }
        
        Row += Buffer->Pitch;
    }
}

The BlueOffset and GreenOffset parameters control the horizontal and vertical scaling of the gradient, respectively. RenderWeirdGradient is being called in the main loop here, after the XInput handling:

 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
33
34
35
36
37
38
// NOTE(casey): Graphics test
int XOffset = 0;
int YOffset = 0;

...

while(GlobalRunning)
            {
            ...

// TODO(casey): Should we poll this more frequently
                for (DWORD ControllerIndex = 0;
                     ControllerIndex < XUSER_MAX_COUNT;
                     ++ControllerIndex)
                {
                    XINPUT_STATE ControllerState;
                    if(XInputGetState(ControllerIndex, &ControllerState) == ERROR_SUCCESS)
                    {
                        // NOTE(casey): This controller is plugged in
                        // TODO(casey): See if ControllerState.dwPacketNumber increments too rapidly
                        XINPUT_GAMEPAD *Pad = &ControllerState.Gamepad;

                        /* more button stuff */

                        int16 StickX = Pad->sThumbLX;
                        int16 StickY = Pad->sThumbLY;

                        XOffset += StickX >> 12;
                        YOffset += StickY >> 12;
                    }
                    else
                    {
                        // NOTE(casey): The controller is not available
                    }

            RenderWeirdGradient(&GlobalBackbuffer, XOffset, YOffset);
            ...
            }


XOffset and YOffset are initialized to zero, and are updated based on the analog stick input. If you don't have a controller plugged in, or if XInput failed to register for whatever reason, XOffset and YOffset will remain 0, and the gradient won't move.

None
shuravi
3 posts
#13967 Day 008 - the window graphics is not moving anymore
7 months, 1 week ago

Thank you, now it works.