我正在尝试将所有纹理存储在一个数组中。但图像没有被返回,这似乎是我的代码
我假设我的 loadTex 和 loadSpri 函数会返回并将它们的数据存储在我制作的数组中,但它似乎并没有那样工作,因为当我运行游戏时它们不会出现,只是白色空间。我的代码可能看起来很糟糕,但我仍然是初学者并试图掌握事情的窍门
这是render.cpp
#include "render.h"
texandsprite render::textures[5];
sf::Texture loadTex(std::string);
sf::Sprite loadSpri(sf::Texture);
//Function to start the game
void render::start()
{
_mainWindow.create(sf::VideoMode(1024, 768, 32), "A New Life");
textures[1].tex = loadTex("civilian.png");
textures[1].spri = loadSpri(textures[1].tex);
GameLoop();
}
void render::GameLoop()
{
sf::Event event;
//Load(1, "civilian.png");
float x = 0;
float y = 0;
while(_mainWindow.isOpen())
{
while (_mainWindow.pollEvent(event))
{
//Check the type of event
switch (event.type)
{
//window closed
case sf::Event::Closed:
_mainWindow.close();
break;
case sf::Event::KeyPressed:
switch(event.key.code)
{
case sf::Keyboard::Up:
--y;
break;
case sf::Keyboard::Down:
y += 5;
break;
case sf::Keyboard::Right:
++x;
break;
case sf::Keyboard::Left:
--x;
break;
}
break;
default:
break;
}
}
_mainWindow.clear(sf::Color::White);
//Draw everything here
//_mainWindow.draw(...);
textures[1].spri.setPosition(x,y);
_mainWindow.draw(textures[1].spri);
//End the current frame
_mainWindow.display();
}
}
sf::RenderWindow render::_mainWindow;
sf::Texture render::loadTex(std::string filename)
{
sf::Texture temptex;
if(!temptex.loadFromFile(filename))
{
std::cout << "Error: could not load image" << filename << std::endl;
}
std::cout << filename << " Loaded" << std::endl;
return temptex;
}
sf::Sprite render::loadSpri(sf::Texture temptex)
{
sf::Sprite tempspri;
tempspri.setTexture(temptex);
return tempspri;
}
这是render.h
#ifndef RENDER_H_INCLUDED
#define RENDER_H_INCLUDED
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include <iostream>
struct texandsprite
{
sf::Texture tex;
sf::Sprite spri;
};
class render
{
public:
static void start();
//Here will go a list of all moving entities
static texandsprite textures[5];
//Here will go a list of all nonmoving entities
//static
private:
static void GameLoop();
static void Rendering();
static sf::Texture loadTex(std::string);
static sf::Sprite loadSpri(sf::Texture);
static sf::Texture texture;
static sf::Sprite sprite;
static sf::RenderWindow _mainWindow;
};
#endif // RENDER_H_INCLUDED