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

Author Topic: Blinking screen when using massive view.move ?  (Read 7736 times)

0 Members and 1 Guest are viewing this topic.

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Blinking screen when using massive view.move ?
« on: August 09, 2015, 10:21:24 pm »
Hi !
I'm trying to move the view following the mouse when Middle button is pressed, like in Google Maps. (In a way that the mouse always shows the same in world point)
I've tried this in my event switch:

case sf::Event::MouseButtonPressed:
// [...]
if (event.mouseButton.button == sf::Mouse::Middle)
                            wheelPos = window.mapPixelToCoords(sf::Mouse::getPosition());
                            wheelPressed = true;
                        break;



case sf::Event::MouseMoved:
                        if (wheelPressed)
                        {
                            wheelPos2 = window.mapPixelToCoords(sf::Mouse::getPosition());
                            wheelPos2.x = wheelPos.x - wheelPos2.x;
                            wheelPos2.y = wheelPos.y - wheelPos2.y;
                            view.move((float)wheelPos2.x, (float)wheelPos2.y);
                            wheelPos = window.mapPixelToCoords(sf::Mouse::getPosition());
                        }
                        break;


case sf::Event::MouseButtonReleased:
                        if (wheelPressed && event.mouseButton.button == sf::Mouse::Middle)
                        wheelPressed = false;
                        break;
 

It does work approximatively, but I get an awful blinking when using this (I tried with Vsynch or frameratelimit, didn't work either)
Also, Sometimes, the view moves even after I release the button.

Am I doing something wrong here ?

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Blinking screen when using massive view.move ?
« Reply #1 on: August 09, 2015, 10:49:57 pm »
Also, Sometimes, the view moves even after I release the button.
That is because you set wheelPressed to true, no matter what mouse button was pressed, but set it to false only when the middle button was released. (Just use {} at your 1st if)

And for your blinking, that is most likely because of the floating position. Try to round the position of your view before rendering, so you don't end up with a position of half a pixel or similar.
Failing to succeed does not mean failing to progress!

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Blinking screen when using massive view.move ?
« Reply #2 on: August 10, 2015, 10:23:37 am »
I feel stupid to have missed the {}, thanks.
I've tried rounding down the position (both by using an round move and ensuring final position is round) but it did'nt work.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Blinking screen when using massive view.move ?
« Reply #3 on: August 10, 2015, 12:56:02 pm »
I get an awful blinking...
I've tried rounding down the position...but it did'nt work.
What exactly is the blinking effect that you are getting?
What didn't work? What happened instead?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Blinking screen when using massive view.move ?
« Reply #4 on: August 10, 2015, 06:55:21 pm »
A video is better than any words :
https://www.youtube.com/watch?v=5--jNIfkPK4&feature=youtu.be
Look at 0:06 for example.

IIRC it is better now with rounded values (float to int, then this int to float) but it's still there, only a bit less visible

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Blinking screen when using massive view.move ?
« Reply #5 on: August 10, 2015, 07:06:14 pm »
Two more things:
  • You're mixing events and real-time input polling. sf::Mouse::getPosition() is real time. The events you have cases for have mouse position members. Use those.
  • sf::Mouse::getPosition() gets the mouse position relative to the desktop, not the window. You'd want sf::Mouse::getPosition(window) instead. However, if you use events as above, this won't be an issue

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Blinking screen when using massive view.move ?
« Reply #6 on: August 10, 2015, 07:13:40 pm »
I'm not sure to understand the problem with mixing events and realtime  :-[
I forgot using window as paramter, bu as you point it, it doesn't matter in this case (did the tests)

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Blinking screen when using massive view.move ?
« Reply #7 on: August 10, 2015, 08:50:31 pm »
Those lines are because you also have zooming, which results in a floating view scale. Try to find some reasonable bounds to round the scaling to just a few decimals. I once tried several different "zoom factors" to get few decimal floating values. It worked for almost every zoom, except a few (which were always the same values like .375 for instance). Rounding them to let's say .370 or .380 did the trick then for those in my project.

Try to get "good floating values" for your scale and then you can test which values do not work and change them a little bit until the lines are gone. It's a bit tedious and the problem does not occur for everyone, but thats the best solution I know of.

PS: With lines I mean your blinking  ::) (for me it was white lines, but that should be the same problem)
« Last Edit: August 10, 2015, 08:59:36 pm by Geheim »
Failing to succeed does not mean failing to progress!

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Blinking screen when using massive view.move ?
« Reply #8 on: August 10, 2015, 10:28:48 pm »
It looked like a nice idea, but the fact is that I get the problem even without zoom. (I did try with floating values too though)

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Blinking screen when using massive view.move ?
« Reply #9 on: August 10, 2015, 10:37:08 pm »
Ah thats a shame. Have you let others compile your project to see if they also get the blinking? You could also give a minimal and complete example so we could try it too.
Failing to succeed does not mean failing to progress!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Blinking screen when using massive view.move ?
« Reply #10 on: August 10, 2015, 11:23:34 pm »
Dabbertorres' point about mixing events and real-time is something you should look into.

