Skip to content

Commit

Permalink
Merge pull request #12 from DominicDolan/develop
Browse files Browse the repository at this point in the history
Merging to Master
  • Loading branch information
DominicDolan authored Jul 30, 2020
2 parents c3f803e + 88c5e90 commit 4ac4671
Show file tree
Hide file tree
Showing 239 changed files with 5,512 additions and 1,710 deletions.
39 changes: 38 additions & 1 deletion README.md
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
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
}
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
}
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
}
}
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()
}
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)
}
}
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)
}
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()

}
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")
}
}
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()
}
}
}
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)
}
}
}
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
}

}
}
}
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
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mechanica.engine.context.loader

import com.mechanica.engine.audio.AudioFile
import java.nio.ByteBuffer
import java.nio.FloatBuffer
import java.nio.IntBuffer
Expand Down
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.mechanica.engine.context.loader

import com.mechanica.engine.text.Font
import com.mechanica.engine.resources.Resource
import com.mechanica.engine.text.FontAtlasConfiguration

interface FontLoader {
val defaultFont: Font
fun font(res: Resource): Font
fun font(res: Resource, initializer: FontAtlasConfiguration.() -> Unit): Font

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.mechanica.engine.context.loader
import com.mechanica.engine.context.GLInitializer
import com.mechanica.engine.shader.qualifiers.AttributeQualifier
import com.mechanica.engine.shader.qualifiers.Qualifier
import com.mechanica.engine.shader.script.Shader
import com.mechanica.engine.shader.script.ShaderScript
import com.mechanica.engine.vertices.ElementArrayType

interface GLLoader {
Expand All @@ -11,10 +13,17 @@ interface GLLoader {
val bufferLoader: BufferLoader
val fontLoader: FontLoader
val graphicsLoader: GraphicsLoader
val audioLoader: AudioLoader
val inputLoader: InputLoader

fun createAttributeLoader(qualifier: AttributeQualifier): AttributeLoader
fun createUniformLoader(qualifier: Qualifier): UniformLoader
fun createElementArray(): ElementArrayType
fun defaultShader(
vertex: ShaderScript,
fragment: ShaderScript,
tessellation: ShaderScript?,
geometry: ShaderScript?): Shader

companion object : GLLoader by GLInitializer.loader
}
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
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mechanica.engine.context.loader

import com.mechanica.engine.shader.qualifiers.Qualifier
import com.mechanica.engine.shader.vars.uniforms.vars.*
import com.mechanica.engine.shader.uniforms.vars.*
import org.joml.Matrix4f

interface UniformLoader {
Expand Down
Loading

0 comments on commit 4ac4671

Please sign in to comment.