Author Topic: Re: Spinning Sprite  (Read 1727 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Spinning Sprite
« on: December 10, 2018, 12:45:42 pm »
I tend to use my handy little DisplayImage routine for this type of task. 

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(256, 256, 32)
  2.  
  3. Sprite = TextToImage("EXAMPLE", 16, &HFFFF0000, 0, 0)
  4.  
  5.  
  6.     CLS
  7.     DisplayImage Sprite, 128, 128, angle, 0
  8.     angle = angle + 4
  9.     _DISPLAY
  10.     _LIMIT 30
  11.  
  12.  
  13.  
  14.  
  15.  
  16. FUNCTION TextToImage& (text$, font&, fc&, bfc&, mode AS _BYTE)
  17.     'text$ is the text that we wish to transform into an image.
  18.     'font& is the handle of the font we want to use.
  19.     'fc& is the color of the font we want to use.
  20.     'bfc& is the background color of the font.
  21.  
  22.     'Mode 1 is print forwards
  23.     'Mode 2 is print backwards
  24.     'Mode 3 is print from top to bottom
  25.     'Mode 4 is print from bottom up
  26.     'Mode 0 got lost somewhere, but it's OK.  We check to see if our mode is < 1 or > 4 and compensate automatically if it is to make it one (default).
  27.  
  28.     IF mode < 1 OR mode > 4 THEN mode = 1
  29.     dc& = _DEFAULTCOLOR: bgc& = _BACKGROUNDCOLOR
  30.     D = _DEST
  31.     F = _FONT
  32.     IF font& <> 0 THEN _FONT font&
  33.     IF mode < 3 THEN
  34.         'print the text lengthwise
  35.         w& = _PRINTWIDTH(text$): h& = _FONTHEIGHT
  36.     ELSE
  37.         'print the text vertically
  38.         FOR i = 1 TO LEN(text$)
  39.             IF w& < _PRINTWIDTH(MID$(text$, i, 1)) THEN w& = _PRINTWIDTH(MID$(text$, i, 1))
  40.         NEXT
  41.         h& = _FONTHEIGHT * (LEN(text$))
  42.     END IF
  43.  
  44.     TextToImage& = _NEWIMAGE(w&, h&, 32)
  45.     _DEST TextToImage&
  46.     IF font& <> 0 THEN _FONT font&
  47.     COLOR fc&, bfc&
  48.  
  49.     SELECT CASE mode
  50.         CASE 1
  51.             'Print text forward
  52.             _PRINTSTRING (0, 0), text$
  53.         CASE 2
  54.             'Print text backwards
  55.             temp$ = ""
  56.             FOR i = 0 TO LEN(text$) - 1
  57.                 temp$ = temp$ + MID$(text$, LEN(text$) - i, 1)
  58.             NEXT
  59.             _PRINTSTRING (0, 0), temp$
  60.         CASE 3
  61.             'Print text upwards
  62.             'first lets reverse the text, so it's easy to place
  63.             temp$ = ""
  64.             FOR i = 0 TO LEN(text$) - 1
  65.                 temp$ = temp$ + MID$(text$, LEN(text$) - i, 1)
  66.             NEXT
  67.             'then put it where it belongs
  68.             FOR i = 1 TO LEN(text$)
  69.                 fx = (w& - _PRINTWIDTH(MID$(temp$, i, 1))) / 2 + .99 'This is to center any non-monospaced letters so they look better
  70.                 _PRINTSTRING (fx, _FONTHEIGHT * (i - 1)), MID$(temp$, i, 1)
  71.             NEXT
  72.         CASE 4
  73.             'Print text downwards
  74.             FOR i = 1 TO LEN(text$)
  75.                 fx = (w& - _PRINTWIDTH(MID$(text$, i, 1))) / 2 + .99 'This is to center any non-monospaced letters so they look better
  76.                 _PRINTSTRING (fx, _FONTHEIGHT * (i - 1)), MID$(text$, i, 1)
  77.             NEXT
  78.     END SELECT
  79.     _DEST D
  80.     COLOR dc&, bgc&
  81.     _FONT F
  82.  
  83.  
  84.  
  85.  
  86.  
  87. SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, angle AS SINGLE, mode AS _BYTE)
  88.     'Image is the image handle which we use to reference our image.
  89.     'x,y is the X/Y coordinates where we want the image to be at on the screen.
  90.     'angle is the angle which we wish to rotate the image.
  91.     'mode determines HOW we place the image at point X,Y.
  92.     'Mode 0 we center the image at point X,Y
  93.     'Mode 1 we place the Top Left corner of oour image at point X,Y
  94.     'Mode 2 is Bottom Left
  95.     'Mode 3 is Top Right
  96.     'Mode 4 is Bottom Right
  97.  
  98.  
  99.     DIM px(3) AS INTEGER, py(3) AS INTEGER, w AS INTEGER, h AS INTEGER
  100.     DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
  101.     w = _WIDTH(Image): h = _HEIGHT(Image)
  102.     SELECT CASE mode
  103.         CASE 0 'center
  104.             px(0) = -w \ 2: py(0) = -h \ 2: px(3) = w \ 2: py(3) = -h \ 2
  105.             px(1) = -w \ 2: py(1) = h \ 2: px(2) = w \ 2: py(2) = h \ 2
  106.         CASE 1 'top left
  107.             px(0) = 0: py(0) = 0: px(3) = w: py(3) = 0
  108.             px(1) = 0: py(1) = h: px(2) = w: py(2) = h
  109.         CASE 2 'bottom left
  110.             px(0) = 0: py(0) = -h: px(3) = w: py(3) = -h
  111.             px(1) = 0: py(1) = 0: px(2) = w: py(2) = 0
  112.         CASE 3 'top right
  113.             px(0) = -w: py(0) = 0: px(3) = 0: py(3) = 0
  114.             px(1) = -w: py(1) = h: px(2) = 0: py(2) = h
  115.         CASE 4 'bottom right
  116.             px(0) = -w: py(0) = -h: px(3) = 0: py(3) = -h
  117.             px(1) = -w: py(1) = 0: px(2) = 0: py(2) = 0
  118.     END SELECT
  119.     sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
  120.     FOR i = 0 TO 3
  121.         x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
  122.         px(i) = x2: py(i) = y2
  123.     NEXT
  124.     _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  125.     _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  126.  

