general cleanup

This commit is contained in:
DaniTheSkunk 2022-09-18 19:38:02 +02:00
parent 287d5482e2
commit 7a792c2a46
11 changed files with 52 additions and 37 deletions

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ClassCanBeRecord" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
</profile>
</component>

View File

@ -1,5 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<pattern value="com.danitheskunk.skunkworks.IRenderContext" method="drawRectangle" />
<pattern value="com.danitheskunk.skunkworks.IRenderContext" method="drawTextureRectangle" />
<pattern value="com.danitheskunk.skunkworks.IRenderContext" />
<pattern value="com.danitheskunk.skunkworks.IWindow" method="loadTextureArray" />
<pattern value="com.danitheskunk.skunkworks.Recti" method="getX" />
<pattern value="com.danitheskunk.skunkworks.Recti" method="getY" />
<pattern value="com.danitheskunk.skunkworks.Recti" />
<pattern value="com.danitheskunk.skunkworks.Vec2f" method="add" />
<pattern value="com.danitheskunk.skunkworks.Vec2f" method="div" />
<pattern value="com.danitheskunk.skunkworks.Vec2f" method="mul" />
<pattern value="com.danitheskunk.skunkworks.Vec2f" method="sub" />
<pattern value="com.danitheskunk.skunkworks.Vec2i" method="add" />
<pattern value="com.danitheskunk.skunkworks.Vec2i" method="div" />
<pattern value="com.danitheskunk.skunkworks.Vec2i" method="divf" />
<pattern value="com.danitheskunk.skunkworks.Vec2i" method="sub" />
<pattern value="com.danitheskunk.skunkworks.GLRenderContext" method="drawRectangle" />
<pattern value="com.danitheskunk.skunkworks.GLRenderContext" method="drawTextureRectangle" />
<pattern value="com.danitheskunk.skunkworks.GLWindow" method="loadTextureArray" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>

View File

