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

时间:2012-06-18 18:28:36

标签: c++ opengl random sdl textures

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

我现在正在开发电源,但出于一些奇怪的原因,它只显示了我在每个电源上指定的3种不同纹理中的一种,并且不时在左上角的cornor中显示出纹理的大版本,什么都不做(没有碰撞功能)。

我在进入游戏循环之前将纹理加载到变量中并检查它是否实际加载并且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");

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

//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);
};

1 个答案:

答案 0 :(得分: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()