Sorry guys… back again.
I keep getting a Segmentation Fault which I can’t manage to pin down.
Here’s the source… could some of you try compiling and see what you get.
It feels to me I’ve tried every possible combination but I keep getting the
segmentation fault. I really need this prog to work.
Thanks again,
Daniel----------------------------------------------------------
#include
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fstream.h>
#include <SDL/SDL.h>
using namespace std;
class pixel_state {
public:
int R : 8, G : 8, B : 8;
pixel_state () {};
pixel_state operator + (pixel_state);
pixel_state operator += (pixel_state);
pixel_state operator = (pixel_state);
pixel_state operator / (double);
pixel_state operator * (double);
};
pixel_state pixel_state :: operator + (pixel_state param) {
pixel_state temp;
temp.R = R + param.R;
temp.G = G + param.G;
temp.B = B + param.B;
return (temp);
}
pixel_state pixel_state :: operator += (pixel_state param1) {
pixel_state temp1;
temp1.R = temp1.R + param1.R;
temp1.G = temp1.G + param1.G;
temp1.B = temp1.B + param1.B;
return (temp1);
}
pixel_state pixel_state :: operator = (pixel_state param4) {
pixel_state temp4;
temp4.R = param4.R;
temp4.G = param4.G;
temp4.B = param4.B;
return (temp4);
}
pixel_state pixel_state :: operator / (double param2) {
pixel_state temp2;
temp2.R = (unsigned int)(R / param2);
temp2.G = (unsigned int)(G / param2);
temp2.B = (unsigned int)(B / param2);
return (temp2);
}
pixel_state pixel_state :: operator * (double param3) {
pixel_state temp3;
temp3.R = (unsigned int)(R * param3);
temp3.G = (unsigned int)(G * param3);
temp3.B = (unsigned int)(B * param3);
return (temp3);
}
int random_seed () {
int rand_seed;
srand((unsigned int)time((time_t *)NULL));
rand_seed = rand();
return rand_seed;
}
int random_range(int lowest_number, int highest_number)
{
srand((unsigned int)time((time_t *)NULL));
if(lowest_number > highest_number){
int q = highest_number;
lowest_number = highest_number;
highest_number = q;
}
int range = highest_number - lowest_number + 1;
return lowest_number + int(range * rand()/(RAND_MAX + 1.0));
}
pixel_state** initialize_species (long k, int seed) {
pixel_state **array;
array = new pixel_state* [640];
for (int i=0; i<640; i++) array[i] =new pixel_state [480];
ofstream evolution("check_color.txt");
for (int n=0; n<640; n++) {
for (int l; l<480; l++) {
srand(seed+random_range(0,k));
int R_temp = (int)floorf(((float)rand()/RAND_MAX)*255);
int G_temp = (int)floorf(((float)rand()/RAND_MAX)*255);
int B_temp = (int)floorf(((float)rand()/RAND_MAX)*255);
array[n][l].R = R_temp;
array[n][l].G = G_temp;
array[n][l].B = B_temp;
evolution << R_temp << " " << B_temp << " " << G_temp << " "
<< array[n][l].R << " " << array[n][l].B << " " << array[n][l].G << “\n\n”;
}
}
evolution.close();
return array;
}
pixel_state** evolve_species (pixel_state **array, int L) {
pixel_state **array_to_evolve, counter;
array_to_evolve = new pixel_state* [640];
for (int i=0; i<160; i++) array_to_evolve[i] =new pixel_state [480];
for (int n=0; n<640; n++) {
for (int l=0; l<480; l++) {
int k;
counter.R = 0;
counter.G = 0;
counter.B = 0;
for (k=1; k<=L; k++) {
if ((n-k)<0 && (n+k)<L) counter += (array[n-k+640][l]+array[n+k][l]);
if ((n-k)<0 && (n+k)>L) counter += (array[n-k+640][l]+array[n+k-640][l]);
if ((n-k)>0 && (n+k)>L) counter += (array[n-k][l]+array[n+k-640][l]);
if ((n-k)>0 && (n+k)<L) counter += (array[n-k][l]+array[n+k][l]);
if ((l-k)<0 && (l+k)<L) counter += (array[n][l-k+480]+array[n][l+k]);
if ((l-k)<0 && (l+k)>L) counter += (array[n][l-k+480]+array[n][l+k-480]);
if ((l-k)>0 && (l+k)>L) counter += (array[n][l-k]+array[n][l+k-480]);
if ((l-k)>0 && (l+k)<L) counter += (array[n][l-k]+array[n][l+k]);
}
array_to_evolve[n][l] = ((counter + (array[n][l]*4)) / (4*k*k+3));
}
}
return array_to_evolve;
}
void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}
void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}
void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 )screen->pixels + yscreen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 )screen->pixels + yscreen->pitch/4 + x;
*bufp = color;
}
break;
}
}
void DrawScene(SDL_Surface *screen, pixel_state **array)
{
Slock(screen);
for(int x=0; x<640; x++)
{
for(int y=0; y<480; y++)
{
int Red = array[x][y].R;
int Green = array[x][y].G;
int Blue = array[x][y].B;
DrawPixel(screen,x,y,(Uint8)Red,(Uint8)Green,(Uint8)Blue);
}
}
Sulock(screen);
SDL_Flip(screen);
}
int main(int argc, char *argv[]) {
pixel_state **evo_array, **evo_array_check;
int seed;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf(“Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}
seed = random_seed();
evo_array = initialize_species(2000, seed);
while(1) {
evo_array = evolve_species(evo_array, 13);
DrawScene(screen, evo_array);
}
return 0;
}