Author Topic: Scale-able Sprite from DATA  (Read 3611 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Scale-able Sprite from DATA
« on: April 04, 2019, 04:38:03 am »
Hey,
Here is a small program that makes sprites off the DATA command and multi-dimensional arrays, but also allows scaling of those sprites :)
Code: QB64: [Select]
  1. DATA 0,15,0,15,0
  2. DATA 15,0,15,0,15
  3. DATA 15,0,15,0,15
  4. DATA 0,15,0,15,0
  5. DATA 15,0,0,0,15
  6.  
  7. DIM SHARED sprite(4, 4)
  8.  
  9. scale = 25 'Change me to scale your sprite
  10.  
  11. FOR y = 0 TO 4
  12.     FOR x = 0 TO 4
  13.         READ sprite(x, y) 'Read the data
  14.     NEXT x
  15.  
  16. FOR y = 0 TO 4 * scale STEP scale 'Factor in the scale
  17.     FOR x = 0 TO 4 * scale STEP scale
  18.         LINE (x, y)-(x + scale, y + scale), sprite(x / scale, y / scale), BF 'Draw each pixel
  19.     NEXT x

Zep
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Scale-able Sprite from DATA
« Reply #1 on: April 04, 2019, 10:20:27 am »
Hi Zeppelin,

I made a mod to generalize and demo loading and drawing a sprite any where at any scale:
Code: QB64: [Select]
  1. 'DATA 0,15,0,15,0
  2. 'DATA 15,0,15,0,15
  3. 'DATA 15,0,15,0,15
  4. 'DATA 0,15,0,15,0
  5. 'DATA 15,0,0,0,15
  6.  
  7. DATA 0,0,0,0,0,0,0,0,0,0,0,0
  8. DATA 1,1,1,1,0,0,0,0,1,0,0,0
  9. DATA 1,0,0,0,1,0,0,0,1,0,0,0
  10. DATA 1,0,0,0,1,0,0,0,1,0,0,0
  11. DATA 1,1,1,1,0,0,1,1,1,1,1,0
  12. DATA 1,0,0,0,1,0,0,0,1,0,0,0
  13. DATA 1,0,0,0,1,0,0,0,1,0,0,0
  14. DATA 1,1,1,1,0,0,0,0,1,0,0,0
  15.  
  16. SCREEN _NEWIMAGE(800, 600, 32)
  17. COLOR _RGB32(0, 160, 0), _RGB32(0, 0, 0)
  18. 'SCREEN 13
  19. CONST spriteMaxX = 11
  20. CONST spriteMaxY = 7
  21. DIM SHARED sprite(spriteMaxX, spriteMaxY)
  22.  
  23. 'scale = 25 'Change me to scale your sprite
  24.  
  25. FOR y = 0 TO spriteMaxY
  26.     FOR x = 0 TO spriteMaxX
  27.         READ sprite(x, y) 'Read the data
  28.     NEXT x
  29.  
  30. ds = 1
  31.     'up and down the scales
  32.     scale = scale + ds
  33.     IF scale > 35 THEN ds = -ds
  34.     IF scale < 1 THEN scale = 1: ds = -ds
  35.     CLS
  36.     drawSprite 200 * RND, 100 * RND, scale
  37.     _DISPLAY
  38.     _LIMIT 3
  39.  
  40. SUB drawSprite (x, y, scale)
  41.     FOR yy = 0 TO spriteMaxY
  42.         FOR xx = 0 TO spriteMaxX
  43.             'Factor in the scale
  44.             IF sprite(xx, yy) THEN LINE (xx * scale + x, yy * scale + y)-STEP(scale, scale), , BF 'Draw each pixel
  45.         NEXT
  46.     NEXT
  47.  

This is only a beginning. We could create an array of spriteType and give the drawSprite sub an index to that array. Which means we could load and draw tons of sprites.

