Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mudlee committed May 5, 2021
0 parents commit 650320d
Show file tree
Hide file tree
Showing 34 changed files with 1,117 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## Java

*.class
*.war
*.ear
hs_err_pid*

## Android Studio and Intellij and Android in general
.idea/
*.ipr
*.iws
*.iml
com_crashlytics_export_strings.xml

## Eclipse

.classpath
.project
.metadata/
/core/bin/
/desktop/bin/
*.tmp
*.bak
*.swp
*~.nib
.settings/
.loadpath
.externalToolBuilders/
*.launch

## NetBeans

/nbproject/private/
/core/nbproject/private/
/desktop/nbproject/private/

/build/
/core/build/
/desktop/build/

/nbbuild/
/core/nbbuild/
/desktop/nbbuild/

/dist/
/core/dist/
/desktop/dist/

/nbdist/
/core/nbdist/
/desktop/nbdist/

nbactions.xml
nb-configuration.xml

## OS Specific
.DS_Store
Thumbs.db

## SPCK Specific
build
.gradle
target
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Project Spck

```bash
# Run from command line:
$ ./gradlew run
```

```bash
# Package
$ ./gradlew jlink
```
22 changes: 22 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id("java")
id("org.javamodularity.moduleplugin").version("1.7.0").apply(false)
}

allprojects {
repositories {
mavenCentral()
maven("https://oss.sonatype.org/content/repositories/snapshots/")
}
}

subprojects {
apply(plugin = "org.javamodularity.moduleplugin")
group = "spck"
version = "1.0.0"

java {
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
}
}
60 changes: 60 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import org.gradle.nativeplatform.platform.internal.ArchitectureInternal
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.gradle.nativeplatform.platform.internal.DefaultOperatingSystem

plugins {
id("java-library")
}

val lwjglVersion = "3.3.0-SNAPSHOT"
val jomlVersion = "1.10.1"
val slf4jVersion = "1.8.0-beta4"
val resourcesPath = "../resources"

val currentOs: DefaultOperatingSystem = DefaultNativePlatform.getCurrentOperatingSystem()
val currentArch: ArchitectureInternal = DefaultNativePlatform.getCurrentArchitecture()
logger.quiet("[SPCK] System: ${currentOs.displayName} / ${currentArch.displayName}")

fun specifyLwjglNatives(): String {
val currentOs = DefaultNativePlatform.getCurrentOperatingSystem()
val currentArch = DefaultNativePlatform.getCurrentArchitecture()
when {
currentOs.isMacOsX -> {
if(currentArch.displayName.contains("aarch64")) {
return "natives-macos-arm64"
}
return "natives-macos"
}
currentOs.isWindows -> {
if(currentArch.displayName.contains("aarch64")) {
return "natives-windows-arm64"
}
return "natives-windows"
}
currentOs.isLinux -> {
return "natives-linux"
}
else -> throw Error("Unrecognized or unsupported Operating system.")
}
}

val lwjglNatives = specifyLwjglNatives()

logger.quiet("[SPCK] Natives in use: $lwjglNatives")
logger.quiet("[SPCK] Resources path: $resourcesPath")

sourceSets.main.get().resources.srcDirs(resourcesPath)

dependencies {
api(platform("org.lwjgl:lwjgl-bom:$lwjglVersion"))

implementation("org.lwjgl", "lwjgl")
api("org.lwjgl", "lwjgl-glfw")
implementation("org.lwjgl", "lwjgl-opengl")
implementation("org.lwjgl", "lwjgl", classifier = lwjglNatives)
implementation("org.lwjgl", "lwjgl-glfw", classifier = lwjglNatives)
implementation("org.lwjgl", "lwjgl-opengl", classifier = lwjglNatives)
implementation("org.joml", "joml", jomlVersion)
implementation("org.slf4j", "slf4j-api", slf4jVersion)
implementation("org.slf4j", "slf4j-simple", slf4jVersion)
}
15 changes: 15 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module spck.core {
requires org.lwjgl;
requires org.lwjgl.natives;
requires org.lwjgl.opengl;
requires org.lwjgl.opengl.natives;
requires transitive org.lwjgl.glfw;
requires org.lwjgl.glfw.natives;
requires org.joml;
requires org.slf4j;

exports spck.core;
exports spck.core.input;
exports spck.core.props;
exports spck.core.props.type;
}
61 changes: 61 additions & 0 deletions core/src/main/java/spck/core/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package spck.core;

import org.joml.Vector4f;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spck.core.props.Preferences;
import spck.core.render.Renderer;
import spck.core.window.Window;

public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
private final Window window;
private final LifeCycleListener lifeCycleListener;
private final Renderer renderer;

