[英]SFML Sprites not displaying at all
我有一個方法,它應該采用 64x128 的 spritesheet 並將其轉換為 8x8 的 sprite。
從存儲的圖像中找到每個紋理后,將其添加到精靈中,並將該精靈添加到在我的主要方法中調用的精靈數組中。
我之前測試過顯示靜態精靈,並且我的代碼有效(我成功顯示了 1 個 8x8 精靈)
但是,當嘗試顯示我的精靈列表中的 128 個精靈中的任何一個時,現在都沒有渲染。
我相信邏輯錯誤可能出在我的 MakeSprite 方法中,但我不確定,我看不出問題出在哪里。
[編輯]:似乎我調用的每個精靈都在精靈表的最后返回精靈(精靈 128)
[編輯 2]:前一個精靈的紋理 (tex) 被下一個要生成的精靈覆蓋。
以下是我的代碼的完整可驗證工作示例:
主程序
#include "Main.h"
int main() {
srand(time(NULL));
RenderWindow window(VideoMode(1280, 720), "Conduit");
MakeCircle(10, 100, 100, Color::White);
MakeCircle(30, 10, 100, Color::Cyan);
MakeCircle(100, 200, 100, Color::Magenta);
MakeCircle(100, 400, 100, Color::Cyan);
if (!LoadSpritesheet())
{
return 1;
}
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
else if (event.key.code == Keyboard::Num1)
{
DrawRandomSprite(window);
}
}
window.clear();
DrawCircles(window);
int c = 0;
for (int i = 0; i < 128; i++)
{
Spritesheet.at(i).setPosition(c, c);
window.draw(Spritesheet.at(i));
c += 8;
}
window.display();
}
}
void DrawRandomSprite(RenderWindow &window)
{
//DEBUG METHOD: draws a random sprite for testing.
int sprite = rand() % 128 + 1;
Spritesheet.at(sprite).setPosition(rand() % 128 + 1, rand() % 128 + 1);
window.draw(Spritesheet.at(sprite));
}
void MakeCircle(float radius, float xpos, float ypos, Color color)
{
//makes a circle then adds it to the circle vector.
CircleShape shape;
shape.setRadius(radius);
shape.setPosition(xpos, ypos);
shape.setFillColor(color);
Circles.push_back(shape);
}
void DrawCircles(RenderWindow &window)
{
//Renders the circles in the circles vector.
for (int i = 0; i < Circles.size(); i++)
{
window.draw(Circles.at(i));
}
}
int LoadSpritesheet()
{
//make sure spritesheet exists, then loads it into an image.
Texture sheet;
if (!sheet.loadFromFile("Sprites/A.png"))
{
return 0;
}
else
{
sheetIMG = sheet.copyToImage();
SetMask();
MakeSprite(8, 4);
return 1;
}
}
void SetMask()
{
//creates a mask.
sheetIMG.createMaskFromColor(sf::Color(151, 56, 14, 0), 100);
}
void MakeSprite(int dimension, int scale)
{
//seperates the spritesheet into a list of 8x8 modular sprites.
int c = 0, r = 0;
do
{
for (int i = 0; i <= (sheetIMG.getSize().x * sheetIMG.getSize().y) / 64; i++)
{
if (r == 64)
break;
if (!tex.loadFromImage(sheetIMG, IntRect(c, r, dimension, dimension)))
break;
else
{
Sprite spr;
spr.setTexture(tex);
spr.setScale(scale, scale);
Spritesheet.push_back(spr);
c += dimension;
if (c == sheetIMG.getSize().x) { c = 0; r+=8; };
}
}
} while (r < sheetIMG.getSize().y);
}
主文件
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
//standard and SFML namespaces;
using namespace std;
using namespace sf;
//===============================VARIBLES===========================
//the main spritesheet.
Texture tex;
Image sheetIMG;
//array to hold circles.
vector<CircleShape> Circles;
//array to hold sprites.
vector<Sprite> Spritesheet;
//===============================PROTOTYPES=============================
void DrawCircles(RenderWindow &window);
void MakeCircle(float radius, float xpos, float ypos, Color color);
void MakeSprite(int dimension, int scale);
void SetMask();
int LoadSpritesheet();
void DrawRandomSprite(RenderWindow &window);
您的代碼有兩個主要問題:
您正在使用全局變量。 雖然這對於某些庫來說可能沒問題,但對於 SFML,這根本不是一件好事。
調用DrawRandomSprite
后,您正在clear
窗口。 因此你的精靈永遠不會出現。
您所有的精靈都具有相同的紋理: tex
。 由於tex
是一個單一的全局變量,當你的循環結束時,它被設置為最后一個紋理。 所以所有的精靈都會繪制最后的紋理。 您需要擺脫全局變量並為每個精靈設置一個紋理(或者每個精靈至少有一個您想要具有不同紋理的紋理)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.