Author Topic: A question about _INFLATE$\_DEFLATE$ usage  (Read 2803 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
A question about _INFLATE$\_DEFLATE$ usage
« on: December 08, 2021, 04:23:35 pm »
I was looking into using these commands for my World Builder program to help with the saved map size issue it is facing now. Recent changes, things I just did last night and today, have ballooned the file size to several megs.

Looking at the WIKI the commands take string values. Now my map data is made up of UDTs and integer arrays. How would I go about using these commands to compress the map size when saved?
Granted after becoming radioactive I only have a half-life!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: A question about _INFLATE$\_DEFLATE$ usage
« Reply #1 on: December 08, 2021, 04:27:39 pm »
I would make a MEM block and then store all of your map data in it. Then, make a string equal to the size of the memory block. Get the contents of the memory block using MEMGET and store it in the string. Use DEFLATE$ on the string. Now you've compressed the raw map data and can store it elsewhere. Basically use INFLATE$ and reverse the process to unpack.
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A question about _INFLATE$\_DEFLATE$ usage
« Reply #2 on: December 08, 2021, 06:02:10 pm »
Like so:

Code: QB64: [Select]
  1. Type DataType 'A datatype to use as an example
  2.     x As String * 112
  3.     y As Long
  4.     z As Long
  5.  
  6. Dim junk As DataType
  7. junk.x = "Hello World.Hello World.Hello World.Hello World.Hello World.Hello World.Hello World.Hello World.Hello World."
  8. junk.y = 1 '
  9. junk.z = 65 'A
  10.  
  11. C$ = Compression(junk)
  12. Print "120 bytes compressed down to "; Len(C$); "bytes."
  13. Print "And our restored data (as an uncompressed string) is:"
  14.  
  15. Print "And our restored data (as an our variable type foo) is:"
  16. Dim foo As DataType
  17. DeCompression foo, C$
  18. Print foo.x
  19. Print foo.y
  20. Print foo.z
  21.  
  22.  
  23.  
  24. Function Compression$ (t As DataType) 'And here, we want to set up the actual sub to work with our example datatype.
  25.     Dim m As _MEM
  26.     m = _Mem(t)
  27.     temp$ = Space$(m.SIZE)
  28.     _MemGet m, m.OFFSET, temp$
  29.     Compression$ = _Deflate$(temp$)
  30.     _MemFree m
  31.  
  32. Sub DeCompression (t As DataType, temp$) 'And here, we want to set up the actual sub to work with our example datatype.
  33.     Dim m As _MEM
  34.     m = _Mem(t)
  35.     _MemPut m, m.OFFSET, _Inflate$(temp$)
  36.     _MemFree m
  37.  
  38.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!