An event is queued and is therefore has happened sometime in the past; real-time querying is the state as it is now.

Here's an example of one consideration:
The mouse is moved, the os sends a message to the window to tell it that it's moved and where to, then SFML reads that message and creates an event and adds it to a queue of events.
Then, the os blocks for some reason. It's busy for whatever reason.
You move the mouse more during this short period.
After this period, your app regains control.
It then processes all the events and finds that the mouse was moved.
At this point, if you read the real-time position, you're ignoring what the event is for: to tell you what happened at that time.
You could instead read the position of the mouse in the event itself that tells you what position the mouse was in when the mouse was moved (and the event was created).

Now, you can read the real-time position of the mouse. It simply gives you the exact position when you read it. There's absolutely no need to wait for an event for this. The position is always there, waiting for you to read it.


Still, I'm pretty sure if you'd have provided a minimal and complete example, someone would've found the exact glitch causing error by now  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Blinking screen when using massive view.move ?
« Reply #11 on: August 11, 2015, 08:53:18 am »
I see what you mean here, I'll try it.
AFAIK, my 1st post has all the required code (excepted {} in 2nd line) but I might give more.
i'away for two days though  :(

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Blinking screen when using massive view.move ?
« Reply #12 on: August 11, 2015, 09:19:50 am »
If you followed the link in Hapax' post, you would see that complete and minimal means others can copy/paste at best one single main file, without other dependencies (if not related to the problem), to test it themselves. So the code in your 1st post is not very helpful. Read the link.
Failing to succeed does not mean failing to progress!

bisthebis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Blinking screen when using massive view.move ?
« Reply #13 on: August 12, 2015, 09:12:19 pm »
I haven't worked on this for 2 days, so here's a cleaned code of how I left it.
I'm almost sure the problem resides in the main file, so other fils are here in case you want to test

Main :
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "BackgroundTileMap.h"
#include <fstream>
#include <iostream>
#include <string>

int const TILESIZE = 32;


