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

Author Topic: Mac os X Lion - RenderTexture show random pixels  (Read 8878 times)

0 Members and 1 Guest are viewing this topic.

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« on: January 15, 2012, 01:59:20 pm »
Some sprites do not appear ... but, instead, you will see some random image who come from (i think) the cache of the GPU...

But in others computer, HP EliteBook with windows 7, it's working fine.

Does the trouble come from our Mac ?

Mac Specifications:

MacBook Pro - 17 pouces, mi-2010
2,66 GHz Intel Core i7
8 Go 1067 MHz DDR3
NVIDIA GeForce GT 330M 512 MB
Mac OS X Lion 10.7.2 (11C74)

Here are a screenshot of the troubles :



here are the code :

Code: [Select]
void DisplaySfml::loadImage(std::string const &name, std::string const & pathImage)
{
    sf::RenderTexture   *renderTexture = new sf::RenderTexture;
    sf::Texture texture;

    if (!(texture.LoadFromFile(pathImage)))
        std::cerr << "Image [" << pathImage << "] is missing" << std::endl;

    sf::Sprite spriteTmp(texture);
    spriteTmp.Scale(Width_ / 1920.f, Height_ / 1200.f);
    if (!renderTexture->Create(texture.GetWidth() * (Width_ / 1920.f),
                          texture.GetHeight() * (Height_ / 1200.f)))
        std::cerr << "RenderTexture [" << pathImage << "] failed to create" << std::endl;
    renderTexture->Clear(sf::Color::Transparent);
    renderTexture->Draw(spriteTmp);
    renderTexture->Display();
    displayImage_[name].sprite_ = new sf::Sprite(renderTexture->GetTexture());
    displayImage_[name].width_ = renderTexture->GetWidth();
    displayImage_[name].height_ = renderTexture->GetHeight();
    displayImage_[name].loop_ = 0;
    displayImage_[name].screenWidth_ = 0;
    saveTexture_.push_back(renderTexture);
}


In advance, thanks a lot for your help ;)

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Mac os X Lion - RenderTexture show random pixels
« Reply #1 on: January 15, 2012, 03:56:00 pm »
We'd probably need a minimal and complete sample code to reproduce your issue.
Want to play movies in your SFML application? Check out sfeMovie!

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #2 on: January 15, 2012, 04:44:48 pm »
Quote from: "Ceylo"
We'd probably need a minimal and complete sample code to reproduce your issue.


Sent by pm ;)

Thanks ;)

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Mac os X Lion - RenderTexture show random pixels
« Reply #3 on: January 15, 2012, 04:47:50 pm »
Well.. I didn't say I'd be the one to find what's wrong here. It's just so that if someone has an idea, he/she can answer here. You should post your minimal and complete code here, not in a pm.
Want to play movies in your SFML application? Check out sfeMovie!

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #4 on: January 15, 2012, 04:52:28 pm »
here is the code >> next post ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Mac os X Lion - RenderTexture show random pixels
« Reply #5 on: January 15, 2012, 05:25:11 pm »
Please post a minimal and complete code directly in the forum (no download site). Reduce your actual code to a few lines, so that the problem is still reproducable.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #6 on: January 15, 2012, 05:46:00 pm »
Here is the MINIMAL code:

main.cpp

Code: [Select]

#include "DisplaySfml.hh"

#define WIDTH (desktop.width())
#define HEIGHT (desktop.height())

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDesktopWidget desktop;

    IDisplay* Win = new DisplaySfml();
    Win->InitWindow(WIDTH, HEIGHT, "Air Type");
//    Win->loadImage("Ship", "./Resources/ShipNormal.png");
//    Win->loadImage("ShipForward", "./Resources/ShipForward.png");
//    Win->loadImage("ShipBack", "./Resources/ShipBack.png");
//    Win->loadImage("ShipLeft", "./Resources/ShipLeft.png");
//    Win->loadImage("ShipRight", "./Resources/ShipRight.png");
    Win->loadImage("Background", "./Resources/Background.jpg");
