C++SDL 2.0-使用循环导入多个纹理

C++ SDL 2.0 - Importing multiple textures using a loop

本文关键字:导入 纹理 循环 C++SDL      更新时间:2023-10-16

我不知道这是否可能,但我在不同的语言中使用过这种技术,但在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;
}