Fast-forward master to working and delete working #1

Merged
AMNatty merged 29 commits from working into master 2022-04-06 09:04:32 +00:00
3 changed files with 165 additions and 0 deletions
Showing only changes of commit 4deb2b1472 - Show all commits

View File

@ -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<String, Font> fonts = new HashMap<>();
public static void loadFont(Path address)
{
String fontname = null;
int width = 0;
int height = 0;
var def = new HashMap<Character, CharacterInfo>();
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;
}
}

View File

@ -0,0 +1,6 @@
package org.plutoengine.gui.font;
public record CharacterInfo(int number, int leftOffset, int rightOffset)
{
}

View File

@ -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();
}
}