Hey all,
I started the the course and completed all the lessons for the Dapper Dasher project up until the scrolling background then went on a vacation. Upon my return, I found that the program no longer updates the Scarfy texture animation frame correctly despite no code having been changed and working as intended before. Has anyone encountered something like this before?
My code is pasted below. I could not find any meaningful differences between my code and the examples from the instructor. Maybe I just need another set of eyes.
Any and all tips or input are greatly appreciated.
#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 anim frame
data.Rec.x = data.frame * data.Rec.width;
data.frame++;
if (data.frame > maxFrame)
{
data.frame = 0;
}
}
return data;
}
int main()
{
//Window Variables
int windowDimensions[2];
windowDimensions[0] = 512;
windowDimensions[1] = 380;
//Initialize Window
InitWindow(windowDimensions[0], windowDimensions[1], "Dasher!");
//Gravity Constant (pixels/sec per sec)
const int gravity{1'000};
const int jumpVel{-600};
bool isInAir{};
//Physics Variables
//Pixels per sec.
int velocity{0};
//Textures Variables
//AnimData Scarfy
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.pos.y = (windowDimensions[1] - scarfyData.Rec.height);
scarfyData.frame = 0;
scarfyData.updateTime = (1.0/12);
scarfyData.runningTime = 0.0;
//AnimData Nebula
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");
//Array nebula hazards
const int sizeOfNebulae{10};
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].updateTime = 0.0;
nebulae[i].runningTime = 0.0;
nebulae[i].pos.x = windowDimensions[0] + i * 300;
}
//Nebula x velocity pixels/sec
int nebVel{-200};
SetTargetFPS(60);
while(!WindowShouldClose())
{
//Start Drawing
BeginDrawing();
ClearBackground(WHITE);
//delta time
float dT{GetFrameTime()};
//Begin Game Logic
//Check if scarfy is in air
if (isOnGround(scarfyData, windowDimensions[1]))
{
//rectangle on the ground
velocity = 0 * dT;
isInAir = false;
}
else
{
//apply gravity
velocity += gravity * dT;
isInAir = true;
}
//Check Jumping
if (IsKeyPressed(KEY_SPACE) && !isInAir)
{
velocity += jumpVel;
}
//update nebula position
for (int i = 0; i < sizeOfNebulae; i++)
{
nebulae[i].pos.x += nebVel * dT;
}
//update Scarfy position (vert.)
scarfyData.pos.y += velocity * dT;
//Update runningTime
scarfyData.runningTime += dT;
//update Scarfy Animation Frame
if (!isInAir)
{
scarfyData = updateAnimData(scarfyData, dT, 5);
}
else
{
scarfyData.Rec.x = scarfyData.frame * scarfyData.Rec.width;
}
//update nebula animation frame
for (int i = 0; i < sizeOfNebulae; i++)
{
nebulae[i] = updateAnimData(nebulae[i], dT, 8);
}
//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);
//End Game Logic
//Stop Drawing
EndDrawing();
}
UnloadTexture(scarfy);
UnloadTexture(nebula);
CloseWindow();
}