Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PNG
#1
Im a french programmer and i have added PNG to mirage source.

First add to reference PaintX 1.0 Type library. (go to : http://www.paintlib.de )

Now create a module modPNG and add:
Code:
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Public Function LoadPNG(FileName As String) As StdPicture
    Dim PictureDecoder As New PAINTXLib.PictureDecoder
    Set LoadPNG = PictureDecoder.LoadPicture(FileName)
End Function

Public Function LoadImage(FileName As String, DDraw As DirectDraw7, SDesc As DDSURFACEDESC2) As DirectDrawSurface7
    Dim TPict As StdPicture
    Set TPict = LoadPNG(FileName)
    
    SDesc.lHeight = CLng((TPict.Height * 0.001) * 567 / Screen.TwipsPerPixelY)
    SDesc.lWidth = CLng((TPict.Width * 0.001) * 567 / Screen.TwipsPerPixelX)
    
    
    Set LoadImage = DDraw.CreateSurface(SDesc)
    
    Dim SDC As Long, TDC As Long
    SDC = LoadImage.GetDC
    TDC = CreateCompatibleDC(0)
    SelectObject TDC, TPict.Handle
    
    BitBlt SDC, 0, 0, SDesc.lWidth, SDesc.lHeight, TDC, 0, 0, vbSrcCopy
        
    LoadImage.ReleaseDC SDC
    DeleteDC TDC
      
    Set TPict = Nothing
End Function

Public Function LoadImageStretch(FileName As String, Height As Long, Width As Long, DDraw As DirectDraw7, SDesc As DDSURFACEDESC2) As DirectDrawSurface7
    Dim TPict As New StdPicture
    Set TPict = LoadPNG(FileName)
    
    SDesc.lHeight = Height
    SDesc.lWidth = Width
    
    Set LoadImageStretch = DDraw.CreateSurface(SDesc)
    
    Dim SDC As Long, TDC As Long
    SDC = LoadImageStretch.GetDC
    TDC = CreateCompatibleDC(0)
    SelectObject TDC, TPict.Handle
    
    StretchBlt SDC, 0, 0, Width, Height, TDC, 0, 0, CLng((TPict.Width * 0.001) * 567 / Screen.TwipsPerPixelX), CLng((TPict.Height * 0.001) * 567 / Screen.TwipsPerPixelY), vbSrcCopy
    
    LoadImageStretch.ReleaseDC SDC
    DeleteDC TDC
        
    Set TPict = Nothing
End Function

Now change all .bmp to .png


Now go to modDirectX en change Sub InitSurfaces()
With :
Code:
Sub InitSurfaces()
Dim KEY As DDCOLORKEY

  
    ' Set the key for masks
    KEY.low = 0
    KEY.high = 0
  
    ' Initialize back buffer
    DDSD_BackBuffer.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
    DDSD_BackBuffer.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
    DDSD_BackBuffer.lWidth = (MAX_MAPX + 1) * PIC_X
    DDSD_BackBuffer.lHeight = (MAX_MAPY + 1) * PIC_Y
    Set DD_BackBuffer = DD.CreateSurface(DDSD_BackBuffer)
  
    ' Init sprite ddsd type and load the bitmap
DDSD_Sprite.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
DDSD_Sprite.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
Set DD_SpriteSurf = LoadImage(App.Path & "\gfx\sprites.png", DD, DDSD_Sprite)
SetMaskColorFromPixel DD_SpriteSurf, 0, 0
  
    ' Init sprite ddsd type and load the bitmap
DDSD_Tile.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
DDSD_Tile.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
Set DD_TileSurf = LoadImage(App.Path & "\gfx\tiles.png", DD, DDSD_Tile)
SetMaskColorFromPixel DD_TileSurf, 0, 0
  
    ' Init sprite ddsd type and load the bitmap
DDSD_Item.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
DDSD_Item.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
Set DD_ItemSurf = LoadImage(App.Path & "\gfx\items.png", DD, DDSD_Item)
SetMaskColorFromPixel DD_ItemSurf, 0, 0
End Sub

And add to modDirectX :
Code:
Public Sub SetMaskColorFromPixel(ByRef TheSurface As DirectDrawSurface7, ByVal X As Long, ByVal Y As Long)
Dim TmpR As RECT
Dim TmpDDSD As DDSURFACEDESC2
Dim TmpColorKey As DDCOLORKEY

With TmpR
.Left = X
.top = Y
.Right = X
.Bottom = Y
End With

TheSurface.Lock TmpR, TmpDDSD, DDLOCK_WAIT Or DDLOCK_READONLY, 0

With TmpColorKey
.low = TheSurface.GetLockedPixel(X, Y)
.high = .low
End With

TheSurface.SetColorKey DDCKEY_SRCBLT, TmpColorKey

TheSurface.Unlock TmpR
End Sub

Now change :
.Picture = LoadPicture(App.Path + "\gfx\tiles.bmp")
With:
Code:
.Picture = LoadPNG(App.Path + "\gfx\tiles.bmp")

Now change :
frmItemEditor.picItems.Picture = LoadPicture(App.Path & "\gfx\items.bmp")
with :
Code:
frmItemEditor.picItems.Picture = LoadPNG(App.Path & "\gfx\items.bmp")

Now change :
frmNpcEditor.picSprites.Picture = LoadPicture(App.Path & "\gfx\sprites.bmp")
with :
Code:
frmNpcEditor.picSprites.Picture = LoadPNG(App.Path & "\gfx\sprites.bmp")


I use it on my game, good luck and good png :mrgreen:

El_Dindonnier.
Reply
#2
slight mistake at the end, you said to change the path from .bmp to .bmp instead of from .bmp to .png Wink

edit: wait.. actually that's right... i think... is it?

just wondering what use loading a .bmp is then calling it a .png would be? would it reduce mem usage or just increase loading times?
Reply
#3
When the PNG is loaded into memory, it's converted to a bitmap (I think). So the only thing this will really do is allow you to package a smaller client.
Reply
#4
Sonire Wrote:When the PNG is loaded into memory, it's converted to a bitmap (I think). So the only thing this will really do is allow you to package a smaller client.

Right on the money. Though it may decrease loading time, depending on the implementation. It is actually not uncommon for files to be compressed so they can load faster. CPUs and RAM increase in speed at a much faster rate than HDDs do at reading, so if the compression is good and the decompression is very fast and purely in memory, it can take a shorter time to load completely. A common example of this is the usage of something like UPX on EXE files. Whether or not this applies to using PNG instead of BMP, at least with this implementation, I have no idea.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)