How to make a mirror of a 2d animation?

Blitz3D Forums/Blitz3D Beginners Area/How to make a mirror of a 2d animation?

Insane Games(Posted 2003) [#1]
How can i do a mirror of a animation (like a mario game that goes right and left with the same moves and animation)? It's possible to use just one image with all frames and make a mirror just when needed?
Tks!

MB


Ross C(Posted 2003) [#2]
It is possible, but not in real time. You would have to do this before the game started, as it's pretty slow.


Insane Games(Posted 2003) [#3]
So it's better to use 2 file images?


Beaker(Posted 2003) [#4]
I suspect it is perfectly possible in realtime for most game types, but you may as well pre-calc the animation. Either, 2 image files, or one with both sets of animation in it.


Ross C(Posted 2003) [#5]
Hold on, i'm trying to whip up a function that will do this :)


Ross C(Posted 2003) [#6]
Hey, got it. whessssssh.

Try this out. What you do is call the function, and give it the name of the animated image you want to mirror and the number of frames the animation currently has.

Say to mirror an image called player, call the function like so.
player=mirror_image(player,5)


this will cause the image player to have the mirrored frames added onto the end.

here's the function
Function mirror_image(image_name,frames#)
mirrorimage=CreateImage(ImageWidth(image_name),ImageHeight(image_name),frames*2)
For loop=0 To frames-1
	CopyRect 0,0,ImageWidth(image_name),ImageHeight(image_name),0,0,ImageBuffer(image_name,loop),ImageBuffer(mirrorimage,loop)
Next

LockBuffer ImageBuffer(image_name)
LockBuffer ImageBuffer(mirrorimage)
For floop=frames To (frames*2)-1
	For loop=0 To ImageWidth(image_name)-2

		For loop1=0 To ImageHeight(image_name)-2

			CopyPixel ImageWidth(image_name)-1-loop,loop1,ImageBuffer(image_name,floop-frames),loop,loop1,ImageBuffer(mirrorimage,floop)
		Next
	Next
Next
UnlockBuffer ImageBuffer(mirrorimage)
UnlockBuffer ImageBuffer(image_name)
Return mirrorimage
End Function


Any problems with it, just post back :o)


Perturbatio(Posted 2003) [#7]
Here's one I wrote as well :)
It will not only flip each frame, but have them positioned in the same order as the originals.

Graphics 640,480,32,2

;setting these here so that I can modify this later to work with commandline or drag n drop
Global ImageName$ = "elfmoves.bmp"
Global FrameWidth% = 32
Global FrameHeight% = 32
Global FirstFrame% = 0
Global tempImage = LoadImage(ImageName)
Global FrameCount% = ImageWidth(tempImage)/FrameWidth
Global OutImageName$ = "new_" + Left(ImageName$, Len(ImageName$)-Len(ExtractFileExt(ImageName$))-1)+".bmp"
Global XOffset = FrameCount * FrameWidth

	FreeImage tempImage
	tempImage = 0

;images
Global inImage = LoadAnimImage(ImageName, FrameWidth , FrameHeight, FirstFrame, FrameCount)
Global NewImage = CreateImage((FrameWidth * FrameCount)*2, FrameHeight, FrameCount * 2)
tempImage = CreateImage(FrameWidth, FrameHeight)


;first, copy the original frames to the new image
SetBuffer ImageBuffer(NewImage)
For Count = 0 To FrameCount -1
	
	DrawImage inImage, Count * FrameWidth, 0, Count
		
Next



;next copy and flip the frames and place them in the new image in the same sequence
StartTime = MilliSecs()

For Count = 0 To FrameCount -1
	SetBuffer ImageBuffer(tempImage)
	Cls
	DrawImage inImage, 0, 0, Count
	tempImage = FlipImageH(tempImage)
	SetBuffer ImageBuffer(NewImage)
	DrawImage tempImage, XOffset + (Count * FrameWidth), 0
Next
EndTime = MilliSecs() - StartTime
;SAVE THE IMAGE
SaveImage(NewImage, OutImageName$)


;DISPLAY THE END RESULTS
SetBuffer BackBuffer()

DrawImage NewImage, 0,0

Text 0, ImageHeight(NewImage), "Done - file saved as: " + OutImageName$
Text 0, ImageHeight(NewImage) + 24, "Time taken to convert:" + EndTime + "ms"

;END OF PROGRAM

WaitKey()

FreeImage NewImage
FreeImage inImage
FreeImage tempImage

EndGraphics

End

;FUNCTION FlipImageH
;Accepts an image, flips it horizontally (using CopyRect), then returns the resulting image

Function FlipImageH(srcImage)
;LOCAL VARS
Local Width = ImageWidth(srcImage)
Local Height = ImageHeight(srcImage)
Local imgTemp = CreateImage(Width, Height)

;MAIN

For X = 0 To ImageWidth(srcImage)-1
	CopyRect X, 0, 1, Height, (Width-1) - X, 0, ImageBuffer(srcImage), ImageBuffer(imgTemp)
Next

Return imgTemp

End Function

;FUNCTION ExtractFileName
;Accepts a filepath and returns the filename

Function ExtractFileName$(sFilePath$)
;LOCAL VARS
Local iStartPos% = 0
Local iSearchPos% = 0
Local iFilePathLength = 0
Local sFileName$ = ""
	
;BEGIN FUNCTION CODE
iFilePathLength = Len(sFilePath$)
iSearchPos% = iFilePathLength
 
While (iStartPos% < 1) And (iSearchPos% > 1)

	iStartPos% = Instr(sFilePath$, "\", iSearchPos%)
	iSearchPos% = iSearchPos% - 1
	
Wend

If iStartPos = 0 Then ;if the filepath contains no backslashes
	sFileName$ = sFilePath$
Else
	sFileName$ = Right$(sFilePath$, iFilePathLength% - iStartPos%)
EndIf


Return sFileName$

End Function

;FUNCTION ExtractFileExt
;Accepts a filepath and returns the extension for the file

Function ExtractFileExt$(sFilePath$)
;LOCAL VARS
Local iStartPos% = 0
Local iSearchPos% = 0
Local iFilePathLength = 0
Local sFileExt$ = ""
	
;BEGIN FUNCTION CODE
iFilePathLength = Len(sFilePath$)
iSearchPos% = iFilePathLength
 
While (iStartPos% < 1) And (iSearchPos% > 1)

	iStartPos% = Instr(sFilePath$, ".", iSearchPos%)
	iSearchPos% = iSearchPos% - 1
	
Wend

If iStartPos = 0 Then ;if the filepath contains no .
	sFileExt$ = sFilePath$
Else
	sFileExt$ = Right$(sFilePath$, iFilePathLength% - iStartPos%)
EndIf


Return sFileExt$

End Function

;FUNCTION ExtractFilePath
;Accepts a filepath with filename and returns only the path
;i.e. pass c:\temp\test.txt the return value will be c:\temp\

Function ExtractFilePath$(sFilePath$)
;LOCAL VARS
Local iStartPos% = 0
Local iSearchPos% = 0
Local iFilePathLength = 0
Local sFileExt$ = ""
	
;BEGIN FUNCTION CODE
iFilePathLength = Len(sFilePath$)
iSearchPos% = iFilePathLength
 
While (iStartPos% < 1) And (iSearchPos% > 1)

	iStartPos% = Instr(sFilePath$, "\", iSearchPos%)
	iSearchPos% = iSearchPos% - 1
	
Wend

If iStartPos = 0 Then ;if the filepath contains no backslashes
	sFileExt$ = sFilePath$
Else
	sFileExt$ = Left$(sFilePath$, iStartPos%)
EndIf


Return sFileExt$

End Function



ford escort(Posted 2003) [#8]
oops...bad reply , think he wanted to miror a part of the screen for water effects..


Perturbatio(Posted 2003) [#9]
I don't think so.

How can i do a mirror of a animation (like a mario game that goes right and left with the same moves and animation)? It's possible to use just one image with all frames and make a mirror just when needed?




Ross C(Posted 2003) [#10]
@ Perturbatio

:o) Mine will return the frames in that order too :P

hehe


Insane Games(Posted 2003) [#11]
Ross C, i try to use your function... but i really don't know why doesn't work ...
I use a image to the right movement of the player going right in the screen, but when i used the function to mirror that image it doesnt change.

The Perturbatio code i used and it worked fine! Thanks, but i've tried just the first function(create a new file), now i'll try the FlipImageH function.

Thanks both you!!!

MB


Ross C(Posted 2003) [#12]
I'll look into it (dam he's got one up on me now !) :D


Insane Games(Posted 2003) [#13]
How i draw the player(your example) in my loop?

MB


Ross C(Posted 2003) [#14]
In mine you would just draw it like a normal image

DrawImage image,200,200,frame


That should do it. All my function does is mirror the image and add it on to the existing animated image.


Insane Games(Posted 2003) [#15]
AAAAH!!!
Your function resize the old image (temp) and put all the frames after the last? That's it?

Please help with other question
I'm using this to control my animation:
If MilliSecs() > tmrPlayer + 200 Then
tmrPlayer=MilliSecs() ; zera o timer
If parado = 1
frmPlayer = ( frmPlayer + 1 ) Mod 3 ; waiting
Else
frmPlayer = 4 + ( frmPlayer + 1 ) Mod 3 ;walking
EndIf
End If

How can i control other frames? (if parado =1 than the animation goes 1 to 4 else, goes 5 to 8...)
If i had an animation in frames 20 to 25 how do i use?
Thanks!


Ross C(Posted 2003) [#16]
Well, i would have an array of all the start locations. If there all on the same image that is.

i'll do more when i get home. :) College is just finished so i need to get home :)


Perturbatio(Posted 2003) [#17]
As long as you know the positions of the beginning of the move anims in the original image, all you need do is offset them by the number of frames in the original image.

i.e. if your original image is your character walking left, and it takes 10 frames, once you run it through my little program, the new image will be 20 frames, and you can just add 10 to make the character walk right.


Ross C(Posted 2003) [#18]
well, he beat me too it :)


EOF(Posted 2003) [#19]
You can use the TFormImage command to flip images.
This example flips the image horizontally:
Const sw=640,sh=480

Graphics sw,sh,0,2
SetBuffer BackBuffer()

img=LoadImage("arrow.bmp")

DrawImage img,50,50
TFormImage img,-1,0,0,1 ; flip the image horizontally
DrawImage img,250,50

WaitKey
End
It also works on 2D animations.
For extra speed use TFormFilter False before doing any transformations.