Skip to content

Commit

Permalink
fixed some problems, added multiplatform.
Browse files Browse the repository at this point in the history
  • Loading branch information
jombidev committed Apr 18, 2023
1 parent ee1fe4d commit f2e4ee6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 31 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly

plugins {
kotlin("jvm") version "1.8.0"
application
Expand All @@ -6,7 +8,7 @@ plugins {
group = "dev.jombi"
version = "1.0-DEV"
val lwjglVersion = "3.3.1"
val lwjglNatives = "natives-macos-arm64"
val natives = arrayOf("natives-windows", "natives-macos-arm64")

repositories {
mavenCentral()
Expand All @@ -21,12 +23,33 @@ dependencies {
implementation("org.lwjgl", "lwjgl-openal")
implementation("org.lwjgl", "lwjgl-opengl")
implementation("org.lwjgl", "lwjgl-stb")
runtimeOnly("org.lwjgl", "lwjgl", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-assimp", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-glfw", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-openal", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-opengl", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-stb", classifier = lwjglNatives)

natives.forEach {
runtimeOnly("org.lwjgl", "lwjgl", classifier = it)
runtimeOnly("org.lwjgl", "lwjgl-assimp", classifier = it)
runtimeOnly("org.lwjgl", "lwjgl-glfw", classifier = it)
runtimeOnly("org.lwjgl", "lwjgl-openal", classifier = it)
runtimeOnly("org.lwjgl", "lwjgl-opengl", classifier = it)
runtimeOnly("org.lwjgl", "lwjgl-stb", classifier = it)
}
}

val fatJar = task("fatJar", type = Jar::class) {
archiveFileName.set("${project.name.toLowerCaseAsciiOnly()}.jar")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Implementation-Title"] = "Gradle Jar File Example"
attributes["Implementation-Version"] = archiveVersion.get()
attributes["Main-Class"] = "dev.jombi.tetris.MainKt"
}
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
with(tasks.jar.get() as CopySpec)
}

tasks {
"build" {
dependsOn(fatJar)
}
}

tasks.test {
Expand All @@ -38,5 +61,5 @@ kotlin {
}

application {
mainClass.set("MainKt")
mainClass.set("dev.jombi.tetris.MainKt")
}
5 changes: 3 additions & 2 deletions src/main/kotlin/dev/jombi/tetris/Field.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Field {
var currentMino: Tetrimino? = null
var minoPosY = 0
set(value) {
if (currentMino == null) return
if (field == value) return
if (value in -2 until blockY && !checkFloor(currentMino!!, minoSpin, value)) {
field = value
Expand Down Expand Up @@ -237,8 +238,8 @@ class Field {
currentMino = null
}
} catch (e: ArrayIndexOutOfBoundsException) {
GLTetrisGame.instance().displayScreen(GuiGameOver())
GLTetrisGame.instance().finalizeGame()
GLTetrisGame.getInstance().displayScreen(GuiGameOver())
GLTetrisGame.getInstance().finalizeGame()
System.gc()
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/dev/jombi/tetris/GLTetrisGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import org.lwjgl.glfw.GLFWErrorCallback
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL11.*
import org.lwjgl.system.MemoryUtil.NULL
import kotlin.properties.Delegates

class GLTetrisGame {
companion object {
private val instance = GLTetrisGame()
fun instance() = instance
@JvmStatic
fun getInstance() = instance
}

private val gameTimer = Timer(20.0f, 0L)
private val keyTimer = Timer(40.0f, 0)
private var window by Delegates.notNull<Long>()
private var window = 0L
private var field: Field? = null
val WIDTH = 400
val HEIGHT = 490
Expand Down Expand Up @@ -170,9 +170,10 @@ class GLTetrisGame {
}

fun clear() {
glfwFreeCallbacks(window)
glfwDestroyWindow(window)

if (window != 0L) {
glfwFreeCallbacks(window)
glfwDestroyWindow(window)
}
glfwTerminate()
glfwSetErrorCallback(null)?.free()
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/dev/jombi/tetris/Main.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package dev.jombi.tetris

import java.lang.Exception

fun main() {
val tetrKt = GLTetrisGame.instance()
val tetrKt = GLTetrisGame.getInstance()
try {
tetrKt.init()
tetrKt.loop()
} catch (e: Exception) {
e.printStackTrace()
println("Error while loop.")
} finally {
tetrKt.clear()
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/dev/jombi/tetris/screen/impl/GuiGameOver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import dev.jombi.tetris.screen.overlay.Button

class GuiGameOver : GuiScreen() {
override fun initGui() {
val halfWidth = GLTetrisGame.instance().WIDTH / 2.0
val halfHeight = GLTetrisGame.instance().HEIGHT / 2.0
val halfWidth = GLTetrisGame.getInstance().WIDTH / 2.0
val halfHeight = GLTetrisGame.getInstance().HEIGHT / 2.0
buttons.add(Button("Retry", halfWidth, halfHeight) {
GLTetrisGame.instance().initGame()
GLTetrisGame.instance().displayScreen(null)
GLTetrisGame.getInstance().initGame()
GLTetrisGame.getInstance().displayScreen(null)
})

buttons.add(Button("Main menu", halfWidth, halfHeight + 40.0) {
GLTetrisGame.instance().displayScreen(GuiMainMenu.getInstance())
GLTetrisGame.getInstance().displayScreen(GuiMainMenu.getInstance())
})
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/dev/jombi/tetris/screen/impl/GuiMainMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class GuiMainMenu private constructor() : GuiScreen() {
fun getInstance() = instance
}
override fun initGui() {
val halfWidth = GLTetrisGame.instance().WIDTH / 2.0
val halfHeight = GLTetrisGame.instance().HEIGHT / 2.0
val halfWidth = GLTetrisGame.getInstance().WIDTH / 2.0
val halfHeight = GLTetrisGame.getInstance().HEIGHT / 2.0
buttons.add(Button("Start", halfWidth, halfHeight) {
GLTetrisGame.instance().initGame()
GLTetrisGame.instance().displayScreen(null)
GLTetrisGame.getInstance().initGame()
GLTetrisGame.getInstance().displayScreen(null)
})
buttons.add(Button("Exit", halfWidth, halfHeight + 40.0) {
GLTetrisGame.instance().exitGame()
GLTetrisGame.getInstance().exitGame()
})
}

Expand All @@ -34,7 +34,7 @@ class GuiMainMenu private constructor() : GuiScreen() {

override fun drawScreen(mouseX: Double, mouseY: Double) {
super.drawScreen(mouseX, mouseY)
val halfWidth = GLTetrisGame.instance().WIDTH / 2.0
val halfWidth = GLTetrisGame.getInstance().WIDTH / 2.0
val TARGET_FONT = FontFactory.getFont(FontType.ARIAL, 48)
val (fWidth, fHeight) = TARGET_FONT.getStringBound("TetrKt")

Expand Down

0 comments on commit f2e4ee6

Please sign in to comment.