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

Author Topic: [2.0] Sprites and Text transparency renders as black (Solved)  (Read 5189 times)

0 Members and 1 Guest are viewing this topic.

JuDelCo

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • JuDelCo's Twitter
[2.0] Sprites and Text transparency renders as black (Solved)
« on: November 06, 2012, 10:22:18 pm »
Hi,

I'm trying to render a sprite, text and a big sprite as background PNG (for testing purposes)

The problem is the transparent color (the ship sprite is a PNG with a transparent alpha color) renders as black in both sprite and text



Simple code i use for text:
sf::Font fuente;
fuente.loadFromFile("resources/Arial.ttf");
sf::Text text("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
text.setFont(fuente);
text.setCharacterSize(30);
text.setStyle(sf::Text::Regular);
text.setColor(sf::Color::Red);
VIDEO.window.draw(text);

/* Sprite and background rendering */
 

Simple code i use for Sprite (in a sprite class):
sf::Image image;
image.loadFromFile("resources/ship.png");
this->setTexture(image);

/* ... later ... */

VIDEO.window.draw(*this);
 

PS: I use "setTextureRect" before draw the sprite because its a spritesheet
« Last Edit: November 06, 2012, 11:15:42 pm by JuDelCo »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Sprites and Text transparency renders as black
« Reply #1 on: November 06, 2012, 10:24:47 pm »
Could you please show a complete and minimal code that reproduces the problem, so that we can test it?
Laurent Gomila - SFML developer

JuDelCo

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • JuDelCo's Twitter
Re: [2.0] Sprites and Text transparency renders as black
« Reply #2 on: November 06, 2012, 10:59:27 pm »
Could you please show a complete and minimal code that reproduces the problem, so that we can test it?

Sorry for the delay, i was making the Minimal Code (Complete code its a lot of source code / classes / files / blah)

Minimal Code (Timer Class its only a wrapper of sf::Clock):

This code makes the same render problem than the screenshot of the first post (but without the mini-view test)

#include <timer.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>


int main(int argc, char *args[])
{
        sf::RenderWindow window;

        //---------------------------------

        window.create(sf::VideoMode(640, 480), "Demo");
        window.setFramerateLimit(60);

        //---------------------------------

        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        glClearColor(0, 0, 0, 0);

        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        window.clear();

        //---------------------------------

        sf::Sprite player;
        sf::Texture playerTexture;

        playerTexture.loadFromFile("resources/player.png");
        player.setTexture(playerTexture);
        player.setTextureRect(sf::IntRect(0, 0, (player.getTexture()->getSize().x / 5), player.getTexture()->getSize().y));
        player.setPosition(200, 300);

        sf::Sprite background;
        sf::Texture backgroundTexture;
        backgroundTexture.loadFromFile("resources/back.png");
        background.setTexture(backgroundTexture);

        sf::Font fuente;
        fuente.loadFromFile("resources/arial.ttf");

        //---------------------------------

        bool run = true;
        int msLastFrame = 0;
        Timer fps;
        Timer update;

        while (run)
        {
                msLastFrame = fps.getTicks();
                fps.start();

                sf::Event event;

                while (window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                        {
                                run = false;
                        }
                }

                //---------------------------------
               
                window.clear();

                glEnable(GL_DEPTH_TEST);
                glClear(GL_DEPTH_BUFFER_BIT);
                glDisable(GL_CULL_FACE);
                glMatrixMode(GL_PROJECTION);
                glLoadIdentity();
                glOrtho(0, 640, 480, 0, -1000.0, 1000.0);
                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();

                sf::Text text("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
                text.setFont(fuente);
                text.setCharacterSize(30);
                text.setStyle(sf::Text::Regular);
                text.setColor(sf::Color::Red);

                window.draw(text);
                window.draw(player);
                window.draw(background);

                window.display();

                //---------------------------------

                if (update.getTicks() >= 1000)
                {
                        update.start();
                }

                if (fps.getTicks() < 17) //59fps
                {
                        sleep(sf::milliseconds(17 - fps.getTicks()));
                }
        }

        //---------------------------------

        return 0;
}

 


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Sprites and Text transparency renders as black
« Reply #3 on: November 06, 2012, 11:01:13 pm »
Read the last section of this tutorial:
http://www.sfml-dev.org/tutorials/2.0/window-opengl.php
Laurent Gomila - SFML developer

JuDelCo

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • JuDelCo's Twitter
Re: [2.0] Sprites and Text transparency renders as black
« Reply #4 on: November 06, 2012, 11:15:25 pm »
Read the last section of this tutorial:
http://www.sfml-dev.org/tutorials/2.0/window-opengl.php

Oh! Okay, I forgot to push/pop SFML opengl states!  ::)

window.popGLStates();

        glEnable(GL_DEPTH_TEST);
        glClear(GL_DEPTH_BUFFER_BIT);
        glOrtho(0, 640, 480, 0, -1000.0, 1000.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

window.pushGLStates();
 

One thing more, i detect that i need now to draw all sprites backwards (first the backgrounds, entities and finally hud)

// Now IS this way:
window.draw(background);
window.draw(player);
window.draw(text);

// NOT this:
window.draw(text);
window.draw(player);
window.draw(background); // because background fills entire screen on top of all

Edit: Screenshot with the problem solved ^^



Thanks Laurent !  ;)
« Last Edit: November 06, 2012, 11:18:22 pm by JuDelCo »