Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Scaling and Rotating
#1
Just a simple scaling and rotating  Cool

Possible use in games, such as
- Space Invaders, Gargoyle Attack
- Tetris

Code:
'==========================================
'       SIMPLE SCALING AND ROTATING
'
'Possible use in games, such as
'- Space Invaders, Gargoyle Attack
'- Tetris
'
'Limitation
'Rotate : only multiplication of 90 degree
'==========================================


'----------------
' INITIALIZATION
'----------------
set window "Pixel Art", 150, 150, false,4
set redraw off

'Color definition
visible cBlack  = [0,0,0]
visible cGreen  = [0,255,0]

'Sprite class definition
Sprite =
[
    '--- Properties ---
    data: [
    [0,0,0,0,0,0,0,0],
    [0,0,1,0,0,1,0,0],
    [0,1,1,1,1,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0],
    [0,1,1,1,1,1,1,0],
    [0,1,0,0,0,0,1,0],
    [0,0,0,0,0,0,0,0]
    ],
   
    img: 0, x:0, y:0,    
   
    '--- Methods ---
    Transform_Scale : function(s)
                        this.img = Scale(this.data,s)
                        this.x   = (width(primary)-8*s)/2
                        this.y   = (height(primary)-8*s)/2
                       endfunc, 
    Transform_Rotate : function(s,r)
                        this.img = Rotate(this.data,s,r)
                        this.x   = (width(primary)-8*s)/2
                        this.y   = (height(primary)-8*s)/2
                       endfunc
]

'--------------
' MAIN PROGRAM
'--------------
TitleScreen()
scale   = 1
rotate  = 0
do
    'Prepare screen
    set color cBlack; cls
    set color cGreen

    'Draw Sprite
    'Sprite.Transform_Scale(scale)
    Sprite.Transform_Rotate(scale,rotate)
    draw image Sprite.img,Sprite.x,Sprite.y

    'Pause and wait until SPACE BAR is pressed, ESCAPE to quit
    set caret width(primary)/2, height(primary)-20
    center "SPACE BAR"; redraw
    do;wait 1;if keydown(KEY_ESCAPE,true) end;until keydown(KEY_SPACE,true)
    if scale < 10 then
        scale = scale + 1
    else
        scale = 1
    endif
    if rotate < 3 then
        rotate = rotate + 1
    else
        rotate = 0
    endif  

    free image Sprite.img 'free image from memory
loop

'-----------
' FUNCTIONS
'-----------
function Scale(data,s)
    img = createimage(8*s,8*s); set image img
    for y = 0 to 7
        for x = 0 to 7
            if data[y][x] then
                set color cGreen
            else 
                set color cBlack
            endif
            draw rect x*s,y*s,s,s,1
        next
    next
    set image primary; return img
endfunc

function Rotate(data,s,r)
    img = createimage(8*s,8*s); set image img
    for y = 0 to 6
        for x = 0 to 6
            if data[y][x] then
                set color cGreen
            else 
                set color cBlack
            endif
                       
            '(a,b)  = center point of rotation
            '(x,y)  is rotated into (m,n)
            'd      = angle of rotation in radian
            'm      = cos(d)*(x-a)-sin(d)*(y-b)+a
            'n      = sin(d)*(x-a)+cos(d)*(y-b)+b
            a = 3
            b = 3
            d = r*90*(22/7)/180
            m = (round((cos(d)*(x-a))-(sin(d)*(y-b))+a))
            n = (round((sin(d)*(x-a))+(cos(d)*(y-b))+b))
                     
            draw rect m*s,n*s,s,s,1
        next
    next
    set image primary; return img
endfunc

function TitleScreen()
    set caret width(primary)/2,5
    center "Scaling and"
    center "Rotating"
    set caret width(primary)/2, height(primary)-20
    center "Press ENTER"
    redraw
    do;wait 1;until keydown(KEY_RETURN,true)
endfunc
Reply
#2
Very clever - I always get so confused with the maths in code like this, so I am full of admiration for those that can write such code......well done Smile
Reply
#3
Cool!

You can also use the raster commands for scaling, flipping and 90 degree rotations. The problem is that 'draw hraster' and 'draw vraster' were created with nothing but raycasting and mode7 in mind (they draw textured horizontal and vertical lines). They treat the alpha channel as "fog density". But as long as you don't need to do colorized/additive drawing and the image data only contains pixels with full opacity or full transparency it works.

Code:
#win32

set window "Raster scaling", 640, 480
set redraw off

img = loadimage("weird.png")

a = 0 ' angle for sin scale effect.
while not keydown(KEY_ESCAPE)   
    a = (a + 0.01)%(PI*2)
    ' calculate width and height.
    w = (0.25 + 0.75*(1.0 + sin(a)))*width(img)
    h = (0.25 + 0.75*(1.0 + sin(a)))*height(img)

    set color 0, 0, 0
    cls
    ' draw centered.
    DrawImageScaled(img, (width(primary) - w)/2, (height(primary) - h)/2, w, h)

    DrawImageRotated90(img, 0, 0)
    redraw
    fwait 60
