GlslTilingTest.kt 5.54 KB
Newer Older
Minjae Song's avatar
Minjae Song committed
1 2 3 4 5 6 7 8 9
package net.torvald.terrarum

import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.ShaderProgram
10
import com.badlogic.gdx.graphics.Color
Minjae Song's avatar
Minjae Song committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import net.torvald.terrarumsansbitmap.gdx.GameFontBase

/**
 * Created by minjaesong on 2017-08-25.
 */
fun main(args: Array<String>) { // LWJGL 3 won't work? java.lang.VerifyError
    val config = LwjglApplicationConfiguration()
    //config.useGL30 = true
    config.vSyncEnabled = false
    config.resizable = false
    config.width = 1072
    config.height = 742
    config.foregroundFPS = 9999
    LwjglApplication(GlslTilingTest, config)
}

object GlslTilingTest : ApplicationAdapter() {

    lateinit var shader: ShaderProgram
    lateinit var font: GameFontBase

    lateinit var tilesQuad: Mesh

    lateinit var camera: OrthographicCamera

    lateinit var batch: SpriteBatch

    lateinit var fucktex: Texture

    val TILE_SIZE = 16
41
    val TILE_SIZEF = 16f
Minjae Song's avatar
Minjae Song committed
42 43


44 45 46
    lateinit var tilesBuffer: Pixmap

    lateinit var tileAtlas: Texture
Minjae Song's avatar
Minjae Song committed
47 48 49 50 51


    override fun create() {
        ShaderProgram.pedantic = false

52
        shader = ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/tiling.frag"))
Minjae Song's avatar
Minjae Song committed
53 54 55 56 57 58 59


        font = GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", flipY = false)


        if (!shader.isCompiled) {
            Gdx.app.log("Shader", shader.log)
60 61 62

            //ErrorDisp.title = "Error in shader ${shader.vertexShaderSource}"
            //ErrorDisp.text = shader.log.split('\n')
63
            //AppLoader.getINSTANCE().setScreen(ErrorDisp)
Minjae Song's avatar
Minjae Song committed
64 65 66 67 68
            System.exit(1)
        }



69 70
        val tilesInHorizontal = (Gdx.graphics.width.toFloat() / TILE_SIZE).ceil() + 1f
        val tilesInVertical = (Gdx.graphics.height.toFloat() / TILE_SIZE).ceil() + 1f
Minjae Song's avatar
Minjae Song committed
71 72 73 74 75 76 77 78

        tilesQuad = Mesh(
                true, 4, 6,
                VertexAttribute.Position(),
                VertexAttribute.ColorUnpacked(),
                VertexAttribute.TexCoords(0)
        )

79 80 81 82 83
        tilesQuad.setVertices(floatArrayOf( // WARNING! not ususal quads; TexCoords of Y is flipped
                0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, 0f,
                Gdx.graphics.width.toFloat(), 0f, 0f, 1f, 1f, 1f, 1f, 1f, 0f,
                Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 1f, 1f,
                0f, Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 0f, 1f
Minjae Song's avatar
Minjae Song committed
84 85 86 87
        ))
        tilesQuad.setIndices(shortArrayOf(0, 1, 2, 2, 3, 0))


88
        tilesBuffer = Pixmap(tilesInHorizontal.toInt(), tilesInVertical.toInt(), Pixmap.Format.RGBA8888)
Minjae Song's avatar
Minjae Song committed
89 90 91 92 93 94 95 96 97 98 99


        camera = OrthographicCamera(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
        camera.setToOrtho(true, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
        camera.update()


        batch = SpriteBatch()

        fucktex = Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"))

100
        tileAtlas = Texture(Gdx.files.internal("assets/terrain.tga"))//BlocksDrawer.tilesTerrain.texture
Minjae Song's avatar
Minjae Song committed
101 102


103 104
        println(tilesBuffer.format)
    }
Minjae Song's avatar
Minjae Song committed
105 106


Minjae Song's avatar
Minjae Song committed
107 108 109
    var cameraX = 0f
    var cameraY = 0f

110 111
    override fun render() {
        Gdx.graphics.setTitle("GlslTilingTest — F: ${Gdx.graphics.framesPerSecond}")
Minjae Song's avatar
Minjae Song committed
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

        // 0brrrrrrrr_gggggggg_bbbbbbbb_aaaaaaaa
        for (y in 0 until tilesBuffer.height) {
            for (x in 0 until tilesBuffer.width) {
                val i = (y * 256) + x
                val color = Color(i.shl(8).or(255))
                tilesBuffer.setColor(color)
                tilesBuffer.drawPixel(x, y)
            }
        }




        Gdx.gl.glViewport(0, 0, Gdx.graphics.width, Gdx.graphics.height)
        Gdx.gl.glClearColor(1f, 0f, 1f, 1f)
129 130 131 132
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
        Gdx.gl.glEnable(GL20.GL_TEXTURE_2D)
        Gdx.gl.glEnable(GL20.GL_BLEND)
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)
Minjae Song's avatar
Minjae Song committed
133

134 135
        val tilesInHorizontal = (Gdx.graphics.width.toFloat() / TILE_SIZE).ceil() + 1f
        val tilesInVertical = (Gdx.graphics.height.toFloat() / TILE_SIZE).ceil() + 1f
Minjae Song's avatar
Minjae Song committed
136 137 138



139
        val tilesBufferAsTex = Texture(tilesBuffer)
140
        tilesBufferAsTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
141 142
        tilesBufferAsTex.bind(2)
        tileAtlas.bind(1) // for some fuck reason, it must be bound as last
Minjae Song's avatar
Minjae Song committed
143

144 145
        shader.begin()
        shader.setUniformMatrix("u_projTrans", batch.projectionMatrix)//camera.combined)
146
        //shader.setUniformf("colourFilter", Color.RED)
147
        shader.setUniformf("screenDimension", Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
148 149
        shader.setUniformi("tilesAtlas", 1)
        shader.setUniformi("tilemap", 2)
150 151
        shader.setUniformi("tilemapDimension", tilesBuffer.width, tilesBuffer.height)
        shader.setUniformf("tilesInAxes", tilesInHorizontal, tilesInVertical)
Minjae Song's avatar
Minjae Song committed
152
        shader.setUniformf("cameraTranslation", cameraX, cameraY)
Minjae Song's avatar
Minjae Song committed
153 154 155
        shader.setUniformi("tileSizeInPx", 16)
        shader.setUniformi("tilesInAtlas", 256, 256) //depends on the tile atlas
        shader.setUniformi("atlasTexSize", 4096, 4096) //depends on the tile atlas
Minjae Song's avatar
Minjae Song committed
156
        tilesQuad.render(shader, GL20.GL_TRIANGLES)
157 158 159
        shader.end()
        tilesBufferAsTex.dispose()

Minjae Song's avatar
Minjae Song committed
160

161 162 163
        cameraX = 6f
        //cameraX += 160 * Gdx.graphics.deltaTime
        //cameraY += 160 * Gdx.graphics.deltaTime
Minjae Song's avatar
Minjae Song committed
164 165 166 167 168
    }

    override fun dispose() {
        shader.dispose()
    }
169 170
}