LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Optimizing this function

Root / Programming Questions / [.]

TriforceOfKirbyCreated:
I made a sprite loading function that uses color palettes and I'm curious to know if anyone knows of a simpler way of doing it. Here’s an example of the DATA:
'Number of colors
DATA 6
'Color palette
DATA &H00000000
DATA &HFF000000
DATA &HFF585870
DATA &HFF8888A0
DATA &HFFB0B0C8
DATA &HFFF8F8F8
'Size
DATA 16,16
'Image
DATA &H11000000,&H00000011
DATA &H14110000,&H00001141
DATA &H01541100,&H00114510
DATA &H01355511,&H11555310
DATA &H00143455,&H55534100
DATA &H00134221,&H12243100
DATA &H00014211,&H11241000
DATA &H00014110,&H01141000
DATA &H00014110,&H01141000
DATA &H00014211,&H11241000
DATA &H00124221,&H12243100
DATA &H00132444,&H44424100
DATA &H01243211,&H11344310
DATA &H01321100,&H00112410
DATA &H12110000,&H00001131
DATA &H11000000,&H00000011
Here’s the function I'm using to read the data:
DEF LOAD_SPRITE
 'GPAGE 0,4
 'Load color palette
 VAR N_COLORS%
 READ N_COLORS%
 VAR COLOR%[N_COLORS%]
 VAR I%
 FOR I%=0 TO N_COLORS%-1
  READ COLOR%[I%]
 NEXT
 'Load image
 VAR WIDTH%,HEIGHT%
 READ WIDTH%,HEIGHT%
 VAR Y%
 FOR Y%=0 TO HEIGHT%-1
  FOR I%=0 TO CEIL(WIDTH%/8)-1
   VAR DATA%
   READ DATA%
   VAR X%
   FOR X%=0 TO 7
    GCOLOR COLOR%[(DATA% AND &HF<<(7-X%)*4)>>(7-X%)*4 AND &HF]
    GPSET I%*8+X%,Y% 'Note: Offset to allow for multiple sprites later
   NEXT
  NEXT
 NEXT
END
The line GCOLOR COLOR%[(DATA% AND &HF<<(7-X%)*4)>>(7-X%)*4 AND &HF] is one I'm looking to simplify if possible. Overall, I like the format of the DATA section; however, the image data has to be separated into 32 bit sections with padding at the end and it only allows up to 16 colors at most. I was thinking perhaps changing it to a string for each horizontal line with Base64 notation, but then there’s the issue of decoding it; which would probably work best as a separate function.

GLOAD can write image data arrays in a palletised mode if you pass a palette table array. It's fairly straightforward assuming you can convert the image data and palette into an array easily. Check the manual, it should help.

Well I wish I knew GLOAD existed before making this, lol. Oh well.