SDL崩溃申请?

时间:2013-07-15 07:34:16

标签: c++ sdl

嘿所以我仍然在掌握C ++,但我想尝试使用SDL在我的游戏中实现一些更好的图形。我的问题是它正在关闭我的程序,它应该保持打开并在后台运行功能..

我真的不知道为什么会这样做,我知道我正确地连接了我的lib。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <windows.h>
#include <time.h>
#include "SDL/SDL.h"
#include <SDL/SDL_opengl.h>

#define HEIGHT 25
#define WIDTH 80
using namespace std;

void makeBoard();
void buildBoard();
void nextStep();
void makeCell();
void setcolor(unsigned short color)  ;

///GLOBALS
int state;
time_t seconds;
int board[HEIGHT][WIDTH] = {0};
int ghostBoard[HEIGHT][WIDTH] = {0};
bool inGame = true;
int seed = 0;
/*
The different color codes are

0   BLACK
1   BLUE
2   GREEN
3   CYAN
4   RED
5   MAGENTA
6   BROWN
7   LIGHTGRAY
8   DARKGRAY
9   LIGHTBLUE
10  LIGHTGREEN
11  LIGHTCYAN
12  LIGHTRED
13  LIGHTMAGENTA
14  YELLOW
15  WHITE
*/

struct Cell //Brick structures which holds 4 elements
{
    //Elements are used for x and y position of the brick and its width and height
    float width;
    float height;
};

int main(int argc, char* args[] )
{
    //initialize SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Set OpenGL memory usage
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    //Caption of the window
    SDL_WM_SetCaption( "Our first game", NULL );

    //Size of the window
    SDL_SetVideoMode(600,400,32, SDL_OPENGL );

    //Specific the clear color
    glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA

    //What portion of the screen we will display
    glViewport(0,0,600,400);

    //Shader model - Use this
    glShadeModel(GL_SMOOTH);

    //2D rendering
    glMatrixMode(GL_PROJECTION);

    //"Save" it
    glLoadIdentity();

    //Disable depth checking
    glDisable(GL_DEPTH_TEST);

    std::cout << "OpenGL is running\n";
    std::cout << "Main loop has started\n";

    //Handles the main loop
    bool isRunning = true;

    //For handling with event
    SDL_Event event;

    float width = 80; //width of the rectangle
    float height = 20; //height of the rectangle


    //Main game loop
    while ( isRunning )
    {
        //EVENTS
        while ( SDL_PollEvent(&event) )
        {
            //if the window was closed
            if ( event.type == SDL_QUIT )
            {
                isRunning = false;
            }

            //If a button was released and the button is escape
            if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
            {
                isRunning = false;
            }

            if ( event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_r )
            {
                glClearColor(1,0,0,1);
            }

            if ( event.type == SDL_KEYDOWN ) //Check for presed down buttons
            {

            }

            else if ( event.type == SDL_KEYUP ) //Checking for released buttons
            {

            }

            //logic that should happen for a certain event
            makeBoard();
            nextStep();
        }

        //RENDERING to the screen
        glClear(GL_COLOR_BUFFER_BIT);

        glPushMatrix(); //Start rendering phase

        glOrtho(0,600,400,0,-1,1); //Set the matrix

        glColor4ub(0,0,0,255); //Black color

        glColor4ub(255,0,0,255); //Red color

        glColor4ub(0,0,255,255); //Blue color

        glBegin(GL_QUADS); //Render the bricks

        glEnd(); //End drawing

        glPopMatrix(); //End rendering phase

        SDL_GL_SwapBuffers();

        SDL_Delay(1); //Delay / pause
    }

    SDL_Quit();

    return 0;
}


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

    for(int y = 0; y < 25; y++)
    {
        for(int x = 0; x < 80; x++)
        {
            /* 50% chance for a cell to be alive */
            //cout << "board["<<x<<"]["<<y<<"] == "<< board[x][y]<<endl;
            if(rand() % 100 < 35)
            {
                board[x][y] = 1;
            }
            else
            {
                board[x][y] = 0;
            }
            if(board[x][y] == 1)
            {
                cout << char(2);
            }
            else
            {
                cout << " ";
            }
            /*if(y == 20 & x == 10){
               cout << "(";

            }else{
            cout << ".";
            }*/
            //this is just printing out the current location it is iterating through.
            //9cout << "X: " << x << " Y: " << y << endl;

        }
        //cout << endl;
        //cout << "================================================================================";
        //cout << endl;
    }


}
void buildBoard()
{
    for(int y = 0; y < 25; y++)
    {
        for(int x = 0; x < 80; x++)
        {
            board[x][y] = 0;
            if(ghostBoard[x][y] == 1)
            {
                board[x][y] = 1;
            }
            else
            {
                board[x][y] = 0;
            }
            if(board[x][y] == 1)
            {
                setcolor(10);
                cout << char(2);
            }
            else
            {
                setcolor(2);
                cout << " ";
            }

        }
        //cout << endl;
        //cout << "================================================================================";
        //cout << endl;
    }
    cin.get();
    nextStep();
}

void nextStep()
{


    for(int y = 0; y < 25; y++)
    {
        for(int x = 0; x < 80; x++)
        {
            //cout << "board[" << x << "]" << "[" << y << "]" << endl;
            //if(board[(x-1)][(y-1)] == 1) cout << "WOOOOOT";
            state = 0;
            state = board[(x)][(y-1)] + state; // top - middle
            state = board[(x-1)][(y-1)] + state; //top - left
            state = board[(x+1)][(y-1)] + state; //top - right
            state = board[(x)][(y+1)] + state; //bottom - middle
            state = board[(x-1)][(y+1)] + state; //bottom - left
            state = board[(x+1)][(y+1)] + state; //bottom - right
            state = board[(x-1)][(y)] + state; //middle - left
            state = board[(x+1)][(y)] + state; //middle - right
            //cout << "state: " << state << " board[" << x << "][" << y << "]" << endl;
            if(state == 3) ghostBoard[x][y] = 1;
            if(state < 2) ghostBoard[x][y] = 0 ;
            //if(board[x][y] == 1){
            //if(state == 2) ghostBoard[x][y] = 1;
            //}else{
            //ghostBoard[x][y] = 0;
            //}
            if(state > 3) ghostBoard[x][y] = 0;
            state = 0;
        }
        //cout << endl;
        //cout << "================================================================================";
        //cout << endl;
    }
    //cout << endl;
    //cout << seconds << endl;
    buildBoard();
}

void setcolor(unsigned short color)                 //The function that you'll use to
{
    //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

void makeCell()
{
    glClearColor(0,0,1,1);
}

1 个答案:

答案 0 :(得分:4)

通过查看代码,有几个问题区域。在几个函数中普遍存在的主要问题是,在迭代WIDTH时,您已切换HEIGHTboard。您最终访问了一个越界索引。

另一个问题是nextStepbuildBoard以递归方式递归调用而没有退出条件。结果是最终的堆栈溢出和崩溃的程序。

您还在程序的执行生命周期内多次调用srand()。结果会比随机值少。这是一个很容易解决的小问题。

可能还有其他问题仍然存在,但修复上述内容可能会让你继续前进。