0
所以我在另一個名爲mapReader的類中創建了一個mapRender函數,它將這些圖塊繪製到Game類中的主窗口,並將主窗口作爲對mapRender函數的引用傳遞,但它不會繪製主窗口的瓷磚?我已經在主遊戲類中測試過它,它工作正常,它只是試圖傳遞無法正常工作的窗口引用。傳遞SF :: RenderWindow引用
完整的例子:
的main.cpp
#include <Game.h>
int main()
{
Game game;
game.run();
}
Game.h
#ifndef GAME_H
#define GAME_H
#include <iostream>
#include <Draw.h>
class Game
{
public:
Game();
void run();
void render();
private:
sf::RenderWindow window;
};
#endif // GAME_H
Game.cpp
#include "Game.h"
Game::Game()
{
sf::VideoMode vmode(640,480);
window.create(vmode,"Reference passing");
Draw d;
d.mapLoader();
}
void Game::run()
{
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
render();
}
}
void Game::render()
{
Draw d;
window.clear();
d.mapRender(window);
window.display();
}
Draw.h
#ifndef DRAW_H
#define DRAW_H
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <SFML/Graphics.hpp>
class Draw
{
public:
void mapLoader();
void mapRender(sf::RenderWindow& rWindow);
private:
private:
sf::Vector2i levelMap[21][16];
sf::Vector2i loadCounter;
sf::Texture tileTexture;
sf::Sprite tiles;
};
#endif // DRAW_H
Draw.cpp
#include "Draw.h"
void Draw::mapLoader()
{
loadCounter=sf::Vector2i(0,0);
if(!tileTexture.loadFromFile("mapSprite.png"))
std::cout<<"\n\nMAP LOAD FAILED!\n"<<std::endl;
tiles.setTexture(tileTexture);
std::ifstream openFile;
openFile.open("mapfile.txt");
loadCounter=sf::Vector2i(0,0);
if(openFile.is_open())
{
while(!openFile.eof())
{
std::string str;
openFile>>str;
char x=str[0],y=str[2];
if(!isdigit(x)&&!isdigit(y))
levelMap[loadCounter.x][loadCounter.y]=sf::Vector2i(-1,-1);
else
levelMap[loadCounter.x][loadCounter.y]=sf::Vector2i(x-'0',y-'0');
if(openFile.peek()=='\n')
{
loadCounter.x=0;
loadCounter.y++;
}
else
loadCounter.x++;
}
loadCounter.y++;
}
}
void Draw::mapRender(sf::RenderWindow& rWindow)
{
for(int i=0;i<20;i++)
{
for(int j=0;j<15;j++)
{
if(levelMap[i][j].x!=-1 && levelMap[i][j].y!=-1)
{
tiles.setPosition(i*32,j*32);
tiles.setTextureRect(sf::IntRect(levelMap[i][j].x*32,levelMap[i][j].y*32,32,32));
rWindow.draw(tiles);
}
}
}
}
mapfile.txt
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x 0,0 x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 0,0 x,x x,x x,x x,x x,x 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 0,0 x,x x,x x,x x,x 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 0,0 x,x x,x x,x 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 1,0 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 1,0 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 1,0 1,0
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
0,0 x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 0,0 x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x 0,0 0,0
通過引用傳遞渲染窗口可以正常工作。該錯誤必須在其他地方。你可以發佈[MCVE](http://stackoverflow.com/help/mcve)嗎? – nvoigt 2015-02-09 06:19:08
我必須在mapReader類中包含Game類標題嗎?我已經在遊戲課中測試過它,它工作正常。當我通過它作爲另一個類的引用時,它不會吸引到窗口。我可以提供一個MCVE,如果需要的話 – 2015-02-09 11:18:25
正如我所說,將它作爲參考在一般作品中傳遞。你的代碼中的某些東西一定是有問題的。發佈一個完整的例子,以便我們可以真正弄清楚它是什麼。 – nvoigt 2015-02-09 11:38:00