فشل تحميل نسيج في SFML دون أي رسالة حول سبب

StackOverflow https://stackoverflow.com/questions/19855730

  •  29-07-2022
  •  | 
  •  

سؤال

لقد كنت أتابع الدروس على SFML وحاولت تحميل نسيج باستخدام الكود التالي

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    return sf::Texture();
}

هذا فشل في تحميل الملمس ، العفريت أبيض وهو ليس لون العفريت الخاص بي ..

هل كانت مفيدة؟

المحلول

كما تم التقاطها مباشرة من البرنامج التعليمي للرسومات على موقع SFML

"The loadFromFile function sometimes fails with no obvious reason. First, check the error message printed by SFML in the
standard output (check the console). If the message is unable to open file, make sure that the working directory (which is the
directory any file path will be interpreted relatively to) is what you think it is: when you run the application from the explorer, the
working directory is the executable folder, but when you launch your program from your IDE (Visual Studio, Code::Blocks, ...) the
working directory is sometimes set to the project directory instead. This can generally be changed easily in the project settings." 

لذلك تأكد من تسمية صورتك أولاً بشكل صحيح وتأكد من أنها في المجلد الصحيح أي في دليل العمل الخاص بك.

أيضًا إذا فشل الملمس في التحميل بدلاً من إرجاع العفريت الفارغ ، فيمكنك الإبلاغ عن خطأ في وحدة التحكم ثم رمي استثناء. وبهذه الطريقة ، سيتم إخبارك بأن Sprite لا يتم تحميله بشكل صحيح وسيتعين على البرنامج التعامل مع الاستثناء وإلا سيتم إنهاءه. وبهذه الطريقة ، يجب ألا يكون لدى العفريت في اللعبة نسيجًا أبيض ما لم يكن مقصودًا

شيء من هذا القبيل:

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    throw std::runtime_error("Could not load image.png");
}

نصائح أخرى

تحميل PNG؟ اجعلها 8 بت. يمكن تحميل تنسيقات PNG الأخرى ولكن يتم عرضها دائمًا كمربعات بيضاء.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top