public Application(LifeCycleListener lifeCycleListener, Preferences preferences) {
if(Spck.app != null) {
throw new SpckRuntimeException("Cannot run multiple applications");
}
Spck.app = this;
this.lifeCycleListener = lifeCycleListener;
renderer = new Renderer(preferences.renderBackend, preferences.debug);
window = new Window(preferences, renderer);
}

public void start() {
log.info("Starting up application...");

window.create();
lifeCycleListener.onCreated();

loop();

log.info("Application is shutting down");
renderer.dispose();
window.dispose();
log.info("Terminated");
}

public void stop() {
window.close();
}

private void loop() {
long lastTime = System.nanoTime();

renderer.setClearColor(new Vector4f(1f, 1f, 1f, 1f));
renderer.setClearFlags(Spck.types.bufferBit.color);

while (!window.shouldClose()) {
renderer.clear();
long now = System.nanoTime();
long frameTimeNanos = now - lastTime;
float frameTime = frameTimeNanos / 1_000_000_000f;
lastTime = now;

renderer.swapBuffers(frameTime);
window.pollEvents();
}
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/spck/core/Disposable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package spck.core;

public interface Disposable {
void dispose();
}
5 changes: 5 additions & 0 deletions core/src/main/java/spck/core/LifeCycleListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package spck.core;

public interface LifeCycleListener {
default void onCreated() {}
}
10 changes: 10 additions & 0 deletions core/src/main/java/spck/core/Spck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package spck.core;

import spck.core.input.InputManager;
import spck.core.props.type.Types;

public class Spck {
public static final InputManager input = new InputManager();
public static Application app;
public static Types types;
}
7 changes: 7 additions & 0 deletions core/src/main/java/spck/core/SpckRuntimeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package spck.core;

public class SpckRuntimeException extends RuntimeException {
public SpckRuntimeException(String message) {
super(message);
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/spck/core/input/InputManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package spck.core.input;

public class InputManager {
private InputMultiplexer multiplexer;

public void setMultiplexer(InputMultiplexer multiplexer) {
this.multiplexer = multiplexer;
}

public void keyCallback(int key, int scancode, int action, int mods) {
if(multiplexer != null) {
multiplexer.keyCallback(key, scancode, action, mods);
}
}
}
34 changes: 34 additions & 0 deletions core/src/main/java/spck/core/input/InputMultiplexer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spck.core.input;

import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.glfw.GLFW.*;

public class InputMultiplexer {
private final List<InputProcessor> processors = new ArrayList<>();

public int addProcessor(InputProcessor processor) {
processors.add(processor);
return processors.size() - 1;
}

public void removeProcessor(int index) {
processors.remove(index);
}

public void keyCallback(int key, int scancode, int action, int mods) {
switch (action) {
case GLFW_PRESS:
for (InputProcessor processor : processors) {
processor.onKeyPress(key);
}
break;
case GLFW_RELEASE:
for (InputProcessor processor : processors) {
processor.onKeyReleased(key);
}
break;
}
}
}
7 changes: 7 additions & 0 deletions core/src/main/java/spck/core/input/InputProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package spck.core.input;

public interface InputProcessor {
default void onKeyPress(int keyCode) {}

default void onKeyReleased(int keyCode) {}
}
20 changes: 20 additions & 0 deletions core/src/main/java/spck/core/props/Antialiasing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package spck.core.props;

public enum Antialiasing {
OFF(0),
ANTIALISING_2X(2),
ANTIALISING_4X(4),
ANTIALISING_8X(8),
ANTIALISING_16X(16);

private int value;

Antialiasing(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

17 changes: 17 additions & 0 deletions core/src/main/java/spck/core/props/Preferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package spck.core.props;

import org.joml.Vector2i;

public class Preferences {
public enum RendererBackend {
OPENGL,
}

public String title = "Project SPCK";
public RendererBackend renderBackend = RendererBackend.OPENGL;
public Antialiasing antialiasing = Antialiasing.OFF;
public Vector2i windowSize = new Vector2i(1280, 720);
public boolean vSync = false;
public boolean fullscreen = false;
public boolean debug = false;
}
13 changes: 13 additions & 0 deletions core/src/main/java/spck/core/props/type/BufferBit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package spck.core.props.type;

public class BufferBit {
public final int color;
public final int depth;
public final int stencil;

public BufferBit(int color, int depth, int stencil) {
this.color = color;
this.depth = depth;
this.stencil = stencil;
}
}
9 changes: 9 additions & 0 deletions core/src/main/java/spck/core/props/type/Types.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package spck.core.props.type;

public class Types {
public final BufferBit bufferBit;

public Types(BufferBit bufferBit) {
this.bufferBit = bufferBit;
}
}
Loading

0 comments on commit 650320d

Please sign in to comment.