sprite sheet coordinates

Advanced game related topics
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

sprite sheet coordinates

Post by J. Baker »

I just recently added cocos2D (code) export for Sprite Monkey and now I'm working on a PureBasic export. What do you think of this for a DataSection?

Code: Select all

Restore elfRun
  Read.l fra
   ;Debug fra
  For Frames = 1 To fra
   For coordinates = 1 To 4
     Read.l frameCoords
      Debug frameCoords
   Next
    ;If Frames = fra ;<--- these four lines make it loop
      ;Restore elfRun
      ;Frames = 1
    ;EndIf
  Next
  
  Debug " "
  
Restore elfRunF5 ;<--- here we just capture Frame 5 coordinates
   For coordinates = 1 To 4
     Read.l frameCoords
      Debug frameCoords
   Next
    
  End  
  
DataSection
  ;SpriteSheet0: IncludeBinary "/Users/jbaker/Desktop/elf_run.png" ;there's an option to change the sprite # in Sprite Monkey (SpriteSheet0)
 elfRun: ;image name altered and minus the extension
  Data.l 15 ;frames or the number of times to call Read.l without going into another label
 elfRunF1: 
  Data.l 0, 0, 128, 128 ;frame 1 coordinates, x, y, width, height
 elfRunF2: 
  Data.l 128, 0, 128, 128
 elfRunF3: 
  Data.l 256, 0, 128, 128
 elfRunF4: 
  Data.l 384, 0, 128, 128
 elfRunF5: 
  Data.l 0, 128, 128, 128
 elfRunF6: 
  Data.l 128, 128, 128, 128
 elfRunF7: 
  Data.l 256, 128, 128, 128
 elfRunF8: 
  Data.l 384, 128, 128, 128
 elfRunF9: 
  Data.l 0, 256, 128, 128
 elfRunF10: 
  Data.l 128, 256, 128, 128
 elfRunF11: 
  Data.l 256, 256, 128, 128
 elfRunF12: 
  Data.l 384, 256, 128, 128
 elfRunF13: 
  Data.l 0, 384, 128, 128
 elfRunF14: 
  Data.l 128, 384, 128, 128
 elfRunF15: 
  Data.l 256, 384, 128, 128
EndDataSection
Screen shot of what the options look like. Everything is working except for the PureBasic export. Just waiting for feedback...
Image
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: sprite sheet coordinates

Post by wilbert »

Aren't the sprite images on a sheet all equal in size ?
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: sprite sheet coordinates

Post by J. Baker »

I just modified the above code to capture a single frame!
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: sprite sheet coordinates

Post by J. Baker »

wilbert wrote:Aren't the sprite images on a sheet all equal in size ?
Yes. Do you have another method?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: sprite sheet coordinates

Post by wilbert »

You could just calculate the offset.
With a zero based frame number something like this

Code: Select all

Structure sheet_info
  width.u; sprite width
  height.u; sprite height
  cols.u
EndStructure

elfRun_sheet.sheet_info
elfRun_sheet\width = 128
elfRun_sheet\height = 128
elfRun_sheet\cols = 4

Procedure.l offsetX(*sheet.sheet_info, frame)
  ProcedureReturn (frame % *sheet\cols) * *sheet\width
EndProcedure

Procedure.l offsetY(*sheet.sheet_info, frame)
  ProcedureReturn (frame / *sheet\cols) * *sheet\height
EndProcedure

Debug offsetX(elfRun_sheet, 6)
Debug offsetY(elfRun_sheet, 6)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: sprite sheet coordinates

Post by wilbert »

Another approach if you want to specify all coordinates.

Code: Select all

Structure frame
  x.u
  y.u
  width.u
  height.u
EndStructure

Structure sheet
  num_frames.u
  frame.frame[0]
EndStructure

*elfRun.sheet = ?elfRun

Debug *elfRun\num_frames
Debug *elfRun\frame[5]\x

DataSection
 elfRun: ;image name altered and minus the extension
  Data.u 15 ;frames
  Data.u 0, 0, 128, 128 ;frame 0 coordinates, x, y, width, height
  Data.u 128, 0, 128, 128
  Data.u 256, 0, 128, 128
  Data.u 384, 0, 128, 128
  Data.u 0, 128, 128, 128
  Data.u 128, 128, 128, 128
  Data.u 256, 128, 128, 128
  Data.u 384, 128, 128, 128
  Data.u 0, 256, 128, 128
  Data.u 128, 256, 128, 128
  Data.u 256, 256, 128, 128
  Data.u 384, 256, 128, 128
  Data.u 0, 384, 128, 128
  Data.u 128, 384, 128, 128
  Data.u 256, 384, 128, 128
EndDataSection
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: sprite sheet coordinates

Post by J. Baker »

Cool wilbert, I like that method too!

But one question. Why is offsetX equal 256? You're talking about the top-left coordinates of the frame, right?

EDIT: Man you're fast. Looking through your second example now. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: sprite sheet coordinates

Post by J. Baker »

Ok, I see what you are doing. You're starting off by frame 0, as where I call it frame 1.

Both examples are very nice. Now I have some thinking to do. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply