So i am following the lessons C++ fondamentals: game programming, in course content section 4 when we made the character instance in the end i try to run the code and the visual studio code stopped me whit the following error : “segmentation fault” inside the class where i made for the knight.
here down the class:
class Character
{
public:
Vector2 getWorldPos() { return worldPos; }
void setScreenPos(int winWidth, int winHeight);
void tick(float deltaTime);
private:
Texture2D texture{LoadTexture("Sprite/characters/knight_idle_spritesheet.png")};
Texture2D idle{LoadTexture("Sprite/characters/knight_idle_spritesheet.png")};
Texture2D run{LoadTexture("Sprite/characters/knight_run_spritesheet.png")};
Vector2 screenPos{};
Vector2 worldPos{};
// 1 direzione destra -1 direzione sinistra
float rightLeft{1.f};
// variabili animazione
float runningTime{};
int frame{};
const int maxFrames{6};
const float updateTime{1.f / 12.f};
const float speed { 4.f };
};
void Character::setScreenPos(int winWidth, int winHeight)
{
screenPos = {
(float)winWidth / 2.0f - 4.0f * (0.5f * (float)texture.width / 6.0f),
(float)winHeight / 2.0f - 4.0f * (0.5f * (float)texture.height)
};
}
void Character::tick(float deltaTime)
{
// movimento della mappa che da illusione di muovere il personaggio
Vector2 direction{};
if (IsKeyDown(KEY_A))
direction.x -= 1.0;
if (IsKeyDown(KEY_D))
direction.x += 1.0;
if (IsKeyDown(KEY_W))
direction.y -= 1.0;
if (IsKeyDown(KEY_S))
direction.y += 1.0;
if (Vector2Length(direction) != 0.0)
{
worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
// inversione animazione destra / sinistra
direction.x < 0 ? rightLeft = -1.f : rightLeft = 1.f;
texture = run;
}
else
{
texture = idle;
}
// update animation
runningTime += deltaTime;
if (runningTime >= updateTime)
{
frame++;
runningTime = 0.f;
if (frame > maxFrames)
frame = 0;
}
// disegno persnaggio
Rectangle source{frame * (float)texture.width / 6.f, 0.0f, rightLeft * (float)texture.width / 6.f, (float)texture.height};
Rectangle dest{screenPos.x, screenPos.y, 4.0f * (float)texture.width / 6.0f, 4.0f * (float)texture.height};
DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);
}