wend

function DrawImageScaled(img, x, y, w, h)
    if w <= 0 or h <= 0  return
    set color 255, 255, 255, 0
    du = 1/w
    u = 0
    for x = x to x + w - 1
        draw vraster img, x, y, y + h - 1, u, 0, u, 1
        u = u + du
    next
endfunc

function DrawImageRotated90(img, x, y)
    set color 255, 255, 255, 0
    dv = 1/height(img)
    v = 1
    for x = x to x + height(img) - 1
        draw vraster img, x, y, y + width(img) - 1, 0, v, 1, v
        v = v - dv
    next   
endfunc

Full support for drawing transformed images (scaling + rotating) is at the absolute top of my todo list. It'll run faster than this solution.


Attached Files
.zip   raster_scaling.zip (Size: 120.97 KB / Downloads: 6)
Reply
#4
This is SO cool... Thank you.
Logic is the beginning of wisdom.
Reply
#5
With so much mathematics I have not understood anything. Big Grin Huh
Reply
#6
@Marcus ... thank you for sharing your "Raster Scaling" code. I'm trying to use your code to make the earth looks bigger and bigger. It works !

@Kevin ... I am actually taking a look at the balls' movement in your "Pool Game", and it's far more advanced in trigonometry. I should learn a lot from you then.

@Johnno... One piece of coding a day, keeps my brain awake 

@Aliensoldier... I'll give you more maths in the code below  Big Grin


Code:
'====================================================
' THE EARTH AND THE SUN
' The earth is bigger and bigger
'
' Author's Note :
' -The Sun https://giphy.com/gifs/nasa-sun-nasagif-1oLeAsuP5UfPK1KR85
' -The Earth https://news.utk.edu/2023/01/25/the-conversation-how-has-the-inside-of-the-earth-stayed-as-hot-as-the-suns-surface-for-billions-of-years/
' -DrawImageScaled() was created by Marcus
'====================================================

'----------------
' INITIALIZATION
'----------------
set window "The Earth and The Sun",800,600
set redraw off
myfont  = createfont("Arial", 30, 1,1,0,0)

'color definition
black = [0,0,0]
white = [255,255,255]
visible white_alpha = [255,255,255,0]

'Sprite class definition
Sprite =[
    '--- Properties ---
    img:0, x:0, y:0, w:0, h:0, f:0,   
   
    '--- Methods ------   
    Draw:function(x,y,w,h)
            set color white_alpha; du = 1/w; u = 0
            for x = x to x + w - 1
                draw vraster this.img, x, y, y + h - 1, u, 0, u, 1; u = u + du
            next     
         endfunc
]

'Initial Value
earth       = copy(Sprite)
earth.img   = loadimage("img/earth1.png")

sun         = copy(Sprite)
sun.img     = loadimage("img/sun1.png",10,3)
sun.x       = (width(primary)-width(sun.img))/2
sun.y       = (height(primary)-height(sun.img))/2

'--------------
' MAIN PROGRAM
'--------------
for angle = 0 to 15*PI step 0.1
    'Clear screen
    set color black;cls; set color white
   
    'Just random Stars in the background
    for j = 1 to 20
        r = rnd(2) 
        draw ellipse rnd(width(primary)),rnd(height(primary)),r,r,1
    next

    'Animate the sun's sprite sheet
    i = i%30
    draw image sun.img,sun.x,sun.y,i
    i = i + 1

    'Moving earth, bigger and bigger
    earth.w =  width(earth.img)*(0.25+earth.f)
    earth.h =  height(earth.img)*(0.25+earth.f)
    earth.f =  earth.f + 0.008
    radius  =  5 + 2 * angle   
    earth.x =  radius * cos(angle) + (width(primary)-earth.w)/ 2
    earth.y =  radius * sin(angle) + (height(primary)-earth.h)/ 2
    earth.Draw(earth.x, earth.y, earth.w, earth.h)
   
    redraw
    fwait 20
next

'Ending text
set color white; set font myfont;
set caret width(primary)/2, height(primary)/2; center "To be Continued..."
temp=rln()'pause

'Clear memory
free image sun.img
free image earth.img


Attached Files
.zip   TheEarth.zip (Size: 995.8 KB / Downloads: 3)
Reply
#7
What? No aliens? lol... Very cool... Nicely done!
Logic is the beginning of wisdom.
Reply
#8
(03-01-2024, 04:33 AM)johnno56 Wrote: What? No aliens?  lol... Very cool... Nicely done!

The aliens are coming in a sleepy tunnel  Big Grin
Please take a look at TUNNEL THE GAME
https://naalaa.com/forum/thread-89-post-511.html#pid511
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)