C++SDL 2.0-使用循环导入多个纹理
C++ SDL 2.0 - Importing multiple textures using a loop
我不知道这是否可能,但我在不同的语言中使用过这种技术,但在C++中很难使用。我有10个图像,我正试图使用循环加载到阵列中,如下所示:
for (int i = 0; i < 10; i++)
{
Sprite[i] = IMG_LoadTexture(renderer, "Graphics/Player" + i + ".png");
}
然而,这似乎在C++中不起作用,所以我想知道我做错了什么,或者我能做些什么来获得相同的结果,而不必像这样单独加载每个图像:
Sprite[0] = IMG_LoadTexture(renderer, "Graphics/Player0.png");
我的错误是:"表达式必须具有整型或非量程枚举类型"
感谢您的帮助=)
您不能这样做:
"This is my number: " + (int)4 + "!";
这是违法的。尝试运算符+const char*和const char[SOME_INT_GOES_HERE]时会出现错误,或者尝试使用运算符+将INT添加到字符串时会出现另一个错误。事情就是不这样。
您必须使用C(即snprintf())或字符串流这是我隔离问题的测试代码:
#include <iostream>
#include <string>
int main()
{
int a = 1;
std::string str = "blah";
std::string end = "!";
//std::string hello = str + a + end;// GIVES AN ERROR for operator+
std::string hello = "blah" + a + "!";
//const char* c_str = "blah" + a + "end";
//std::cout << c_str << std::endl;
std::cout << hello << std::endl;
return 0;
}
这里有一个使用字符串流的替代解决方案
#include <iostream>
#include <string>
#include <sstream>
int main()
{
int i = 0;
std::string str;
std::stringstream ss;
while (i < 10)
{
//Send text to string stream.
ss << "text" << i;
//Set string to the text inside string stream
str = ss.str();
//Print out the string
std::cout << str << std::endl;
//ss.clear() doesn't work. Calling a constructor
//for std::string() and setting ss.str(std::string())
//will set the string stream to an empty string.
ss.str(std::string());
//Remember to increment the variable inside of while{}
++i;
}
}
或者,如果您使用的是C++11(只需要-std=C++11),但std::to_string()在某些编译器集(即常规MinGW)上已损坏,则也可以使用std:to_string()切换到它工作的另一种风格(即MinGW-w64),或者在幕后使用字符串流编写自己的to_string()函数。
snprintf()可能是做这件事最快的方法,但为了更安全的C++和更好的风格,建议你使用非C的方法。
我遇到了一个类似的问题,我用这种方式解决了它:
#include <iostream>
using namespace std;
int main() {
string line;
for (int i = 0; i < 10; i++) {
line = "Graphics/Player" + inttostr(i) + ".png"; //I wrote inttostr function because built in inttostr functions messed up my program (see below)
char charger[line.length()]; //creating char array
for (int i = 0; i < sizeof(line); i++) {
charger[i] = line[i]; // copying string to char arry
}
Sprite[i] = IMG_LoadTexture(renderer, charger);
}
}
string inttostr(int integer) { //I know it isn't the best way to convert integer to string, but it works
string charakter;
int swap;
bool negativ = false;
if (integer < 0) {
integer = -integer;
negativ = true;
}
if (integer == 0) {
charakter = "0";
}
while (integer >= 1) {
swap = integer % 10;
integer = integer / 10;
charakter = char(swap + 48) + charakter;
}
if (negativ) {
charakter = "-" + charakter;
}
return charakter;
}
相关文章:
- 尝试导入pybind-opencv模块时出现libgtk错误
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- 导入库可以跨dll版本工作吗
- SFML纹理像播放器
- OpenGL大的3D纹理(>2GB)非常慢
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 建议在运行时将带有类实例的列表从c++导入qml
- 着色器纹理值与创建纹理时写入的值不同
- OpenGL将纹理四边形渲染为(0,0)
- 宽度为奇数的16位纹理为片状
- 导入/导出变量时出错
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 将QT项目导入Visual Studio 2019会给我带来很多LNK2001错误
- 是否可以将函数导入命名空间,但不能导出它?
- 使用 CUDA 和纹理进行图像减法
- 如何将 CLion C++ 项目导入 Visual Studio?
- Python 3.8 不能与 Pybind11 一起导入 [Windows]
- Assimp - 如何使用任何文件格式导入带有纹理的网格?
- 移植DXUT SDKMesh以导入OBJ文件,在哪里添加纹理坐标和索引
- C++SDL 2.0-使用循环导入多个纹理