@ -1,11 +1,9 @@
import com.danitheskunk.skunkworks.*; import com.danitheskunk.skunkworks.*;
import org.w3c.dom.css.Rect;
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
var engine = new Engine(); var engine = new Engine();
var window = engine.openWindow(new Vec2i(1280, 720), "Skunkworks"); var window = engine.openWindow(new Vec2i(1280, 720), "Skunkworks");
var img2 = engine.loadImage("C:\\art\\pixel stuff.png");
var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png");
var tex2 = window.loadTexture("C:\\art\\pixel stuff.png"); var tex2 = window.loadTexture("C:\\art\\pixel stuff.png");
var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16)); var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16));
@ -16,10 +14,6 @@ public class Test {
while(!window.shouldClose()) { while(!window.shouldClose()) {
window.tick(); window.tick();
var renderContext = window.renderStart(); var renderContext = window.renderStart();
//renderContext.drawRectangle(
// new Recti(100, 100, 100, 100),
// new Color(150, 200, 250)
//);
renderContext.drawTextureRectangle( renderContext.drawTextureRectangle(
new Recti(0, 0, 1280, 720), new Recti(0, 0, 1280, 720),
tex tex

View File

@ -3,14 +3,13 @@ package com.danitheskunk.skunkworks;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import static com.danitheskunk.skunkworks.GraphicsBackend.*; import static com.danitheskunk.skunkworks.GraphicsBackend.*;
public class Engine { public class Engine {
private GraphicsBackend graphicsBackend; private final GraphicsBackend graphicsBackend;
//Constructors //Constructors
public Engine() { public Engine() {
@ -27,8 +26,6 @@ public class Engine {
byte[] bytes; byte[] bytes;
try(FileInputStream fis = new FileInputStream(path)) { try(FileInputStream fis = new FileInputStream(path)) {
bytes = fis.readAllBytes(); bytes = fis.readAllBytes();
} catch(FileNotFoundException e) {
throw new RuntimeException(e);
} catch(IOException e) { } catch(IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -3,7 +3,7 @@ package com.danitheskunk.skunkworks;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;
class GLRenderContext implements IRenderContext{ class GLRenderContext implements IRenderContext{
private GLTextureAtlas atlas; private final GLTextureAtlas atlas;
public GLRenderContext(GLTextureAtlas atlas) { public GLRenderContext(GLTextureAtlas atlas) {
this.atlas = atlas; this.atlas = atlas;

View File

@ -4,7 +4,7 @@ class GLTexture implements ITexture {
private Recti texArea; private Recti texArea;
private Image img; //for re-blitting onto texture atlas private final Image img; //for re-blitting onto texture atlas
GLTexture(Recti texArea, Image img) { GLTexture(Recti texArea, Image img) {
this.texArea = texArea; this.texArea = texArea;

View File

@ -1,10 +1,7 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.stb.STBRPContext;
import org.lwjgl.stb.STBRectPack;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -15,12 +12,11 @@ class GLTextureAtlas {
private GLTexture atlasTexture; //for debugging private GLTexture atlasTexture; //for debugging
private Image img; private Image img;
private boolean shouldUpdate; private boolean shouldUpdate;
private int textureID; private final List<GLTexture> textures;
private List<GLTexture> textures;
GLTextureAtlas() { GLTextureAtlas() {
img = new Image(new Vec2i(32, 32)); img = new Image(new Vec2i(32, 32));
textureID = glGenTextures(); int textureID = glGenTextures();
textures = new ArrayList<>(); textures = new ArrayList<>();
shouldUpdate = true; shouldUpdate = true;
@ -62,9 +58,9 @@ class GLTextureAtlas {
height = textures.get(0).getImg().getHeight(); height = textures.get(0).getImg().getHeight();
for(var tex : textures) { for(var tex : textures) {
var teximg = tex.getImg(); var texImg = tex.getImg();
//texture larger than atlas? resize atlas and try again //texture larger than atlas? resize atlas and try again
if(teximg.getHeight() > img.getHeight() || teximg.getWidth() > img.getWidth()) { if(texImg.getHeight() > img.getHeight() || texImg.getWidth() > img.getWidth()) {
System.out.println("Texture too large"); System.out.println("Texture too large");
doubleAtlasSize(); doubleAtlasSize();
repack(); repack();
@ -72,10 +68,10 @@ class GLTextureAtlas {
} }
//row full? start the next row //row full? start the next row
if(img.getWidth() - x < teximg.getWidth()) { if(img.getWidth() - x < texImg.getWidth()) {
x = 0; x = 0;
y += height; y += height;
height = teximg.getHeight(); height = texImg.getHeight();
//not enough space for new row? resize atlas and try again //not enough space for new row? resize atlas and try again
if(y + height > img.getHeight()) { if(y + height > img.getHeight()) {
@ -87,8 +83,8 @@ class GLTextureAtlas {
} }
//take space, advance to right //take space, advance to right
tex.setTexArea(new Recti(new Vec2i(x, y), teximg.getSize())); tex.setTexArea(new Recti(new Vec2i(x, y), texImg.getSize()));
x += teximg.getWidth(); x += texImg.getWidth();
} }
} }
@ -124,7 +120,7 @@ class GLTextureAtlas {
return img.getSize(); return img.getSize();
} }
private class TextureHeightComparator implements Comparator<GLTexture> { private static class TextureHeightComparator implements Comparator<GLTexture> {
@Override @Override
public int compare(GLTexture o1, GLTexture o2) { public int compare(GLTexture o1, GLTexture o2) {
return o1.getImg().getHeight() - o2.getImg().getHeight(); return o1.getImg().getHeight() - o2.getImg().getHeight();

View File

@ -3,7 +3,6 @@ package com.danitheskunk.skunkworks;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.stb.STBRectPack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -14,12 +13,12 @@ import static org.lwjgl.system.MemoryUtil.NULL;
class GLWindow implements IWindow { class GLWindow implements IWindow {
private boolean debug; private boolean debug;
private Engine engine; private final Engine engine;
private GLRenderContext renderContext; private final GLRenderContext renderContext;
private boolean shouldClose; private boolean shouldClose;
private Vec2i size; private final Vec2i size;
private GLTextureAtlas textureAtlas; private final GLTextureAtlas textureAtlas;
private long window; private final long window;
public GLWindow(Vec2i size, String title, Engine engine) { public GLWindow(Vec2i size, String title, Engine engine) {
GLFWErrorCallback.createPrint(System.err).set(); GLFWErrorCallback.createPrint(System.err).set();

View File

@ -5,8 +5,8 @@ import org.lwjgl.stb.STBImage;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
public class Image { public class Image {
private byte[] data; private final byte[] data;
private Vec2i size; private final Vec2i size;
//constructors //constructors
Image(ByteBuffer buffer) { Image(ByteBuffer buffer) {
@ -15,6 +15,7 @@ public class Image {
var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4); var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4);
size = new Vec2i(x[0], y[0]); size = new Vec2i(x[0], y[0]);
data = new byte[x[0] * y[0] * 4]; data = new byte[x[0] * y[0] * 4];
assert img != null;
img.get(data); img.get(data);
} }
@ -43,7 +44,7 @@ public class Image {
public Color getPixel(Vec2i pos) { public Color getPixel(Vec2i pos) {
int i = pos.getX() * 4 + pos.getY() * 4 * size.getX(); int i = pos.getX() * 4 + pos.getY() * 4 * size.getX();
return new Color( return new Color(
((int) data[i + 0]) & 0xFF, ((int) data[i]) & 0xFF,
((int) data[i + 1]) & 0xFF, ((int) data[i + 1]) & 0xFF,
((int) data[i + 2]) & 0xFF, ((int) data[i + 2]) & 0xFF,
((int) data[i + 3]) & 0xFF ((int) data[i + 3]) & 0xFF
@ -52,7 +53,7 @@ public class Image {
public void setPixel(Vec2i pos, Color col) { public void setPixel(Vec2i pos, Color col) {
int i = pos.getX() * 4 + pos.getY() * 4 * size.getX(); int i = pos.getX() * 4 + pos.getY() * 4 * size.getX();
data[i + 0] = (byte) (col.getR() & 0xFF); data[i] = (byte) (col.getR() & 0xFF);
data[i + 1] = (byte) (col.getG() & 0xFF); data[i + 1] = (byte) (col.getG() & 0xFF);
data[i + 2] = (byte) (col.getB() & 0xFF); data[i + 2] = (byte) (col.getB() & 0xFF);
data[i + 3] = (byte) (col.getA() & 0xFF); data[i + 3] = (byte) (col.getA() & 0xFF);

View File

@ -1,5 +1,6 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
@SuppressWarnings("SpellCheckingInspection")
public final class Recti { public final class Recti {
private final Vec2i pos, size; private final Vec2i pos, size;

View File

@ -1,5 +1,6 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
@SuppressWarnings("SpellCheckingInspection")
public final class Vec2i { public final class Vec2i {
private final int x, y; private final int x, y;