Refactoring this

I tried to refactor this code but I guess I dont remember what I learned during this course. I tried to work backwards and pseudo code things out but I struggle with figuring out what variables are what and how I should name them. How do I make figuring this out easier?

Summary
//clip
struct stageData{
};'

float updateStageScroll(float positionX, float scrollSpeed, float stageSize, float deltaTime)
{
    positionX.rec.x -= scrollSpeed * deltaTime;
    if(positionX.rec.x <= - stageSize.width*2)
    {
        positionX.rec.x = 0.0;
    }
}


//clip

int main()
{
 ...

    //Generate stage data
    const int propsToDraw[6];
    SetData stageSet[propsToDraw]{};

    //clip

    Texture2D background = LoadTexture("textures/far-buildings.png");
    Texture2D midground = LoadTexture("textures/back-buildings.png");
    Texture2D foreground = LoadTexture("textures/foreground.png");

    float stageX[3]{};

    /*
    float bgX{};
    float mgX{};
    float fgX{};*/

    SetTargetFPS(60);
    while(!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(WHITE);

        //initialize vector2s
        for(int i=0; i<stageGrounds; i++);
        {
            if (i%2 = 0)
            {
                stageSet[i].pos.x = stageX[i];
            }
            if (i%2 = 1)
            {
                stageSet[i].pos.x = stageX[i] + stageSet[i].width;
            }
            stageSet[i].pos.y = 0.0;
        }

        //Draw stage textures, there are 6 textures in total, 2 bg, 2 mg, and 2 fg
        for(int i=0; i < stageGrounds; i++);
        {
            DrawTextureEx(stageSet[i], stageSet[i].pos, 0.0, 2.0, WHITE);
        }

        /*
        //draw the background

        Vector2 bg1Pos{bgX, 0.0};
        DrawTextureEx(background, bg1Pos, 0.0, 2.0, WHITE);

        Vector2 bg2Pos{bgX + background.width*2, 0.0};
        DrawTextureEx(background, bg2Pos, 0.0, 2.0, WHITE);

        Vector2 mg1Pos{mgX, 0.0};
        DrawTextureEx(midground, mg1Pos,0.0, 2.0, WHITE);

        Vector2 mg2Pos{mgX + midground.width*2, 0.0};
        DrawTextureEx(midground, mg2Pos, 0.0, 2.0, WHITE);

        Vector2 fg1Pos{fgX, 0.0};
        DrawTextureEx(foreground, fg1Pos, 0.0, 2.0, WHITE);

        Vector2 fg2Pos{fgX + foreground.width*2, 0.0};
        DrawTextureEx(foreground, fg2Pos, 0.0, 2.0, WHITE);
        */


        //update the scrolling speed of stage
        for(int i =0, i < numberOfStages, i++)
        {
            stageX[i] = updateStageScroll(stageSet[i].pos.x, stageSet[i].scrollSpeed, stageSet[i].width, dT);
        }

        /*
        //scroll background
        bgX -= 20 * dT;
        if(bgX <= -background.width*2)
        {
            bgX = 0.0;
        }

        //scroll midground
        mgX -= 40*dT;
        if (mgX <= -midground.width*2)
        {
            mgX = 0.0;
        }

    //scroll foreground
        fgX -= 80*dT;
        if (fgX <= -foreground.width*2)
        {
            fgX = 0.0;
        }*/

//clip

1 Like

Judging from what I can see you’re wanting to have a Texture and a Vector2

1 Like

:+1:
I gave up, i fixed some of the variables but i think i dont have enough experience and its to much effort to go away from the editor and figure out what needs to be in the struct on scratch paper. I think its okay that i moved on since its out of scope for the course anyway

1 Like

Pushing yourself like that is a great way to build understanding of the material, even if you don’t succeed.

I’m sure the next time you try it’ll turn out better :wink:

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms