Oh! Thanks! So I remove all sizeofs except those in malloc, but it still doesnt work. Wait! It doesnt go till there. Im looking now...
PS: Are you sure? Because it now hangs at the first loop! It seems like there is no pointer to array[0][0]?
Well, here the whole code:
gof.h:
Code:
#include <SDL\SDL.h>
#include <stdio.h>
SDL_Surface *screen;
SDL_Event *event;
SDL_Color *color;
SDL_Rect *rect;
int init()
{
int red, green, blue;
if (SDL_Init(SDL_INIT_EVERYTHING)) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(512, 512, 0, SDL_SWSURFACE);
if (!screen) {
fprintf(stderr, "Couldn't initialize the screen: %s\n", SDL_GetError());
return 1;
}
red = 0;
green = 255;
blue = 255;
color = SDL_MapRGB(screen->format, red, green, blue);
return 0;
}
#define Screen_Update(screen) SDL_UpdateRect((screen), 0, 0, 0, 0);
#define Draw_Cell(x, y) rect->x = (x) * 8; rect->y = (y) * 8; rect->w = 8; rect->h = 8; SDL_FillRect(screen, rect, color)
cells.h (like above)
Code:
#include <SDL\SDL.h>
#include <stdio.h>
#define GRID_SIZE 64
typedef struct {
int *states;
int *next_states;
} Cells;
int Array_Get(int *array, int position)
{
return *(array + position);
}
void Array_Set(int *array, int position, int value)
{
*(array + position) = value;
}
Cells *Cells_New()
{
Cells *cells;
cells->states = malloc(GRID_SIZE * GRID_SIZE * sizeof(int));
cells->next_states = malloc(GRID_SIZE * GRID_SIZE * sizeof(int));
memset(cells, 0, sizeof(*cells));
return cells;
}
int Cells_Get(Cells *cells, int x, int y)
{
return *(cells->states + x * GRID_SIZE + y);
}
int Cells_GetNext(Cells *cells, int x, int y)
{
return *(cells->next_states + x * GRID_SIZE + y);
}
void Cells_Set(Cells* cells, int x, int y, int value)
{
*(cells->states + x * GRID_SIZE + y) = value;
}
void Cells_SetNext(Cells *cells, int x, int y, int value)
{
*(cells->next_states + x * GRID_SIZE + y) = value;
}
int *Cells_GetRow(Cells *cells, int x)
{
return cells->states + x * GRID_SIZE;
}
int *Cells_GetNextRow(Cells *cells, int x)
{
return cells->next_states + x * GRID_SIZE;
}
gof.c:
Code:
#include <SDL\SDL.h>
#include <stdio.h>
#include "gof.h"
#include "cells.h"
Cells *Cells_Calculate(Cells *cells)
{
int x, y;
for (x = 0; x < GRID_SIZE; x++) {
printf("New Row %d\n", x);
int *prev_row = Cells_GetRow(cells, (GRID_SIZE + x - 1) % GRID_SIZE);
int *current_row = Cells_GetRow(cells, x);
int *next_row = Cells_GetRow(cells, (x + 1) % GRID_SIZE);
printf("Calculating row...\n");
for (y = 0; y < GRID_SIZE; y++) {
//printf("\tNew Cell (%d, %d) is \t%d\n", x, y, Cells_Get(cells, x, y));
printf("\tNew Cell (%d, %d)\n", x, y);
int prev_item = (GRID_SIZE + y - 1) % GRID_SIZE;
int next_item = (y + 1) % GRID_SIZE;
int neighbours = 0;
/* Ignore this fprintf please for now */
fprintf(stderr, "\n");
neighbours += Array_Get(prev_row, prev_item);
neighbours += Array_Get(prev_row, y);
neighbours += Array_Get(prev_row, next_item);
neighbours += Array_Get(current_row, prev_item);
neighbours += Array_Get(current_row, next_item);
neighbours += Array_Get(next_row, prev_item);
neighbours += Array_Get(next_row, y);
neighbours += Array_Get(next_row, next_item);
if (neighbours == 3) Cells_SetNext(cells, x, y, 1);
else if (neighbours != 2 && !Cells_Get(cells, x, y)) Cells_SetNext(cells, x, y, 0);
}
}
return cells;
}
Cells *Cells_Draw(Cells *cells)
{
int x, y;
for (x = 0; x < GRID_SIZE; x++) {
printf("NewRow %d\n", x);
for (y = 0; y < GRID_SIZE; y++) {
printf("\tNewCol (%d, %d) is \t%d\n", x, y, Cells_GetNext(cells, x, y));
if (Cells_GetNext(cells, x, y)) {
Cells_Set(cells, x, y, 1);
Draw_Cell(x, y);
}
else {
Cells_Set(cells, x, y, 0);
}
}
}
}
int main(int argc, char **argv)
{
int x, y;
Cells *cells = Cells_New(64, 64);
if (init()) {
printf("Error initializing the game. Press any key to quit...\n");
getchar();
return 1;
}
printf("Initialized\n");
cells = Cells_Draw(Cells_Calculate(cells));
printf("Calculated\n");
Screen_Update(screen);
return 0;
}