-2

无论出于何种原因,我的代码崩溃,但只有在我退出程序(通过点击“X”)之后,任何人都知道它为什么这样做?这是我的代码。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <windows.h>
#include <time.h>


#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
using namespace std;

/*
const int AIR = 0;
const int STONE = 1;
const int DIRT = 3;
const int GRASS = 4;
*/


float SCALE = .5;
const int chunkSize = 16;
const int chunkSizeY = 256;
const int blocksTotal = 4;
int chunk[chunkSize][chunkSizeY][chunkSize];
time_t seconds;
int seed = 0;
int quads;

struct block {
  bool Solid;
  int DepthMAX;
  int DepthMIN;
  int R;
  int G;
  int B;
  int id;
} AIR, STONE, DIRT, GRASS;

    block * blockType = new block[3];


void buildChunk();
void renderChunk();
void countFaces();
void render();
void logic();

void drawCube(float size,int x,int y,int z,float redValue,float greenValue,float blueValue)
{

    float halfSize = size / 2;
    float xSize = x * size;
    float ySize = y * size;
    float zSize = z * size;

    glBegin(GL_QUADS);
glColor3f(redValue,greenValue,blueValue);

///     front face
    if(z+1 < chunkSize)
    {
        if(chunk[x][y][z+1] == AIR.id)
        {
            // front face
            glNormal3f(0.0,0.0,1.0);
    //glColor3f(0,200,255);
    glTexCoord2f(1.0,1.0);
    glVertex3f(xSize + halfSize, ySize + halfSize, zSize + halfSize);
    glTexCoord2f(0.0,1.0);
    glVertex3f(xSize - halfSize, ySize + halfSize, zSize + halfSize);
    glTexCoord2f(0.0,0.0);
    glVertex3f(xSize - halfSize, ySize - halfSize, zSize + halfSize);
    glTexCoord2f(1.0,0.0);
    glVertex3f(xSize + halfSize, ySize - halfSize, zSize + halfSize);
                quads++;


        }
    }
    else
    {
        //glColor3f(1,0,0);
        if(z+1 >= chunkSize)
        {
            glNormal3f(0.0,0.0,1.0);
    //glColor3f(0,200,255);
    glVertex3f(xSize + halfSize, ySize + halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize + halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize - halfSize, zSize + halfSize);
    glVertex3f(xSize + halfSize, ySize - halfSize, zSize + halfSize);
                quads++;
        }
    }


    // left face
    if(x-1 > -1)
    {
        if(chunk[x-1][y][z] == AIR.id)
        {
            //glColor3f(0.0,1.0,0.0);
            glNormal3f(-1.0,0.0,0.0);
            glVertex3f(xSize - halfSize, ySize + halfSize, zSize + halfSize);
            glVertex3f(xSize - halfSize, ySize - halfSize, zSize + halfSize);
            glVertex3f(xSize - halfSize, ySize - halfSize, zSize - halfSize);
            glVertex3f(xSize - halfSize, ySize + halfSize, zSize - halfSize);
            quads++;
        }
    }
    else
    {
        //glColor3f(1,1,1);
        if(x-1 < 0)
        {
            glNormal3f(-1.0,0.0,0.0);
            glVertex3f(xSize - halfSize, ySize + halfSize, zSize + halfSize);
            glVertex3f(xSize - halfSize, ySize - halfSize, zSize + halfSize);
            glVertex3f(xSize - halfSize, ySize - halfSize, zSize - halfSize);
            glVertex3f(xSize - halfSize, ySize + halfSize, zSize - halfSize);
            quads++;
        }
    }

    // back face

    if(z-1 > -1)
    {
        if(chunk[x][y][z-1] == AIR.id)
        {
            //glColor3f(0.0,0.0,1.0);
            glNormal3f(0.0,0.0,-1.0);
            glVertex3f(xSize + halfSize, ySize + halfSize, zSize - halfSize);
            glVertex3f(xSize - halfSize, ySize + halfSize, zSize - halfSize);
            glVertex3f(xSize - halfSize, ySize - halfSize, zSize - halfSize);
            glVertex3f(xSize + halfSize, ySize - halfSize, zSize - halfSize);
            quads++;
        }
    }
    else
    {
        //glColor3f(0,1,1);
        if(z-1 < 0)
        {
            glNormal3f(0.0,0.0,-1.0);
            glVertex3f(xSize + halfSize, ySize + halfSize, zSize - halfSize);
            glVertex3f(xSize - halfSize, ySize + halfSize, zSize - halfSize);
            glVertex3f(xSize - halfSize, ySize - halfSize, zSize - halfSize);
            glVertex3f(xSize + halfSize, ySize - halfSize, zSize - halfSize);
            quads++;
        }
    }

    // right face
    if(x+1 < chunkSize)
    {
        if(chunk[x+1][y][z] == AIR.id)
        {
            //glColor3f(1.0,1.0,0.0);
            glNormal3f(1.0,0.0,0.0);
            glVertex3f(xSize + halfSize, ySize + halfSize, zSize + halfSize);
            glVertex3f(xSize + halfSize, ySize - halfSize, zSize + halfSize);
            glVertex3f(xSize + halfSize, ySize - halfSize, zSize - halfSize);
            glVertex3f(xSize + halfSize, ySize + halfSize, zSize - halfSize);
            quads++;
        }
    }
    else
    {
        //glColor3f(0,1,0);
        if(x+1 >= chunkSize)
        {
            glNormal3f(1.0,0.0,0.0);
            glVertex3f(xSize + halfSize, ySize + halfSize, zSize + halfSize);
            glVertex3f(xSize + halfSize, ySize - halfSize, zSize + halfSize);
            glVertex3f(xSize + halfSize, ySize - halfSize, zSize - halfSize);
            glVertex3f(xSize + halfSize, ySize + halfSize, zSize - halfSize);
            quads++;      }
    }

    // top face
    if(y+1 < chunkSizeY)
    {
        if(chunk[x][y+1][z] == AIR.id)
        {
    //glColor3f(1.0,0.0,1.0);
    glNormal3f(0.0,1.0,0.0);
    glVertex3f(xSize + halfSize, ySize + halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize + halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize + halfSize, zSize - halfSize);
    glVertex3f(xSize + halfSize, ySize + halfSize, zSize - halfSize);
    quads++;
        }
    }else
    {
        //glColor3f(0,1,0);
        if(y+1 >= chunkSizeY)
        {
glNormal3f(0.0,1.0,0.0);
    glVertex3f(xSize + halfSize, ySize + halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize + halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize + halfSize, zSize - halfSize);
    glVertex3f(xSize + halfSize, ySize + halfSize, zSize - halfSize);
    quads++;
        }
    }
    // bottom face
    if(y-1 > -1)
    {
        if(chunk[x][y-1][z] == AIR.id)
        {
    //glColor3f(0.0,1.0,1.0);
    glNormal3f(0.0,-1.0,0.0);
    glVertex3f(xSize + halfSize, ySize - halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize - halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize - halfSize, zSize - halfSize);
    glVertex3f(xSize + halfSize, ySize - halfSize, zSize - halfSize);
    quads++;
        }
    }else
    {
        //glColor3f(1,1,1);
        if(y-1 < 0)
        {
    glNormal3f(0.0,-1.0,0.0);
    glVertex3f(xSize + halfSize, ySize - halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize - halfSize, zSize + halfSize);
    glVertex3f(xSize - halfSize, ySize - halfSize, zSize - halfSize);
    glVertex3f(xSize + halfSize, ySize - halfSize, zSize - halfSize);
    quads++;
        }
    }

    glEnd();
}
float angle = 0.0;
const int triangle = 1;

