-
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
(http://i.imgur.com/tjxoG.png)
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
-
Could you please show a complete and minimal code that reproduces the problem, so that we can test it?
-
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;
}
-
Read the last section of this tutorial:
http://www.sfml-dev.org/tutorials/2.0/window-opengl.php
-
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 ^^
(http://i.imgur.com/4vsfv.png)
Thanks Laurent ! ;)