Drawing From Sprite Sheets - Texture2D scarfy = LoadTexture

Hello,

Issue: Loading texture for display using Texture2D command raylibs
Course: C++ Fundamentals: Game Programming For Beginners - Drawing From Sprite Sheets

Below is my code.

#include “raylib.h”

int main()

{

// Window Demensions

const int windowWidth{512};

const int windowHeight{380};

// initialize the window for play

InitWindow(windowWidth, windowHeight, "Dapper Dasher");

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

const int gravity{1};

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

Rectangle scarfyRec;

scarfyRec.width = scarfy.width/6;

scarfyRec.height = scarfyRec.height;

scarfyRec.x = 0;

scarfyRec.y = 0;

Vector2 scarfyPos;

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

scarfyPos.y = windowHeight - scarfyRec.height;

// jump velocity

const int jumpVel{-22};

// is rectangle in air?

bool isInAir{};

int velocity{0};

// sets FPS, creates window for game

SetTargetFPS(60);



while (!WindowShouldClose())

{

    // begins drawing

    BeginDrawing();

    ClearBackground(WHITE);

    // ground checker, gravity on ground

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

    {

        // rectangle is on the ground

        velocity = 0;

        isInAir = false;

    }

    else

    {

        // rectangle is in the air

        //apply gravity

        velocity += gravity;

        isInAir = true;

    }

        // moving rectangle using velocity/check for jumping

     if(IsKeyPressed(KEY_SPACE) && !isInAir)

    {

        velocity += jumpVel;

    }

    // updates position

    scarfyPos.y += velocity;

    // draws a rectangle to the screen

    DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);

    // stop drawing

    EndDrawing();

}

UnloadTexture(scarfy);

CloseWindow();

}

VSCode tells me the file is loaded correctly but nothing displays.

I have InitWindow(windowWidth, windowHeight, “Dapper Dasher”); ahead of the call for textures.


File path.
I tried Texture2D scarfy = LoadTexture(“Dapper Dasher/textures/scarfy.png”);
I tried Texture2D scarfy = LoadTexture(“Game Dev/Dapper Dasher/textures/scarfy.png”);

Launching the game using master file shows sprites.

Any help would be greatly appreciated, I have exhausted my knowledge and others posted.

EDIT: Error was my fault, used a Code Diff Tool or any code diff tool and compared the github to mine. Here was the error.

17 scarfyRec.height = scarfy.height; 17 scarfyRec.height = scarfyRec.height;
18 scarfyRec.x = 0; 18 scarfyRec.x = 0;
19 scarfyRec.y = 0; 19 scarfyRec.y = 0;
20 Vector2 scarfyPos; 20 Vector2 scarfyPos;
21 scarfyPos.x = windowWidth/2 - scarfyRec.width/2; 21 scarfyPos.x = windowWidth/2 - scarfyRec.width/2;
22 scarfyPos.y = windowHeight - scarfyRec.height; 22 scarfyPos.y = windowHeight - scarfy.height;

Left is correct, right is incorrect.
Line 17 and 22 were incorrect, check your code. Good to close.

1 Like

The scarfy vs scarfyRec error strikes again! Good job figuring out the issue.

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

Privacy & Terms