void init()
{
    //AIR
blockType[0].id = 0;
blockType[0].R = 0;
blockType[0].G = 0;
blockType[0].B = 0;
    //STONE
blockType[1].id = 1;
blockType[1].R = 0;
blockType[1].G = 0;
blockType[1].B = 1;
blockType[1].DepthMAX = 5;
blockType[1].DepthMIN = 0;
    //DIRT
blockType[2].id = 2;
blockType[2].R = 1;
blockType[2].G = 0;
blockType[2].B = 0;
blockType[2].DepthMAX = 10;
blockType[2].DepthMIN = 6;
    //GRASS
blockType[3].id = 3;
blockType[3].R = 0;
blockType[3].G = 1;
blockType[3].B = 0;
blockType[3].DepthMAX = 20;
blockType[3].DepthMIN = 11;


AIR.Solid = false;
AIR.id = 0;
STONE.id = 1;
DIRT.id = 2;
GRASS.id = 3;


    seconds = time (NULL);
    seed = seconds;
    srand(seed);
    /* Seed the random number generator with the specified seed */

    glClearColor(0.0,0.0,0.0,1.0);  //background color and alpha
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,640.0/480.0,1.0,500.0);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        float dif[] = {1.0,1.0,1.0,1.0};
        glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
        float amb[] = {0.2,0.2,0.2,1.0};
        glLightfv(GL_LIGHT0, GL_AMBIENT, amb);

    buildChunk();
}

