2017-02-16 48 views
1

我的問題是,該功能不做我想要的。 這個「CreateWindow」函數有主循環。在主循環中,我想要一個固定的背景,每次按H按鈕,我都想在背景上繪製一張卡片(精靈)。 這裏有什麼問題?我的功能畫卡,但是當我按下H時,先前的卡被刪除並且下一張卡被繪製。 這是關於我認爲的事件,因爲每次發生事件(我移動鼠標,我按其他鍵等)以前的卡被刪除... 我使用sfml 2.0Sfml pollEvent,每個新的圖像刪除前一個

這是我的實現圖形類

#include "Graphic.h" 
#include "SFML/Graphics.hpp" 
#include <iostream> 
#include <string> 
#include "Card.h" 
#include <string> 
#include <sstream> 

Graphic::Graphic(int offset) 
{ 
    this->offset = offset; 
} 

Graphic::~Graphic() 
{ 
    //dtor 
} 

int Graphic::CreateWindow(sf::RenderWindow& window, Deck &deck0) 
{ 
    sf::Vector2i screenDimensions(800, 600); 

    //Dimensioni della window 
    window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "BlackJack", sf::Style::Titlebar | sf::Style::Close); 

    int index = 0; 
    window.setKeyRepeatEnabled(false); 

    //settare il background 
    sf::Texture bTexture; 
    sf::Sprite bImage; 
    if(!bTexture.loadFromFile("Background.png")) 
     std::cout << "Error" << std::endl; 
    bImage.setTexture(bTexture); 
    bImage.setScale(1.0, (float)screenDimensions.y/bTexture.getSize().y); 

    //MAIN LOOP---------------------- 
    while(window.isOpen()) 
    { 
     sf::Event Event; 

     while(window.pollEvent(Event)) 
     { 
      window.clear(); 
      window.draw(bImage); // this is the function which draw the background 

      if (Event.type == sf::Event::Closed) 
      { 
       window.close(); 
      } 

      if(Event.key.code == sf::Keyboard::H) 
      { 

       Card * y = deck0.dealFirst(); 
       drawCard(window,y->graphNumber,y->getSeed(),offset); 
       offset = offset + 50; 
      } 

      window.display(); 

     } 
    } 
} 

int Graphic::drawCard(sf::RenderWindow &window, int graphNumber, string seed, int offset) 
{ 
    std::ostringstream oss; 
    oss << graphNumber << seed << ".png"; 
    std::string var = oss.str(); 

    sf::Texture QHTexture; 
    sf::Sprite QHImage; 
    if(!QHTexture.loadFromFile(var)) 
     std::cout<< "Error" <<std::endl; 
    QHImage.setTexture(QHTexture); 
    QHImage.setScale(0.5, 0.5); 
    QHImage.setPosition(offset + 100, 400); 
    window.draw(QHImage); //this is the function which draw the card's sprite 
    return 0; 
} 
+0

分開程序的邏輯和渲染。你的程序應該有一個狀態,不能僅僅由程序計數器,輸出緩衝區和一些變量來表示,而是一個存儲所有卡的數據結構。我在這裏沒有看到一個。哦,是的,可能是因爲這不是** MVCE **。好吧,所以你只是沒有繪製所有的卡...... PS:你可能不想每幀都調用sf :: Texture :: loadFromFile。 – LogicStuff

+0

我想調用loadFromFile每一幀,因爲我想繪製一個52的圖像,然後我不調用loadFromFile everyframe,但我只有當我按下H時調用此方法。 我認爲問題應該在drawCard( )方法,因爲我可能會覆蓋以前的紋理和精靈。 –

+0

您只在一個框架中繪製一個精靈/紋理/圖像。該幀在開始時被清除。 – LogicStuff

回答

1

好吧,你不應該你while(window.pollEvent())循環內進行繪製的,你應該畫這樣的事:

while(window.isOpen()) 
    { 
    sf::Event Event; 
    while(window.pollEvent(Event)) 
    { 
     if (Event.type == sf::Event::Closed) 
     { 
      window.close(); 
     } 

     if(Event.key.code == sf::Keyboard::H) 
     { 

      Card * y = deck0.dealFirst(); 
      drawCard(window,y->graphNumber,y->getSeed(),offset); 
      offset = offset + 50; 
     } 
    } 

    window.clear(); 
    window.draw(bImage); // this is the function which draw the background 

    window.display(); 
} 

的方式你畫你的畫,來電會只有在發生SFML事件時纔會發生,並且只有在發生sfml事件時纔會清除(如果您不希望它持續呈現每一幀,並且不計劃任何類型的動畫,則可以這樣做) 。

所以當你點擊H時,一個sfml事件是觸發器,它會調用你的繪圖卡功能,但是因爲你的卡是你寫的函數的局部變量,所以在函數結束時它被清除。你需要將你的卡片存放在某個地方,比如一個向量或sf :: Sprite列表。所以一個例子是:

#include "Graphic.h" 
#include "SFML/Graphics.hpp" 
#include <iostream> 
#include <string> 
#include "Card.h" 
#include <string> 
#include <sstream> 

Graphic::Graphic(int offset) 
{ 
    this->offset = offset; 
} 

Graphic::~Graphic() 
{ 
    //dtor 
} 

int Graphic::CreateWindow(sf::RenderWindow& window, Deck &deck0) 
{ 
    sf::Vector2i screenDimensions(800, 600); 

    //Dimensioni della window 
    window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "BlackJack", sf::Style::Titlebar | sf::Style::Close); 

    int index = 0; 
    window.setKeyRepeatEnabled(false); 

    //settare il background 
    sf::Texture bTexture; 
    sf::Sprite bImage; 
    if(!bTexture.loadFromFile("Background.png")) 
     std::cout << "Error" << std::endl; 
    bImage.setTexture(bTexture); 
    bImage.setScale(1.0, (float)screenDimensions.y/bTexture.getSize().y); 

    //MAIN LOOP---------------------- 
     while(window.isOpen()) 
    { 
    sf::Event Event; 
    while(window.pollEvent(Event)) 
    { 
     if (Event.type == sf::Event::Closed) 
     { 
      window.close(); 
     } 

     if(Event.key.code == sf::Keyboard::H) 
     { 

      Card * y = deck0.dealFirst(); 
      drawCard(window,y->graphNumber,y->getSeed(),offset); 
      offset = offset + 50; 
     } 
    } 

    window.clear(); 
    window.draw(bImage); // this is the function which draw the background 

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

    window.display(); 
    } 

} 

int Graphic::drawCard(sf::RenderWindow &window, int graphNumber, string seed, int offset) 
{ 
    std::ostringstream oss; 
    oss << graphNumber << seed << ".png"; 
    std::string var = oss.str(); 

    sf::Texture QHTexture; 
    sf::Sprite QHImage; 
    if(!QHTexture.loadFromFile(var)) 
     std::cout<< "Error" <<std::endl; 
    QHImage.setTexture(QHTexture); 
    QHImage.setScale(0.5, 0.5); 
    QHImage.setPosition(offset + 100, 400); 
    cardList.push_back(QHImage); //this adds the sprite to our list 
    return 0; 
}