Uncompressed Game Graphics

General FreeBASIC programming questions.
Post Reply
nehakakar
Posts: 7
Joined: Jul 04, 2023 6:42
Location: India
Contact:

Uncompressed Game Graphics

Post by nehakakar »

Looking for advice on storing uncompressed graphics efficiently for a game project. Bitmaps seem too bulky, and while a pallet/tile system could work, the conversion process seems labor-intensive. Are there alternative methods to minimize file size without resorting to JPEG or GIF formats for that I'm using jpeg compressor this is the great or there is another method? Appreciate any suggestions.
badidea
Posts: 2592
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Uncompressed Game Graphics

Post by badidea »

PNG work well for certain graphics. Not sure what the best PNG library for freebasic is at the moment however.
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: Uncompressed Game Graphics

Post by xlucas »

I made a little Targa library to load and save graphics files natively, that is, in pure FreeBasic. I frequently use it when I don't want to incorporate 3rd party libraries into my programs, especially small programs for which so much code doesn't make much sense to add. I've posted this library somewhere here, but I can find it or it again if necessary. Of course, these Targa images are compressed, just not requiring lots of (3rd-party) code and not lossy.

If you want speed, therefore, truly uncompressed images, and your images are all the same width, placing your images vertically in the "palette" will allow you to read them faster than if they're side by side (though the difference is minimal) and it'd be easier to write to code to read them. If you are loading the whole image palette into memory and then picking the images from there instead of from the file, the difference becomes even less important. But if you have a huge amount of images, having them in a file vertically would allow you to load them "randomly", which can be very convenient.

So this very much depends on what you prioritise, whether speed, space, amount of code, amount of work if you're going to do it yourself.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Uncompressed Game Graphics

Post by dodicat »

Both compress and uncompress are available in the windows fb distribution (zlib1.dll)
They are in the bin/win32 and bin/win64 folders.
I have put in a path to my 64 bit dll and loaded the dll at runtime.
Here is an example to save much space.

Code: Select all

Const path="C:\Users\wlhut\Desktop\fb\fb10.1\FreeBASIC-1.10.1-winlibs-gcc-9.3.0\FreeBASIC-1.10.1-winlibs-gcc-9.3.0\bin\win64\zlib1.dll"

'load the dll and get the symbols:
Var L=Dylibload(path)
Dim Shared compressBound As Function(Byval sourceLen As Ulong) As Ulong
Dim Shared uncompress As Function(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
Dim Shared compress As Function(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
If L=0 Then Print "dll not found":Sleep:End
compressBound = Dylibsymbol(L,"compressBound")
If compressBound=0 Then Print "symbol compressbound not found":Sleep:End
uncompress= Dylibsymbol(L,"uncompress")
If uncompress=0 Then Print "symbol uncompress not found":Sleep:End
compress=Dylibsymbol(L,"compress")
If compress=0 Then Print "symbol compress not found":Sleep:End


Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

Function savefile(filename As String,p As String) As String
    Dim As Long n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:Sleep:End
    End If
    Return filename
End Function

Function loadfile(file As String) As String
    Dim As Long  f=Freefile
    If Open (file For Binary Access Read As #f)=0 Then
        Dim As String text
        If Lof(f) > 0 Then
            text = String(Lof(f), 0)
            Get #f, , text
        End If
        Close #f
        Return text
    Else
        Print file;" not found":Sleep:End
    End If
End Function
'=========================================================
Screen 20,32
Dim As Any Ptr i=Imagecreate(1024,768)
Circle i,(512,400),200,Rgb(0,0,200),,,,f
Draw String i,(500,400),"BITMAP"
Bsave "screen20.bmp",i
Var f=loadfile("screen20.bmp")
Var cf=pack(f)
Print "size of compressed  bitmap" ;Len(cf)
Kill "screen20.bmp"
savefile("screen20.bmp.compressed",cf)
Print "check your folder for size of compressed file (screen20.bmp.compressed), then press any key"
Sleep
Var f2=loadfile("screen20.bmp.compressed")
Var upf2=unpack(f2)
savefile("screen20.bmp",upf2)
If Bload("screen20.bmp")=0 Then
    Print "size of actual bitmap ";Len(loadfile("screen20.bmp"))
Else
    Print "Failed to bload"
End If

Sleep
Imagedestroy(i)
Kill  "screen20.bmp"

 
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: Uncompressed Game Graphics

Post by angros47 »

I remind you that PNG is a compressed format.
If you want to store uncompressed images, bitmap is the format, and yes, it's bulky. I suppose the reason you don't want to use jpeg format is the loss in quality that comes with it: this happens because jpeg is a so-called lossy compression format. Likely, what you are looking for is not an uncompressed format, but a lossless compressed format: if you want to look for information, you should look for it. PNG is one of those formats.
Post Reply