From 4deb2b1472881bc22f7a1384cf96b792169c86e7 Mon Sep 17 00:00:00 2001 From: Natty <31473117+493msi@users.noreply.github.com> Date: Tue, 5 Apr 2022 19:10:08 +0200 Subject: [PATCH] SDK restructure, package reorganization, new ModLoader, new resource system --- .../graphics/font/FontManager.java | 96 +++++++++++++++++++ .../plutoengine/gui/font/CharacterInfo.java | 6 ++ .../graphics/PlutoSpriteSheetMod.java | 63 ++++++++++++ 3 files changed, 165 insertions(+) create mode 100755 engine-core/plutogui/src/main/java/org/plutoengine/graphics/font/FontManager.java create mode 100755 engine-core/plutogui/src/main/java/org/plutoengine/gui/font/CharacterInfo.java create mode 100755 engine-core/plutospritesheet/src/main/java/org/plutoengine/graphics/PlutoSpriteSheetMod.java diff --git a/engine-core/plutogui/src/main/java/org/plutoengine/graphics/font/FontManager.java b/engine-core/plutogui/src/main/java/org/plutoengine/graphics/font/FontManager.java new file mode 100755 index 0000000..be6eed3 --- /dev/null +++ b/engine-core/plutogui/src/main/java/org/plutoengine/graphics/font/FontManager.java @@ -0,0 +1,96 @@ +package org.plutoengine.graphics.font; + +import org.plutoengine.graphics.texture.MagFilter; +import org.plutoengine.graphics.texture.MinFilter; +import org.plutoengine.graphics.texture.Texture; +import org.plutoengine.graphics.texture.WrapMode; +import org.plutoengine.graphics.texture.texture2d.RectangleTexture; +import org.plutoengine.gui.font.CharacterInfo; +import org.plutoengine.gui.font.Font; +import org.plutoengine.logger.Logger; +import org.plutoengine.logger.SmartSeverity; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + +public class FontManager +{ + private static final Map fonts = new HashMap<>(); + + public static void loadFont(Path address) + { + String fontname = null; + int width = 0; + int height = 0; + var def = new HashMap(); + + int row = 0; + + try + { + var lines = Files.readAllLines(address.resolve("definitions#txt")); + + for (var line : lines) + { + if (line.startsWith("//")) + continue; + + if (row == 0) + { + String[] fontinfo = line.split(","); + + fontname = fontinfo[0]; + + String[] dim = fontinfo[1].split("x"); + + width = Integer.parseInt(dim[0]); + height = Integer.parseInt(dim[1]); + } + else + { + String[] offs = line.split(" ")[1].split(";"); + + def.put(line.charAt(0), new CharacterInfo(row - 1, Integer.parseInt(offs[0]), Integer.parseInt(offs[1]))); + } + + row++; + } + } + catch (Exception e) + { + Logger.log(SmartSeverity.ERROR, "Could not load font: " + address.toString()); + e.printStackTrace(); + } + + Font font = new Font(fontname, width, height, def); + RectangleTexture texture = new RectangleTexture(); + texture.load(address.resolve("tex#png"), MagFilter.NEAREST, MinFilter.LINEAR, WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); + font.setTexture(texture); + + fonts.put(fontname, font); + } + + public static void unloadAll() + { + fonts.values() + .stream() + .map(Font::getTexture) + .forEach(Texture::delete); + fonts.clear(); + } + + public static Font getFontByName(String fontname) + { + var font = fonts.get(fontname); + + if (font == null) + { + // Logger.log(SmartSeverity.WARNING, "Font with name " + fontname + " could not be found, using the default one instead (if there is one)."); + return fonts.get("default"); + } + + return font; + } +} diff --git a/engine-core/plutogui/src/main/java/org/plutoengine/gui/font/CharacterInfo.java b/engine-core/plutogui/src/main/java/org/plutoengine/gui/font/CharacterInfo.java new file mode 100755 index 0000000..f0187cb --- /dev/null +++ b/engine-core/plutogui/src/main/java/org/plutoengine/gui/font/CharacterInfo.java @@ -0,0 +1,6 @@ +package org.plutoengine.gui.font; + +public record CharacterInfo(int number, int leftOffset, int rightOffset) +{ + +} diff --git a/engine-core/plutospritesheet/src/main/java/org/plutoengine/graphics/PlutoSpriteSheetMod.java b/engine-core/plutospritesheet/src/main/java/org/plutoengine/graphics/PlutoSpriteSheetMod.java new file mode 100755 index 0000000..da3b35d --- /dev/null +++ b/engine-core/plutospritesheet/src/main/java/org/plutoengine/graphics/PlutoSpriteSheetMod.java @@ -0,0 +1,63 @@ +package org.plutoengine.graphics; + +import org.plutoengine.ModLWJGL; +import org.plutoengine.Pluto; +import org.plutoengine.graphics.spritesheet.FramebufferTiledSpriteSheet; +import org.plutoengine.mod.IModEntryPoint; +import org.plutoengine.mod.Mod; +import org.plutoengine.mod.ModEntry; +import org.plutoengine.shader.PlutoShaderMod; +import org.plutoengine.shader.RenderShaderBuilder; + +@ModEntry(modID = PlutoSpriteSheetMod.MOD_ID, + version = Pluto.VERSION, + dependencies = { ModLWJGL.class, PlutoShaderMod.class }) +public class PlutoSpriteSheetMod implements IModEntryPoint +{ + public static final String MOD_ID = "tefek.plutospritesheet"; + + public static Mod instance; + + /** + * Strictly internal use only, do NOT use this outside of plutospritesheet + */ + private static Shader2D shader2D; + + /** + * Strictly internal use only, do NOT use this outside of plutospritesheet + */ + private static ShaderRectangle2D shaderRectangle2D; + + /** + * Strictly internal use only, do NOT use this outside of plutospritesheet + */ + private static ShaderRectangle2D spriteSheetShader; + + @Override + public void onLoad(Mod mod) + { + instance = mod; + + shader2D = new RenderShaderBuilder(mod.getResource("shaders.v2D#glsl"), mod.getResource("shaders.f2D#glsl")).build(Shader2D.class, false); + shaderRectangle2D = new RenderShaderBuilder(mod.getResource("shaders.VertexRectangle2D#glsl"), mod.getResource("shaders.FragmentRectangle2D#glsl")).build(ShaderRectangle2D.class, false); + spriteSheetShader = new RenderShaderBuilder(mod.getResource("shaders.VertexSpriteSheet#glsl"), mod.getResource("shaders.FragmentSpriteSheet#glsl")).build(ShaderRectangle2D.class, false); + + Renderer2D.load(shader2D); + RectangleRenderer2D.load(shaderRectangle2D); + + FramebufferTiledSpriteSheet.setSpriteShader(spriteSheetShader); + } + + @Override + public void onUnload() + { + FramebufferTiledSpriteSheet.setSpriteShader(null); + + spriteSheetShader.dispose(); + shaderRectangle2D.dispose(); + shader2D.dispose(); + + RectangleRenderer2D.unload(); + Renderer2D.unload(); + } +}