hey still getting grip on c++ in general wanted try implementing better graphics game here using sdl. issue is closing program when should staying open , running functions in background..
i have no idea why doing , know linked lib's correctly.
#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; /* different color codes 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 holds 4 elements { //elements used x , y position of brick , width , 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 window sdl_wm_setcaption( "our first game", null ); //size of window sdl_setvideomode(600,400,32, sdl_opengl ); //specific clear color glclearcolor(1,1,1,1); //red,green,blue,alpha //what portion of screen display glviewport(0,0,600,400); //shader model - use glshademodel(gl_smooth); //2d rendering glmatrixmode(gl_projection); //"save" glloadidentity(); //disable depth checking gldisable(gl_depth_test); std::cout << "opengl running\n"; std::cout << "main loop has started\n"; //handles main loop bool isrunning = true; //for handling event sdl_event event; float width = 80; //width of rectangle float height = 20; //height of rectangle //main game loop while ( isrunning ) { //events while ( sdl_pollevent(&event) ) { //if window closed if ( event.type == sdl_quit ) { isrunning = false; } //if button released , button 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 presed down buttons { } else if ( event.type == sdl_keyup ) //checking released buttons { } //logic should happen event makeboard(); nextstep(); } //rendering screen glclear(gl_color_buffer_bit); glpushmatrix(); //start rendering phase glortho(0,600,400,0,-1,1); //set 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 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 random number generator specified seed */ for(int y = 0; y < 25; y++) { for(int x = 0; x < 80; x++) { /* 50% chance cell 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 printing out current location 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 you'll use { //set colour handle hcon = getstdhandle(std_output_handle); setconsoletextattribute(hcon,color); } void makecell() { glclearcolor(0,0,1,1); }
from looking @ code there couple of problem areas. major 1 that's pervasive in several functions you've switched width
, height
when iterating through board
. end accessing out-of-bound index.
the other issue nextstep
, buildboard
recursively call each other no exit condition. result eventual stack overflow , crashing program.
you're calling srand()
more once during execution lifetime of program. you'll less random values result. it's minor issue that's easy fix.
there other problems still lurking fixing above outlined should on way.
Comments
Post a Comment