Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved]Textures aren't showing  (Read 1026 times)

0 Members and 2 Guests are viewing this topic.

floppa_dev

  • Newbie
  • *
  • Posts: 11
    • View Profile
[Solved]Textures aren't showing
« on: January 21, 2023, 12:03:42 pm »
The textures arent showing
I will put my project below
In advance, thank you
main.cpp:
// Comp issues win any rez other tham 1920*1080
#include "smth.hpp"
#include "grass.cpp"
using namespace sf;
int main()
{
    init_grass();
    Object player, bg;
    player.position = {960, 100};
    bg.position = {0, 0};
    if (!player.texture.loadFromFile("Images/big-floppa-player.png") || !bg.texture.loadFromFile("Images/bg.jpg"))
    {
        return -1;
    }
    player.position = {0, 100};
    player.sprite.setTexture(player.texture);
    bg.sprite.setTexture(bg.texture);
    RenderWindow window(sf::VideoMode(1920, 1080), "Super Floppa", Style::Fullscreen);
    Clock clock;
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            sf::Time elapsed = clock.restart();
            float dt = elapsed.asSeconds();
            if (event.type == Event::Closed || Mouse::isButtonPressed(Mouse::Right))
                window.close();
            if (Keyboard::isKeyPressed(Keyboard::A))
                player.position += {-10.f, 0.f};
            if (Keyboard::isKeyPressed(Keyboard::D))
                player.position += {10.f, 0.f};
            if (Keyboard::isKeyPressed(Keyboard::W))
                player.position += {0.f, -10.f};
            if (Keyboard::isKeyPressed(Keyboard::S))
                player.position += {0.f, 10.f};
            window.clear();
            window.draw(bg.sprite);
            window.draw(grass);
            display_smth(player, window);
            while (!touch_grass_lol(player, grass))
            {
                player.position += {0.f, 10.f};
            }
            std::cout << player.position.x << &#39;-&#39; << player.position.y << &#39;\n&#39;;
            window.display();
            if (dt < 1.f / 60.f)
            {
                sleep(sf::seconds(1.f / 60.f - dt));
            }
        }
    }
    return 0;
}
// no credits here
//------------------
grass.cpp
#include <SFML/Graphics.hpp>
#include "smth.hpp"
using namespace sf;
RectangleShape grass(Vector2f(1920.f, 100.f));
void init_grass()
{

    Texture grass_tex;
    if (!grass_tex.loadFromFile("Images/grass.png"))
    {
        std::cout << -1192919929;
    }
    grass.setTexture(&grass_tex);
    grass.setPosition(0, 980);
}
// my friend ... needs to (-insert someone name-)
bool touch_grass_lol(Object &object, RectangleShape grass)
{
    if (object.sprite.getGlobalBounds().intersects(grass.getGlobalBounds()))
        return 1;
    return 0;
}
 
smth.hpp
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <cmath>
const double PI = 3.14159265358979323846;
struct Object
{
    sf::Vector2f position;
    float angle;
    sf::Sprite sprite;
    sf::Texture texture;
};

void update_pos(Object &object1, Object &object2)
{
    object1.position = object2.position;
}
void moveObject_angle(Object &object, float &distance, float &angle)
{
    // Calculate the displacement in the x and y directions
    float dx = distance * cos(angle * PI / 180.0f);
    float dy = distance * sin(angle * PI / 180.0f);

    // Update the object&#39;s position
    object.position.x += dx;
    object.position.y += dy;

    // Update the object&#39;s sprite position
    object.sprite.setPosition(object.position);
}
void display_smth(Object &object, RenderWindow &window)
{
    object.sprite.setPosition(object.position);
    window.draw(object.sprite);
}
/*bool jump(int f)
{
    int o = f;
    f-= o/10;
    player.position += player.position += {0.f, f};
    if(player.getGlobalBounds.itersects.grass.sprite)
    return 0;
}*/
« Last Edit: January 23, 2023, 09:56:58 am by floppa_dev »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Textures aren't showing
« Reply #1 on: January 21, 2023, 06:41:17 pm »
you are creating the 'Texture grass_tex;' inside the function 'init_grass()', but as soon as the function ends, the texture (and all other local variables) are lost
you need to declare this Texture somewhere. next to the declaration of 'grass' (rectangleshape) should work
Visit my game site (and hopefully help funding it? )
Website | IndieDB

floppa_dev

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [Solved]Textures aren't showing
« Reply #2 on: January 23, 2023, 09:57:09 am »
tq