SDL 上的 OpenGL 纹理仅显示某些纹理

OpenGL Textures on SDL display only some textures

本文关键字:纹理 显示 OpenGL SDL 上的      更新时间:2023-10-16

我遇到了一个关于放置在 2D 四边形上的 2D 纹理的问题。我有一个太空街机游戏,它有小行星,行星和一艘船(有纹理),加载得很好。

我现在正在研究道具,但由于某种奇怪的原因,它只显示我在每次道具上指定的 3 种不同纹理之一,并且不时在左上角显示一个大版本的纹理,它什么都不做(没有碰撞功能)。

在进入游戏循环之前,我将纹理加载到变量中,并检查它是否实际加载并且 int 值保存值,所以这很好。当小行星被摧毁时,将实例化一个 PowerUp 对象,并使用先前加载的纹理中的 int 值填充 GLuint 纹理属性。

我将发布一些代码片段以显示我可能犯的一些错误:

//Initialisation of Vectors that hold all PowerUps dropped in game
vector<PowerUp_Speed> powerUps;
vector<PowerUp_Gun> powerUps_Gun;
vector<PowerUp_FireRate> powerUps_FireRate;
//PowerUps Texture Loading  (Before the main loop and after SDL init)
int speedTexture = loadTexture("sprites/Speed.png");
int rateTexture = loadTexture("sprites/Rate.png");
int gunTexture = loadTexture("sprites/Gun.png");

以下部分是在主循环中(当子弹与小行星碰撞发生时)

//PowerUps
PowerUp_Speed boost;
boost.texture = speedTexture;
bool push = boost.Drop(powerUps, destr[d]);
if(push)    powerUps.push_back(boost);
PowerUp_Gun gunBoost;
gunBoost.texture = rateTexture;
push = gunBoost.Drop(powerUps_Gun, destr[d]);
if(push)    powerUps_Gun.push_back(gunBoost);
PowerUp_FireRate fireBoost;
fireBoost.texture = gunTexture;
push = fireBoost.Drop(powerUps_FireRate, destr[d]);
if(push)    powerUps_FireRate.push_back(fireBoost);

以及其中一个 PowerUp 对象,用于显示这些功能:

#include "SpaceGame.h"
PowerUp_Speed::PowerUp_Speed()
{
    isAlive = true;
    width = 40;
    height = width;
    chance = 5;
}
void PowerUp_Speed::boostPlayer(Ship &ship)
{
    ship.vel += 0.1f;
}
void PowerUp_Speed::Draw()
{
    if(isAlive)
    {
        glPushMatrix();
        glColor4ub(255, 255, 255, 255);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture);
        glBegin(GL_QUADS);
            glTexCoord2d(0,0); glVertex2f(x, y);
            glTexCoord2d(1,0); glVertex2f(x + width, y);
            glTexCoord2d(1,1); glVertex2f(x + width, y + height);
            glTexCoord2d(0,1); glVertex2f(x, y + height);
        glDisable(GL_TEXTURE_2D);
        glPopMatrix();
    }
}
bool PowerUp_Speed::Drop(vector<PowerUp_Speed> &powerUps, Destructable d)
{
    if(rand() % chance == 1)
    {
        if(powerUps.size() == 0)
        {
            x = d.x;
            y = d.y;
            return true;
        }
        else
        {
            bool succes = false;
            for(unsigned int i = 0; i < powerUps.size(); i++)
            {
                if(!powerUps[i].isAlive)
                {
                    powerUps[i].isAlive = true;
                    powerUps[i].x = d.x;
                    powerUps[i].x = d.y;
                    powerUps[i].texture = texture;
                    succes = true;
                    return false;
                }
            }
            if(!succes)
            {
                x = d.x;
                y = d.y;
                return true;
            }
        }
    }
    return false;
}

及其在头文件中的降级:

class PowerUp_Speed
{
    public:
        float x, y;
        int width, height;
        int chance;
        bool isAlive;
        GLuint texture;
        PowerUp_Speed();
        void Draw();
        void boostPlayer(Ship &ship);
        bool Drop(vector<PowerUp_Speed> &powerUps, Destructable d);
};

添加一个glEnd()PowerUp_Speed::Draw()

glBegin(GL_QUADS);
    glTexCoord2d(0,0); glVertex2f(x, y);
    glTexCoord2d(1,0); glVertex2f(x + width, y);
    glTexCoord2d(1,1); glVertex2f(x + width, y + height);
    glTexCoord2d(0,1); glVertex2f(x, y + height);
glEnd();

每个glBegin()都需要相应的glEnd()