Public Class CSBinaryWriter
    Dim wr As IO.BinaryWriter

    Public Sub New(ByVal stream As IO.Stream)
        Me.wr = New IO.BinaryWriter(stream)
    End Sub

    Public Sub WriteByte(ByVal v As Byte)
        wr.Write(v)
    End Sub

    Public Sub WriteInt32(ByVal v As Integer)
        Dim b1 As Byte = (v >> 24) And 255
        Dim b2 As Byte = (v >> 16) And 255
        Dim b3 As Byte = (v >> 8) And 255
        Dim b4 As Byte = (v >> 0) And 255
        wr.Write((CInt(b4) << 24) Or (CInt(b3) << 16) Or (CInt(b2) << 8) Or CInt(b1))
    End Sub

    Public Sub WriteInt16(ByVal v As Short)
        Dim b1 As Byte = (v >> 8) And 255
        Dim b2 As Byte = (v >> 0) And 255
        wr.Write((CShort(b2) << 8) Or CShort(b1))
    End Sub

    Public Sub WriteRectPos(ByVal v As RectPos)
        WriteInt16(v.x)
        WriteInt16(v.y)
        WriteInt16(v.cx)
        WriteInt16(v.cy)
    End Sub

    Public Sub WriteSOUND(ByVal v As SOUND)
        WriteInt16(v.word0)
        WriteByte(v.byte2)
        WriteByte(v.byte3)
        WriteByte(v.byte4)
        WriteByte(v.byte5)
        WriteByte(v.byte6)
        WriteByte(v.byte7)
    End Sub

    Public Sub WriteICONDISPLAY(ByVal v As ICONDISPLAY)
        WriteInt16(v.pixelX)
        WriteInt16(v.pixelY)
        WriteInt16(v.objectType)
    End Sub

    Public Sub WritePALETTE(ByVal v As PALETTE)
        Dim i As Integer
        For i = 1 To 16
            WriteInt16(v.color(i))
        Next
    End Sub
End Class
