• Advertisement

Archived

This topic is now archived and is closed to further replies.

Texture from image

This topic is 6515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone give me a code sample of making a texture from a part of the image (200,230 to 264 to 294 for example)? And yes VB please And perhaps a code for using some other format than bmp would be useful,too (but not really necessary now). That''s the code I''ve been using, it''s the same than in DX SDK tutorials
Public Function CreateTextureSurface(sFile As String, extraFlags As Integer, w As Long, h As Long)

    Dim bOK As Boolean
    Dim enumTex As Direct3DEnumPixelFormats
    Dim sLoadFile As String
    Dim i As Long
    Dim ddsd As DDSURFACEDESC2
    Dim SurfaceObject As DirectDrawSurface7
    Dim Init As Boolean
    
    ddsd.lFlags = DDSD_CAPS Or DDSD_TEXTURESTAGE Or DDSD_PIXELFORMAT
    If ((h <> 0) And (w <> 0)) Then
        ddsd.lFlags = ddsd.lFlags Or DDSD_HEIGHT Or DDSD_WIDTH
        ddsd.lHeight = h
        ddsd.lWidth = w
    End If
    
     
    Set enumTex = g_d3dDevice.GetTextureFormatsEnum()
    
   
    For i = 1 To enumTex.GetCount()
        bOK = True
        Call enumTex.GetItem(i, ddsd.ddpfPixelFormat)
            
        With ddsd.ddpfPixelFormat
            
            If .lRGBBitCount <> 16 Then bOK = False
            If .lFourCC <> 0 Then bOK = False
                        
            
            If ((D3DTEXTR_TRANSPARENTBLACK And extraFlags) _
                  Or (D3DTEXTR_TRANSPARENTWHITE And extraFlags)) Then
                  If (.lRGBAlphaBitMask = 0) Then bOK = False ''DDPF_ALPHAPIXELS
            Else
                  If (.lRGBAlphaBitMask <> 0) Then bOK = False
            End If
                  
            
        End With
        If bOK = True Then Exit For
    Next
    
    If bOK = False Then
        Debug.Print "Unable to find 16bit surface support on your hardware - exiting"
        Init = False
    End If
    
    If (((D3DTEXTR_TRANSPARENTBLACK And extraFlags) _
          Or (D3DTEXTR_TRANSPARENTWHITE And extraFlags))) Then
        ddsd.ddpfPixelFormat.lFlags = DDPF_ALPHAPIXELS Or DDPF_RGB
        ddsd.lFlags = ddsd.lFlags Or DDSD_PIXELFORMAT
        
    Else
        ddsd.ddpfPixelFormat.lFlags = DDPF_RGB
  
    End If
    
    If g_d3dDevice.GetDeviceGuid() = "IID_IDirect3DHALDevice" Then
            ddsd.ddscaps.lCaps = DDSCAPS_TEXTURE
            ddsd.ddscaps.lCaps2 = DDSCAPS2_TEXTUREMANAGE
            ddsd.lTextureStage = 0
    Else
            ddsd.ddscaps.lCaps = DDSCAPS_TEXTURE Or DDSCAPS_SYSTEMMEMORY
            ddsd.ddscaps.lCaps2 = 0
            ddsd.lTextureStage = 0
    End If

    If sFile = "" Then
        Set SurfaceObject = dd.CreateSurface(ddsd)
    Else
        Set SurfaceObject = dd.CreateSurfaceFromFile(sFile, ddsd)
    End If
    
    
    Set CreateTextureSurface = SurfaceObject
    
    If Not (D3DTEXTR_TRANSPARENTBLACK = extraFlags) _
    Or (D3DTEXTR_TRANSPARENTWHITE = extraFlags) Then Exit Function

        
        
    Dim ddsd3 As DDSURFACEDESC2
    Dim mem() As Integer
    Dim lAlphaMask As Long
    Dim lRGBMask As Long
    Dim lColorKey As Long
    Dim c As Long
    Dim x As Long
    Dim y As Long
    Dim r1 As RECT
    
    ReDim mem(ddsd.lHeight * ddsd.lPitch)
                        
    With ddsd.ddpfPixelFormat
        lAlphaMask = .lRGBAlphaBitMask
        lRGBMask = .lRBitMask Or .lGBitMask Or .lBBitMask
    End With
    If (extraFlags And D3DTEXTR_TRANSPARENTWHITE) Then
        lColorKey = lRGBMask     ''color key on white
    End If
    If (extraFlags And D3DTEXTR_TRANSPARENTBLACK) Then
        lColorKey = 0    ''color key on black
    End If
    
    ''pixel format should be 16 bit because thats what we selected
    Dim rl As RECT
    
    r1.Bottom = ddsd.lHeight
    r1.right = ddsd.lWidth
    SurfaceObject.Lock r1, ddsd3, DDLOCK_WAIT, 0
    
    '' Add an opaque alpha value to each non-colorkeyed pixel
    For y = 0 To ddsd3.lHeight - 1
        For x = 0 To ddsd3.lWidth - 1
            c = SurfaceObject.GetLockedPixel(x, y)
            If c And lRGBMask <> lColorKey Then
                SurfaceObject.SetLockedPixel x, y, c Or lAlphaMask
            End If
        Next
    Next

    SurfaceObject.Unlock r1
        
    
End Function
 

Share this post


Link to post
Share on other sites
Advertisement
Well let''s see now. What you want is to actually just create a DDSURFACE with 3DDEVICE as one of it''s flags and has dimensions of 64x64.

Once you have that surface, just do a BLT from your original surface to the texture surface.

I''d write you the code but I''m at the office right now and no SDK in sight.


-------------------------
-Now Working on Pokemon like Engine!

Share this post


Link to post
Share on other sites

  • Advertisement