[closed] My Dasher stopped running! (C++ fundamentals)

When I added the array WinDimensions everything stopped running. My sprites are still and nothing moves.
What happened? I’ve checked and re-checked…
No fatal errors detected… just narrowing of int to float… (screenshot below)

#include "raylib.h"

//STRUCT
struct AnimData {
    Rectangle Rec;
    Vector2 Pos;
    int frame;
    float UpdateTime;
    float RunningTime;
};

//MAIN
int main () {
    int WinDimensions[2];
    WinDimensions[0] = 1280;
    WinDimensions[1] = 720;
    
    InitWindow(WinDimensions[0], WinDimensions[1], "ECCOCI!");
    SetTargetFPS(60);

    //GAME VARIABLES
    const int gravity{1'000};
    const int JumpVel{-600};
    bool CHECKAIR{};
    const float dT{GetFrameTime()};
          //characters velocity
    int Nvelocity{-20};
    int Svelocity{0};

    //---------------------------------------

    //TEXTURES

    Texture2D nebula = LoadTexture("nebula.png");
    Texture2D scarfy = LoadTexture("scarfy.png");

    //NEBULA 1 E 2
         ////short struct initialization
    AnimData nebulaData{
        {0.0, 0.0, nebula.width/8, nebula.height/8},//REC
        {WinDimensions[0], WinDimensions[1]-nebula.height/8}, //POS
        0, //FRAME
        1.0/34.0, //Update time
        0, // Running Time
        };

    AnimData nebula2Data{
        {0.0, 0.0, nebula.width/8, nebula.height/8},//REC
        {WinDimensions[0] + 300, WinDimensions[1]-nebula.height/8}, //POS
        0, //FRAME
        1.0/34.0, //Update time
        0, // Running Time
        };

    //SCARFY
                //long struct initialization

    AnimData scarfyData;
    scarfyData.Rec.x=0;
    scarfyData.Rec.y=0;
    scarfyData.Rec.width=scarfy.width/6;
    scarfyData.Rec.height=scarfy.height;
    scarfyData.Pos.x = WinDimensions[0]/3 - scarfyData.Rec.width/2;
    scarfyData.Pos.y = WinDimensions[1] - scarfyData.Rec.height;
    scarfyData.frame=0;
    scarfyData.UpdateTime = 1.0/8.0;
    scarfyData.RunningTime = 0;

    //----------------------------------------

    while(WindowShouldClose()==false) {

        BeginDrawing();
        ClearBackground(BLACK);

    //check gravity
        if(scarfyData.Pos.y>=WinDimensions[1]-scarfyData.Rec.height) {
            Svelocity = 0;
            CHECKAIR=false;
        }

        else {
            Svelocity += gravity *dT;
            CHECKAIR=true;
        }

    //check space press
        if(IsKeyPressed(KEY_SPACE) && !CHECKAIR) {
        Svelocity += JumpVel;
        }

    //UPDATE ANIMATION POSITIONS
        nebulaData.Pos.x += Nvelocity*dT;
        nebula2Data.Pos.x += Nvelocity*dT;
        scarfyData.Pos.y += Svelocity*dT;

    //UPDATE ANIMATION
            //update scarfy
        if(!CHECKAIR){
            scarfyData.RunningTime+=dT;
            if(scarfyData.RunningTime>=scarfyData.UpdateTime) {
                scarfyData.RunningTime=0.0;
                scarfyData.Rec.x=scarfyData.frame*scarfyData.Rec.width;
                scarfyData.frame++;
                if(scarfyData.frame>5) {
                scarfyData.frame=0;
                }

            }

        }

        nebulaData.RunningTime+=dT;
        if(nebulaData.RunningTime>=nebulaData.UpdateTime) {
            nebulaData.RunningTime=0.0;
            nebulaData.Rec.x=nebulaData.frame*nebulaData.Rec.width;
            nebulaData.frame++;
            if(nebulaData.frame>7) {
                nebulaData.frame=0;
            }

        }

        nebula2Data.RunningTime+=dT;
        if(nebula2Data.RunningTime>=nebula2Data.UpdateTime) {
            nebula2Data.RunningTime=0.0;
            nebula2Data.Rec.x=nebula2Data.frame*nebula2Data.Rec.width;
            nebula2Data.frame++;
            if(nebula2Data.frame>7) {
                nebula2Data.frame=0;
            }

        }

        //draw textures
        DrawTextureRec(nebula, nebulaData.Rec, nebulaData.Pos, WHITE);
        DrawTextureRec(nebula, nebula2Data.Rec, nebula2Data.Pos, GREEN);
        DrawTextureRec(scarfy,scarfyData.Rec,scarfyData.Pos,WHITE);

        EndDrawing();
        }

    UnloadTexture(scarfy);
    UnloadTexture(nebula);
    CloseWindow();

}

edit: I’ve been erasing all parts getting back to stage one of the course basically ahah

Your code would be much more readable if you removed the blank lines between each lline of code :wink:
(And even better if you had the code block tagged as cpp for some syntax highlighting)

It’s been a while and you seem to be further into the course than I had been.
I don’t have an array for the dimensions, just const values.

    // Window dimensions
    const int winWidth{1920};
    const int winHeight{1080};
    const char *winTitle{"Dapper Dasher"};

// [...]

    InitWindow(winWidth, winHeight, winTitle);
// [...]
    SetTargetFPS(60);

    while (!WindowShouldClose())
// ...

One difference I see would be that I call SetTargetFPS() which you seem to be missing.
I’m not sure how much of a difference this might have but at least it seems to be worth trying…

1 Like

Thank you! How can I define the code as cpp in the comments?

Yeah… it’s a mess I’ve been erasing moving e writing things to get it work then rage pasted it.

Moving and removing SetTargetFPS didn’t do the trick :frowning:

Edit: FIXED IT

The lack of SetTargetFPS wasn’t the only problem, but also that const float dT{GetFrameTime()}; was placed outside of the while-loop and thus was never getting updated.

1 Like

Who would’ve guessed, essential! Thank you so much, that solved it.

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

Privacy & Terms