Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Game of life in allegro

This thread is locked; no one can reply to it. rss feed Print
Game of life in allegro
francisco diogo
Member #8,229
January 2007

HI. I have made a simulation of the game of life in C, but I wanted to make it in Allegro language.
The question is the following: my original program makes a board of 300*150 of aleatory 0 and 1, and then everytime i press enter the board transforms into another one of 0 and 1, according to preset rules.
How can i make, for example, that each 1 appears as a white square and every 0 as a black square? I know it is probably very basic by i've read many tutorials and i haven't seen it explained anywhere.
If seeing the program in C would help answer the question i can post it, its not very big.
Thank you.

CursedTyrant
Member #7,080
April 2006
avatar

I'll post what I can, based on the info you posted (just the drawing bit, tough it's not tested and can be buggy like hell):

1#include "allegro.h"
2 
3#define MAPW 300
4#define MAPH 150
5#define RECTSIZE 16
6 
7int map[MAPW][MAPH];
8 
9BITMAP *buffer;
10 
11int main()
12{
13 //init
14 //populate the map array
15
16 buffer = create_bitmap(SCREEN_W, SCREEN_H);
17
18 while (!key[KEY_ESC)
19 {
20 clear(buffer);
21
22 for (int y=0; y<MAPH; y++)
23 {
24 for (int x=0; x<MAPW; x++)
25 {
26 //I'm assuming you have a non-black background
27 if (map[x][y]==0)
28 { rectfill(buffer, x*RECTSIZE, y*RECTSIZE, (x+1)*RECTSIZE, (y+1)*RECTSIZE, makecol(0, 0, 0)); }
29 else if (map[x][y]==1)
30 { rectfill(buffer, x*RECTSIZE, y*RECTSIZE, (x+1)*RECTSIZE, (y+1)*RECTSIZE, makecol(255, 255, 255)); }
31 }
32 }
33
34 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
35 }
36 destroy_bitmap(buffer); buffer = 0;
37 //exit
38}
39END_OF_MAIN()

---------
Signature.
----
[My Website] | [My YouTube Channel]

francisco diogo
Member #8,229
January 2007

Well, the program wasn't very buggy, but i tried to run it two times and both of them the windows said there was an error and the window had to close. Anyway thanks for your post.
I'll just post now the program i have made and if someone has got the pacience, i would appreciate if he/she could give a little help, cause i have to make this as a college project: (just the main)

int main()
{
int i, j, h;
char x;

board_init();

for(h=1; h <= MAX_GEN; h++) {
board_out(h);

for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
stone(i, j);
}
}

c = getchar();
}
return 0;
}

CursedTyrant
Member #7,080
April 2006
avatar

Yes, well... I did put //init and such in the code since I thought you knew how to init all the allegro stuff.

allegro_init();
set_color_depth(desktop_color_depth());
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
install_keyboard();

---------
Signature.
----
[My Website] | [My YouTube Channel]

GullRaDriel
Member #3,861
September 2003
avatar

Since when allegro is a language ? I always thought it was a library :p

Edited

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Jonatan Hedborg
Member #4,886
July 2004
avatar

COUNTER NITPICK ATTACK FOR MASSIVE DAMAGE!
tough = thought

LennyLen
Member #5,313
December 2004
avatar

Quote:

tough = thought

Super Nitpick Combo Breaker: tough != thought.

Jonatan Hedborg
Member #4,886
July 2004
avatar

There is no defense :(

GullRaDriel
Member #3,861
September 2003
avatar

No need to defend when I see no attack :p

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

LennyLen
Member #5,313
December 2004
avatar

Quote:

No need to defend when I see no attack

It appears you need glasses. :)

GullRaDriel
Member #3,861
September 2003
avatar

I am a good guy with positive mind ;-p

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

LennyLen
Member #5,313
December 2004
avatar

It was a good natured and friendly attack (as contradictory as that sounds). :)

GullRaDriel
Member #3,861
September 2003
avatar

Héhé. We made the thread going mad.

So, do the OP is happy with the stuff and knowledge CursedTyrant gaves to him ?

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Richard Phipps
Member #1,632
November 2001
avatar

I think The Game of Life has been run on a GPU as well. 8-)

Johan Halmén
Member #1,550
September 2001

Quote:

Quote:

Quote:

tough = thought

Super Nitpick Combo Breaker: tough != thought.

No. Writing "tough = thought" means assigning thought to tough. After that one can write tough.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Onewing
Member #6,152
August 2005
avatar

Quote:

No. Writing "tough = thought" means assigning thought to tough. After that one can write tough.

Good, that's what I tough.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'd do it as:

1//8 bit version
2 
3#include <allegro.h>
4 
5RGB white = { 63, 63, 63, 0 };
6RGB black;
7 
8int color;
9 
10int main(void)
11{
12 allegro_init();
13 install_keyboard();
14 if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0))
15 {
16 set_gfx_mode(GFX_TEXT,0,0,0,0);
17 allegro_message("Bad graphic mode");
18 return -1;
19 }
20 
21 set_color(0,&black);
22 set_color(1,&white);
23 //loop would start here
24 color = some_arcane_calculation;
25 circlefill(screen,10,10,20,color);
26 readkey();
27 return 0;
28}
29END_OF_MAIN()
30 
31 
32//Truecolor version
33#include <allegro.h>
34 
35int colorvals[2];
36 
37int color;
38 
39int main(void)
40{
41 allegro_init();
42 install_keyboard();
43 set_color_depth(24);
44 
45 if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0))
46 {
47 set_gfx_mode(GFX_TEXT,0,0,0,0);
48 allegro_message("Bad graphic mode");
49 return -1;
50 }
51 
52 colorvals[0] = 0;
53 colorvals[1] = makecol(255,255,255);
54 //loop would start here
55 color = some_arcane_calculation();
56 circlefill(screen,10,10,20,colorvals[color]);
57 readkey();
58 return 0;
59}
60END_OF_MAIN()

[EDIT] I did have "putpixel()" where circlefill is but they were too small to see when quick testing that it'd actually work.

They all watch too much MSNBC... they get ideas.

Go to: