when i run the code all i see is a white screen without anything.
when i load:
Texture2D scarfy = LoadTexture(“textures/scarfy”);
it doesn’t load it and if I try “textures/scarfy.png”
it said, “exception has occurred, segmentation fault.”
what to do?
Hi Morkooshino,
Is it possible for you to show us your code? It helps provided needed context to the problem.
If I had to guess, though. The usual cause for seg faults when calling LoadTexture is that it’s being called before InitWindow. InitWindow needs to be called before LoadTexture as it gets initializes GPU memory for us to use.
just changed it to before but same thing - white screen only.
here is the code:
#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)
{
data.runningTime += deltaTime;
if (data.runningTime >= data.updateTime)
{
data.runningTime = 0.0;
//updata animation frame
data.rec.x = data.frame * data.rec.width;
data.frame++;
if (data.frame > maxFrame)
{
data.frame = 0;
}
}
return data;
}
int main ()
{
SetTargetFPS(60);
//array of window dimensions.
int windowDimensions [2];
windowDimensions [0] = 512;
windowDimensions [1] = 380;
int velocity { 0 };
//acceleration due to gravity - (pixels/sec)/sec.
const int gravity { 1'000 };
//is the rectangle in air?
bool isInAir{};
//jump velocity (pixels/sec)
const int jumpVelocity { -600 };
InitWindow(windowDimensions[0], windowDimensions[1], “Dapper Dasher”);
//background
Texture2D background = LoadTexture("textures/far-buildings");
float bgX{};
//nebula variables
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet");
const int numberOfNebulae{6};
AnimData nebulae [numberOfNebulae] {};
for (int i = 0; i < numberOfNebulae; 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] + (300*i);
}
//nebula X velocity (picels/sec)
int nebula_Velocity{ -200 };
//scarfy variables
Texture2D scarfy = LoadTexture("textures/scarfy.");
AnimData scarfyData{
{ 0, 0, scarfy.width/6, scarfy.height },
{ windowDimensions[0]/2 - scarfyData.rec.width/2, windowDimensions[1] - scarfyData.rec.height },
0, // int frame
1.0/12.0, // float updateTime
0.0 // float runningTime
};
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(WHITE);
//delta time (time since last frame)
const float deltaTime{ GetFrameTime()};
bgX -= 20 * deltaTime;
//draw the background
Vector2 BG1pos{bgX, 0.0};
DrawTextureEx(background, BG1pos, 0.0, 2.0, WHITE);
//perform ground check.
if (isOnGround (scarfyData, windowDimensions[1]))
{
//rectangle is on the ground.
velocity = 0;
isInAir = false;
}
else
{
//rectangle is in the air.
velocity += gravity * deltaTime;
isInAir = true;
}
//perform jump check.
if (IsKeyPressed(KEY_SPACE) && !isInAir)
{
velocity += jumpVelocity;
}
//update position of each nebula
for (int i = 0; i < numberOfNebulae; i++)
{
nebulae[i].pos.x += nebula_Velocity * deltaTime;
}
//update scarfy's Y position.
scarfyData.pos.y += velocity * deltaTime;
//if scarfy not on the ground, dont update animation.
if (!isInAir)
{
scarfyData = updateAnimData(scarfyData, deltaTime, 5);
}
else
{
}
//update nebula's running time
for (int i = 0; i < numberOfNebulae; i++)
{
nebulae[i] = updateAnimData(nebulae[i], deltaTime, 7);
}
//draw all nebulas
for (int i = 0; i < numberOfNebulae; i++)
{
DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);
}
//draw scarfy
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE);
EndDrawing();
}
UnloadTexture(scarfy);
UnloadTexture(nebula);
UnloadTexture(background);
CloseWindow();
}
You definitely need the file extension (the “.PNG” part) otherwise the file won’t be found.
Can you show me your logs when the seg fault happens?
added .png and now it’s working! probably what you said about Initwindow that needs to come first.
Thanks a lot!
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.