2

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

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

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

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

//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 Objects 来显示这些功能:

#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);
};
4

1 回答 1

2

添加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().

于 2012-06-19T16:31:00.897 回答