int main()
{



    sf::RenderWindow window(sf::VideoMode(320, 320), "Tilemap");
    sf::RenderWindow referencePic(sf::VideoMode(512, 512), "Tilemap", sf::Style::Titlebar); //Not tied to our problem, it's a secondary windows
    //window.setVerticalSyncEnabled(true); Didn't solve the probleme in any way so we don't care : I got rid of it
    window.setFramerateLimit(60);
    sf::View view(sf::FloatRect(0, 0, 160, 160));
    view.setViewport(sf::FloatRect(0, 0, 1, 1));
    std::string fileoutputname;
    // on définit le niveau à l'aide de numéro de tuiles
    const int level[] =
    {
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
        0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };

    // on crée la tilemap avec le niveau précédemment défini
    BackgroundTileMap map;
    if (!map.load("tileset_castle.png", sf::Vector2u(TILESIZE, TILESIZE), level, 16, 8))
        return -1;

    map.setRefWindow(&referencePic);
    referencePic.setSize(sf::Vector2u(512,512));
    referencePic.setTitle("Reference");




    int newvalue;
    int valueToApply = -1;
    int brushSize = 1;
     sf::Vector2i pixelPos;
     sf::Vector2f worldPos;
     sf::Vector2f wheelPos, wheelPos2;
    bool wheelPressed = false;

    // Main events handling
    while (window.isOpen() || referencePic.isOpen())
    {
        // on gère les évènements
        sf::Event event;
        while (window.pollEvent(event))
        {

                switch(event.type)
                {
                    case sf::Event::Closed:
                        window.close();
                        referencePic.close();
                        break;
                    case sf::Event::MouseButtonPressed:
                        pixelPos = sf::Mouse::getPosition(window);

                        worldPos = window.mapPixelToCoords(pixelPos);
                        worldPos.x /= TILESIZE;
                        worldPos.y /= TILESIZE;
                        newvalue = 1;
                        if (event.mouseButton.button == sf::Mouse::Right)
                            newvalue = -1;
                        if (event.mouseButton.button == sf::Mouse::Right || event.mouseButton.button == sf::Mouse::Left)
                        {
                            if (brushSize == 1)
                            {


                                if (valueToApply == -1)
                                map.changeTile((int)worldPos.x + (int)worldPos.y*map.getDimensions().x, newvalue);
                                else
                                map.changeTile((int)worldPos.x + (int)worldPos.y*map.getDimensions().x, valueToApply, false);
                                std::cout << "X : " << (int)worldPos.x << ". Y : " << (int)worldPos.y << std::endl;
                            }
                            else
                            {
                                 for (int i = -brushSize +1 ; i < brushSize; i++)
                                    for (int j = -brushSize +1; j < brushSize; j++)
                                        {
                                            if(map.getDimensions().x * j + i > 0)
                                            {
                                                if (valueToApply == -1)
                                                map.changeTile((int)(worldPos.x + j) + (int)(worldPos.y+i)*map.getDimensions().x, newvalue);
                                                else
                                                map.changeTile((int)(worldPos.x + j) + (int)(worldPos.y+i)*map.getDimensions().x, valueToApply, false);
                                                std::cout << "X : " << (int)worldPos.x << ". Y : " << (int)worldPos.y << std::endl;

                                            }

                                        }
                            }



                            /*


                            //*/

                        }
                        else if (event.mouseButton.button == sf::Mouse::Middle)
                            {
                            wheelPos = window.mapPixelToCoords(sf::Mouse::getPosition(window));

                            wheelPressed = true;
                            }
                        break;

                    case sf::Event::MouseMoved:
                        if (wheelPressed)
                        {
                            wheelPos2 = window.mapPixelToCoords(sf::Mouse::getPosition(window));
                            wheelPos2.x = (int)(wheelPos.x - wheelPos2.x);
                            wheelPos2.y = (int)(wheelPos.y - wheelPos2.y);
                            view.move((float)wheelPos2.x, (float)wheelPos2.y);
                            std:: cout << (float)wheelPos2.x << "x" << (float)wheelPos2.y << std::endl;
                            wheelPos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
                        }



                        break;


                    case sf::Event::MouseButtonReleased:
                        if (wheelPressed && event.mouseButton.button == sf::Mouse::Middle)
                        wheelPressed = false;
                        break;

                    case sf::Event::KeyPressed:
                        if (event.key.code == sf::Keyboard::Subtract)
                            view.zoom(2.6);
                        else if (event.key.code == sf::Keyboard::Add)
                            view.zoom(0.38);
                        else if (event.key.code == sf::Keyboard::T)
                        {
                            pixelPos = sf::Mouse::getPosition(referencePic);
                            valueToApply = (int)(pixelPos.x / TILESIZE) + (int)(pixelPos.y / TILESIZE) * 16;
                            std::cout << valueToApply ;
                        }
                        else if (event.key.code == sf::Keyboard::Left)
                        {
                            view.move(-TILESIZE,0);
                        }
                        else if (event.key.code == sf::Keyboard::Right)
                        {
                            view.move(TILESIZE,0);
                        }
                        else if (event.key.code == sf::Keyboard::Up)
                        {
                            view.move(0,-TILESIZE);
                        }
                        else if (event.key.code == sf::Keyboard::Down)
                        {
                            view.move(0,TILESIZE);
                        }



                        break;

                    case sf::Event::MouseWheelScrolled:
                        if (event.mouseWheelScroll.delta < 0)
                            view.zoom(2.6);
                        else if (event.mouseWheelScroll.delta > 0)
                            view.zoom(0.38);
                        break;

                    default:
                        break;
                }



                //"Boucle" secondaire
                referencePic.pollEvent(event);
                    switch (event.type)
                    {
                        case sf::Event::Closed:
                            referencePic.close();
                            break;

                    }


        }

        // on dessine le niveau
        window.clear();
        referencePic.clear();
        //Arrondi de la vue
        sf::Vector2f viewPos;
        viewPos = view.getCenter();
        viewPos.x = (int)viewPos.x;
        viewPos.y = (int)viewPos.y;
        view.setCenter(viewPos);
        window.setView(view);
        window.draw(map);





        referencePic.display();
        window.display();
    }

    return 0;
}

 