Also the data could have codes for different colors (which you might have started with the 15's). For setting that up, could use a sprite editor to create data statements for an app. Might be nice project.
« Last Edit: April 04, 2019, 10:50:15 am by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Scale-able Sprite from DATA
« Reply #2 on: April 04, 2019, 12:05:39 pm »
What to say? :-D

Code: QB64: [Select]
  1. REDIM SHARED Sprite(0, 0) AS _BYTE
  2. REDIM SHARED Frame(0) AS LONG
  3. CONST TimeDelay = 1 / 25
  4. DIM SHARED OldTime
  5.  
  6.  
  7. r:
  8. DATA "/","|","\","-"
  9. FOR V = 1 TO 4
  10.     READ r$
  11.     TextToArray (r$)
  12.     AS_FRAME
  13.  
  14. T$ = "Nice!"
  15. TextToArray (T$)
  16.  
  17. ds = 1
  18. SCREEN _NEWIMAGE(800, 600, 32)
  19. COLOR _RGB32(0, 160, 0), _RGB32(0, 0, 0)
  20. 'SCREEN 13
  21.  
  22.  
  23.     scale = scale + ds
  24.     IF scale > 35 THEN ds = -ds
  25.     IF scale < 1 THEN scale = 1: ds = -ds
  26.     CLS
  27.     drawSprite 200 * RND, 100 * RND, scale
  28.     ViewFram 190, 190, scale * 10
  29.     _DISPLAY
  30.     _LIMIT 10
  31.  
  32. SUB drawSprite (x, y, scale)
  33.     FOR yy = 0 TO UBOUND(sprite, 2) - 1
  34.         FOR xx = 0 TO UBOUND(sprite, 1) - 1
  35.             'Factor in the scale
  36.             IF Sprite(xx, yy) THEN LINE (xx * scale + x, yy * scale + y)-STEP(scale, scale), , BF 'Draw each pixel
  37.         NEXT
  38.     NEXT
  39.  
  40.  
  41.  
  42. SUB TextToArray (text AS STRING)
  43.     virtual& = _NEWIMAGE(1024, 50, 256)
  44.     a = _DEST
  45.     _DEST virtual&
  46.     _FONT 8, virtual&
  47.     PRINT text
  48.     REDIM Sprite(_PRINTWIDTH(text, virtual&), 50) AS _BYTE
  49.     FOR y = 1 TO 50
  50.         FOR x = 1 TO _PRINTWIDTH(text, virtual&)
  51.             _SOURCE virtual&
  52.             IF POINT(x, y) THEN Sprite(x, y) = 1
  53.     NEXT x, y
  54.     _DEST a: _SOURCE a
  55.     _FREEIMAGE virtual&
  56.  
  57. SUB AS_FRAME
  58.     REDIM _PRESERVE Frame(UBOUND(frame) + 1) AS LONG
  59.     Frame(UBOUND(frame)) = _NEWIMAGE(50, 50, 256)
  60.     a = _DEST
  61.     _DEST Frame(UBOUND(frame))
  62.     drawSprite 1, 1, 5
  63.     _DEST a
  64.  
  65. SUB ViewFram (x, y, size)
  66.     SHARED FrameNr
  67.     IF OldTime + TimeDelay < TIMER THEN FrameNr = FrameNr + 1: OldTime = TIMER
  68.     IF FrameNr > UBOUND(Frame) THEN FrameNr = LBOUND(Frame) + 1
  69.     _PUTIMAGE (x, y)-(x + size, y + size), Frame(FrameNr), 0 :REM This is repaired
  70.  
  71.  
« Last Edit: April 04, 2019, 01:20:51 pm by Petr »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Scale-able Sprite from DATA
« Reply #3 on: April 04, 2019, 12:13:27 pm »
LOL.
Best reply ever.
QB64 is the best!

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Scale-able Sprite from DATA
« Reply #4 on: April 04, 2019, 04:42:43 pm »
Petr: Nice! HAhahahahaha.
bplus: Nice ;) addition.

Zep
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.