//    Win->loadImage("FrontStars", "./Resources/FrontStars.png");
//    Win->loadImage("BackStars", "./Resources/BackStars.png");
//    Win->loadImage("DoubleStars", "./Resources/FrontStars.png");
//    Win->getSprite("Ship")->SetPosition(WIDTH / 2, HEIGHT / 2);
    Win->DisplayWindow();
    return EXIT_SUCCESS;
}


DisplaySfml.cpp

Code: [Select]


#include "DisplaySfml.hh"

DisplaySfml::DisplaySfml()
    :Speed_(0.01f)
{
    turnSendDirection_[sf::Keyboard::Down] = &moveRight;
    turnSendDirection_[sf::Keyboard::Up] = &moveLeft;
    turnSendDirection_[sf::Keyboard::Left] = &moveBack;
    turnSendDirection_[sf::Keyboard::Right] = &moveForward;
    turnSendDirection_[sf::Keyboard::Space] = &moveSpace;
}

DisplaySfml::~DisplaySfml()
{
    for (std::vector<sf::RenderTexture*>::iterator it = saveTexture_.begin(); it != saveTexture_.end(); ++it)
        delete *it;
    for (std::map<std::string, Sprite>::iterator it = displayImage_.begin(); it != displayImage_.end(); ++it)
        delete (it->second).sprite_;
    delete Window_;
}

void DisplaySfml::InitWindow(int Width, int Height, const std::string & Title)
{
    Width_ = Width;
    Height_ = Height;
    Window_ = new sf::RenderWindow(sf::VideoMode(Width_, Height_), Title.c_str(), sf::Style::None);
    Window_->EnableVerticalSync(true);
}

void DisplaySfml::loadImage(std::string const &name, std::string const & pathImage)
{
    sf::RenderTexture   *renderTexture = new sf::RenderTexture;
    sf::Texture texture;

    if (!(texture.LoadFromFile(pathImage)))
        std::cerr << "Image [" << pathImage << "] is missing" << std::endl;

    sf::Sprite spriteTmp(texture);
    spriteTmp.Scale(Width_ / 1920.f, Height_ / 1200.f);
    if (!renderTexture->Create(texture.GetWidth() * (Width_ / 1920.f), texture.GetHeight() * (Height_ / 1200.f)))
        std::cerr << "RenderTexture [" << pathImage << "] failed to create" << std::endl;
    renderTexture->Clear(sf::Color::Transparent);
    renderTexture->Draw(spriteTmp);
    renderTexture->Display();
    displayImage_[name].sprite_ = new sf::Sprite(renderTexture->GetTexture());
    displayImage_[name].width_ = renderTexture->GetWidth();
    displayImage_[name].height_ = renderTexture->GetHeight();
    displayImage_[name].loop_ = 0;
    displayImage_[name].screenWidth_ = 0;
    saveTexture_.push_back(renderTexture);
}

sf::Sprite  *DisplaySfml::getSprite(std::string const &name)
{
    return displayImage_[name].sprite_;
}

int DisplaySfml::getXSprite(std::string const &name)
{
    return displayImage_[name].sprite_->GetPosition().x;
}

int DisplaySfml::getYSprite(std::string const &name)
{
    return displayImage_[name].sprite_->GetPosition().y;
}

void DisplaySfml::drawImage(std::string const &name, int const &x, int const &y)
{
    displayImage_[name].sprite_->SetPosition(x, y);
    Window_->Draw(*(displayImage_[name].sprite_));
}

void DisplaySfml::DisplayWindow()
{
    x = 0;
    y = 0;
    Clock_.Reset();
    Window_->SetFramerateLimit(60);
    while (Window_->IsOpen())
    {
        event();
        ElapsedTime_ = Clock_.GetElapsedTime();
        if (ElapsedTime_ > 17)
        {
            Window_->Clear();
            drawImage("Background", 0, 0);
//             loopSprite("Background", 3);
//            loopSprite("FrontStars", 2);
//            loopSprite("BackStars", 1);
//            drawImage("Ship", getXSprite("Ship") + x, getYSprite("Ship") + y);
//            drawImage("Ship", 500, 500);
//            loopSprite("DoubleStars", 6);
            Window_->Display();
            Clock_.Reset();
        }
    }
}

