Author Topic: Rotating Sprites  (Read 3652 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Rotating Sprites
« on: November 06, 2019, 03:04:14 am »
Hey everyone,
Is it possible to rotate sprites with PUT, e.g. rotating an image to a specific degree.
Or am I going to have to calculate each corner of the images coordinates?

Thanks,
Zep
« Last Edit: November 06, 2019, 07:39:38 pm by Zeppelin »
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Rotating Sprites
« Reply #1 on: November 06, 2019, 03:28:02 am »
Have a look on the examples here http://www.qb64.org/wiki/MAPTRIANGLE
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Rotating Sprites
« Reply #2 on: November 06, 2019, 07:40:58 pm »
RhoSigma,
MAPTRIANGLE doesnt seem to be working for me. Whenever I try to display the sprite, nothing appears (its just black).
Could this be that I'm not using an actual image?

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Rotating Sprites
« Reply #3 on: November 06, 2019, 08:14:14 pm »
Hi Zep,

Here is my first test code with Rotozoom mod:

Offline William33

  • Newbie
  • Posts: 5
    • View Profile
Re: Rotating Sprites
« Reply #4 on: November 08, 2019, 04:22:42 pm »
I once changed rotozoom, so it lets you draw a part of an image, rotated and scaled:

Code: QB64: [Select]
  1. SUB RotoZoom (X AS INTEGER, Y AS INTEGER, Image AS LONG, startx AS INTEGER, starty AS INTEGER, xoffset AS INTEGER, yoffset AS INTEGER, Scale AS SINGLE, Rotation AS SINGLE)
  2.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE
  3.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  4.     px(0) = (-xoffset + startx) / 2: py(0) = (-yoffset + starty) / 2: px(1) = (-xoffset + startx) / 2: py(1) = (yoffset - starty) / 2
  5.     px(2) = (xoffset - startx) / 2: py(2) = (yoffset - starty) / 2: px(3) = (xoffset - startx) / 2: py(3) = (-yoffset + starty) / 2
  6.     sinr! = SIN(-Rotation / 57.2957795131): cosr! = COS(-Rotation / 57.2957795131)
  7.     FOR i& = 0 TO 3
  8.         x2& = Scale * (px(i&) * cosr! + sinr! * py(i&)) + X: y2& = Scale * (py(i&) * cosr! - px(i&) * sinr!) + Y
  9.         px(i&) = x2&: py(i&) = y2&
  10.     NEXT
  11.     _MAPTRIANGLE (startx, starty)-(startx, yoffset - 1)-(xoffset - 1, yoffset - 1), Image& TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  12.     _MAPTRIANGLE (startx, starty)-(xoffset - 1, starty)-(xoffset - 1, yoffset - 1), Image& TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
« Last Edit: November 08, 2019, 04:26:48 pm by William33 »
Kind Regards,
William

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Rotating Sprites
« Reply #5 on: November 08, 2019, 04:43:12 pm »
I've got a simple little DisplayImage routine which I use for rotating images and such, as illustrated here:

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