Page 1 of 1

How to use a image as background of a sprite(solved)

Posted: Fri Feb 11, 2011 5:10 am
by goldmate
Hello every one!
I use sprite operation over a image gadget in purebasic, but sprite background become pure black and see not background image after running the command of openwindowscreen.


How to do that I can display background image below the sprite.

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 8:59 am
by blueznl
Please add a little sample code.

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 10:43 am
by goldmate
Code :

UseJPEGImageDecoder()
IncludeFile "Common.pb"
Declare.b getcoord(x.w,y.w)
Declare pressgo(x.b,y.b)
Declare putoff(x.b,y.b)
steps.w=1
Define.b xindex=0,yindex=0
Dim qp.b(18,18)
Define thisx.w,thisy.w

; open window 0

open_window_0()

;SetClassLongPtr_(WindowID(#window_0), #GCL_HBRBACKGROUND, hBrush)
InitSprite()
OpenWindowedScreen(WindowID(0),319,39,650,650,0,0,0)
#MinX=319:#MaxX=968
#MinY=39:#MaxY=688
;Result = ExamineMouse()

CreateSprite(0,30,30)
CreateSprite(1,30,30)
StartDrawing(SpriteOutput(0))
LoadSprite(0,"blackman.bmp")
StopDrawing()
StartDrawing(SpriteOutput(0))
LoadSprite(1,"whiteman.bmp")
StopDrawing()
TransparentSpriteColor(0,RGB(128,128,128))
TransparentSpriteColor(1,RGB(128,128,128))

Repeat
event=WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case #Image_0
EventType=EventType()
If eventtype=#PB_EventType_LeftClick
thisx= WindowMouseX(0)
thisy= WindowMouseY(0)
If getcoord(thisx,thisy)
If qp(xindex,yindex)=0
pressgo(xindex,yindex)
steps+1
Else
putoff(xindex,yindex)
EndIf
EndIf
EndIf
EndSelect
Case #WM_PAINT

EndSelect

Until event= #PB_Event_CloseWindow

End

Procedure.b getcoord(x.w,y.w)
Shared qp(),xindex,yindex
Define.f xx,yy
x-#minx
y-#miny
xx=x/(#maxx-#minx)*18
yy=y/(#maxy-#miny)*18
If ((xx-Int(xx))>0.8 Or (xx-Int(xx))<0.2) And ((yy-Int(yy)>0.8) Or (yy-Int(yy))<0.2)
xindex=Int(xx+0.5)
yindex=Int(yy+0.5)
If xindex>=0 And xindex<19 And yindex>=0 And yindex<19
If qp(xindex,yindex)=0
ProcedureReturn 1
Else
ProcedureReturn 1
EndIf
EndIf
EndIf
EndProcedure

Procedure pressgo(x.b,y.b)
Shared steps,qp()
Define.w xcoord,ycoord
xcoord=(x+1)*(#maxx-#minx)/18
ycoord=(Y+1)*(#maxy-#miny)/18

;FlipBuffers()
If steps%2
DisplayTransparentSprite(0,xcoord-15,ycoord-15)
qp(x,y)=1
Else
DisplayTransparentSprite(1,xcoord-15,ycoord-15)
qp(x,y)=-1
EndIf
RedrawWindow_(WindowID(0), 0, 0, #RDW_INVALIDATE|#RDW_UPDATENOW)
EndProcedure

Procedure putoff(x.b,y.b)
Shared qp()
Define.w xcoord,ycoord
Define.rect rect
xcoord=#minx+x*(#maxx-#minx)/18
ycoord=#miny+y*(#maxy-#miny)/18
rect\left=xcoord-15
rect\right=xcoord+15
rect\top=ycoord+15
rect\bottom=ycoord-15
hwnd.l=WindowID(0)
;hdc.l=StartDrawing(ImageOutput(0))
;DrawingMode(#PB_2DDrawing_XOr)
;RedrawWindow_(hwnd, @rect, #Null, #RDW_NOFRAME|#RDW_ERASE|#RDW_INVALIDATE | #RDW_UPDATENOW)
;If qp(x,y)>0
;Circle(xcoord,ycoord,14,#White)
;Else
;Circle(xcoord,ycoord,14,#White)
;EndIf

;SetGadgetState(#Image_0,ImageID(0))


;InvalidateRect_(hwnd,@rect,#True)
;UpdateWindow_(hwnd)
;StopDrawing()
;RedrawWindow_(hwnd, @rect, 0, #RDW_ERASE|#RDW_INVALIDATE|#RDW_UPDATENOW)
;qp(x,y)=0
EndProcedure

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 10:49 am
by TomS
What?
..are you doing?
Either you have a sprite "blackman.bmp", that you can load with LoadSprite(), or you have an image that you want to paint on a sprite.
In that case you'll have to use DrawImage()

Code: Select all

LoadImage(0, "image.bmp")
SreateSprite(0, 30, 30)
StartDrawing(SpriteOutpu(0))
DrawImage(0, 0, 0)
StopDrawing()
But this really doesn't make sense, because you can load the sprite directly.

Your

Code: Select all

CreateSprite(0,30,30)
CreateSprite(1,30,30)
StartDrawing(SpriteOutput(0))
LoadSprite(0,"blackman.bmp")
StopDrawing()
StartDrawing(SpriteOutput(0))
LoadSprite(1,"whiteman.bmp")
StopDrawing()
Should simply be this:

Code: Select all

LoadSprite(0,"blackman.bmp")
LoadSprite(1,"whiteman.bmp")

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 11:54 am
by goldmate
This is a program to play chinesego, I hope to display chessman over a image drawing a Chessboard.
but background become pure black and see not chessboard after running the command of openwindowscreen.

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 12:04 pm
by TomS
Your code is useless. You haven't included common.pb
And where do you load or create the chessboard?

Normally you would do it like this.

Code: Select all

LoadSprite(0, "chessboard.bmp")
LoadSprite(1, "whiteman.bmp")
LoadSprite(2, "blackman.bmp")
TranspararentSpriteColor(1, RGB(255, 0, 255)) ;Transparent color is FF00FF (pink)
TranspararentSpriteColor(2, RGB(255, 0, 255))
;...OpenWindow, Screen etc....
Repeat
;EventLoop etc...

DisplaySprite(0, 0, 0) ;Display Chessboard in the upper left corner
DisplayTransparentSprite(1, 0, 0) ;Display Whiteman
DisplayTransparentSprite(2, 30, 0) ;Display Blackman

Forever
Could you add the common.pb to your posted code and include information about the sprites, you're trying to load (size/dimensions)?
That would help.

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 12:41 pm
by goldmate
Sorry,Here is common.pbi:
;- Window Constants
;
Enumeration
#Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
#Image_0
#Button_1
#Button_2
#Button_3
#Button_4
#Image_5
EndEnumeration

;- Image Plugins

;- Image Globals
Global Image0

;- Catch Images
Image0 = CatchImage(0, ?Image0)

;- Images
DataSection
Image0:
IncludeBinary "F:\wuyibao\wqboard.bmp"
EndDataSection


Procedure Open_Window_0()
If OpenWindow(#Window_0, 0, 0, 1024, 768, "围棋打谱 V1.0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_BorderLess | #PB_Window_ScreenCentered )

ImageGadget(#Image_0, 281, 0, 640, 640, Image0, #PB_Image_Border)
;qj.i=CreateImage(#image_6,720,720)
ResizeImage(0,720,720 )
SetGadgetState(#Image_0,ImageID(#Image_0))
LoadImage(#image_5,"lanketu.jpg")
;StartDrawing(WindowOutput(0))
;DrawImage(ImageID(#image_5),20,450,250,260)
;StopDrawing()
ButtonGadget(#Button_1, 100, 30, 90, 30, "Open")
GadgetToolTip(#Button_1, "Open a exist game")
ButtonGadget(#Button_2, 100, 80, 90, 30, "New")
GadgetToolTip(#Button_2, "New game")
ButtonGadget(#Button_3, 100, 130, 90, 30, "Save")
GadgetToolTip(#Button_3, "Save current game")
ButtonGadget(#Button_4, 100, 180, 90, 30, "Setup")
GadgetToolTip(#Button_4, "Game setup")
EndIf
EndProcedure

Re: How to use a image as background of a sprite

Posted: Fri Feb 11, 2011 12:49 pm
by TomS
Could you please add code tags?

Code: Select all

 [code]Your Purebasic code here...
[/code]

Now, you've got to clarify, what you want to do.
You can not use an ImageGadget simultaniously with an WindowedScreen
Either you display the chessboard and the chess pieces on an image, or you display them as sprites on a screen.
Your code creates a WindowedScreen and an ImageGadget in the same position. That's nonsense.

Re: How to use a image as background of a sprite

Posted: Sat Feb 12, 2011 1:26 am
by goldmate
Thanks, Solved this problem yesterday. Use sprite command for all images.