Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Make Tetris sample more readable, add config reader. #1773

Merged
merged 1 commit into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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