void DisplaySfml::event()
{
    sf::Event event;
    while (Window_->PollEvent(event))
    {
        if (event.Type == sf::Event::KeyPressed)
        {
            if ((event.Key.Code == sf::Keyboard::Escape))
            {
                Window_->Close();
                break;
            }
            for (std::map<sf::Keyboard::Key, setNewPos>::iterator it = turnSendDirection_.begin(); it != turnSendDirection_.end(); ++it)
            {
                if (sf::Keyboard::IsKeyPressed(it->first))
                    turnSendDirection_[it->first](x, y);
            }
        }
    }
}

void DisplaySfml::loopSprite(const std::string name, int const speed)
{
    if ((displayImage_[name]).screenWidth_ >= Width_)
        (displayImage_[name]).screenWidth_ = 0;

    if ((displayImage_[name]).loop_ >= (displayImage_[name]).width_)
        (displayImage_[name]).loop_ = 0;

    sf::IntRect leftRect((displayImage_[name]).loop_, 0,
                         Width_ - (displayImage_[name]).screenWidth_, Height_);

    (displayImage_[name]).sprite_->SetTextureRect(leftRect);
    drawImage(name, 0, 0);


    if ((displayImage_[name]).loop_ + Width_ >= (displayImage_[name]).width_)
    {
        sf::IntRect rightRect(0 ,0, (displayImage_[name]).screenWidth_, Height_);
        (displayImage_[name]).sprite_->SetTextureRect(rightRect);
        drawImage(name, Width_ - (displayImage_[name]).screenWidth_, 0);
        (displayImage_[name]).screenWidth_ += speed;
    }
    (displayImage_[name]).loop_ += speed;
}


Any help ?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Mac os X Lion - RenderTexture show random pixels
« Reply #7 on: January 15, 2012, 05:51:05 pm »
Quote from: "pirate42"
Here is the MINIMAL code:
The code is neither minimal nor complete, even though this has been told you three times.

What we need is a short code with only a main() function that we can directly compile and test. Debugging gets very tedious if we first must become acquainted to your project-specific code. Remove everything that's unnecessary and not directly related to your problem, including dependencies to other libraries like Qt.

