tiling using shader, sorta works

This commit is contained in:
minjaesong
2017-08-26 23:48:07 +09:00
parent e7c4a7b1e8
commit 3ffbb8bef6
6 changed files with 114 additions and 59 deletions

View File

@@ -5,30 +5,28 @@ import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.GdxRuntimeException
import net.torvald.terrarum.langpack.Lang
object ErrorDisp : Screen {
val logoTex = TerrarumAppLoader.logo
val font = TerrarumAppLoader.fontGame
private val logoTex = TerrarumAppLoader.logo
private val font = TerrarumAppLoader.fontGame
var title = Lang["ERROR_GENERIC_TEXT"]
var text = arrayListOf<String>("")
var text: List<String> = arrayListOf<String>("")
lateinit var batch: SpriteBatch
private lateinit var batch: SpriteBatch
val textPosX = 45f
private val textPosX = 45f
lateinit var camera: OrthographicCamera
val titleTextLeftMargin = 8
val titleText = "${TerrarumAppLoader.GAME_NAME} ${TerrarumAppLoader.getVERSION_STRING()}"
private val titleTextLeftMargin = 8
private val titleText = "${TerrarumAppLoader.GAME_NAME} ${TerrarumAppLoader.getVERSION_STRING()}"
override fun show() {

View File

@@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import net.torvald.terrarum.gameactors.ceilInt
import net.torvald.terrarum.worlddrawer.BlocksDrawer
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
/**
@@ -40,13 +41,15 @@ object GlslTilingTest : ApplicationAdapter() {
val TILE_SIZE = 16
lateinit var tilesBuffer: FloatArray
lateinit var tilesBuffer: Pixmap
lateinit var tileAtlas: Texture
override fun create() {
ShaderProgram.pedantic = false
shader = ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/loadingCircle.frag"))
shader = ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/tiling.frag"))
font = GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", flipY = false)
@@ -54,6 +57,10 @@ object GlslTilingTest : ApplicationAdapter() {
if (!shader.isCompiled) {
Gdx.app.log("Shader", shader.log)
//ErrorDisp.title = "Error in shader ${shader.vertexShaderSource}"
//ErrorDisp.text = shader.log.split('\n')
//TerrarumAppLoader.getINSTANCE().setScreen(ErrorDisp)
System.exit(1)
}
@@ -70,15 +77,15 @@ object GlslTilingTest : ApplicationAdapter() {
)
tilesQuad.setVertices(floatArrayOf(
0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, tilesInVertical,
Gdx.graphics.width.toFloat(), 0f, 0f, 1f, 1f, 1f, 1f, tilesInHorizontal, tilesInVertical,
Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, tilesInHorizontal, 0f,
0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, 1f,
Gdx.graphics.width.toFloat(), 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f,
Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 1f, 0f,
0f, Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 0f, 0f
))
tilesQuad.setIndices(shortArrayOf(0, 1, 2, 2, 3, 0))
tilesBuffer = FloatArray(tilesInHorizontal.ceilInt() * tilesInVertical.ceilInt())
tilesBuffer = Pixmap(tilesInHorizontal.ceilInt(), tilesInVertical.ceilInt(), Pixmap.Format.RGBA8888)
camera = OrthographicCamera(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
@@ -89,39 +96,50 @@ object GlslTilingTest : ApplicationAdapter() {
batch = SpriteBatch()
fucktex = Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"))
tileAtlas = Texture(Gdx.files.internal("assets/terrain.tga"))//BlocksDrawer.tilesTerrain.texture
println(tilesBuffer.format)
// 0brrrrrrrr_gggggggg_bbbbbbbb_aaaaaaaa
for (x in 0 until tilesBuffer.width * tilesBuffer.height) {
val color = Color(0f, 1f/16f, 0f, 1f)
tilesBuffer.drawPixel(x / tilesBuffer.width, x % tilesBuffer.width, 0x00ff00ff)
}
}
override fun render() {
Gdx.graphics.setTitle("ShitOnGlsl — F: ${Gdx.graphics.framesPerSecond}")
Gdx.graphics.setTitle("GlslTilingTest — F: ${Gdx.graphics.framesPerSecond}")
Gdx.gl.glClearColor(.094f, .094f, .094f, 0f)
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight())
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
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)
val tilesInHorizontal = Gdx.graphics.width.toFloat() / TILE_SIZE
val tilesInVertical = Gdx.graphics.height.toFloat() / TILE_SIZE
batch.inUse {
val tilesBufferAsTex = Texture(tilesBuffer)
tilesBufferAsTex.bind(2)
tileAtlas.bind(1) // for some fuck reason, it must be bound as last
batch.shader = shader
shader.setUniformMatrix("u_projTrans", camera.combined)
shader.setUniformi("u_texture", 0)
shader.setUniform1fv("tilesBuffer", tilesBuffer, 0, tilesBuffer.size)
//tilesQuad.render(shader, GL20.GL_TRIANGLES)
batch.draw(fucktex, 0f, 0f, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
}
/*shader.begin()
shader.setUniformMatrix("u_projTrans", batch.projectionMatrix)
shader.setUniformi("u_texture", 0)
shader.setUniformf("circleCentrePoint", Gdx.graphics.width / 2f, Gdx.graphics.height / 2f)
shader.setUniformf("colorCentrePoint", Gdx.graphics.width / 2f, Gdx.graphics.height / 2f)
shader.setUniformf("circleSize", 200f)
shader.begin()
shader.setUniformMatrix("u_projTrans", batch.projectionMatrix)//camera.combined)
shader.setUniformi("tilesAtlas", 1)
shader.setUniformi("tilemap", 2)
shader.setUniformf("tilemapSize", tilesBuffer.width.toFloat(), tilesBuffer.height.toFloat())
shader.setUniformf("tileInDim", tilesInHorizontal, tilesInVertical)
shader.setUniformf("cameraTranslation", 4f, 1f)
tilesQuad.render(shader, GL20.GL_TRIANGLES)
shader.end()*/
shader.end()
tilesBufferAsTex.dispose()
}
override fun dispose() {

View File

@@ -160,7 +160,7 @@ object Terrarum : Screen {
private set
private val localeSimple = arrayOf("de", "en", "es", "it")
var gameLocale = "lateinit"
var gameLocale = "lateinit" // TODO move into AppLoader
set(value) {
if (value.isBlank() || value.isEmpty()) {
field = sysLang

View File

@@ -17,8 +17,20 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack;
*/
public class TerrarumAppLoader implements ApplicationListener {
private static TerrarumAppLoader INSTANCE = null;
private TerrarumAppLoader() { }
public static TerrarumAppLoader getINSTANCE() {
if (INSTANCE == null) {
INSTANCE = new TerrarumAppLoader();
}
return INSTANCE;
}
public static final String GAME_NAME = "Terrarum";
public static final String COPYRIGHT_DATE_NAME = "Copyright 2013-2017 Torvald (minjaesong)";
public static final String GAME_LOCALE = System.getProperty("user.language") + System.getProperty("user.country");
/**
* 0xAA_BB_XXXX
@@ -62,9 +74,6 @@ public class TerrarumAppLoader implements ApplicationListener {
public Screen screen;
private TerrarumAppLoader() {
}
private void initViewPort(int width, int height) {
// Set Y to point downwards
camera.setToOrtho(true, width, height);

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum.langpack
import net.torvald.terrarum.utils.JsonFetcher
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.TerrarumAppLoader
import java.io.*
import java.util.*
@@ -111,7 +112,7 @@ object Lang {
fun fallback(): String = langpack["${key}_$FALLBACK_LANG_CODE"] ?: "$$key"
val ret = langpack["${key}_${Terrarum.gameLocale}"]
val ret = langpack["${key}_${TerrarumAppLoader.GAME_LOCALE}"]
val ret2 = if (ret.isNullOrEmpty()) fallback() else ret!!
// special treatment