Copy a 2D Sprite to an Image?

Just starting out? Need help? Post your questions and find answers here.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Copy a 2D Sprite to an Image?

Post by Shannara »

Does anybody know the quickest way to copy a 2D sprite (not 3d), to an Image?

Such as something like ..

tmpImage.l

tmpImage = CopyFromSprite(SpriteID)

?

Or some way to get a pointer to a sprite, so then I can use CatchImage?

tmpImage = CatchImage(#PB_ANY, SpritePointer)
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

Post by waffle »

SpareImage=CreateSprite(#PB_Any,TileWidth,TileHeight)
spop=SpriteOutput(SpareImage)
StartDrawing(spop)
DrawImage(UseImage(OKSpot),0,0,TileWidth,TileHeight)
StopDrawing()
OKSpot=SpareImage

....
But this assumes you know the image used to create the sprite ...
It does resize the original image to match the size of the new sprite.
Another option would be to past the sprite to your screen, and then take
a screen shot of that spot .....
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

@Shannara
This should be a fast way: :D

Code: Select all

Procedure CreateBitmapFromSprite(Sprite)
  hDC=StartDrawing(SpriteOutput(Sprite))
  bmp.BITMAP\bmWidth=SpriteWidth(Sprite)
  bmp\bmHeight=SpriteHeight(Sprite)
  bmp\bmPlanes=1
  bmp\bmBitsPixel=GetDeviceCaps_(hDC,#BITSPIXEL)
  bmp\bmBits=DrawingBuffer()
  bmp\bmWidthBytes=DrawingBufferPitch()
  hBmp=CreateBitmapIndirect_(bmp)
  StopDrawing()
  ProcedureReturn hBmp
EndProcedure

;Example:
InitSprite()

OpenScreen(800,600,16,"TEST")

LoadSprite(1,"C:\Programme\PureBasic\Examples\Sources\Data\Background.bmp",#PB_Sprite_Memory)

ImageID=CreateBitmapFromSprite(1)

StartDrawing(ScreenOutput())
DrawImage(ImageID,0,0,800,600)
StopDrawing()

FlipBuffers()
Delay(2000)

DeleteObject_(ImageID);Don't forget to free the bitmap
regards
Stefan
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

Post by waffle »

wow
thanks, I'll add that code and a thankyou into my grab bag of code...

so, if I was using a sprite to combine various effects, I would then copy it
to memory ...

Result = CopySprite(#Sprite1, #Sprite2 [, Mode])
and then call your function so that the sprite would be
copyied to an image, the image could then be resized and
then used for other sprites or saved ...

cool.
Code is good... Its an international language.
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post by griz »

Cool, thanks S.m. :)

How about moving an image back to a sprite?
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

@griz
Here, is a procedure which makes a sprite out of a image. :wink:

Code: Select all

Procedure CreateSpriteFromBitmap(Sprite,ImageID)
GetObject_(ImageID,SizeOf(BITMAP),bmp.BITMAP)
CreateSprite(Sprite,bmp\bmWidth,bmp\bmHeight)
MemDC=CreateCompatibleDC_(0)
OldBitmap=SelectObject_(MemDC,ImageID)
hDC=StartDrawing(SpriteOutput(Sprite))
BitBlt_(hDC,0,0,bmp\bmWidth,bmp\bmHeight,MemDC,0,0,#SRCCOPY)
SelectObject_(MemDC,OldBitmap)
DeleteDC_(MemDC)
StopDrawing()
EndProcedure



;Example:
InitSprite()
OpenScreen(800,600,16,"CreateSpriteFromBitmap()")

ImageID=LoadImage(1,"C:\Programme\PureBasic\Examples\Sources\Data\Background.bmp")

CreateSpriteFromBitmap(1,ImageID)

DisplaySprite(1,0,0)

FlipBuffers()
Delay(1000)
regards
Stefan
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post by griz »

Thanks S.M. :!: :D
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

S.M.:

Is there a way to do that without using the harddrive, like in pure memory? Right now, due to the OpenWindowedScreen bug in PBWin 3.93, I have to do a work around and am looking for a way to do so without accessing the harddrive twice.

Right now I use vBin (version 1) to place all my images into a packed file. Then I catch the images into an Image variable (placeholder), this is in the beginning of the program before openwindowedscreen is called. Then, once the open screen is called, Im looking for a way to direct (memory) copy the image onto an empty sprite ..

In PBWIN 3.92, I can openwindowedscreen on an hidden regular window, and it will not show until I unhide the window (normal), and because of that, I was able to catch the sprites directly from the pack files ... in 3.93, I can no longer do that.

[/url]
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

Hi Shannara
Is there a way to do that without using the harddrive, like in pure memory? Right now, due to the OpenWindowedScreen bug in PBWin 3.93, I have to do a work around and am looking for a way to do so without accessing the harddrive twice.
Neither CreateSpriteFromBitmap() nor CreateBitmapFromSprite() use the harddisk. :wink:
In PBWIN 3.92, I can openwindowedscreen on an hidden regular window, and it will not show until I unhide the window (normal), and because of that, I was able to catch the sprites directly from the pack files ... in 3.93, I can no longer do that.
You can do it this way, till Fred fix this ugly bug:

Code: Select all

InitSprite()
OpenWindow(1,0,0,640,480,#PB_Window_SystemMenu|#PB_Window_Invisible,"DX")
OpenWindowedScreen(WindowID(),0,0,640,480,0,0,0)
ShowWindow_(WindowID(),#SW_HIDE) ;HideWindow(WindowID(),1) crashes !

Delay(2500)

ShowWindow_(WindowID(),#SW_SHOW)

Repeat
   
ClearScreen(0,0,128)
FlipBuffers()    
    
Until WindowEvent()=#PB_Event_CloseWindow
The only problem is that the window is shown for a few milliseconds. :roll:

regards
Stefan
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Thanks for the info :) Sorry about taking so long for a reply.
Post Reply