BackgroundTileMap.h
#ifndef BACKGROUNDTILEMAP_H
#define BACKGROUNDTILEMAP_H

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <string>
#include <sstream>


class BackgroundTileMap : public sf::Drawable, public sf::Transformable
{
    public:
    //tmp
    //*
     std::string WriteLevel() const
    {
        std::string retour = "";
        for (int i = 0; i < m_level.size(); i++)
        {
            //retour += (m_level[i]+'0');
            //retour += std::string(m_level[i]);

                std::stringstream ss;
                ss << m_level[i];
                std::string str = ss.str();
            retour += str;
            retour += " ";
            if ((i+1)%m_dimensions.x == 0)
                retour+= "\r\n";




        }
        return retour;

    }
    bool WriteToFile(std::string const &filename);
    const sf::Vector2u &getDimensions() const {return m_dimensions;}
    const int getSize() const {return m_dimensions.x*m_dimensions.y;}
    bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);


    bool setTexture(const std::string& texture) {


         if (!m_tileset.loadFromFile(texture))
         {  //Modification de la seconde fenetre
            /*
            if (secWindow != NULL)
            {
                sf::Sprite text(m_tileset);
                secWindow->draw(text);
            }
            //*/



             return false;
             }

            else return true;


                                                    }
    bool loadNewMap(std::string const &filename); //fichier texte

    bool changeTile(int tileNumber, int newvalue, bool addMode = true); //Si true, on met le tile suivant, si false, on change en valeur absolue

    bool update();//Sans changer les valeurs

    void CreateBlankMap(int newx, int newy);

    void setRefWindow (sf::RenderWindow *new_window) {m_window = new_window;}


private:

    sf::RenderWindow *m_window;

    //attributs primaires
    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
    //attributs secondaires:
    sf::Vector2u m_tileSize; //dimensions d'un Tile
    sf::Vector2u m_dimensions; //Du tileset, en nombre de tiles (ex : 16*8)
    std::vector<int> m_level;  //contient les numéros d'un tile
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // on applique la transformation
        states.transform *= getTransform();

        // on applique la texture du tileset
        states.texture = &m_tileset;

        // et on dessine enfin le tableau de vertex
        target.draw(m_vertices, states);

        //if(m_window->isOpen())
        m_window->draw(sf::Sprite(m_tileset));
    }

};




#endif // BACKGROUNDTILEMAP_H
 

And finally BackGroundTileMap.cpp
#include "BackgroundTileMap.h"
#include <fstream>

bool BackgroundTileMap::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
        // on charge la texture du tileset
        if (!m_tileset.loadFromFile(tileset))
            return false;

        //Changement des "attributs secondaires"
        m_tileSize = tileSize;
        m_dimensions.x = width;
        m_dimensions.y = height;

        m_level.resize(width*height);

        for (int i = 0; i < m_level.size(); i++)
        {
            m_level[i] = tiles[i];
        }

        // on redimensionne le tableau de vertex pour qu'il puisse contenir tout le niveau
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);

        // on remplit le tableau de vertex, avec un quad par tuile
        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // on récupère le numéro de tuile courant
                int tileNumber = tiles[i + j * width];

                // on en déduit sa position dans la texture du tileset
                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                // on récupère un pointeur vers le quad à définir dans le tableau de vertex
                sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                // on définit ses quatre coins
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // on définit ses quatre coordonnées de texture
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }

        return true;
    }

