Thread: weird problems with Svgalib's vga_setcolor()

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Question weird problems with Svgalib's vga_setcolor()

    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;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your neighboring() function will break when passed an argument that is 0, since it will try to access an array element of -1. (mod'ing negative numbers is like moding the absolute value and then adding the sign back in.) This might cause random crashes and it will certainly introduce much more random values (with larger magnitude) than you were expecting.

    The entire screen flashing different colours sounds strange. I guess it could be caused because you're not syncing with the vertical retrace, but all that should do is to show a frame that is half the previous frame and half the new one.

    Other than that, do you have any way of viewing or setting the palette? It's possible that this is getting messed up somehow. How are you running this program? I didn't think modern versions of Windows would even let you switch into mode 13h anymore. (Certainly my old DOS programs don't work, but then those were compiled using DJGPP with a different library.)

    By the way, unless you have an ancient laptop or some other reason for using DOS, consider using a modern (cross-platform) library like the SDL. Lazy Foo' Productions
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Most Common problems in C
    By Bayint Naung in forum C Programming
    Replies: 18
    Last Post: 06-02-2010, 08:20 PM
  2. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  4. weird typedef struct problems
    By olidem in forum C Programming
    Replies: 3
    Last Post: 07-28-2008, 02:59 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

Tags for this Thread