[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Tiling a Rotated Bitmap
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Tiling a Rotated Bitmap
- From: "Brian Fisher" <brian@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 2 Apr 2008 11:58:35 -0700
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Wed, 02 Apr 2008 14:58:43 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; bh=VEOovcxHtMbdVEvnrCwc4/0H/g9CgxZ37AiOEXMSog4=; b=XfZy3GzWcj9ONf/H0wkkmDF0HNqTl6RCNoMX5392qgO8zzBewcRLjH9nTKtWZ+J7LisTTmFB+JN200lrE9+Umx101t0k0wKuAATKl9sS+CQwFn7YqGHyC4N//JQOVxf8yobYYizA4X/NzjHaPGtnVHaDaEbRfnHu8VPOYEsSD+A=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=qTfcUdIyO2/UuHG70aPtBPpLFr9dkAYVWR0xHiv1SdogjJHOcI0kcQU89xGf4yfN00p0Zab6/L4wBEeNe+McAjDJc+UhmvXBNgTc3AI78Y3m4oVDiwZ8SCcolH/Qoy7pqxkWXF3ODI5DW46q83arSaDAyYJ4UXUBLiI16tg1iYg=
- In-reply-to: <47F3D81F.7040704@xxxxxxxxxxxx>
- References: <47F2F604.7000204@xxxxxxxxxxxx> <47F3BC3E.70603@xxxxxxxxxxxx> <47F3D81F.7040704@xxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
there were 3 problems with the thing above
---
the major problem was that sin & cos work in a space where +y is up,
pygame in a space where +y is down
a minor problem was that the four corners will never be visible at all
(think about the surface rotate produces - the bounds go through the
four corners of the rotated image)
a problem that actually isn't a problem (once it was tiling okay) was
that it kept drawing over itself (rotsurf.blit(rotsruf))
also as a note, you can tile a rotated image on a rotated grid (as
this code demonstrates) but their are complexities with filtering (as
I mentioned earlier)
but if you want to make a rectangular tile that tiles on a rectangular
grid from your rotated image, then that is more complicated (unless
the rotation is 45 and the original image square - which is what Aaron
was talking about), but I would guess it can be done, but may result
in a disgustingly large tile (you'd have to keep tiling the image on
the rotated grid until the edge lines line up and cut a rect at that
point)
anyways, here's the corrected code
----
import pygame
import math
filename=r"MockUpMemory.png"
SCREENWIDTH = 800
SCREENHEIGHT = 600
def Rotated(surf, degrees = 0):
# rotate surface
print 'Rotated ', degrees, ' degrees.'
rotsurf = pygame.transform.rotate(surf, degrees)
# compute the offsets needed
c = math.cos(math.radians(degrees))
s = math.sin(math.radians(degrees))
widthx = (surf.get_width() - 1) * c
widthy = (surf.get_width() - 1) * s
heightx = (surf.get_height() - 1) * s
heighty = (surf.get_height() - 1) * c
# the 8 positions, corners first
positions = [
# (widthx + heightx, widthy + heighty),
# (widthx - heightx, widthy - heighty),
# (-widthx + heightx, -widthy + heighty),
# (-widthx - heightx, -widthy - heighty),
# (widthx, widthy),
(widthx, -widthy),
# (-widthx, -widthy),
(-widthx, widthy),
# (heightx, heighty),
(heightx, heighty),
# (-heightx, -heighty),
(-heightx, -heighty),
]
rot_copy = rotsurf.copy()
# apply the blits
for pos in positions:
rotsurf.blit(rot_copy, pos)
return rotsurf
def main():
pygame.init()
bg = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), 0, 32)
quit = 0
pic = pygame.image.load(filename).convert_alpha()
pic2 = pic
while not quit:
bg.fill((255, 0, 0, 255))
bg.blit(pic2, (0, 0))
pygame.display.flip()
for e in pygame.event.get():
if e.type == pygame.QUIT:
quit = 1
break
elif e.type == pygame.MOUSEMOTION:
pic2 = Rotated(pic, e.pos[0])
pygame.quit()
main()