March 01, 2021, 09:47:05 PM

Author Topic: [bmx] Run Length Encoding by zoqfotpik [ 1+ years ago ]  (Read 450 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Run Length Encoding by zoqfotpik [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Run Length Encoding
Author : zoqfotpik
Posted : 1+ years ago

Description : Run Length Encoding is a simple form of compression for strings or arrays that contain a lot of blocks of the same consecutive characters, like "1111111111155555555222222222" which would compress to
11,1
8,5
9,2

Obviously real game levels from eg. platformers offer much larger dividends and they're close to best-case, like thousand-to-one compression ratios. If you have lots and lots of little blocks you might be better off doing something else.

This expects your level to be 128x128 and in a 2D array called maparray.  It is just two functions with no demo code-- to test it just make a global maparray:int[128,128], write some data in there, then call writelevel and respond to the input on the text console.

The reason why I wrote this was for distribution of mobile games with hundreds of levels in a tiny and quick download.

Enjoy!


Code :
Code: BlitzMax
  1. Function writelevel()
  2.  
  3.         Local levname$
  4.        
  5.         levname$ = Input ("Filename:")
  6.         Local success=CreateFile(levname$)
  7.         If Not success RuntimeError "error creating file"      
  8.         Local file:TStream =WriteFile(levname$)
  9.  
  10.         Local x = 0  ' set to upper left of array
  11.         Local y = 0
  12.         Local currentrun = 0
  13.         Local currenttile = 0
  14.        
  15.         Print "trying to write file..."
  16.         While y < 127
  17.         currenttile = maparray[x,y]
  18.         x = x + 1
  19.         If x > 127
  20.                 y = y + 1
  21.                 x = 0
  22.         EndIf
  23.        
  24.         If maparray[x,y] = currenttile
  25.                 currentrun = currentrun + 1
  26.        
  27.         Else  ' run is broken
  28.                 Print "writing line"
  29.                 WriteLine file,currentrun + 1+ "," + currenttile
  30.                 currentrun = 0
  31.         EndIf
  32.         Wend
  33.         WriteLine file,"10000,1"
  34.         CloseFile file
  35. End Function
  36.  
  37. Function readlevel()
  38.                 Local levname$
  39.                 Local file:TStream
  40.                 Local lines:String[]
  41.                 Local line:String
  42.                 Local values:String[]
  43.                 Local templine:String[]
  44.                 Local teststringiter:String
  45.                 Local x:Int
  46.                 Local y:Int
  47.                 Local i:Int
  48.                
  49.                 levname$ = Input ("Filename:")
  50.                
  51.                 file=ReadFile(levname)
  52.  
  53.                 If Not file RuntimeError "could not open file " + levname
  54.                
  55.                 While Not Eof(file)
  56.                         templine = ReadLine(file).split("
  57. ")
  58.                                 templine = templine[0].split(",")
  59.                                 Print templine[0]
  60.                             printtilename(Int(templine[0]))
  61.                                 i = 0
  62.                                 Local runnumber:Int = Int(templine[0])
  63.                         Local tiletype:Int = Int(templine[1])
  64.                                 For i = 0 To runnumber-1
  65.                                 maparray[x,y]=tiletype
  66.                                     x = x + 1
  67.                                     If x > 127
  68.                                         x = 0
  69.                                         y = y + 1
  70.                                     EndIf
  71.                                  Next
  72.                 Wend
  73.                 CloseStream file
  74. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal