Below is a program I have more-or-less finished coding. However, there's a weird error: instead of drawing each pixel in its correct color, the entire screen flashes different colors.
This program is meant to be a clone of the 'Demon' screensaver that comes default with most Linux distros. The program starts with a 320-by-200 matrix of numbers between 0 and 4. It then draws this to the screen, and steps three times, before repeating. In each step, it makes a copy of the map, except every 1 that's adjacent to a 2 gets replaced with a 2 in the new map, and likewise for 2 and 3, 3 and 4, 4 and 0, 0 and 1. Then the map is overwritten with the modified copy.
What this is supposed to look like: the map starts looking like noise, with lots of multicolored pixel. Then black regions 'eat' white regions, white regions 'eat' red regions, etc., so eventually the screen shows psychedelic patterns - spirals, large contiguous regions of colours, etc..
The code:
Code:#include <stdlib.h> #include <ctime> #include <unistd.h> #include <vga.h> int map[320][200]; void init_map() { srand(time(0)); for (int i = 0; i < 320; i++) for (int j = 0; j < 200; j++) map[i][j] = random() % 5; //any other number in place of 5 should also work } void show_map() { for (int i = 0; i < 320; i++) { for (int j = 0; j < 200; j++) { vga_setcolor(map[i][j]); vga_drawpixel(i, j); } } } bool neighboring(int x, int y, int col) { //returns whether there is an element of col adjacent to map[x][y] for (int i = -1; i <= 1; i++) for (int j = -1; j <= 1; j++) if (map[(x+i)%320][(y+j)%200] == col) return 1; return 0; } void step() { //update the map, by first creating a new map, then overwriting map[][] with it int nmap[320][200]; //the new map for (int i = 0; i < 320; i++) for (int j = 0; j < 200; j++) { int k = map[i][j]; if (neighboring(i, j, k)) nmap[i][j] = k + 1; else nmap[i][j] = k; } for (int i = 0; i < 320; i++) for (int j = 0; j < 200; j++) map[i][j] = nmap[i][j]; } int main() { vga_init(); vga_setmode(G320x200x256); init_map(); while (not vga_getkey()) { sleep(1); // removing this may cause seizures :s show_map(); step(); step(); step(); } vga_setmode(TEXT); return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks


