/* copyright 2010 Jezra J Lickter * Licensed GPLv3 /*compile with valac --pkg sdl game_of_life.vala -o game_of_life */ using SDL; public class Cell { public Rect on_screen_rect; public Rect self_rect; public Surface surface; public weak Screen screen; public bool alive {get;set;} public bool updated; public int16 size; private uint32 life_color; private uint32 dead_color; public Cell(Screen s, uint32 flags,int16 x, int16 y, int16 size,uint32 lcolor,uint32 bg_color ) { life_color = lcolor; dead_color = bg_color; updated=false; screen = s; on_screen_rect.w = size; on_screen_rect.h = size; on_screen_rect.x = x; on_screen_rect.y = y; self_rect.w = size; self_rect.h = size; self_rect.x=0; self_rect.y=0; this.size = (int16)size; alive=false; //create the surface surface = new Surface.RGB(flags, size, size, 32, 0,0,0, 255); } public void set_life(bool life) { alive=life; if (alive) { surface.fill(null,life_color); }else{ surface.fill(null,dead_color); } } public void draw() { surface.blit(self_rect,screen,on_screen_rect); } } public class Game { private const int SCREEN_WIDTH = 800; private const int SCREEN_HEIGHT = 600; private const int SCREEN_BPP = 32; private const int DELAY = 100; private weak SDL.Screen screen; private bool do_loop; private Cell[,] cells; private int16 num_cols; private int16 num_rows; private uint32 bg_color; private int random_max; public Game(int16 cell_size, int random_max) { this.random_max = random_max; //initialize the video uint32 surface_flags = SurfaceFlag.DOUBLEBUF | SurfaceFlag.HWACCEL | SurfaceFlag.HWSURFACE; screen = Screen.set_video_mode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, surface_flags); if (screen == null) { GLib.error ("Could not set video mode."); } //we have a screen, define some colors bg_color=screen.format.map_rgb(0,0,0); uint32 life_color=screen.format.map_rgb(0,255,0); SDL.WindowManager.set_caption ("Game of Life: Vala, SDL", ""); //make all of the cells //'''fill the screen with rectangles''' num_cols = (int16)SCREEN_WIDTH/cell_size; num_rows = (int16)SCREEN_HEIGHT/cell_size; uint32 cells_to_create = num_cols*num_rows; int16 y; int16 x; cells = new Cell[num_rows,num_cols]; stdout.printf( "%0.f cells to create\n",cells_to_create); for ( int16 row=0; row? dead_array = new Array(false,false,(uint)sizeof(int)); Array? alive_array = new Array(false,false,(uint)sizeof(int)); bool alive; int ln; for(r=0; r3 )) { //this cell will die; boohoo dead_array.append_val(r); dead_array.append_val(c); } else if(!alive && ln==3) { //this cell is now alive; damn zombies alive_array.append_val(r); alive_array.append_val(c); } } } //kill what needs killing uint final_index = dead_array.length-1; for(index=0 ; index