[英]Where is my sfml texture going out of scope at?
So I have the infamous white square issue (again) and this time I'm returning references wherever possible, but to no avail. 因此,我再次遇到了臭名昭著的白方块问题,这次我尽可能地返回参考,但无济于事。 I have a
Level
that holds a vector of GamePieces
, and each GamePiece
object holds an sf::sprite
. 我有一个
Level
,其中包含一个GamePieces
向量,每个GamePiece
对象都包含一个sf::sprite
。 Here's the code trying to draw a sprite from GamePiece
: 这是试图从
GamePiece
绘制精灵的代码:
Engine.h: Engine.h:
#pragma once
#include "stdafx.h"
#include "SFML/Graphics.hpp"
#include "ImageManager.h"
#include "Level.h"
#include "GamePiece.h"
class Engine{
public:
Engine();
~Engine();
void Go();
private:
void ProcessInput();
void UpdateCPU();
void Render();
sf::RenderWindow window;
ImageManager imgr;
Level level;
};
Engine's function that does the drawing: 进行绘图的引擎功能:
void Engine::Render(){
window.clear();
//draw wall tiles
for(int i = 0; i < 160; i++){
if(i < 60){
level.GetWallTile().setPosition(0, i * 10);
window.draw(level.GetWallTile());
}
if(i >= 60 && i < 100){
level.GetWallTile().setPosition((i - 60) * 10, 590);
window.draw(level.GetWallTile());
}
if(i >= 100){
level.GetWallTile().setPosition(390, (i - 100) * 10);
window.draw(level.GetWallTile());
}
}
//draw BG tiles
for(int i = 1; i < 39; i++){
for(int j = 0; j < 59; j++){
level.GetBGTile().setPosition(i * 10, j * 10);
window.draw(level.GetBGTile());
}
}
for(int i = 0; i < level.GetGamePieces().size(); i++){
window.draw(level.GetGamePieces()[i].GetPieceSprite());
}
window.display();
}
Keep in mind that the wallTile
's and bgTile
's are drawing just fine, it's that last for loop that's giving me white squares. 请记住,
wallTile
和bgTile
的绘制效果很好,因为for循环的最后一个给了我白色方块。
Here are the relevant functions and classes: 以下是相关的函数和类:
//level.h
#pragma once
#include "stdafx.h"
#include "SFML/Graphics.hpp"
#include "ImageManager.h"
#include "GamePiece.h"
class Level{
public:
Level(ImageManager &im);
~Level();
sf::Sprite &GetWallTile();
sf::Sprite &GetBGTile();
void AddPiece();
std::vector<GamePiece> &GetGamePieces();
private:
ImageManager &imgr;
//not sure if vector needed, or one sprite
//std::vector<sf::Sprite> wallTiles_;
sf::Sprite wallTile;
//std::vector<sf::Sprite> bgTiles_;
sf::Sprite bgTile;
std::vector<GamePiece> gamePieces_;
};
//Level.cpp
#include "stdafx.h"
#include "Level.h"
Level::Level(ImageManager &im) : imgr(im){
im.AddResourceDirectory("images/");
wallTile.setTexture(im.GetImage("walltile1.png"));
bgTile.setTexture(im.GetImage("bgtile1.png"));
AddPiece();
}
Level::~Level(){
}
void Level::AddPiece(){
GamePiece piece(imgr);
gamePieces_.push_back(piece);
}
std::vector<GamePiece> &Level::GetGamePieces(){
return gamePieces_;
}
//GamPiece.h
class GamePiece{
public:
GamePiece(ImageManager &im);
~GamePiece();
void SetShape();
sf::Sprite &GetPieceSprite();
private:
ImageManager &imgr;
sf::Sprite pieceShape;
};
//and GamePiece.cpp
#include "stdafx.h"
#include "GamePiece.h"
GamePiece::GamePiece(ImageManager &im) : imgr(im){
SetShape();
}
GamePiece::~GamePiece(){
}
void GamePiece::SetShape(){
//int temp = rand() % 7;
int temp = 0;
switch(temp){
case 0:
pieceShape.setTexture(imgr.GetImage("line.png"));
case 1:
pieceShape.setTexture(imgr.GetImage("llblock.png"));
case 2:
pieceShape.setTexture(imgr.GetImage("rlblock.png"));
case 3:
pieceShape.setTexture(imgr.GetImage("square.png"));
case 4:
pieceShape.setTexture(imgr.GetImage("lsquiggly.png"));
case 5:
pieceShape.setTexture(imgr.GetImage("rsquiggly.png"));
case 6:
pieceShape.setTexture(imgr.GetImage("tblock.png"));
}
}
sf::Sprite &GamePiece::GetPieceSprite(){
return pieceShape;
}
Lots of code, I know, but I really don't know where the texture is going out of scope, assuming that's what's wrong. 我知道很多代码,但是我真的不知道纹理超出了范围,假设这是错误的。 Looking at similar posts, that's probably the issue, but again, I need help finding it.
看看类似的帖子,这可能是问题所在,但是再次,我需要帮助才能找到它。
void Level::AddPiece() {
GamePiece piece(imgr);
gamePieces_.push_back(piece);
piece.SetShape();
}
piece
is being copied into the vector. piece
被复制到向量中。 The element that resides in the vector is not associated with the object that it was copied from (the one that is bound to the scope of AddPiece()
). 驻留在向量中的元素与从其复制对象(绑定到
AddPiece()
范围的AddPiece()
)不相关。 Therefore any actions taken on piece
do not affect the element that was inserted. 因此,对
piece
执行的任何操作都不会影响插入的元素。
If you need to access the element that previously push_back()
'ed, then use back()
to return a reference to it: 如果您需要访问先前
push_back()
需要的元素,请使用back()
返回对其的引用:
gamePieces_.back().SetShape();
You could have also just called SetShape()
before inserting it into the vector. 您也可以在将其插入向量之前调用
SetShape()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.