I kept pouring over the SDL sections of the code. There was no problem with SDL code, just the plain vanilla C++ code. Happy cells.clear(); to all.
I went through the Python version of this program and figured out what functions would have to be ported to get a bare bones working version of the program. It looks like just a few:
- countNeighbors
- cellNextgen
- updateCAMatrix
- a 2D vector to hold the ruleset information.
One thing I learned after writing the paper and pencil code is that using c++ vector <bool> is probably not a good idea. Too bad, it looks so good on paper.
#include <iostream>
#include <vector>
#include <cstdlib>
#include "SDL/SDL.h"
using namespace std;
// Globals
const int XRES = 1024;
const int YRES = 768;
const int BLOCKSIZE = 4;
const int DELTA_T = 250;
const int FCOLOR = 0x0000FF00;
const int BCOLOR = 0x00000000;
const int ROWS = YRES / BLOCKSIZE;
const int COLS = XRES / BLOCKSIZE;
int randomStart(vector& cells, vector > & world) {
srand(time(NULL));
SDL_Rect p;
int pct;
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
pct = rand() % 1000;
if (pct < 200)
{
p.x = col * BLOCKSIZE;
p.y = row * BLOCKSIZE;
p.w = BLOCKSIZE;
p.h = BLOCKSIZE;
cells.push_back(p);
world[row][col] = 1;
}
else
world[row][col] = 0;
}
}
return 0;
}
int main() {
vectorcells;
vector< vector> world(ROWS, vector (COLS,0));
// initialize SDL
SDL_Init(SDL_INIT_VIDEO);
// populate the world
//randomStart(cells, world); // pass by reference
// set the title bar
SDL_WM_SetCaption("Cellular Automata", "Cellular Automata");
// create window
SDL_Surface* screen = SDL_SetVideoMode(XRES, YRES, 0, SDL_DOUBLEBUF);
// Create background and block
SDL_Surface* bg = SDL_CreateRGBSurface(SDL_SWSURFACE,XRES, YRES, 32, 0, 0, 0, 0);
SDL_FillRect(bg, NULL, BCOLOR);
// Display the bank screen
SDL_BlitSurface(bg, NULL, screen, NULL);
SDL_Flip(screen);
// creak the block image for a live cell
SDL_Surface* block = SDL_CreateRGBSurface(SDL_SWSURFACE, BLOCKSIZE - 2, BLOCKSIZE - 2, 32, 0, 0, 0, 0);
SDL_FillRect(block, NULL, FCOLOR);
SDL_Event event;
bool gameover = false;
// message pump
while (!gameover)
{
// look for an event
if (SDL_PollEvent(&event)) {
// an event was found
switch (event.type) {
// close button clicked
case SDL_QUIT:
gameover = true;
break;
// handle the keyboard
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
gameover = true;
break;
}
break;
}
}
randomStart(cells, world);
// draw the background
SDL_BlitSurface(bg, NULL, screen, NULL);
// blit the blocks
for (unsigned int i = 0; i < cells.size(); ++i)
SDL_BlitSurface(block, NULL, screen, &cells[i]);
// empty the vector of cells
cells.clear();
// update screen
SDL_Flip(screen);
}
// free the background surface
SDL_FreeSurface(bg);
// cleanup SDL
SDL_Quit();
return 0;
}
No comments:
Post a Comment