ねえ、私はまだ一般的に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);
}