Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- image:blitRotated(x,y,angle [,cx, cy])
- Blit a rotated image, where the rotation's center is the point (cx,cy)
- - x: The X-coordinate to blit the image
- - y: The Y-coordinate to blit the image
- - angle: The angle of the rotation, in radian
- - cx: The X-coordinate of the rotation's center (where the up left corner is (0;0)), default: imageWidth/2
- - cy: The Y-coordinate of the rotation's center (where the up left corner is (0;0)), default: imageHeight/2
- ]]--
- local cos, sin = math.cos, math.sin
- local function getCoord(point, center, a)
- local t = {}
- t.x = point.x*cos(a) - center.x*cos(a) - point.y*sin(a) - center.y*sin(a) + center.x;
- t.y = point.x*sin(a) + center.x*sin(a) + point.y*cos(a) + center.y*cos(a) - center.y;
- return t;
- end
- function g2d.BlitRotated(image, x, y, angle, cx, cy)
- local center = {x = x + cx - image:width() / 2 or image:width() /2, y = y + image:height() / 2 - cy or image:height() / 2}
- --local origin = {x = image:width() / 2, y = image:height() / 2}
- local corners = {} -- All the image's corners
- corners[1] = {x = -image:width() / 2, y = image:height() / 2}
- corners[2] = {x = image:width() / 2, y = image:height() / 2}
- corners[3] = {x = image:width() / 2, y = -image:height() / 2}
- corners[4] = {x = -image:width() / 2, y = -image:height() / 2}
- local rot = {}
- for i = 1, table.getn(corners) do
- rot[i] = getCoord(corners[i], center, angle)
- end
- g2d.BeginQuads(image)
- --g2d.SetColor(Color.create(255,0,0))
- g2d.SetCoordMode(g2d.CENTER)
- for _, obj in ipairs(rot) do
- g2d.SetCoordXY(obj.x + x, obj.y + y)
- g2d.Add()
- end
- g2d.End()
- return rot
- end
- local hippo = g2d.TexLoad("discret1.png", g2d.SWIZZLE)
- while 1 do
- g2d.Clear()
- g2d.BeginRects(hippo)
- g2d.SetCoordXY(240, 136)
- --g2d.Add()
- g2d.End()
- local t = hippo:BlitRotated(240, 136, math.rad(35), 20, 20)
- if Ctrl.read():start() then break end
- Xtream.print(300, 200, tostring(math.floor(t[1].x)).." "..tostring(math.floor(t[1].y)))
- Xtream.print(300, 215, tostring(math.floor(t[2].x)).." "..tostring(math.floor(t[2].y)))
- g2d.Flip(g2d.VSYNC)
- end
- -- EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement