DirectDraw Overlay don't work

Just starting out? Need help? Post your questions and find answers here.
Lukaso
User
User
Posts: 20
Joined: Fri Apr 29, 2005 11:53 am
Location: Germany
Contact:

DirectDraw Overlay don't work

Post by Lukaso »

Hi community,

i have a problem, i have started programming a overlay. I have get an c++ code and stat translating it to pb. Everything looks good but my translatet Code don't work. I get the last error on "m_overlay_surface\UpdateOverlay(0, m_primary_surface, dest_rect, m_overlay_flags, m_overlay_fx)". The return value isn't #DD_OK. And I can't solve the problem myself. Here my code:

Code: Select all

DataSection

  IID_IDirectDraw7:
  Data.l $15E65EC0 
  Data.w $3B9C, $11D2 
  Data.b $B9, $2F, $00, $60, $97, $97, $EA, $5B

EndDataSection 


#DD_ROP_SPACE = 256/32


Structure DDCOLORKEY 
  dwColorSpaceLowValue.l 
  dwColorSpaceHighValue.l 
EndStructure 

Structure DDPIXELFORMAT 
  dwSize.l 
  dwFlags.l 
  dwFourCC.l 
  dwRGBBitCount.l 
  dwRBitMask.l 
  dwGBitMask.l 
  dwBBitMask.l 
  dwRGBAlphaBitMask.l 
EndStructure 

Structure DDSCAPS
  dwCaps.l
EndStructure

Structure DDSCAPS2 
  dwCaps.l 
  dwCaps2.l 
  dwCaps3.l 
  dwCaps4.l 
EndStructure 

Structure DDSURFACEDESC2 
  dwSize.l 
  dwFlags.l 
  dwHeight.l 
  dwWidth.l 
  lPitch.l 
  dwBackBufferCount.l 
  dwRefreshRate.l 
  dwAlphaBitDepth.l 
  dwReserved.l 
  lpSurface.l 
  ddckCKDestOverlay.DDCOLORKEY 
  ddckCKDestBlt.DDCOLORKEY 
  ddckCKSrcOverlay.DDCOLORKEY 
  ddckCKSrcBlt.DDCOLORKEY 
  ddpfPixelFormat.DDPIXELFORMAT 
  ddsCaps.DDSCAPS2 
  dwTextureStage.l 
EndStructure

Structure DDOVERLAYFX
  dwSize.l 
  dwAlphaEdgeBlendBitDepth.l 
  dwAlphaEdgeBlend.l 
  dwReserved.l
  dwAlphaDestConstBitDepth.l 
  
  StructureUnion
    dwAlphaDestConst.l
    lpDDSAlphaDest.IDirectDrawSurface7
  EndStructureUnion
  
  StructureUnion
    dwAlphaSrcConstBitDepth.l
    lpDDSAlphaSrc.IDirectDrawSurface7
  EndStructureUnion

  dckDestColorkey.DDCOLORKEY
  dckSrcColorkey.DDCOLORKEY 
  dwDDFX.l 
  dwFlags.l
EndStructure 
 
Structure DDCAPS
  dwSize.l
  dwCaps.l
  dwCaps2.l
  dwCKeyCaps.l
  dwFXCaps.l
  dwFXAlphaCaps.l
  dwPalCaps.l
  dwSVCaps.l
  dwAlphaBltConstBitDepths.l
  dwAlphaBltPixelBitDepths.l
  dwAlphaBltSurfaceBitDepths.l
  dwAlphaOverlayConstBitDepths.l
  dwAlphaOverlayPixelBitDepths.l
  dwAlphaOverlaySurfaceBitDepths.l
  dwZBufferBitDepths.l
  dwVidMemTotal.l
  dwVidMemFree.l
  dwMaxVisibleOverlays.l
  dwCurrVisibleOverlays.l
  dwNumFourCCCodes.l
  dwAlignBoundarySrc.l
  dwAlignSizeSrc.l
  dwAlignBoundaryDest.l
  dwAlignSizeDest.l
  dwAlignStrideAlign.l
  dwRops.l[#DD_ROP_SPACE]
  ddsOldCaps.DDSCAPS 
  dwMinOverlayStretch.l
  dwMaxOverlayStretch.l
  dwMinLiveVideoStretch.l
  dwMaxLiveVideoStretch.l
  dwMinHwCodecStretch.l
  dwMaxHwCodecStretch.l
  dwReserved1.l
  dwReserved2.l
  dwReserved3.l
  dwSVBCaps.l
  dwSVBCKeyCaps.l
  dwSVBFXCaps.l
  dwSVBRops.l[#DD_ROP_SPACE]
  dwVSBCaps.l
  dwVSBCKeyCaps.l
  dwVSBFXCaps.l
  dwVSBRops.l[#DD_ROP_SPACE]
  dwSSBCaps.l
  dwSSBCKeyCaps.l
  dwSSBFXCaps.l
  dwSSBRops.l[#DD_ROP_SPACE]
  dwMaxVideoPorts.l
  dwCurrVideoPorts.l
  dwSVBCaps2.l
  dwNLVBCaps.l
  dwNLVBCaps2.l
  dwNLVBCKeyCaps.l
  dwNLVBFXCaps.l
  dwNLVBRops.l[#DD_ROP_SPACE]
  ddsCaps.DDSCAPS2
EndStructure


#DD_OK                  = $00000000
#DDSCL_NORMAL           = $00000008
#DDPF_RGB               = $00000040
#DDOVER_SHOW            = $00000002

#DDFLIP_WAIT            = $00000001

#DDSD_CAPS              = $00000001
#DDSD_HEIGHT            = $00000002
#DDSD_WIDTH             = $00000004
#DDSD_BACKBUFFERCOUNT   = $00000020
#DDSD_PIXELFORMAT       = $00001000

#DDCAPS_OVERLAY         = $00000800

#DDSCAPS_BACKBUFFER     = $00000004
#DDSCAPS_COMPLEX        = $00000008
#DDSCAPS_FLIP           = $00000010
#DDSCAPS_OVERLAY        = $00000080
#DDSCAPS_PRIMARYSURFACE = $00000200
#DDSCAPS_VIDEOMEMORY    = $00004000

#DDERR_EXCLUSIVEMODEALREADYSET = 2289435205
#DDERR_WRONGMODE = 2289435211

#DDCKEYCAPS_SRCOVERLAY  = $00002000

#DDOVER_KEYSRCOVERRIDE  = $00002000
#DDOVER_DDFX            = $00080000


Declare initialize_surfaces()
Declare cleanup()
Declare initialize()


Global m_direct_draw.IDirectDraw7
Global m_caps.DDCAPS
Global m_primary_surface.IDirectDrawSurface7
Global m_overlay_surface.IDirectDrawSurface7
Global m_surfaces_lost.b
Global m_overlay_flags.l
Global m_overlay_back_surface.IDirectDrawSurface7
Global m_overlay_fx.DDOVERLAYFX
Global ddsd.DDSURFACEDESC2


Global OVERLAY_WIDTH, OVERLAY_HEIGHT

OVERLAY_WIDTH = 256
OVERLAY_HEIGHT = 64


Procedure overlay_renderer_t()

  If OpenLibrary(0, "ddraw.dll")
    
    If CallFunction(0, "DirectDrawCreateEx", 0, @m_direct_draw, ?IID_IDirectDraw7, 0) = #DD_OK 
      
      m_caps\dwSize = SizeOf(DDCAPS)
      m_direct_draw\GetCaps(@m_caps, 0)

      ProcedureReturn #True
      
    EndIf 
  
  EndIf

EndProcedure

Procedure support_available()
  
  num_visible_overlays.l = 0
  
  If m_overlay_surface
    
    num_visible_overlays = m_caps\dwCurrVisibleOverlays - 1
    
  EndIf
  
	If m_caps\dwMaxVisibleOverlays > num_visible_overlays And m_caps\dwCaps & #DDCAPS_OVERLAY
    
    ProcedureReturn #True
    
  Else
    
    ProcedureReturn #False
    
  EndIf

EndProcedure  
  
Procedure initialize()

  m_direct_draw\SetCooperativeLevel(0, #DDSCL_NORMAL)
  
  ddsd.DDSURFACEDESC2
  ddsd\dwSize = SizeOf(DDSURFACEDESC2)
  ddsd\dwFlags = #DDSD_CAPS
  ddsd\ddsCaps\dwCaps = #DDSCAPS_PRIMARYSURFACE

  m_direct_draw\CreateSurface(ddsd, @m_primary_surface, 0)

  initialize_surfaces()
  
EndProcedure

Procedure update()
  
  hr.l = m_direct_draw\TestCooperativeLevel()
  
  If hr <> #DD_OK
    
    Select hr
      
      Case #DDERR_EXCLUSIVEMODEALREADYSET
        
        m_surfaces_lost = #True
        Delay(1000)
        ProcedureReturn #False
        
      Case #DDERR_WRONGMODE
        
        cleanup()
        initialize()
        
    EndSelect    
    
  EndIf
  
  If m_surfaces_lost
    
    m_direct_draw\RestoreAllSurfaces()
    m_surfaces_lost = #False
    
  EndIf
  
  dc.l = 0
  str.s = "This is overlayed text"
  
  m_overlay_back_surface\GetDC(@dc)
  
  SetTextColor_(dc, RGB(255, 255, 255))
  SetBkColor_(dc, RGB(0, 0, 0))
  TextOut_(dc, 5, 5, @str, Len(str))
  
  m_overlay_back_surface\ReleaseDC(dc)
  
  m_overlay_surface\Flip(0, #DDFLIP_WAIT)
  
  dest_rect.RECT
  dest_rect\left   = 0
  dest_rect\top    = 0
  dest_rect\right  = OVERLAY_WIDTH
  dest_rect\bottom = OVERLAY_HEIGHT
  
  m_overlay_flags = #DDOVER_SHOW
  
  If m_caps\dwCKeyCaps & #DDCKEYCAPS_SRCOVERLAY
    
    m_overlay_fx\dckDestColorkey\dwColorSpaceLowValue = 0
    m_overlay_fx\dckDestColorkey\dwColorSpaceHighValue = 0
    m_overlay_flags = m_overlay_flags | #DDOVER_DDFX | #DDOVER_KEYSRCOVERRIDE
    
  EndIf
  
  m_overlay_surface\UpdateOverlay(0, m_primary_surface, dest_rect, m_overlay_flags, m_overlay_fx)
  
EndProcedure

Procedure initialize_surfaces()
  
  ddsd.DDSURFACEDESC2
  ddsd\dwSize = SizeOf(DDSURFACEDESC2)
  ddsd\dwFlags = #DDSD_CAPS | #DDSD_HEIGHT | #DDSD_WIDTH | #DDSD_BACKBUFFERCOUNT | #DDSD_PIXELFORMAT
  ddsd\ddsCaps\dwCaps = #DDSCAPS_OVERLAY | #DDSCAPS_FLIP | #DDSCAPS_COMPLEX | #DDSCAPS_VIDEOMEMORY
  ddsd\dwBackBufferCount = #True
  ddsd\dwWidth = OVERLAY_WIDTH
	ddsd\dwHeight = OVERLAY_HEIGHT
  
  ddsd\ddpfPixelFormat\dwSize = SizeOf(DDPIXELFORMAT)
	ddsd\ddpfPixelFormat\dwFlags = #DDPF_RGB
  
  ddsd\ddpfPixelFormat\dwRGBBitCount = 32 
  ddsd\ddpfPixelFormat\dwRBitMask = $00FF0000
  ddsd\ddpfPixelFormat\dwGBitMask = $0000FF00
  ddsd\ddpfPixelFormat\dwBBitMask = $000000FF
  
  ddscaps.DDSCAPS2
  
  m_direct_draw\CreateSurface(ddsd, @m_overlay_surface, 0)
  
  ddscaps\dwCaps = #DDSCAPS_BACKBUFFER
  
  m_overlay_surface\GetAttachedSurface(ddscaps, @m_overlay_back_surface)
  
	m_overlay_fx\dwSize = SizeOf(DDOVERLAYFX)
  
EndProcedure

Procedure cleanup()
  
  m_overlay_surface = 0
  m_primary_surface = 0
  m_surfaces_lost = #False
  
EndProcedure


If overlay_renderer_t()

  If support_available()
    
    initialize()
    
    Repeat
    
      update()
      Delay(0)

    ForEver
  
  EndIf
  
  CloseLibrary(0)
  
EndIf
The C++ code you can get here: http://nexe.gamedev.net/files/Overlay-2005-11-21.zip

I hope somebody can help me to fix the problem.

Thanks :)

Lukaso
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

I found out that, there is no mistake in your code. :D



But your DDOVERLAYFX-Structure and the #DDOVER_SHOW-Constant is wrong! :!:
Try this instead:

Code: Select all

Structure DDOVERLAYFX
  dwSize.l 
  dwAlphaEdgeBlendBitDepth.l
  dwAlphaEdgeBlend.l 
  dwReserved.l
  dwAlphaDestConstBitDepth.l
  StructureUnion 
    dwAlphaDestConst.l
    lpDDSAlphaDest.IDirectDrawSurface7
  EndStructureUnion
  dwAlphaSrcConstBitDepth.l
  StructureUnion 
    dwAlphaSrcConst.l 
    lpDDSAlphaSrc.IDirectDrawSurface7
  EndStructureUnion
  dckDestColorkey.DDCOLORKEY 
  dckSrcColorkey.DDCOLORKEY
  dwDDFX.l
  dwFlags.l 
EndStructure

#DDOVER_SHOW=16384
BTW, You should also support 16-Bit- and FOURCC-Overlayformats, or it wont run with some GFX-Cards.

regards
Stefan
Lukaso
User
User
Posts: 20
Joined: Fri Apr 29, 2005 11:53 am
Location: Germany
Contact:

Post by Lukaso »

The mistake in DDOVERLAYFX I have found myself 2 hours ago. I have changed the #DDOVER_SHOW constant and the code is running. Thank You ... I have googled vor DDOVER_SHOW and found on a site that DDOVER_SHOW is 0x00000002 :evil:.

Yes i change my code for 16bit. But how i realise FOURCC i dont know jet .... I code a lot wih pb but this was my first dx try. Translating is ok but to do it myself ..... I think I can't. Maybe you have a example for me?

I thank you alot ... a small mistake and a big problem :roll:

Lukaso
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

Lukaso wrote:I thank you alot ... a small mistake and a big problem :roll:
Yes, because it's the last thing you look at. :lol:
Here is a old example from me, which supports 16 and 15 Bit RGB,
and the most important FOURCC(YUY2,UYVY) formats.
Link:http://forums.purebasic.com/german/view ... 43&start=0
If you want, I could also write a new one for you. :wink:

regards
Stefan
Lukaso
User
User
Posts: 20
Joined: Fri Apr 29, 2005 11:53 am
Location: Germany
Contact:

Post by Lukaso »

S.M wrote: Yes, because it's the last thing you look at. :lol:
Here is a old example from me, which supports 16 and 15 Bit RGB,
and the most important FOURCC(YUY2,UYVY) formats.
Link:http://forums.purebasic.com/german/view ... 43&start=0
If you want, I could also write a new one for you. :wink:
No, a I would like to learn a bit myself. :wink:.
Thx for the link, I implementet the other formats. It solves the DDERR_INVALIDPIXELFORMAT on nVidia Cards. But now I get DDERR_CANTCREATEDC when getting the DC (m_overlay_back_surface\GetDC(@dc)). Have you any idee what it could be? On ATI cards it works fine.

Lukas
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

But now I get DDERR_CANTCREATEDC when getting the DC (m_overlay_back_surface\GetDC(@dc)).
Have you any idee what it could be? On ATI cards it works fine.
Yes, GetDC is driver-dependant, which means that GDI supports the format only if the driver supports it.The only formats which work for sure are the supported screenformats.(normally R5G6B5(16-Bit) and X8R8G8B8(32-Bit))
For other formats you should(have to) use the Lock()-method. (For RGB-formats(like X1R5G5B5) you could use a DIBSection to get a DC. Then you could copy it easyly to the DirectDrawSurface)
With my ATI-card (Radeon X800GT), GetDC fails with formats other than X8R8G8B8 and R5G6B5.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

it works well. Thank You :)

How do you position the overlayed text tho? How would you put it in the center of a window using the windows size and then centering in that window?
Post Reply