1
General / Texture is not loading
« on: May 23, 2024, 02:16:58 pm »
Hello! I am trying to make a small platformer game but I have a huge problem, the texture isn't loading.
https://github.com/epicfloppadev/floppa/ this is the github name
So I have this code in src/tile.cpp
this code in main.cpp:
https://github.com/epicfloppadev/floppa/ this is the github name
So I have this code in src/tile.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
class tile
{
public:
bool activated = 0;
Vector2f position={0.f,0.f};
Sprite sprite;
Texture texture;
};
Texture bgtex;
void loadTiles(tile tiles[][14])
{
bgtex.loadFromFile("Images/tile.jpg");
for (int i = 0; i <= 12; i++)
{
tiles[6][i].activated = 1;
}
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 13; j++)
{
tiles[i][j].position={float(100*i), float(100*j)};
tiles[i][j].sprite.setPosition(tiles[i][j].position);
if (tiles[i][j].activated == 1)
{
tiles[i][j].sprite.setTexture(bgtex);
}
}
}
}
void display_tile(tile &object, sf::RenderWindow &window)
{
window.draw(object.sprite);
}
#include <iostream>
using namespace sf;
class tile
{
public:
bool activated = 0;
Vector2f position={0.f,0.f};
Sprite sprite;
Texture texture;
};
Texture bgtex;
void loadTiles(tile tiles[][14])
{
bgtex.loadFromFile("Images/tile.jpg");
for (int i = 0; i <= 12; i++)
{
tiles[6][i].activated = 1;
}
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 13; j++)
{
tiles[i][j].position={float(100*i), float(100*j)};
tiles[i][j].sprite.setPosition(tiles[i][j].position);
if (tiles[i][j].activated == 1)
{
tiles[i][j].sprite.setTexture(bgtex);
}
}
}
}
void display_tile(tile &object, sf::RenderWindow &window)
{
window.draw(object.sprite);
}
this code in main.cpp:
#include "smth.hpp"
#include "grass.cpp"
#include "tile.cpp"
using namespace sf;
bool jumping = 0;
float jump_velocity = 0;
float accelerationV = 0.5;
float speedV = 0;
bool o_g = 0;
int main()
{
tile tiles[8][14];
loadTiles(tiles);
//
// Music battle;
// battle.openFromFile("Audio/battle-of-the-dragons-8037.wav");
// battle.play();
Object player = {{500, 500}, 0},
bg{{1000, 1000}, 0};
if (!player.texture.loadFromFile("Images/big-floppa-player.png") || !bg.texture.loadFromFile("Images/bg.jpg"))
{
return -1;
}
player.sprite.setScale(0.7, 0.7);
init_grass();
bg.sprite.setTexture(bg.texture);
player.sprite.setTexture(player.texture);
RenderWindow window(sf::VideoMode(1280, 720), "Super Floppa", Style::Titlebar | Style::Close);
Clock clock;
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed || Mouse::isButtonPressed(Mouse::Right))
window.close();
}
Time elapsed = clock.restart();
float dt = elapsed.asSeconds();
if (Keyboard::isKeyPressed(Keyboard::A))
{
speedV += accelerationV;
if (speedV > maxSpeedV)
speedV = maxSpeedV;
player.position += {-speedV, 0.f};
}
if (Keyboard::isKeyPressed(Keyboard::D))
{
speedV += accelerationV;
if (speedV > maxSpeedV)
speedV = maxSpeedV;
player.position += {speedV, 0.f};
}
else
{
speedV -= (-1 * bool(speedV < 0)) * accelerationV;
}
if (Keyboard::isKeyPressed(Keyboard::Space) && !jumping)
{
jumping = 1;
o_g = 0;
jump_velocity = sqrt(2 * GRAVITY * JUMP_HEIGHT);
}
if (o_g == 0)
{
player.position.y -= jump_velocity * dt;
jump_velocity -= GRAVITY * dt;
}
if (touch_grass_lol(player, grass))
{
jumping = 0;
o_g = 1;
player.position.y = grass.getPosition().y - player.texture.getSize().y * player.sprite.getScale().y;
}
if (dt < 1.f / 60.f)
{
sleep(sf::seconds(1.f / 60.f - dt));
}
std::cout<<tiles[1][1].sprite.getTexture().getSize();
window.clear();
// window.draw(bg.sprite);
// window.draw(grass);
for (int i = 0; i <= 6; i++)
{
for (int j = 0; j <= 12; j++)
{
if (tiles[i][j].activated == 0)
{
display_tile(tiles[i][j], window);
// std::cout<<tiles[i][j].position.x<<' '<<tiles[i][j].position.y<<std::endl;
}
}
}
display_smth(player, window);
window.display();
}
return 0;
}
when I am running the program the texture is not showing but if i modify the player texture to be bgtex(the tiles texture), the texture is showing as normal, what should i change in the loadTiles() function
#include "grass.cpp"
#include "tile.cpp"
using namespace sf;
bool jumping = 0;
float jump_velocity = 0;
float accelerationV = 0.5;
float speedV = 0;
bool o_g = 0;
int main()
{
tile tiles[8][14];
loadTiles(tiles);
//
// Music battle;
// battle.openFromFile("Audio/battle-of-the-dragons-8037.wav");
// battle.play();
Object player = {{500, 500}, 0},
bg{{1000, 1000}, 0};
if (!player.texture.loadFromFile("Images/big-floppa-player.png") || !bg.texture.loadFromFile("Images/bg.jpg"))
{
return -1;
}
player.sprite.setScale(0.7, 0.7);
init_grass();
bg.sprite.setTexture(bg.texture);
player.sprite.setTexture(player.texture);
RenderWindow window(sf::VideoMode(1280, 720), "Super Floppa", Style::Titlebar | Style::Close);
Clock clock;
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed || Mouse::isButtonPressed(Mouse::Right))
window.close();
}
Time elapsed = clock.restart();
float dt = elapsed.asSeconds();
if (Keyboard::isKeyPressed(Keyboard::A))
{
speedV += accelerationV;
if (speedV > maxSpeedV)
speedV = maxSpeedV;
player.position += {-speedV, 0.f};
}
if (Keyboard::isKeyPressed(Keyboard::D))
{
speedV += accelerationV;
if (speedV > maxSpeedV)
speedV = maxSpeedV;
player.position += {speedV, 0.f};
}
else
{
speedV -= (-1 * bool(speedV < 0)) * accelerationV;
}
if (Keyboard::isKeyPressed(Keyboard::Space) && !jumping)
{
jumping = 1;
o_g = 0;
jump_velocity = sqrt(2 * GRAVITY * JUMP_HEIGHT);
}
if (o_g == 0)
{
player.position.y -= jump_velocity * dt;
jump_velocity -= GRAVITY * dt;
}
if (touch_grass_lol(player, grass))
{
jumping = 0;
o_g = 1;
player.position.y = grass.getPosition().y - player.texture.getSize().y * player.sprite.getScale().y;
}
if (dt < 1.f / 60.f)
{
sleep(sf::seconds(1.f / 60.f - dt));
}
std::cout<<tiles[1][1].sprite.getTexture().getSize();
window.clear();
// window.draw(bg.sprite);
// window.draw(grass);
for (int i = 0; i <= 6; i++)
{
for (int j = 0; j <= 12; j++)
{
if (tiles[i][j].activated == 0)
{
display_tile(tiles[i][j], window);
// std::cout<<tiles[i][j].position.x<<' '<<tiles[i][j].position.y<<std::endl;
}
}
}
display_smth(player, window);
window.display();
}
return 0;
}