Thread: Compression

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    Compression

    I did a search in this section for compression and didn't find anything really useful for those of you who would like to compress a map file here is some code that may help you out.
    Code:
    ifstream asd;
    		int counter = -1, data= 0, olddata,sets=0;
    		asd.open("game.dat");
    		data = mymap[0][0];
    		olddata = data;
    		for(h = 0;h < 50;h++)
    		{
    			for(w = 0;w < 100;w++)
    			{
    				counter++;
    				data = mymap[w][h];
    				if(data != olddata)
    				{
    					asd<<counter<<" "<<olddata<<endl;
    					counter=0;
    				}
    				olddata = data;
    			}
    		}
    		asd<<"999999";
    		asd.close();
    here is a simple walkthrough of how this works.
    1) Im assuming you have your map loaded into the array mine is 100x50 squares and have incuded fstream.h
    2) we set the counter of repetitions to -1 because this code offsets it easy fix actually but i was just tring to get it to work at the time.
    3) we loop for a while counting how many times we see the same number in a row. then we save the count and what tile it was to the file and start on the new one.
    4) we keep doing that till we are done.

    Ive obtained a 40% encryption on my map which is suitable atm. if you use alot of different tiles in your maps you may not want to use this because if the number of different tiles > 50% of your map size you will be lucky to break even. But for maps with water and somewhat open areas this compression can save alot of file space. In very extreme cases you can have nearly 99.9% compression to -100% compression (tile changes every time)

    Hope this helps someone. Also note 999999 is just my way of saying "end" change to your liking
    Last edited by ~Kyo~; 10-02-2004 at 12:24 AM.

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Quote Originally Posted by ~Kyo~
    I did a search in this section for compression and didn't find anything really useful for those of you who would like to compress a map file here is some code that may help you out.
    Code:
    ifstream asd;
    		int counter = -1, data= 0, olddata,sets=0;
    		asd.open("game.dat");
    		data = mymap[0][0];
    		olddata = data;
    		for(h = 0;h < 50;h++)
    		{
    			for(w = 0;w < 100;w++)
    			{
    				counter++;
    				data = mymap[w][h];
    				if(data != olddata)
    				{
    					asd<<counter<<" "<<olddata<<endl;
    					counter=0;
    				}
    				olddata = data;
    			}
    		}
    		asd<<"999999";
    		asd.close();
    here is a simple walkthrough of how this works.
    1) Im assuming you have your map loaded into the array mine is 100x50 squares and have incuded fstream.h
    2) we set the counter of repetitions to -1 because this code offsets it easy fix actually but i was just tring to get it to work at the time.
    3) we loop for a while counting how many times we see the same number in a row. then we save the count and what tile it was to the file and start on the new one.
    4) we keep doing that till we are done.

    Ive obtained a 40% encryption on my map which is suitable atm. if you use alot of different tiles in your maps you may not want to use this because if the number of different tiles > 50% of your map size you will be lucky to break even. But for maps with water and somewhat open areas this compression can save alot of file space. In very extreme cases you can have nearly 99.9% compression to -100% compression (tile changes every time)

    Hope this helps someone. Also note 999999 is just my way of saying "end" change to your liking

    It's far easier to simply write your map to a file, and compress the file then it is to come up with a map compression and store it to a file. My suggestion is you simply write your map to a text file, and use Hauffman (spelling?) Compression (in binary) to shorten the file length. If you have a need to "encrypt" the map so some one can't come along with the simple knowledge of the compression algorythm and decode the map, then do something like:
    Write the map to a buffer,
    Alter the binary by use of a math expression
    Use the HCompression algorythem on it
    Write the buffer to a file.

    When reading it, just perform all that in reverse (inverse math expression)
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I would rather write code and see it work than use something that is already well known. Thats what makes programming fun to me at leat write something and see it go.

    EDIT: isn't this board for learning how to do stuff and not all just ask n learn?
    Last edited by ~Kyo~; 10-02-2004 at 01:41 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ifstream asd;
    Don't you mean an ofstream if you're outputting?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Quote Originally Posted by Salem
    > ifstream asd;
    Don't you mean an ofstream if you're outputting?

    o opps yea i defined that way at the start of that function lol had just gotten done with the decompression but yea its of and not if

  6. #6
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Just now noticed i didn't toss the decrompression in here lol here it is:
    Code:
    void Load()
    {
    	int cnt=0,data=0,x=0,y=0;
    	ifstream qwerty;
    	qwerty.open("game.dat");
    	do
    	{
    		qwerty>>cnt;
    		qwerty>>data;
    		if(cnt == 999999)break;
    		
    		for(cnt;cnt > 0;cnt--)
    		{
    			mymap[x][y] = data;			
    			if(x == BMPX-1)
    			{
    				x = -1;
    				y++;
    			}
    			x++;
    		}
    	}while(data != 999999);
    	qwerty.close();
    }
    BMPX is a global int for max size across

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I doubt that any map is going to be large enough to warrant compression. Images are more what I need compression for.

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    actually my map is around 100000 rooms so i can either deal with 100000 ints or alot less. Image compression isnt as hard as it sounds just use jpegs instead of bitmaps

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    JPGs are far too lossless to use them in a game for textures. They get really ugly up close.
    Your map can be stored in a different format on disk which would save a lot of space. Storing them on disk is a matter of storing room information and room coordinates - you don't even need a 2D map or 2D array at all in the game or on disk.

    You can also use a linked list method where each room actually has a pointer to the next room or connecting room.

  10. #10
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Use wads then they seem to have good compression ratios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Compression
    By Ezerhorden in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2006, 10:19 AM
  2. compression
    By X PaYnE X in forum C Programming
    Replies: 20
    Last Post: 01-11-2005, 05:14 PM
  3. efficient compression algorithm pls
    By gooddevil in forum C Programming
    Replies: 7
    Last Post: 05-08-2004, 03:33 AM
  4. Compression utility source code
    By Blizzarddog in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2003, 08:15 PM
  5. help: compression algorithms...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2002, 06:10 AM