void render()
{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
        float pos[] = {-2.0,2.0,-3.0,1.0};
        glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glTranslatef(0.0,0.0,-40.0);
    glRotatef(angle,1.0,0.0,0.0);   // angle, x-axis, y-axis, z-axis
    renderChunk();



}

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen;
    screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_OPENGL);
//     screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_FULLSCREEN);
    bool running = true;
    const int FPS = 30;
    Uint32 start;
    SDL_Event event;
    init();
    while(running)
    {
        start = SDL_GetTicks();
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            }
        }

        logic();
        quads = 0;
        render();

        SDL_GL_SwapBuffers();
        angle += 0.5;
        if(angle > 360)
            angle -= 360;
        if(1000/FPS > SDL_GetTicks()-start)
            SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
    }

    countFaces();
    cout << "QUADS: " << quads << endl;
    cout<<"vertics: " << quads * 4 << endl;

delete [] blockType;
    SDL_Quit();
    return 0;
}

void buildChunk()
{

    int x;
    int y;
    int z;



    for(x=0; x < chunkSize; x++)
    {
        for(y=0; y < chunkSizeY; y++)
        {
            for(z=0; z < chunkSize; z++)
            {




                /* 50% chance for a cell to be alive */
                if(rand() % 100 < 50)
                {
                    chunk[x][y][z] = AIR.id;
                }
                else
                {
                    for(int i=1; i < blocksTotal; i++){
                    if(y <= blockType[i].DepthMAX && y >= blockType[i].DepthMIN){
                            chunk[x][y][z] = blockType[i].id;
                        }

                    }
                }
                cout<< "chunk[" << x << "][" << y << "][" << z << "]  state: " << chunk[x][y][z]<<endl;
            }
        }
    }
}

void renderChunk()
{
    int x;
    int y;
    int z;

    for(x=0; x < chunkSize; x++)
    {
        for(y=0; y < chunkSizeY; y++)
        {
            for(z=0; z < chunkSize; z++)
            {

                if(chunk[x][y][z] != blockType[0].id)
                {
                    for(int i=1; i < blocksTotal; i++){
                    if(chunk[x][y][z] == blockType[i].id)
                    {
                        drawCube(SCALE,x,y,z,blockType[i].R,blockType[i].G,blockType[i].B);

                        }
                    }
                }


            }
        }
    }
}

void logic()
{

}

void countFaces()
{

}

我认为这可能与我添加和删除的结构数组有关?

4

1 回答 1

7

您为三个元素分配空间:

block * blockType = new block[3];

但随后访问四个:

    //AIR
blockType[0].id = 0;
...
    //STONE
blockType[1].id = 1;
...
    //DIRT
blockType[2].id = 2;
...
    //GRASS
blockType[3].id = 3;
...

这会导致内存损坏(技术上,未定义的行为)。

应该new

block * blockType = new block[4];
于 2013-07-20T07:33:25.923 回答