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

Author Topic: Texture not Rendering on Screen  (Read 12083 times)

0 Members and 1 Guest are viewing this topic.

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Texture not Rendering on Screen
« on: May 23, 2013, 10:31:22 pm »
Here are the quick ones:

  • OS: Windows 7 64-bit Home Edition
  • GFX: ATi Radeon 6970
  • SFML Version: Using SFML 2.0
  • Dynamic or Static: Static

For some reason the Texture that I try to draw won't show up on screen.
My theory is that I point to a place in memory that is out of scope before usage. But I don't really know how to solve this. One solution might be to somehow use Smart Pointers but that didn't work out at all and there must be a more simple solution than this.

I am currently following a slightly out of date tutorial on how to make a simple Tile Engine with SFML 2.0 . In the tutorial he uses the Image type instead of Texture, but the problem I had with that, was that Image didn't have a Draw() method. So I switch it out for Textures. I am not sure if this would be the problem.

I don't get any errors, the texture just won't render on screen.

Here are the relevant bits from Engine.cpp:
bool Engine::Init()
{
        LoadTextures();
        window = new sf::RenderWindow(sf::VideoMode(800,600,32), "RPG");
        if(!window)
                return false;

        return true;
}

void Engine::LoadTextures()
{
        sf::Texture sprite;
        sprite.loadFromFile("C:\\Users\\Vipar\\Pictures\\sprite1.png");
        textureManager.AddTexture(sprite);
        testTile = new Tile(textureManager.GetTexture(0));
}

void Engine::RenderFrame()
{
        window->clear();
        testTile->Draw(0,0,window);
        window->display();
}

void Engine::MainLoop()
{
        //Loop until our window is closed
        while(window->isOpen())
        {
                ProcessInput();
                Update();
                RenderFrame();
        }
}
 

Bits from TextureManager.cpp
void TextureManager::AddTexture(sf::Texture texture)
{
    textureList.push_back(texture);
}

sf::Texture& TextureManager::GetTexture(int index)
{
        return textureList[index];
}

Tried to solve this for a little over 2 hours now.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Texture not Rendering on Screen
« Reply #1 on: May 23, 2013, 11:25:51 pm »
If you had taken the time to create a minimal and complete example, things would've been easier to figure out for yourself and would be easier to explain things for us. ;)
The tutorial explains the usage of textures very good and has important things in red.
One of those things could be, that you should not copy around texture too much or at beast not at all. But with the small snippets you provided, you copy the texture when you push_back it to a vector (?) and when you pass it to the add function. Next to that I see unnecessary new (why would the window have to be allocated on the heap?) and no deletes at all...
Init() functions should be mostly avoided and instead one should make use of the constructor.
Don't use backslashes ever, every compiler you'll most likely ever work with, will support forward slashes for paths. :)
Also you should check pointers against nullptr and not use it as boolean.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Texture not Rendering on Screen
« Reply #2 on: May 24, 2013, 12:50:30 am »
Your answer was not really helpful.

But I've been looking around with Breakpoints now for a while.

When I call void Engine::LoadImages() the picture is never even loaded for whatever reason.
I checked the Image object and the file is not even in there. If it fails already there, I don't really know what to do. Any suggestions?

Seems it's an issue with Image.loadFromFile() and not my code.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Texture not Rendering on Screen
« Reply #3 on: May 24, 2013, 03:09:48 am »
Your answer was not really helpful.

It was very helpful and right on the money. You can not copy textures. When you do so strange things happen and stuff does not work right. Learn about the & symbol and how it affects the passing of objects in C++. Managing memory in C++ is not a walk in the park like in .NET like you mentioned you were using in your other thread. Maybe pick up a C++ book and learn some basics about memory management.  ;)


Quote
Seems it's an issue with Image.loadFromFile() and not my code.

Nothing is wrong with SFML's functions, only how you use them.
« Last Edit: May 24, 2013, 03:13:18 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Texture not Rendering on Screen
« Reply #4 on: May 24, 2013, 01:30:30 pm »
Also you should check pointers against nullptr and not use it as boolean.
if (ptr) is a common convention. So common that smart pointers implement the safe bool idiom to allow it. However, the real problem is checking the return value of new. It will never be a null pointer. If the new operator fails, std::bad_alloc is thrown.

Seems it's an issue with Image.loadFromFile() and not my code.
No, it's your code. The way you use C++ is very questionable and error-prone, thus you take mistakes effectively into account. On the other hand, I use the loadFromFile() function all the time, without any issues.

In fact, in LoadTextures() you have a local texture object that is destroyed at the end of the function. Please read more on memory management, scopes and object lifetime -- these topics are crucial in C++.

You can not copy textures.
You can, but it's usually not what you want.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Texture not Rendering on Screen
« Reply #5 on: May 24, 2013, 03:48:52 pm »
Now I've tried to construct an Image and Texture Object separately with the .NET binding instead.

