What I'm trying is basically loading a texture and then drawing it on the screen. I tried to do it with just one cpp file and it works just fine. But right now I have a structure like this:
#ifndef PLAYER_H
#define PLAYER_H
#include "raylib.h"
class Player
{
public:
Player(const char* texturePath);
~Player();
void Draw();
private:
Texture2D ShipTexture;
Vector2 position;
const char* texturePath;
};
#endif
#include "Player.h"
Player::Player(const char* texturePath)
{
position = { 0, 0 };
ShipTexture = LoadTexture(texturePath);
if (ShipTexture.id == 0)
{
// Texture loading failed
TraceLog(LOG_ERROR, "Failed to load texture from path: %s", texturePath);
}
else
{
TraceLog(LOG_INFO, "Texture loaded successfully from path: %s", texturePath);
}
}
Player::~Player()
{
UnloadTexture(ShipTexture);
}
void Player::Draw()
{
if (ShipTexture.id != 0)
{
DrawTexture(ShipTexture, position.x, position.y, WHITE);
}
else
{
TraceLog(LOG_WARNING, "Attempted to draw player texture before it was loaded!");
}
}
#ifndef GAME_H
#define GAME_H
#include "raylib.h"
#include "Player.h"
class Game
{
public:
Game(const int screenWidth, const int screenHeight, const char* title);
~Game();
// OTHER FUNCTIONS
private:
// OTHER VARIABLES
Player player;
};
#endif
#include "Game.h"
Game::Game(const int screenWidth, const int screenHeight, const char* title)
: screenWidth(screenWidth), screenHeight(screenHeight), title(title),
player("texture/path/png")
{
}
void Game::Init()
{
InitWindow(screenWidth, screenHeight, title);
SetTargetFPS(60);
}
void Game::Draw()
{
BeginDrawing();
ClearBackground(RAYWHITE);
player.Draw();
EndDrawing();
}
void Game::Loop()
{
while(!WindowShouldClose())
{
Draw();
}
}
#include "Game.h"
int main()
{
Game game(800, 600, "Space Invaders");
game.Init();
game.Loop();
game.Shutdown();
return 0;
}
When I compile and run this I get entire white screen(which is expected because of clearing the background) and I have a console output saying "Attempted to draw player texture before it was loaded!". So it means loading the texture is succsessful but when it comes to the Draw() function function takes the default value as a texture which is null. IDK why it acts like that can someone explain please?