Project

General

Profile

« Previous | Next » 

Revision 40d24895

Added by Leszek Koltunski 1 day ago

new package 'platform' and implementations of ready/writing a file.

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.kt
26 26
import org.distorted.library.effectqueue.EffectQueue
27 27
import org.distorted.library.main.DistortedLibrary
28 28
import org.distorted.library.main.InternalBuffer
29
import org.distorted.library.platform.FileReader
30
import org.distorted.library.platform.FileWriter
29 31
import org.distorted.library.program.DistortedProgram
30 32
import org.distorted.library.type.Static4D
31 33
import org.distorted.library.uniformblock.UniformBlockAssociation
32 34
import org.distorted.library.uniformblock.UniformBlockCenter
33 35

  
34
import java.io.DataInputStream
35
import java.io.DataOutputStream
36
import java.io.IOException
37 36
import java.nio.ByteBuffer
38 37
import java.nio.ByteOrder
39 38

  
......
914 913

  
915 914
    ///////////////////////////////////////////////////////////////////////////////////////////////////
916 915
    // Return the number of bytes read.
917
    fun read(stream: DataInputStream): Int
916
    fun read(reader: FileReader): Int
918 917
    {
919 918
        var buffer = ByteArray(BYTES_PER_FLOAT*4)
920 919
        val version: Int
......
926 925
        //////////////////////////////////////////////////////////////////////////////////////////
927 926
        try
928 927
        {
929
            stream.read(buffer)
928
            reader.read(buffer)
930 929
            val byteBuf = ByteBuffer.wrap(buffer)
931 930

  
932 931
            version = byteBuf.getInt(0)
......
968 967

  
969 968
            try
970 969
            {
971
                stream.read(buffer)
970
                reader.read(buffer)
972 971
            }
973 972
            catch (e: Exception)
974 973
            {
......
1094 1093
    /**
1095 1094
     * Write the Mesh to a File.
1096 1095
     */
1097
    fun write(stream: DataOutputStream)
1096
    fun write(writer: FileWriter)
1098 1097
    {
1099 1098
        var tex: TexComponent
1100 1099

  
1101 1100
        val numTex = mTexComponent.size
1102 1101
        val numEff = mEffComponent.size
1103 1102

  
1104
        try
1105
        {
1106
            stream.writeInt(2) // version
1107
            stream.writeInt(numVertices)
1108
            stream.writeInt(numTex)
1109
            stream.writeInt(numEff)
1103
        writer.writeInt(2) // version
1104
        writer.writeInt(numVertices)
1105
        writer.writeInt(numTex)
1106
        writer.writeInt(numEff)
1110 1107

  
1111
            for (i in 0 until numTex)
1112
            {
1113
                tex = mTexComponent[i]
1114

  
1115
                stream.writeFloat(tex.mEndIndex.toFloat())
1116
                stream.writeFloat(tex.mTextureMap.get0())
1117
                stream.writeFloat(tex.mTextureMap.get1())
1118
                stream.writeFloat(tex.mTextureMap.get2())
1119
                stream.writeFloat(tex.mTextureMap.get3())
1120
            }
1121

  
1122
            for (i in 0 until numEff)
1123
            {
1124
                stream.writeFloat(mEffComponent[i].toFloat())
1125
            }
1108
        for (i in 0 until numTex)
1109
        {
1110
            tex = mTexComponent[i]
1126 1111

  
1127
            val centers = if (useCenters) mUBC!!.backingArray else FloatArray(4*numEff)
1112
            writer.writeFloat(tex.mEndIndex.toFloat())
1113
            writer.writeFloat(tex.mTextureMap.get0())
1114
            writer.writeFloat(tex.mTextureMap.get1())
1115
            writer.writeFloat(tex.mTextureMap.get2())
1116
            writer.writeFloat(tex.mTextureMap.get3())
1117
        }
1128 1118

  
1129
            for (i in 0 until numEff)
1130
            {
1131
                stream.writeFloat(centers[4*i])
1132
                stream.writeFloat(centers[4*i+1])
1133
                stream.writeFloat(centers[4*i+2])
1134
            }
1119
        for (i in 0 until numEff)
1120
        {
1121
            writer.writeFloat(mEffComponent[i].toFloat())
1122
        }
1135 1123

  
1136
            val vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*numVertices)
1137
            vertBuf1.asFloatBuffer().put(mVertAttribs1)
1138
            val vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*numVertices)
1139
            vertBuf2.asFloatBuffer().put(mVertAttribs2)
1124
        val centers = if (useCenters) mUBC!!.backingArray else FloatArray(4*numEff)
1140 1125

  
1141
            stream.write(vertBuf1.array())
1142
            stream.write(vertBuf2.array())
1143
        }
1144
        catch (ex: IOException)
1126
        for (i in 0 until numEff)
1145 1127
        {
1146
            DistortedLibrary.logMessage("MeshBase: IOException trying to write a mesh: $ex")
1128
            writer.writeFloat(centers[4*i])
1129
            writer.writeFloat(centers[4*i+1])
1130
            writer.writeFloat(centers[4*i+2])
1147 1131
        }
1132

  
1133
        val vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*numVertices)
1134
        vertBuf1.asFloatBuffer().put(mVertAttribs1)
1135
        val vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*numVertices)
1136
        vertBuf2.asFloatBuffer().put(mVertAttribs2)
1137

  
1138
        writer.write(vertBuf1.array())
1139
        writer.write(vertBuf2.array())
1148 1140
    }
1149 1141

  
1150 1142
    ///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/mesh/MeshFile.kt
20 20

  
21 21
package org.distorted.library.mesh
22 22

  
23
import java.io.DataInputStream
23
import org.distorted.library.platform.FileReader
24 24

  
25 25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 26
/**
......
38 38
     *
39 39
     * File format: as written by MeshBase.write().
40 40
     */
41
    constructor(stream: DataInputStream) : super()
41
    constructor(reader: FileReader) : super()
42 42
    {
43
        numBytes = read(stream)
43
        numBytes = read(reader)
44 44
    }
45 45

  
46 46
    ///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/platform/FileReader.kt
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2025 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
package org.distorted.library.platform
22

  
23
interface FileReader
24
{
25
    fun read(arr: ByteArray)
26
}
src/main/java/org/distorted/library/platform/FileReaderImplementation.kt
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2025 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
package org.distorted.library.platform
22

  
23
import java.io.DataInputStream
24

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
class FileReaderImplementation(val input: DataInputStream) : FileReader
28
{
29
    override fun read(arr: ByteArray)
30
    {
31
        input.read(arr)
32
    }
33
}
src/main/java/org/distorted/library/platform/FileWriter.kt
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2025 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
package org.distorted.library.platform
22

  
23
interface FileWriter
24
{
25
    fun writeInt(a: Int)
26
    fun writeFloat(a: Float)
27
    fun write(a: ByteArray)
28
}
src/main/java/org/distorted/library/platform/FileWriterImplementation.kt
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2025 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
package org.distorted.library.platform
22

  
23
import java.io.DataOutputStream
24

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
class FileWriterImplementation(val output: DataOutputStream) : FileWriter
28
{
29
    override fun writeInt(a : Int)
30
    {
31
        output.writeInt(a)
32
    }
33

  
34
    override fun writeFloat(a : Float)
35
    {
36
        output.writeFloat(a)
37
    }
38

  
39
    override fun write(a : ByteArray)
40
    {
41
        output.write(a)
42
    }
43
}

Also available in: Unified diff