Substitute sprite

so i tried just inputting a santa sprite instead of scarfy and it never shows up on screen
santa

What changes have you made to get the new sprite to work?

i cut the santa sprite in half so that it was 1 row 6 images to match the scarfy dimensions

Tried using the spritesheet mayself, make sure you’re using PNG for your spritesheets as Raylib does not support JPEG for texture loading.

image
it says .PNG

Weird, the version I grabbed from your post came in as jpeg. Can you I see your code? All I had to do was change the file being pointed at for “scarfy” and it worked just fine.

#include “raylib.h”

struct AnimData

{

Rectangle rec;

Vector2 pos;

int frame;

float updateTime;

float runningTime;

};

bool isOnGround(AnimData data, int windowHeight)

{

return data.pos.y >= windowHeight - data.rec.height;

}

AnimData updateAnimData(AnimData data, float deltaTime, int maxFrame)

{

// update running time

data.runningTime += deltaTime;

if (data.runningTime >= data.updateTime)

{

    data.runningTime = 0.0;

    // update animation frame

    data.rec.x = data.frame * data.rec.width;

    data.frame++;

    if (data.frame > maxFrame)

    {

        data.frame = 0;

    }

}

return data;

}

int main()

{

int windowDimensions[2];

windowDimensions[0] = 512;

windowDimensions[1] = 380;

// initialize the window

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

// acceleration due to gravity (pixels/s)/s

const int gravity{1'000};

// jump velocity (pixels/second)

const int jumpVel{-600};

bool isInAir{};

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

// AnimData for nebulae  

const int sizeOfNebulae{3};

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 = 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;

}

float finishline{ nebulae[sizeOfNebulae - 1].pos.x };



// nebula x velocity (pixels/second)

int nebVel{-200};

// scarfy variables

Texture2D scarfy = LoadTexture("textures/santa.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/1;

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

scarfyData.frame = 0;

scarfyData.updateTime = 1.0/12.0;

scarfyData.runningTime = 0.0;

int velocity{0};

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

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

Texture2D foreground = LoadTexture("textures/foreground.png");

float bgX{};

float mgX{};

float fgX{};

bool collision{};

SetTargetFPS(60);

while (!WindowShouldClose())

{

    // delta time ( time since last frame)

    const float dt{GetFrameTime()};

    // start drawing

    BeginDrawing();

    ClearBackground(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);

    // draw the midground

    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);

    // draw the foreground

    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);

    // 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;

    }

    // ground check

    if(isOnGround(scarfyData, windowDimensions[1]))

    {

        // rectangle is on the ground

        velocity = 0;

        isInAir = false;

    }

    else

    {

        // rectangle is in the air

        velocity += gravity * dt;

        isInAir = true;

    }

    // check for jumping

    if(IsKeyPressed(KEY_SPACE) && !isInAir)

    {

        velocity += jumpVel;

    }

    for (int i = 0; i < sizeOfNebulae; i++)

    {   // update the position of each nebula

        nebulae[i].pos.x += nebVel * dt;

    }

    // update finishline

    finishline += nebVel * dt;

   

    for (int i = 0; i < sizeOfNebulae; i++ )

    {

        nebulae[i] = updateAnimData(nebulae[i], dt, 7);

    }



    // update scarfy position

    scarfyData.pos.y += velocity * dt;

    // update scarfy animation frame

    if (!isInAir)

    {

     scarfyData = updateAnimData(scarfyData, dt, 5);

    }

    for (AnimData nebula : nebulae)

    {

        float pad{50};

        Rectangle nebRec{

            nebula.pos.x + pad,

            nebula.pos.y + pad,

            nebula.rec.width - 2*pad,

            nebula.rec.height -2*pad

        };

        Rectangle scarfyRec{

            scarfyData.pos.x,

            scarfyData.pos.y,

            scarfyData.rec.width,

            scarfyData.rec.height

        };

        if (CheckCollisionRecs(nebRec, scarfyRec))

        {

            collision = true;

        }

    }

    if (collision)        

    {

        // lose the game

        DrawText("Game Over!", windowDimensions[0]/4, windowDimensions[1]/2, 40, RED);

    }

    else if (scarfyData.pos.x >= finishline)

    {

        // win the game

        DrawText("You Win!", windowDimensions[0]/4, windowDimensions[1]/2, 40, RED);

    }

    else

        {

            for (int i = 0; i < sizeOfNebulae; i++)

            {

                    // draw nebula

                DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);

                }

               

                // draw nebula

                DrawTextureRec(nebula, nebulae[0].rec, nebulae[0].pos, WHITE);

                // draw nebula 2

                DrawTextureRec(nebula, nebulae[1].rec, nebulae[1].pos, RED);

                // draw nebula 3

                DrawTextureRec(nebula, nebulae[2].rec, nebulae[2].pos, GREEN);

                // draw scarfy

                DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE);

    }

         

    // stop drawing

    EndDrawing();    

}

UnloadTexture(scarfy);

UnloadTexture(nebula);

UnloadTexture(background);

UnloadTexture(midground);

UnloadTexture(foreground);

CloseWindow();

}

Ok, so your code isn’t the problem, this is the result I got.

Screenshot from 2022-06-10 12-13-40

Can you take a look at your terminal tab and see if there are any errors with image loading?

i seem to get this error when i just open the project directly

What do you mean by “open the project directly” ?

Make sure you’re opening the whole folder, as that is your project.

opening the c++ source file inside the folder

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

Privacy & Terms