Skip to content

Commit

Permalink
Merge pull request #32 from nickd3000/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nickd3000 authored Jan 14, 2025
2 parents ca7a868 + 1918d5d commit 5691033
Show file tree
Hide file tree
Showing 64 changed files with 1,023 additions and 236 deletions.
18 changes: 14 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<groupId>io.github.nickd3000</groupId>
<artifactId>garnet</artifactId>
<version>0.5.2</version>
<version>0.5.3</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/nickd3000/garnet</url>
<description>LWJGL Based game engine</description>

<properties>
<lwjgl.version>3.3.0</lwjgl.version>
<lwjgl.natives>natives-macos</lwjgl.natives>
<lwjgl.version>3.3.1</lwjgl.version>
<lwjgl.natives>natives-macos-arm64</lwjgl.natives>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
Expand Down Expand Up @@ -60,12 +60,14 @@

<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>

<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources_examples</directory>
</resource>

</resources>

<plugins>
Expand All @@ -75,22 +77,29 @@
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
<version>3.13.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<version>3.1.0</version>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<additionalOptions>-Xdoclint:none</additionalOptions> <!-- this prevents failed build -->
</configuration>
<executions>
<execution>
<id>attach-javadoc</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<release>17</release>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
</plugin>
Expand All @@ -105,6 +114,7 @@
</goals>
</execution>
</executions>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Feedback is really useful at this stage, so please feel free to create a small p
<dependency>
<groupId>io.github.nickd3000</groupId>
<artifactId>garnet</artifactId>
<version>0.5.2</version>
<version>0.5.3</version>
</dependency>
```

Expand Down
49 changes: 23 additions & 26 deletions src/main/java/com/physmo/garnet/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,30 @@
*/
public class Display {

private long windowHandle;
public double[] glViewportScale = new double[2];
public int[] glViewportOffsets = new int[2];
int primaryMonitorWidth;
int primaryMonitorHeight;

double windowScale = 1;
int storedWindowX = 0;
int storedWindowY = 0;
int canvasWidth = 0; // Canvas refers to the internal game width and height.
int canvasHeight = 0;

int[] canvasSize = new int[2];
private long windowHandle;
int stretchMode = 2;
private int windowWidth;
private int windowHeight;

public Display(int windowWidth, int windowHeight) {
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
canvasSize[0] = windowWidth;
canvasSize[1] = windowHeight;
}

public int[] getCanvasSize() {
return canvasSize;
}

public int getWindowWidth() {
return windowWidth;
Expand All @@ -67,12 +79,10 @@ public int getWindowHeight() {
return windowHeight;
}


public long getWindowHandle() {
return windowHandle;
}


public double[] getWindowToPixelsScale() {
int[] bufferSize = getWindowSize();
double w = (double) bufferSize[0] / (double) windowWidth;
Expand All @@ -86,7 +96,6 @@ public int[] getWindowSize() {

return new int[]{w2[0], h2[0]};
}
private int windowWidth;

public int[] getBufferSize() {
int[] w = new int[1], h = new int[1];
Expand Down Expand Up @@ -130,25 +139,13 @@ public void setFullScreen(boolean val) {
glfwSetWindowMonitor(windowHandle, 0, storedWindowX, storedWindowY, windowWidth, windowHeight, GLFW_DONT_CARE);
}
}
private int windowHeight;

public Display(int windowWidth, int windowHeight) {
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
canvasWidth = windowWidth;
canvasHeight = windowHeight;
}

public double[] getWindowToBufferScale() {
int[] bufferSize = getBufferSize();
double w = (double) bufferSize[0] / (double) windowWidth;
double h = (double) bufferSize[1] / (double) windowHeight;
return new double[]{w, h};

// int[] bufferSize = getBufferSize();
// double w = (double) canvasWidth / (double) windowWidth;
// double h = (double) canvasHeight / (double) windowHeight;
// return new double[]{w, h};
}

public void setWindowScale(double windowScale, boolean centerWindow) {
Expand Down Expand Up @@ -251,26 +248,26 @@ public void placeGlViewport() {
glViewport(0, 0, bufferSize[0], bufferSize[1]);
glViewportOffsets[0] = 0;
glViewportOffsets[1] = 0;
glViewportScale[0] = (double) canvasWidth / bufferSize[0];
glViewportScale[1] = (double) canvasHeight / bufferSize[1];
glViewportScale[0] = (double) canvasSize[0] / bufferSize[0];
glViewportScale[1] = (double) canvasSize[1] / bufferSize[1];
}

// Scale and keep aspect.
if (stretchMode == 2) {
double aspect = (double) canvasWidth / (double) canvasHeight;
double aspect = (double) canvasSize[0] / (double) canvasSize[1];
double windowAspect = (double) bufferSize[0] / (double) bufferSize[1];

int newWidth, newHeight;
double scale;

if (aspect > windowAspect) {
scale = (double) canvasWidth / bufferSize[0];
scale = (double) canvasSize[0] / bufferSize[0];
} else {
scale = (double) canvasHeight / bufferSize[1];
scale = (double) canvasSize[1] / bufferSize[1];
}

newWidth = (int) (canvasWidth / scale);
newHeight = (int) (canvasHeight / scale);
newWidth = (int) (canvasSize[0] / scale);
newHeight = (int) (canvasSize[1] / scale);
int xOffset = (bufferSize[0] - newWidth) / 2;
int yOffset = (bufferSize[1] - newHeight) / 2;

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/physmo/garnet/Garnet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class Garnet {

private final List<KeyboardCallback> keyboardCallbacks = new ArrayList<>();
private final GameClock gameClock = new GameClock();
private final double tickRate = 1;
private final Input input;
private final Display display;
private final Graphics graphics;
private final Sound sound;
private final DebugDrawer debugDrawer;
private final boolean drawFrameGraph = false;
private double tickRate = 1;
private GarnetApp garnetApp;
private double runningLogicDelta = 0;
/**
Expand All @@ -61,6 +61,14 @@ public Garnet(int windowWidth, int windowHeight) {
debugDrawer = new DebugDrawer(input);
}

public double getTickRate() {
return tickRate;
}

public void setTickRate(double tickRate) {
this.tickRate = tickRate;
}

public void setGarnetApp(GarnetApp garnetApp) {
this.garnetApp = garnetApp;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/physmo/garnet/graphics/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ private int calculateFrameFromTime(double t) {
/**
* The current frame of the animation as an index of the total number of frames.
*
* @return
* @return the current frame index
*/
public int getCurrentFrameIndex() {
return currentFrame;
}

/**
* Returns the current from as a SubImage object compatible with image drawing methods.
* Returns the current frame as a SubImage object compatible with image drawing methods.
*
* @return A SubImage object representing the image area of the frame.
*/
public SubImage getImage() {
public void getImage(SubImage outSubImage) {
FrameInfo frameInfo = frameList.get(currentFrame);
return tileSheet.getSubImage(frameInfo.col, frameInfo.row);
tileSheet.getSubImage(frameInfo.col, frameInfo.row, outSubImage);
}

private class FrameInfo {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/physmo/garnet/graphics/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public class Graphics {
private int currentDrawOrder;
private int currentlyBoundTextureId;
private int backgroundColor = 0;
private double xo = 0;
private double yo = 0;
private int clipRectHash = 0;
private int activeViewportId = 0;
SubImage subImage = new SubImage();

public Graphics(Display display) {
this.display = display;
Expand Down Expand Up @@ -152,9 +151,9 @@ public void setActiveViewport(int id) {
if (id == activeViewportId) return;
activeViewportId = id;
viewportManager.setActiveViewport(id);
Viewport viewport = viewportManager.getActiveViewport();
xo = viewport.getWindowX() - viewport.getX();
yo = viewport.getWindowY() - viewport.getY();
// Viewport viewport = viewportManager.getActiveViewport();
// xo = viewport.getWindowX() - viewport.getX();
// yo = viewport.getWindowY() - viewport.getY();
}

public void drawRect(float x, float y, float w, float h) {
Expand Down Expand Up @@ -205,6 +204,7 @@ public Sprite2D drawImage(TileSheet tileSheet, double x, double y, int tileX, in
return sprite2D;
}


/**
* Draws an image from a specified tile in the given TileSheet at the specified coordinates.
*
Expand All @@ -216,7 +216,8 @@ public Sprite2D drawImage(TileSheet tileSheet, double x, double y, int tileX, in
* @return the Sprite2D object representing the drawn image
*/
public Sprite2D drawImage(TileSheet tileSheet, double x, double y, int tileX, int tileY) {
return drawImage(tileSheet.getSubImage(tileX, tileY), x, y);
tileSheet.getSubImage(tileX, tileY, subImage);
return drawImage(subImage, x, y);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/physmo/garnet/graphics/ObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import java.lang.reflect.Array;

/**
* A generic object pool implementation that allows for efficient reuse of objects.
* The ObjectPool class maintains a pool of reusable objects to minimize object creation
* overhead by recycling objects instead of creating new ones each time.
*
* @param <T> the type of object to be pooled in this ObjectPool instance
*/
public class ObjectPool<T> {

T[] pool;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/physmo/garnet/graphics/SubImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@ public class SubImage {
public int w;
public int h;

public SubImage() {
texture = null;
x = 0;
y = 0;
w = 0;
h = 0;
}

public SubImage(Texture texture, int x, int y, int w, int h) {
this.texture = texture;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}

public void configure(Texture texture, int x, int y, int w, int h) {
this.texture = texture;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
}
22 changes: 20 additions & 2 deletions src/main/java/com/physmo/garnet/graphics/TileSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,28 @@ public int[] getTileCoordsFromIndex(int index) {
return new int[]{x, y};
}

public SubImage getSubImage(int column, int row) {
return new SubImage(texture, column * tileWidth, row * tileHeight, tileWidth, tileHeight);
/**
* Retrieves a portion of the texture based on the specified tile column and row,
* and configures the provided SubImage object with the resulting subimage's properties.
* <p>
* NOTE: To avoid allocating many new subImage objects, an output parameter is used.
*
* @param column the column index of the desired tile.
* @param row the row index of the desired tile.
* @param outSubImage the SubImage object to configure with the properties of the designated tile.
*/
public void getSubImage(int column, int row, SubImage outSubImage) {
outSubImage.configure(texture, column * tileWidth, row * tileHeight, tileWidth, tileHeight);
}

/**
* Calculates the index of a tile in a 1D array representation of the tile map
* based on its 2D coordinates within the tile sheet.
*
* @param x the x-coordinate (column) of the tile.
* @param y the y-coordinate (row) of the tile.
* @return the index of the tile in the 1D array representation.
*/
public int getTileIndexFromCoords(int x, int y) {
return x + (y * tilesWide);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/physmo/garnet/input/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ public void tick() {
keyboard.update();
}

/**
* Retrieves the Mouse instance used for handling mouse input.
*
* @return the Mouse instance associated with this input manager
*/
public Mouse getMouse() {
return mouse;
}

/**
* Retrieves the Keyboard instance used for handling keyboard input.
*
* @return the Keyboard instance associated with this input manager
*/
public Keyboard getKeyboard() {
return keyboard;
}
Expand Down
Loading

0 comments on commit 5691033

Please sign in to comment.