Dapper Dasher For Loops / Array problems

I got the code 1:1 from the course but somehow my nebulae dont work and my windowDimensions influence the height of my scarfy texture.
After i put in the for loops the nebulae didnt show up and when i initialize sizeOfNebulae to more than 2 my scarfy texture also disappears for some reason.
There are no errors when debuging and the terminal says that the textures are successfully loaded

Here is my Code:

#include "raylib.h"

struct AnimData
{
    Rectangle rec;
    Vector2 pos;
    int frame;
    float updateTime;
    float runningTime;
};

int main()
{
    // window size
    int windowDimensions[2];
    windowDimensions[0] = 512;
    windowDimensions[1] = 380;

    InitWindow(windowDimensions[0], windowDimensions[1], "Dapper Dasher");

    // acceleration due to gravity (pixels/s)s
    const int gravity{1000};
    const int jumpVel{-600};

    // nebula variables
    Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");

    // much nebulae
    const int sizeOfNebulae{6};

    AnimData nebulae[sizeOfNebulae]{};

    for (int i = 0; i < sizeOfNebulae; i++)
    {
        nebulae[i].rec.x = 0.0;
        nebulae[i].rec.y = 0.0;
        nebulae[i].rec.width = 0.0, 0.0, nebula.width/8;
        nebulae[i].rec.height = nebula.height/8;
        nebulae[i].pos.y = windowDimensions[1] - nebula.height/8;
        nebulae[i].frame = 0;
        nebulae[i].runningTime = 0.0;
        nebulae[i].updateTime = 0.0;
        nebulae[i].pos.x = windowDimensions[0] + i * 300;
    }

    // nebula x velocity (pixels/s)
    int nebVel{-200};


    // scarfy variables
    Texture2D scarfy = LoadTexture("textures/scarfy.png");
    AnimData scarfyData;
    scarfyData.rec.width = scarfy.width/6;
    scarfyData.rec.height = scarfy.height;
    scarfyData.rec.x = 0;
    scarfyData.rec.y = 0;
    scarfyData.pos.x = windowDimensions[0]/2 - scarfyData.rec.width/2;
    scarfyData.rec.y = windowDimensions[1] - scarfyData.rec.height;
    scarfyData.frame = 0;
    scarfyData.updateTime = 1.0/12.0;
    scarfyData.runningTime = 0.0;


    int velocitiy = 0;

    // ground check
    bool isInAir;

    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        // delta time
        const float dT = GetFrameTime();

        // start drawing
        BeginDrawing();
        ClearBackground(BLACK);


        // ground check 
        if (scarfyData.pos.y >= windowDimensions[1] - scarfyData.rec.height)
        {
            // rectangle is on the ground
            velocitiy = 0;
            isInAir = false;
        }
        else
        {
            // rectangle is in the air
            velocitiy += gravity * dT;
            isInAir = true;
        }
        // check for jumping
        if (IsKeyPressed(KEY_SPACE) && !isInAir)
        {
            velocitiy += jumpVel;
        }

        // update nebula position
        for (int i = 0; i < sizeOfNebulae; i++)
        {
            nebulae[i].pos.x += nebVel * dT;    
        }
        
        // update scarfy position
        scarfyData.pos.y += velocitiy * dT;

        if (!isInAir)
        {
            // update running time each frame
            scarfyData.runningTime += dT;
            if (scarfyData.runningTime >= scarfyData.updateTime)
            {
                scarfyData.runningTime = 0.0;
                // update animation frame
                scarfyData.rec.x = scarfyData.frame * scarfyData.rec.width;
                scarfyData.frame++;
                if (scarfyData.frame >5)
                {
                    scarfyData.frame = 0;
                }
            }
        
        }

        // update nebula anim
       for (int i = 0; i < sizeOfNebulae; i++)
       {
            nebulae[i].runningTime += dT;
            if (nebulae[i].runningTime >= nebulae[i].updateTime)
            {
                nebulae[i].runningTime = 0.0;
                nebulae[i].rec.x = nebulae[i].frame * nebulae[i].rec.width;
                nebulae[i].frame++;
                if (nebulae[i].frame > 7)
                {
                    nebulae[i].frame = 0;
                }   
            }  
       }

        // draw nebula
        for (int i = 0; i < sizeOfNebulae; i++)
        {
        DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);
        }
        
        // Draw scarfy
        DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE);


        // stop drawing
        EndDrawing();
    }
    UnloadTexture(scarfy);
    UnloadTexture(nebula);
    CloseWindow();
}

For Scarfy’s height, take a look at this line

scarfyData.rec.y = windowDimensions[1] - scarfyData.rec.height;

Which I think you meant this:

scarfyData.pos.y = windowDimensions[1] - scarfyData.rec.height;

For the Nebulae, I’m not sure if this is causing issues but you can simplify this:

nebulae[i].rec.width = 0.0, 0.0, nebula.width/8;

to this:

nebulae[i].rec.width = nebula.width/8;

Let’s start with these and see where we’re at. Also, to correct a misunderstanding. If your code compiles that just means there are no compilation errors, your code can still contain bugs that take the form of undefined or unintended behaviour.

oh yes, now it makes sense to me. It fixed all the issues, thank you very much

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

Privacy & Terms