Tells me it can't read my file. Can't load it. Just like in my C++ example.

void MainLoop()
        {
            Image sprite;
            //sprite = new Image("C:\\Users\\Vipar\\Dekstop\\sprite1.jpg");
            //imageManager.AddImage(sprite);
            Texture tex = new Texture("sprite.jpg");
            Sprite spr = new Sprite(tex);
            //testTile = new Tile(tex);

            while (window.IsOpen())
            {
                ProcessInput();
                Update();
                RenderFrame();
                window.Draw(spr);
            }
        }

Though in my other application using the C# binding it works just fine:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;

namespace SFMLCarGame
{
    class Program
    {
        private static RenderWindow window;
        private static Sprite car;

        static void Main(string[] args)
        {
            window = new RenderWindow(new VideoMode(256, 512), "Car Game");
            window.Closed += new EventHandler(OnClose);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

            Sprite bg = new Sprite(new Texture("road.png"));
            car = new Sprite(new Texture("car.png"));
            car.Position = new Vector2f(window.Size.X / 2, window.Size.Y - 96);
            while (window.IsOpen())
            {
                CheckCollision();
                window.DispatchEvents();

                window.Clear();

                window.Draw(bg);
                window.Draw(car);

                window.Display();
            }
        }

        static void CheckCollision()
        {
            CarOutOfBoundsCheck();
        }

        static void CarOutOfBoundsCheck()
        {
            Vector2f newPos = new Vector2f(0, car.Position.Y);
            if (car.Position.X < 0)
            {
                newPos.X = 0;
                car.Position = newPos;
            }
            else if (car.Position.X > window.Size.X - 32)
            {
                newPos.X = window.Size.X - 32;
                car.Position = newPos;
            }
        }

        static void OnClose(object sender, EventArgs e)
        {
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }

        static void OnKeyPressed(object sender, EventArgs e)
        {
            Vector2f newPos = new Vector2f(car.Position.X, car.Position.Y);
            KeyEventArgs ke = (KeyEventArgs)e;
            if (ke.Code.Equals(Keyboard.Key.A))
            {
                newPos.X = car.Position.X - 8;
                car.Position = newPos;
            }
            else if (ke.Code.Equals(Keyboard.Key.D))
            {
                newPos.X = car.Position.X + 8;
                car.Position = newPos;
            }
        }
    }
}
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Texture not Rendering on Screen
« Reply #6 on: May 24, 2013, 04:13:46 pm »
If it works in one program and not another then something is wrong with the file you are trying to load. Either it does not exist in the location you are trying to load it from or the file is possibly corrupted somehow.

And by the way, how do you know that in your example that "does not work" that the problem is loading the sprite. You are missing the 2 most important functions in your game loop in your second example, Window.Clear() and Window.Display().
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Texture not Rendering on Screen
« Reply #7 on: May 24, 2013, 04:17:49 pm »
Because I've tested it, and it works..?

The only problem there is with the first example is that it doesn't even load the sprite. Until that is solved, the rest won't really matter because there will be nothing to render.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Texture not Rendering on Screen
« Reply #8 on: May 24, 2013, 04:35:41 pm »
The only problem there is with the first example is that it doesn't even load the sprite. Until that is solved, the rest won't really matter because there will be nothing to render.

Are you sure that your "sprite.jpg" exists next to your executable? Or in the working directory when you run your program? One more thing to try would be to use an absolute path to "sprite.jpg".
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Texture not Rendering on Screen
« Reply #9 on: May 24, 2013, 04:38:53 pm »
I have tried to place it in all folders in the entire project.

I have also tried to give an absolute path. None of them works.
The picture is not corrupt either. I can open and edit the picture as I please.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Texture not Rendering on Screen
« Reply #10 on: May 24, 2013, 04:51:54 pm »
I have tried to place it in all folders in the entire project.

I have also tried to give an absolute path. None of them works.
The picture is not corrupt either. I can open and edit the picture as I please.

Upload the picture and then and I will see if I can load it.
« Last Edit: May 24, 2013, 05:04:37 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Re: Texture not Rendering on Screen
« Reply #12 on: May 24, 2013, 05:07:09 pm »
How big is that sprite, out of interest? Is that JPG full size?
SFML 2.1

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Texture not Rendering on Screen
« Reply #13 on: May 24, 2013, 05:15:00 pm »
Yes, it's a spritesheet I found randomly to try and render something.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Texture not Rendering on Screen
« Reply #14 on: May 24, 2013, 05:15:49 pm »
Just tried to load it and it would not load. But if you had read the console window output you would have easily read this too.

Quote
Failed to load image "C:\Users\zsbzsb\Downloads\sprite.jpg". Reason : PNG not supported: 8-bit only

There is something wrong with that file when you saved it, not even Adobe PS will open it.

How big is that sprite, out of interest? Is that JPG full size?

It's only 640x432.
« Last Edit: May 24, 2013, 05:18:49 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor