-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from DominicDolan/develop
Merging to Master
- Loading branch information
Showing
239 changed files
with
5,512 additions
and
1,710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,38 @@ | ||
|
||
## Mechanica, a 2D Game Engine in Kotlin | ||
A powerful 2D Game Engine written in Kotlin. This project is still under development and it is not | ||
thoroughly tested or documented but feel free to try it out nonetheless | ||
|
||
### Setting up the project with Gradle | ||
Clone or download the repository and build with gradle | ||
|
||
Create a new Project with gradle. In the `settings.gradle` file add: | ||
|
||
```kotlin | ||
includeBuild("C:/path/to/mechanica") | ||
``` | ||
And in the `build.gradle` file add a dependency on the project: | ||
```kotlin | ||
dependencies { | ||
implementation("com.mechanica.engine:mechanica:1.0") | ||
} | ||
``` | ||
|
||
In the new project create a main method with something similar to the following: | ||
```kotlin | ||
fun main() { | ||
Game.configure { | ||
setViewport(height = 10.0) | ||
} | ||
|
||
// Create an instance of the Drawer class, | ||
// it can be used to draw anything from rounded rectangles to custom shaders | ||
val draw = Drawer.create() | ||
|
||
// Start the game loop and draw text to the screen | ||
Game.run { | ||
draw.centered.grey.text("Hello, Mechanica") | ||
} | ||
} | ||
``` | ||
|
||
More samples can be seen in the `samples` module in Mechanica |
10 changes: 10 additions & 0 deletions
10
application-interface/src/main/kotlin/com/mechanica/engine/audio/AudioFile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.mechanica.engine.audio | ||
|
||
import java.nio.ShortBuffer | ||
|
||
interface AudioFile { | ||
val channels: Int | ||
val sampleRate: Int | ||
val buffer: ShortBuffer | ||
val format: Int | ||
} |
9 changes: 9 additions & 0 deletions
9
application-interface/src/main/kotlin/com/mechanica/engine/audio/AudioObject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mechanica.engine.audio | ||
|
||
import org.joml.Vector3f | ||
|
||
interface AudioObject { | ||
var gain: Float | ||
var position: Vector3f | ||
var velocity: Vector3f | ||
} |
24 changes: 24 additions & 0 deletions
24
application-interface/src/main/kotlin/com/mechanica/engine/audio/Listener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.mechanica.engine.audio | ||
|
||
import com.mechanica.engine.context.loader.GLLoader | ||
import org.joml.Vector3f | ||
|
||
interface Listener : AudioObject { | ||
var distanceModel: DistanceModel | ||
val orientation: Orientaion | ||
|
||
interface Orientaion { | ||
var at: Vector3f | ||
var up: Vector3f | ||
} | ||
|
||
companion object : Listener by GLLoader.audioLoader.listener() | ||
|
||
enum class DistanceModel { | ||
INVERSE, | ||
LINEAR, | ||
EXPONENTIAL; | ||
|
||
var clamped: Boolean = false | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
application-interface/src/main/kotlin/com/mechanica/engine/audio/Sound.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mechanica.engine.audio | ||
|
||
interface Sound : AudioFile { | ||
val id: Int | ||
val frequency: Int | ||
fun destroy() | ||
} |
21 changes: 21 additions & 0 deletions
21
application-interface/src/main/kotlin/com/mechanica/engine/audio/SoundSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mechanica.engine.audio | ||
|
||
import com.mechanica.engine.context.loader.GLLoader | ||
import com.mechanica.engine.resources.AudioResource | ||
|
||
interface SoundSource : AudioObject { | ||
val id: Int | ||
val sound: Sound | ||
var pitch: Float | ||
var rolloff: Float | ||
var maxDistance: Float | ||
var referenceDistance: Float | ||
fun play() | ||
fun pause() | ||
fun stop() | ||
fun destroy() | ||
|
||
companion object { | ||
fun create(res: AudioResource) = GLLoader.audioLoader.source(res.sound) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
application-interface/src/main/kotlin/com/mechanica/engine/configuration/Configurable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mechanica.engine.configuration | ||
|
||
import com.mechanica.engine.context.Application | ||
|
||
interface Configurable<T> { | ||
fun configureAs(application: Application, setup: T.() -> Unit) | ||
} |
13 changes: 13 additions & 0 deletions
13
application-interface/src/main/kotlin/com/mechanica/engine/context/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mechanica.engine.context | ||
|
||
import com.mechanica.engine.display.Window | ||
|
||
interface Application { | ||
|
||
fun initialize(window: Window) | ||
|
||
fun terminate() | ||
|
||
fun startFrame() | ||
|
||
} |
15 changes: 14 additions & 1 deletion
15
application-interface/src/main/kotlin/com/mechanica/engine/context/GLInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
package com.mechanica.engine.context | ||
|
||
import com.mechanica.engine.context.callbacks.EventCallbacks | ||
import com.mechanica.engine.context.loader.DisplayLoader | ||
import com.mechanica.engine.context.loader.GLLoader | ||
|
||
object GLInitializer { | ||
private var _loader: GLLoader? = null | ||
internal val loader: GLLoader | ||
get() = _loader ?: throw UninitializedPropertyAccessException("The OpenGL context has not been initialized") | ||
|
||
fun initialize(loader: GLLoader) { | ||
private var _displayLoader: DisplayLoader? = null | ||
internal val displayLoader: DisplayLoader | ||
get() = _displayLoader ?: throw UninitializedPropertyAccessException("The OpenGL context has not been initialized") | ||
|
||
fun initializeDisplay(loader: DisplayLoader) { | ||
if (_loader == null) { | ||
_displayLoader = loader | ||
} else throw IllegalStateException("The Display context has already been initialized") | ||
} | ||
|
||
fun initialize(loader: GLLoader): EventCallbacks { | ||
if (_loader == null) { | ||
_loader = loader | ||
return EventCallbacks.create() | ||
} else throw IllegalStateException("The OpenGl context has already been initialized") | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...cation-interface/src/main/kotlin/com/mechanica/engine/context/callbacks/EventCallbacks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mechanica.engine.context.callbacks | ||
|
||
import com.mechanica.engine.input.TextInput | ||
|
||
interface EventCallbacks { | ||
val keyboardHandler: KeyboardHandler | ||
val mouseHandler: MouseHandler | ||
|
||
companion object { | ||
fun prepare() { | ||
TextInput.prepare() | ||
MouseHandler.prepare() | ||
} | ||
|
||
internal fun create(): EventCallbacks = object : EventCallbacks { | ||
override val keyboardHandler = KeyboardHandler.create() | ||
override val mouseHandler = MouseHandler.create() | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ation-interface/src/main/kotlin/com/mechanica/engine/context/callbacks/KeyboardHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.mechanica.engine.context.callbacks | ||
|
||
import com.mechanica.engine.input.KeyInput | ||
import com.mechanica.engine.input.TextInput | ||
|
||
interface KeyboardHandler { | ||
fun keyPressed(key: Int) | ||
fun keyReleased(key: Int) | ||
fun textInput(codepoint: Int) | ||
|
||
companion object { | ||
internal fun create(): KeyboardHandler = object : KeyboardHandler { | ||
override fun keyPressed(key: Int) = KeyInput.addPressed(key) | ||
override fun keyReleased(key: Int) = KeyInput.removePressed(key) | ||
override fun textInput(codepoint: Int) = TextInput.addCodePoint(codepoint) | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
application-interface/src/main/kotlin/com/mechanica/engine/context/callbacks/MouseHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.mechanica.engine.context.callbacks | ||
|
||
import com.mechanica.engine.input.KeyInput | ||
|
||
interface MouseHandler { | ||
fun buttonPressed(key: Int) | ||
fun buttonReleased(key: Int) | ||
fun cursorMoved(x: Double, y: Double) | ||
fun scroll(x: Double, y: Double) | ||
|
||
companion object { | ||
var cursorX: Double = 0.0 | ||
private set | ||
var cursorY: Double = 0.0 | ||
private set | ||
|
||
var scrollX: Double = 0.0 | ||
private set | ||
var scrollY: Double = 0.0 | ||
private set | ||
|
||
internal fun prepare() { | ||
KeyInput.removePressed(1000) | ||
KeyInput.removePressed(1001) | ||
KeyInput.removePressed(1002) | ||
scrollX = 0.0 | ||
scrollY = 0.0 | ||
} | ||
|
||
internal fun create() = object : MouseHandler { | ||
override fun buttonPressed(key: Int) { | ||
KeyInput.addPressed(key) | ||
} | ||
|
||
override fun buttonReleased(key: Int) { | ||
KeyInput.removePressed(key) | ||
} | ||
|
||
override fun cursorMoved(x: Double, y: Double) { | ||
cursorX = x | ||
cursorY = y | ||
} | ||
|
||
override fun scroll(x: Double, y: Double) { | ||
if (y > 0.0) { | ||
KeyInput.addPressed(1000) | ||
} else if (y < 0.0) { | ||
KeyInput.addPressed(1001) | ||
} | ||
KeyInput.addPressed(1002) | ||
scrollX = x | ||
scrollY = y | ||
} | ||
|
||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
application-interface/src/main/kotlin/com/mechanica/engine/context/loader/AudioLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mechanica.engine.context.loader | ||
|
||
import com.mechanica.engine.audio.Listener | ||
import com.mechanica.engine.audio.Sound | ||
import com.mechanica.engine.audio.SoundSource | ||
|
||
interface AudioLoader { | ||
fun sound(file: String): Sound | ||
fun source(sound: Sound): SoundSource | ||
fun listener(): Listener | ||
} |
1 change: 1 addition & 0 deletions
1
application-interface/src/main/kotlin/com/mechanica/engine/context/loader/BufferLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
application-interface/src/main/kotlin/com/mechanica/engine/context/loader/DisplayLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.mechanica.engine.context.loader | ||
|
||
import com.mechanica.engine.context.GLInitializer | ||
import com.mechanica.engine.display.Monitor | ||
import com.mechanica.engine.display.Window | ||
|
||
interface DisplayLoader { | ||
fun createWindow(title: String, width: Int, height: Int): Window | ||
fun createWindow(title: String, monitor: Monitor): Window | ||
fun createWindow(title: String, width: Int, height: Int, monitor: Monitor): Window | ||
|
||
val allMonitors: Array<out Monitor> | ||
|
||
fun getPrimaryMonitor(): Monitor | ||
|
||
companion object : DisplayLoader by GLInitializer.displayLoader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
application-interface/src/main/kotlin/com/mechanica/engine/context/loader/InputLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mechanica.engine.context.loader | ||
|
||
import com.mechanica.engine.input.KeyIDs | ||
|
||
interface InputLoader { | ||
fun keyIds(): KeyIDs | ||
} |
2 changes: 1 addition & 1 deletion
2
application-interface/src/main/kotlin/com/mechanica/engine/context/loader/UniformLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.