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

Author Topic: White texture while loading textures from array(beginner)  (Read 2142 times)

0 Members and 1 Guest are viewing this topic.

Stolle

  • Newbie
  • *
  • Posts: 6
    • View Profile
White texture while loading textures from array(beginner)
« on: December 21, 2014, 02:25:13 pm »
Hello!

So I'm getting a stange error when I'm creating a array from something I call "Obstacle", the problem I'm having is when I'm trying to load the array(sprites) they are all turning white. Now to the strange everything works perfectly when I'm not having the array (only loading one object).

I think that I'm loading the array too many times so that everything get overwriten.

I'm not going to post the Object and the Obstacle code tho I known thats right beacuse it works when I'm creating only one obejct in gameHandler.

gameHandler.h(creating the objects here and calling to load the sprites)

#pragma once
#include "Player.h"
#include "Obstacle.h"
#include "Background.h"

class gameHandler :public sf::Drawable
{
private:
        static const int CAP = 3;
        Player player = ("img/playerSheet.png");
        Obstacle obstacles[CAP];
        //Obstacle obstacles = ("img/obstacle.png");
        Background bg = ("img/cloud.png");
        void draw(sf::RenderTarget &target, sf::RenderStates states)const;

gameHandler.cpp

calling for the constructor to load the image
gameHandler::gameHandler()
{
        for (int i = 0; i < this->CAP; i++)
        {
                this->obstacles[i] = Obstacle("img/obstacle.png");
        }
}

drawing the sprites
void gameHandler::draw(sf::RenderTarget &target, sf::RenderStates states)const
{
        target.draw(this->bg, states);
        target.draw(this->player, states);
        target.draw(this->obstacles[0], states);
        target.draw(this->obstacles[1], states);
       
}

update function
void gameHandler::update()
{
        this->player.jump();

        for (int i = 0; i < this->CAP; i++)
        {
                this->collision(this->obstacles[i]);
        }
        int yPos = rand() % 690+200;
        int xPos = rand() % 200 + 1;;

        this->obstacles[0].moveObstacle(1000, -yPos);
        this->obstacles[1].moveObstacle(1000, 1050 - yPos);

}


game.cpp (main)

#include <sfml/Graphics.hpp>
#include "gameHandler.h"
#include <iostream>
using namespace std;

int main()
{

        gameHandler handler;
        sf::RenderWindow window(sf::VideoMode(1000, 900), "Flappy bird <<Fist14>> Edition");
        srand(int(time(0)));
        sf::Clock clock;
        window.setFramerateLimit(900);
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();

                }
                sf::Time time = clock.getElapsedTime();
                //cout << 1.0f/time.asSeconds() << endl;
                clock.restart().asSeconds();
               
                window.clear();
                handler.update();
                window.draw(handler);
                window.display();

        }

        return 0;
}


Thanks Stolle
« Last Edit: December 21, 2014, 02:41:54 pm by Stolle »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: White texture while loading textures from array(beginner)
« Reply #1 on: December 21, 2014, 04:22:09 pm »
http://www.sfml-dev.org/tutorials/2.2/graphics-sprite.php#the-white-square-problem
this->obstacles[i] = Obstacle("img/obstacle.png");
Copying is not working out for you. If you do not understand what I mean, read up on C++ copy assignment.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Stolle

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: White texture while loading textures from array(beginner)
« Reply #2 on: December 21, 2014, 05:45:21 pm »
Okej so I have to overload the =operator or is there a easier way?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: White texture while loading textures from array(beginner)
« Reply #3 on: December 21, 2014, 09:48:53 pm »
An easier way would be; just don't copy the objects... As in; allocate them once, pass references or smart pointers around, leave the objects in their original location until you no longer need them.
« Last Edit: December 21, 2014, 09:51:04 pm by Jesper Juhl »

 

anything