You can see from the main loop how simple usage is for it:

DO
    CLS
    DisplayImage Sprite, 128, 128, angle, 0
    angle = angle + 4
    _DISPLAY
    _LIMIT 30
LOOP UNTIL _KEYHIT




And TextToImage isn't needed for it to work in any manner.  I just tend to use it to plug in a quick method to convert text to an image, so folks can use it instead of needing to download any resource files.
« Last Edit: December 10, 2018, 12:47:12 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #1 on: December 10, 2018, 01:08:32 pm »
@[banned user] your code already seems to be spinning the loaded image around an imaginary vertical axis:

Gravação de Tela 2018-12-10 às 16.06.00.gif

If that's not what you originally intended then maybe you really wanted it to rotate instead of spin like Steve has demoed.

What is it that you actually wanted to do?


FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #2 on: December 10, 2018, 01:26:07 pm »
Maybe you can show an example of something spinning in the fashion you're targeting because your verbal explanation isn't doing anything to help me visualize it.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Spinning Sprite
« Reply #3 on: December 10, 2018, 01:40:58 pm »
[banned user] please paste working code in the top post. (include the MAP function?)

bet i can help ya
You're not done when it works, you're done when it's right.

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #4 on: December 10, 2018, 01:42:03 pm »
Here:

Code: QB64: [Select]
  1. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  2.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  3.  

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #5 on: December 10, 2018, 02:05:56 pm »
I am not sure how to show an example

I searched YouTube for spinning logo

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #6 on: December 10, 2018, 02:20:57 pm »
Quote
If it REALLY spun around then you would have the OGOL (letters reversed) and then that would spin around to show the word LOGO (Letters normal).

And you think your original code was doing that and that's what you wanted to change? Still trying to figure you out here.

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #7 on: December 10, 2018, 02:43:06 pm »
So you want a 3d model to spin in 3d. Not with _LOADIMAGE, not with that little code, indeed.

Likely not with QB64 unless you resort to OpenGL. Hopefully STx will be more useful (and cheerful) here.

Best of luck!

/me out.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Spinning Sprite
« Reply #8 on: December 10, 2018, 03:24:31 pm »
I am not sure how to show an example

I searched YouTube for spinning logo


Yeah and the first one seemed okay because the front and back were mirror image if eachother, but the word LOGO was all wrong.  If it REALLY spun around then you would have the OGOL (letters reversed) and then that would spin around to show the word LOGO (Letters normal).

From Fellippe’s screenshot, that’s what it’s doing.  The BR is printed forward, and then reversed, and then it goes back to BR again.  I’m not certain what it’s supposed to do differently either.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #9 on: December 10, 2018, 07:51:53 pm »
No files provided.

Offline ForSe

  • Newbie
  • Posts: 6
    • View Profile
Re: Spinning Sprite
« Reply #10 on: December 18, 2018, 09:48:34 am »
Do you mean like this?
 
Card Rotation 6-8-2555-10-59-35.jpg


I made something like that a few years ago (also with _MAPTRIANGLE).

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Spinning Sprite
« Reply #11 on: December 25, 2018, 03:48:00 pm »
Hi
following [banned user] example at the beginning of the post and using that image of note I get an image that rotates itself on a vertical axis from the left to the right for 180° and then comes back!

While in your fantastic GIF (how do you make gif from your screen?) shows a real 360° rotating image of Brazil flag.
Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: Spinning Sprite
« Reply #12 on: December 25, 2018, 04:24:03 pm »
(how do you make gif from your screen?)

I use a screen capture software (QuickTime in my case) then I use an online tool to convert the video to GIF.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Spinning Sprite
« Reply #13 on: December 25, 2018, 05:50:23 pm »
@Fellippe
Thanks
it's cool!


@[banned user]

IMHO it seems that using simmetrical images in front of vertical axis you can get an appareance of rotating of 360°...
but with asimmetrical images they seems to rotate only of 180° on the plan and then coming back.

Looking at it deeper I see that the values getting back from MAP function are ranging from 0 to 256 so it seems that with this function you can get a progressive value in a range.
The _PUTIMAGE keywork IMHO cannot give more than flipping in the 180° the image on the screen...
see here http://qb64sourcecode.com/Task16.html at this section (after parallax)
Quote
- Using _PUTIMAGE to Alter Images-

The next feature of _PUTIMAGE we'll discuss is how to use it to change the size of an image, flip it horizontally, vertically or both. _PUTIMAGE supports using two coordinates to identify where the upper left and lower right corners of the image should be. By manipulating these coordinates _PUTIMAGE can alter the image.

so really with that code you shrink and then stretch the image loaded  flip it on the vertical axis.

In this way you must get the Anti-image on a hide _newimage and then using it to get the rest of rotation to get 30° rotation.

Good Luck
« Last Edit: December 25, 2018, 07:05:00 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee