PNG Slicer
Posted: Tue Jun 24, 2025 1:21 am
I have a Question, I'm writing a Client Server Wargame engine, and the server needs to control everything. The initial Game Start the server is initialized with a "local" Game folder consisting of a JSON Game file a Map Image (PNG) file, Grid Image (PNG), and 2 Sprite Sheets both PNG.
The Transmitted Buffer Size I've Choose (for NO specific Reason other than I've always like 4K or 4096).
I began looking at ways to slice up the PNG File and came up with the following code. I take any large PNG Slice it up into an Array of memory allocations, then spin the Array and write it back to a New file, Clean up memory and it opens great. It Works, all I would need to do send these Chunks to the Game Player Clients...which I don't think will be too tough.
So the question is...Is there a better stronger faster way? Anyone's thoughts?
(You can choose ANY image file on your pc in the Code)
The Transmitted Buffer Size I've Choose (for NO specific Reason other than I've always like 4K or 4096).
I began looking at ways to slice up the PNG File and came up with the following code. I take any large PNG Slice it up into an Array of memory allocations, then spin the Array and write it back to a New file, Clean up memory and it opens great. It Works, all I would need to do send these Chunks to the Game Player Clients...which I don't think will be too tough.
So the question is...Is there a better stronger faster way? Anyone's thoughts?
(You can choose ANY image file on your pc in the Code)
Code: Select all
Define BlockCnt.i, Block, Remain.i, Length.i
#File$ = "D:\Development\PBProjects\VideoGame\Images\VideoWarGame.png"
Block = 4000
If ReadFile(0, #File$)
length = Lof(0)
*MemoryID = AllocateMemory(length)
BlockCnt = length / Block
Remain = Mod(length, Block)
Dim Chunks.i(BlockCnt+1)
If *MemoryID
For X = 0 To BlockCnt
If X < BlockCnt
Chunks(X) = AllocateMemory(Block)
ReadData(0, Chunks(X), Block)
Else
Chunks(X) = AllocateMemory(Remain)
ReadData(0, Chunks(X), Remain)
EndIf
Next
EndIf
CloseFile(0)
EndIf
;Write Chunks back into file
CreateFile(0, "D:\Development\PBProjects\VideoGame\Images\VideoWarRedo.png")
For X = 0 To BlockCnt
If X < BlockCnt
WriteData(0, Chunks(X), Block)
Else
WriteData(0, Chunks(X), Remain)
EndIf
Next
CloseFile(0)
FreeMemory(*MemoryID)
For X = 0 To BlockCnt
FreeMemory(Chunks(x))
Next