bool BackgroundTileMap::loadNewMap(std::string const &filename)
{
    //ouverture du fichier (avec la librairie standard)
        std::ifstream file(filename.c_str());
        if(!file.is_open())
            return false;
            std::string line;
            //trois étapes : trouver la longuer, la largeur, les valeurs
            int step = 0;
            int value = 0;
            m_dimensions.x=0;
            m_dimensions.y=0;
            m_level.resize(0);
            while(step == 0)
            {
                std::getline(file, line);
                if ( ! (std::istringstream(line) >> m_dimensions.x) ) return false;
                //std::cout << "largeur "<<m_dimensions.x << std::endl;
                if (m_dimensions.x > 0)
                    step++;

            }
            while(step == 1)
            {
                std::getline(file, line);
                if ( ! (std::istringstream(line) >> m_dimensions.y) ) return false;
                //std::cout << "hauteur "<< m_dimensions.y << std::endl;
                if (m_dimensions.y > 0)
                    step++;

            }
            file.ignore();
            while(step == 2)
            {
                char a;
                file.get(a);
                if (a == ':')
                {
                    step++;

                }

            }
            file.ignore();
            while(m_level.size() < m_dimensions.y * m_dimensions.x)
            {
                file >> line;
                if ( ! (std::istringstream(line) >> value) ) return false;
                m_level.push_back(value);
            }

        file.close();
        //std::cout << "fini !";

        //suite
        //Changement des "attributs secondaires"

        //*
       // m_tileSize = tileSize; //Déjà assigné : dans l'autre sens
        sf::Vector2u tileSize = m_tileSize;
    //m_dimensions.x = width;   //ci-dessus
       // m_dimensions.y = height;


       int width = m_dimensions.x;
       int height = m_dimensions.y;

        if (m_level.size()!=width*height)
            return false;
        //for (int i = 0; i < m_level.size(); i++)
        //{
        //    m_level[i] = tiles[i];
        //}

        // on redimensionne le tableau de vertex pour qu'il puisse contenir tout le niveau
        //m_vertices.setPrimitiveType(sf::Quads); Pas besoin car déjà chargé
        m_vertices.resize(width * height * 4);

        // on remplit le tableau de vertex, avec un quad par tuile
        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // on récupère le numéro de tuile courant
                int tileNumber = m_level[i + j * width];

                // on en déduit sa position dans la texture du tileset
                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                // on récupère un pointeur vers le quad à définir dans le tableau de vertex
                sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                // on définit ses quatre coins
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // on définit ses quatre coordonnées de texture
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }
        //*/


        return true;
}


bool BackgroundTileMap::changeTile(int tileNumber, int newvalue, bool addMode)
{
    if (addMode)
        m_level[tileNumber] += newvalue;
    else
        m_level[tileNumber] = newvalue;

        //std::cout << m_level[tileNumber];

    return update();
/*
    int width = m_dimensions.x;
    int height = m_dimensions.y;
    //int j = tileNumber%width;
    //int i = tileNumber-(j*width); //x


                // on en déduit sa position dans la texture du tileset
                int tu = tileNumber % (m_tileset.getSize().x / m_tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x /  m_tileSize.x);

                // on récupère un pointeur vers le quad à définir dans le tableau de vertex
                sf::Vertex* quad = &m_vertices[(tileNumber) * 4];

                // on définit ses quatre coins
                /* // La position ne change pas !
                quad[0].position = sf::Vector2f(i * m_tileSize.x, j * m_tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * m_tileSize.x, j * m_tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * m_tileSize.x, (j + 1) * m_tileSize.y);
                quad[3].position = sf::Vector2f(i * m_tileSize.x, (j + 1) * m_tileSize.y);
                //*/

                // on définit ses quatre coordonnées de texture
                /* // défaillant
                m_vertices[tileNumber*4].texCoords = sf::Vector2f(tu * m_tileSize.x, tv * m_tileSize.y);
                m_vertices[tileNumber*4+1].texCoords = sf::Vector2f((tu + 1) * m_tileSize.x, tv * m_tileSize.y);
                m_vertices[tileNumber*4+2].texCoords = sf::Vector2f((tu + 1) * m_tileSize.x, (tv + 1) * m_tileSize.y);
                m_vertices[tileNumber*4+3].texCoords = sf::Vector2f(tu * m_tileSize.x, (tv + 1) * m_tileSize.y);
                //*/

                /* BACKUP
                quad[0].texCoords = sf::Vector2f(tu * m_tileSize.x, tv * m_tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * m_tileSize.x, tv * m_tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * m_tileSize.x, (tv + 1) * m_tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * m_tileSize.x, (tv + 1) * m_tileSize.y);

                //*/

                /*/ TEST 0
                quad[0].texCoords = sf::Vector2f(0,0);
                quad[1].texCoords = sf::Vector2f(31,0);
                quad[2].texCoords = sf::Vector2f(31,31);
                quad[3].texCoords = sf::Vector2f(0,31);
                //*/


                //*/

    return true;



}


