The Sprite isn't showing

Hello, the sprite isn’t showing. Could someone please let me know the fix or if I’m missing something. Code is below.

Thanks!

#include “raylib.h”

main()

{

//window dimensions

const int window_width{1280};

const int window_height{720};

//Initialize window with framerate 60fps

InitWindow(window_width,window_height,“Dapper Dasher: Scarfy Go!”);

SetTargetFPS(60);

//Speed calculations

int velocity {0};

const int gravity {1}; //pixel per frame / per frame

const int jumpSpeed {-20};

bool isInAir {};

//Sprite Variables

Texture2D scarfy = LoadTexture(“Dasher_Project_010923\textures\scarfy.png”);

Rectangle scarfyRec;

scarfyRec.width = scarfy.width/6;

scarfyRec.height = scarfy.height;

scarfyRec.x = 0;

scarfyRec.y = 0;

Vector2 scarfyPos;

scarfyPos.x = window_width/2 - scarfyRec.width/2;

scarfyPos.y = window_height - scarfyRec.height;

// ! - negation operator is the same as ==false

while(!WindowShouldClose())

{

   

    BeginDrawing();

    ClearBackground(SKYBLUE);



// Rectangele touches top of window

 if (scarfyPos.y <= 0)

{

    velocity += gravity;

   

}

//Rectangle touches bottom of window

if (scarfyPos.y >= window_height - scarfyRec.height)

{

    //ground rectangle

    velocity =0;

    isInAir = false;

     

}

else

{

    //apply gravity to velocity

    velocity += gravity;

    isInAir = true;

}

// Increase jump velocity with spacebar if not in air

if (IsKeyPressed(KEY_SPACE) && !isInAir)

{



     velocity += jumpSpeed;

       

}

 //update Y position  

    scarfyPos.y += velocity;

DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);  



//stop drawing

    EndDrawing();

}

UnloadTexture(scarfy);

CloseWindow();

}

There are two things of note, both have to do with the path you’ve used for your call to LoadTexture.

  1. Be careful of which slash you use when defining the contents of a string. A back-slash \ follow by a character can result you in using what is called an escape character instead. As a result, the program will think you’re doing something else and not treat that backslash as what you intended. Instead, use a forward-slash /. As an aside, if you wanted to use a backslash, we can utilize that same escape-character feature and type \\ instead.

  2. The path that is supplied to the LoadTexutre function is meant to be relative. What this means is that any path supplied is assumed to be based on the root folder that contains the executable for your project (the exe file generated when you compile). In your case, Dasher_Project_010923\textures\scarfy.png means that Raylib was looking for scarfy.png in a folder called textures inside of a folder called Dasher_Project_010923 that was also contained within your root folder. Assuming that Dasher_Project_010923 is the name of your project’s root folder, this means you path should be textures\scarfy.png

With those two things in mind, “Dasher_Project_010923\textures\scarfy.png” should actually be “textures/scarfy.png”

Thank you so much for your help. Even after typing “textures/scarfy.png” the sprite still did not show up. The only thing that has worked so far is putting the scarfy.png into the same folder as the .cpp file and then loading “scarfy.png” from there.

Is there a reason why this might happen?

Can you show me a screenshot of your full project (not the code, but as it looks in VS Code)?

That will help me get a better idea of what’s going on and explain the situation.

I found some typos in my code. Correcting the slashes the way you suggested worked after all. Thanks again!

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

Privacy & Terms