I know this implies some work, but otherwise we have to do it, who need even more time because we're not familiar with your code.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #8 on: January 15, 2012, 06:43:39 pm »
Sorry for the misunderstood :(

here is a minimal code, as requested :

Code: [Select]


#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main()
{
  // Adjust these two variables to your screen resolution
  int Width_ = 1680;
  int Height_ = 1050;
  float ElapsedTime_;

  // Creating the sf::RenderWindow
  sf::RenderWindow *Window_ = new sf::RenderWindow(sf::VideoMode(Width_, Height_), "Title", sf::Style::Fullscreen);
  sf::Clock Clock_;


  sf::Texture texture;

  // Loading an image in a sf::Texture
  if (!(texture.LoadFromFile("./image.png")))
    return -1;

  // Creating the sf::Sprite from the texture, and scaling it
  sf::Sprite spriteTmp(texture);
  spriteTmp.Scale(Width_ / 1920.f, Height_ / 1200.f);

  // Creating the sf::RenderTexture, and filling it with the scaled sprite
  sf::RenderTexture   *renderTexture = new sf::RenderTexture;
  if (!renderTexture->Create(texture.GetWidth() * (Width_ / 1920.f), texture.GetHeight() * (Height_ / 1200.f)))
    return -1;
  renderTexture->Clear(sf::Color::Transparent);
  renderTexture->Draw(spriteTmp);
  renderTexture->Display();

  // Creating the new sf::sprite with the new scaled texture
  sf::Sprite *sprite = new sf::Sprite(renderTexture->GetTexture());

  while (Window_->IsOpen())
    {
      // event
      ElapsedTime_ = Clock_.GetElapsedTime();
      if (ElapsedTime_ > 17)
{
 Window_->Clear();
 Window_->Draw(*sprite);
 Window_->Display();
 Clock_.Reset();
}
      else
sf::Sleep(2);
    }
}


here is the compilation line :

Code: [Select]
g++ main.cpp  -lsfml-graphics -lsfml-system -lsfml-window

I know that i can use a sf::Texture instead of sf::RenderTexture, but, somewhere in my program, i need a new texture already scaled.

Please, refer to the screenshot i posted before.

sometimes, the full texture is showing good, but it often load some random pixels

by the way, I tried on 2 macbook pro with different graphic cards

thx a lot for your time

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mac os X Lion - RenderTexture show random pixels
« Reply #9 on: January 15, 2012, 07:02:25 pm »
Can you try to:
- remove the custom time handling, just render everything at full speed
- add an event loop
Laurent Gomila - SFML developer

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #10 on: January 15, 2012, 07:11:31 pm »
ok

I tried this, but its still not working :(

Code: [Select]
 while (Window_->IsOpen())
    {
      sf::Event event;
      while (Window_->PollEvent(event))
{
 if (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape)
   {
     Window_->Close();
     break;
   }
}
 Window_->Clear();
 Window_->Draw(*sprite);
 Window_->Display();
    }

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #11 on: January 15, 2012, 09:12:52 pm »
I also tried different file format, it doesn't work

any idea ?

thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mac os X Lion - RenderTexture show random pixels
« Reply #12 on: January 15, 2012, 10:04:06 pm »
Quote
by the way, I tried on 2 macbook pro with different graphic cards

And it works? Or not?
Have you tried on PCs (Linux or Windows)?
Laurent Gomila - SFML developer

pirate42

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mac os X Lion - RenderTexture show random pixels
« Reply #13 on: January 15, 2012, 10:29:12 pm »
Quote from: "Laurent"
Quote
by the way, I tried on 2 macbook pro with different graphic cards

And it works? Or not?
Have you tried on PCs (Linux or Windows)?


no it doesnt work on mac computers

it works on windows (pc) computer and also on ubuntu virtual machine under mac os

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Mac os X Lion - RenderTexture show random pixels
« Reply #14 on: January 15, 2012, 11:21:30 pm »
There's definitely a issue here. I modified a little bit the code sample from the Xcode templates like this :
Code: [Select]
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

int main (int argc, const char * argv[])
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
   
    // Load a sprite to display
    sf::Texture texture;
    if (!texture.LoadFromFile(ResourcePath() + "cute_image.jpg"))
    return EXIT_FAILURE;
    sf::Sprite spriteTmp(texture);
   
    sf::RenderTexture rt;
    if (!rt.Create(texture.GetWidth(), texture.GetHeight()))
        return EXIT_FAILURE;
   
    // Create RT with some content. DOESN'T works here.
    /*
     rt.Clear();
     rt.Draw(spriteTmp);
     rt.Display();
     sf::Sprite sprite(rt.GetTexture());
    //*/
   
    // Start the game loop
    while (window.IsOpen())
    {
    // Process events
    sf::Event event;
    while (window.PollEvent(event))
    {
    // Close window : exit
    if (event.Type == sf::Event::Closed)
    window.Close();
           
    // Escape pressed : exit
    if (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape)
    window.Close();
    }
       
        // Create RT with some content. Works here after the second round of the loop.
        //*
         rt.Clear();
         rt.Draw(spriteTmp);
         rt.Display();
         sf::Sprite sprite(rt.GetTexture());
        //*/
       
        // Draw the sprite
        window.Draw(sprite);
       
    // Update the window
    window.Display();
    }
   
return EXIT_SUCCESS;
}


It appears that the RT's content must be drawn/displayed several times (in fact, 2 times) in the main loop to render as expected. If the RT's content is drawn before the main loop then some artefacts appear.

However I have no clue why this is happening!  :?

An archive can be downloaded here with all the material to test this issue.
SFML / OS X developer

 

anything