bool BackgroundTileMap::update()
{


   // on remplit le tableau de vertex, avec un quad par tuile
        for (unsigned int i = 0; i < m_dimensions.x; ++i)
            for (unsigned int j = 0; j < m_dimensions.y; ++j)
            {
                // on récupère le numéro de tuile courant
                int tileNumber = m_level[i + j * m_dimensions.x];

                // on en déduit sa position dans la texture du tileset
                int tu = tileNumber % (m_tileset.getSize().x / m_tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / m_tileSize.x);

                // on récupère un pointeur vers le quad à définir dans le tableau de vertex
                sf::Vertex* quad = &m_vertices[(i + j * m_dimensions.x) * 4];

                // on définit ses quatre coins
                quad[0].position = sf::Vector2f(i * m_tileSize.x, j * m_tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * m_tileSize.x, j * m_tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * m_tileSize.x, (j + 1) * m_tileSize.y);
                quad[3].position = sf::Vector2f(i * m_tileSize.x, (j + 1) * m_tileSize.y);

                // on définit ses quatre coordonnées de texture
                quad[0].texCoords = sf::Vector2f(tu * m_tileSize.x, tv * m_tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * m_tileSize.x, tv * m_tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * m_tileSize.x, (tv + 1) * m_tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * m_tileSize.x, (tv + 1) * m_tileSize.y);
            }
        //*/
        return true;
}



bool BackgroundTileMap::WriteToFile(std::string const &filename)
{
    std::ofstream outputFile;
    outputFile.open(filename.c_str());


    outputFile << m_dimensions.x << std::endl << m_dimensions.y << std::endl << std::endl << ":" << std::endl;
    for (int i = 0; i < m_level.size(); i++)
    {
        outputFile << m_level[i] << " ";
    }
}


void BackgroundTileMap::CreateBlankMap(int newx, int newy)
{


        m_dimensions.x = newx;
        m_dimensions.y = newy;

        m_level.resize(newx*newy);

        for (int i = 0; i < m_level.size(); i++)
        {
            m_level[i] = 0;
        }

        // on redimensionne le tableau de vertex pour qu'il puisse contenir tout le niveau
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(newx * newy * 4);

        // on remplit le tableau de vertex, avec un quad par tuile
        for (unsigned int i = 0; i < newx; ++i)
            for (unsigned int j = 0; j < newy; ++j)
            {
                // on récupère le numéro de tuile courant
                int tileNumber = m_level[i + j * newx];

                // on en déduit sa position dans la texture du tileset
                int tu = tileNumber % (m_tileset.getSize().x / m_tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / m_tileSize.x);

                // on récupère un pointeur vers le quad à définir dans le tableau de vertex
                sf::Vertex* quad = &m_vertices[(i + j * newx) * 4];

                // on définit ses quatre coins
                quad[0].position = sf::Vector2f(i * m_tileSize.x, j * m_tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * m_tileSize.x, j * m_tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * m_tileSize.x, (j + 1) * m_tileSize.y);
                quad[3].position = sf::Vector2f(i * m_tileSize.x, (j + 1) * m_tileSize.y);

                // on définit ses quatre coordonnées de texture
                quad[0].texCoords = sf::Vector2f(0,0);
                quad[1].texCoords = sf::Vector2f(31,0);
                quad[2].texCoords = sf::Vector2f(31,31);
                quad[3].texCoords = sf::Vector2f(0,31);
            }



        }
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Blinking screen when using massive view.move ?
« Reply #14 on: August 12, 2015, 10:24:46 pm »
This is not anywhere near minimal code so I'm sure you be able to forgive me when I say that I have no intention of testing this code  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything