0

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

我真的不知道它为什么这样做,我知道我正确地链接了我的库。

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

1 回答 1

4

通过查看您的代码,有几个问题区域。在多个功能中普遍存在的主要功能是您已经切换WIDTHHEIGHT在迭代board. 您最终访问了一个超出范围的索引。

另一个问题是在没有退出条件的情况下递归地相互调用nextStepbuildBoard结果是最终的堆栈溢出和程序崩溃。

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

可能还有其他问题仍然存在,但解决上述问题应该会让您顺利上路。

于 2013-07-15T08:31:48.377 回答