chestedtiger February 2016

SFML createMaskFromColor() not working correctly

I'm creating a game where I have 3 separate objects to display. A floor, a player sprite and multiple crates. Below is the code for all three.

#include <iostream>
#include <fstream>
#include "Game.h"

Game::Game()
{

    std::ifstream fileHndl;

    fileHndl.open("Assets/mapdata.txt");

    fileHndl >> mapCols;
    fileHndl >> mapRows;

    playerX = mapCols / 2;
    playerY = mapRows / 2;

    mapData = new int *[mapRows];
    for (int i = 0; i < mapRows; i++) {
        mapData[i] = new int[mapCols];
    }

    for (int i = 0; i < mapRows; i++) {
        for (int j = 0; j < mapCols; j++) {
            fileHndl >> mapData[i][j];
        }
    }

    window.create(sf::VideoMode(1280, 1024), "COMP2501 Demo");
    window.setFramerateLimit(5);

    playerImage.loadFromFile("Assets/actor.png");
    playerImage.createMaskFromColor(sf::Color::White);
    playerTexture.loadFromImage(playerImage);
    playerTexture.setSmooth(true);
    playerSprite.setTexture(this->playerTexture);

    floorTexture.loadFromFile("Assets/floor.png");
    floorTexture.setSmooth(true);

    crateImage.loadFromFile("Assets/crate.png");
    crateImage.createMaskFromColor(sf::Color::White);
    crateTexture.loadFromImage(crateImage);
    crateTexture.setSmooth(true);

    mapSprites = new sf::Sprite *[mapRows];
    for (int i = 0; i < mapRows; i++) {
        mapSprites[i] = new sf::Sprite[mapCols];
    }

    for (int i = 0; i < mapRows; i++)
    {
        for (int j = 0; j < mapCols; j++)
        {
            mapSprites[i][j].setPosition(j * 64, i * 64);
            if (mapData[i][j] == 1)
            {
                mapSprites[i][j].setTexture(this->floorTexture);
            }
            else
            {
                mapSprites[i][j].setTexture(this->crateTexture);
            }
        }
    }

}

I'm attempting to remove the white background of all 3 images as they're all .pngs however when do

Answers


iksemyonov February 2016

In the absence of additional information, this would be my logical chain towards a guess:

Since we see the createMaskFromColor() function work fine for the player model, it's not the error source. Since we see the same code being invoked for the other two objects, the chroma keying is working fine there as well. Hence, the following answer:

There is nothing but the black background behind the crate sprites. The chroma keying works correctly, and happily demonstrates us the black background behind the transparent corners.

How to fix:

Make sure the floor sprite is present behind the crate sprites as well. Maybe create an additional sprite, "crate over a floor", to not overlay them every time such a combination is encountered (no idea if this really affects performance, just a guess.)

Post Status

Asked in February 2016
Viewed 2,893 times
Voted 10
Answered 1 times

Search




Leave an answer