Skip to content

Commit

Permalink
Make Tetris sample more readable, add config reader. (JetBrains#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
olonho authored Jul 6, 2018
1 parent d474f86 commit 25690fa
Show file tree
Hide file tree
Showing 5 changed files with 576 additions and 479 deletions.
55 changes: 55 additions & 0 deletions samples/tetris/src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tetris

import platform.posix.*
import kotlinx.cinterop.*

object Config {
var width: Int = 10
private set
var height: Int = 20
private set
var startLevel = 0
private set

init {
val file = fopen("config.txt", "r")
if (file != null) {
try {
memScoped {
val bufferLength = 2 * 1024
val buffer = allocArray<ByteVar>(bufferLength)
while (true) {
val nextLine = fgets(buffer, bufferLength, file)?.toKString()
if (nextLine == null || nextLine.isEmpty()) break
val records = nextLine.split('=')
if (records.size != 2) continue
val key = records[0].trim()
val value = records[1].trim()
when (key) {
"width" -> width = value.toInt()
"height" -> height = value.toInt()
"startLevel" -> startLevel = value.toInt()
}
}
}
} finally {
fclose(file)
}
}
}
}
Loading

0 comments on commit 25690fa

Please sign in to comment.