Create ByteBufferAssetManager
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.bartlomiejpluta.base.engine.common.manager;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public interface ByteBufferAssetManager<A> {
|
||||
void registerAsset(A asset);
|
||||
|
||||
ByteBuffer loadObjectByteBuffer(String uid);
|
||||
}
|
||||
@@ -100,7 +100,7 @@ public class DefaultContext implements Context {
|
||||
@Override
|
||||
public GUI newGUI() {
|
||||
log.info("Creating new GUI");
|
||||
var gui = new NanoVGGUI(fontManager);
|
||||
var gui = new NanoVGGUI(fontManager, imageManager);
|
||||
guis.add(gui);
|
||||
gui.init(screen);
|
||||
return gui;
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.bartlomiejpluta.base.engine.gui.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.gui.asset.FontAsset;
|
||||
import com.bartlomiejpluta.base.engine.gui.model.Font;
|
||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||
import com.bartlomiejpluta.base.engine.util.res.ResourcesManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -30,7 +29,7 @@ public class DefaultFontManager implements FontManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Font loadObject(String uid) {
|
||||
public ByteBuffer loadObjectByteBuffer(String uid) {
|
||||
var buffer = fontBuffers.get(uid);
|
||||
|
||||
if (buffer == null) {
|
||||
@@ -46,6 +45,6 @@ public class DefaultFontManager implements FontManager {
|
||||
fontBuffers.put(uid, buffer);
|
||||
}
|
||||
|
||||
return new Font(uid, buffer.duplicate());
|
||||
return buffer.duplicate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.bartlomiejpluta.base.engine.gui.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
||||
import com.bartlomiejpluta.base.engine.common.manager.ByteBufferAssetManager;
|
||||
import com.bartlomiejpluta.base.engine.gui.asset.FontAsset;
|
||||
import com.bartlomiejpluta.base.engine.gui.model.Font;
|
||||
|
||||
public interface FontManager extends AssetManager<FontAsset, Font> {
|
||||
public interface FontManager extends ByteBufferAssetManager<FontAsset> {
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.bartlomiejpluta.base.engine.gui.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class Font {
|
||||
private final String name;
|
||||
private final ByteBuffer byteBuffer;
|
||||
}
|
||||
@@ -11,10 +11,15 @@ import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||
import com.bartlomiejpluta.base.engine.gui.widget.ScreenWidget;
|
||||
import com.bartlomiejpluta.base.engine.world.image.manager.ImageManager;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.lwjgl.nanovg.NVGColor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.lwjgl.nanovg.NanoVG.*;
|
||||
@@ -22,22 +27,19 @@ import static org.lwjgl.nanovg.NanoVGGL3.*;
|
||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class NanoVGGUI implements GUI {
|
||||
private final FontManager fontManager;
|
||||
private final ImageManager imageManager;
|
||||
|
||||
private long context;
|
||||
|
||||
private ScreenWidget screenWidget;
|
||||
|
||||
private NVGColor fillColor;
|
||||
private NVGColor strokeColor;
|
||||
private final Set<String> loadedFonts = new HashSet<>();
|
||||
private final Map<String, Integer> loadedImages = new HashMap<>();
|
||||
private final float[] boundBuffer = new float[4];
|
||||
|
||||
private final FontManager fontManager;
|
||||
|
||||
public NanoVGGUI(FontManager fontManager) {
|
||||
this.fontManager = fontManager;
|
||||
}
|
||||
|
||||
public void init(Screen screen) {
|
||||
context = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
|
||||
|
||||
@@ -213,8 +215,8 @@ public class NanoVGGUI implements GUI {
|
||||
@Override
|
||||
public void setFontFace(String fontUid) {
|
||||
if (!loadedFonts.contains(fontUid)) {
|
||||
var font = fontManager.loadObject(fontUid);
|
||||
nvgCreateFontMem(context, fontUid, font.getByteBuffer(), 0);
|
||||
var fontBuffer = fontManager.loadObjectByteBuffer(fontUid);
|
||||
nvgCreateFontMem(context, fontUid, fontBuffer, 0);
|
||||
loadedFonts.add(fontUid);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||
import com.bartlomiejpluta.base.engine.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.engine.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.engine.util.res.ResourcesManager;
|
||||
import com.bartlomiejpluta.base.engine.world.image.asset.ImageAsset;
|
||||
import com.bartlomiejpluta.base.engine.world.image.model.DefaultImage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -14,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -23,7 +25,9 @@ import java.util.Map;
|
||||
public class DefaultImageManager implements ImageManager {
|
||||
private final MeshManager meshManager;
|
||||
private final TextureManager textureManager;
|
||||
private final ResourcesManager resourcesManager;
|
||||
private final Map<String, ImageAsset> assets = new HashMap<>();
|
||||
private final Map<String, ByteBuffer> imageBuffers = new HashMap<>();
|
||||
private final ProjectConfiguration configuration;
|
||||
|
||||
|
||||
@@ -55,6 +59,26 @@ public class DefaultImageManager implements ImageManager {
|
||||
return new DefaultImage(mesh, material, initialWidth, initialHeight, gcd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer loadObjectByteBuffer(String uid) {
|
||||
var buffer = imageBuffers.get(uid);
|
||||
|
||||
if (buffer == null) {
|
||||
var asset = assets.get(uid);
|
||||
|
||||
if (asset == null) {
|
||||
throw new AppException("The image asset with UID: [%s] does not exist", uid);
|
||||
}
|
||||
|
||||
var source = configuration.projectFile("images", asset.getSource());
|
||||
buffer = resourcesManager.loadResourceAsByteBuffer(source);
|
||||
log.info("Loading image from assets to cache under the key: [{}]", uid);
|
||||
imageBuffers.put(uid, buffer);
|
||||
}
|
||||
|
||||
return buffer.duplicate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
log.info("There is nothing to clean up here");
|
||||
|
||||
@@ -3,7 +3,8 @@ package com.bartlomiejpluta.base.engine.world.image.manager;
|
||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||
import com.bartlomiejpluta.base.api.internal.gc.Cleanable;
|
||||
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
||||
import com.bartlomiejpluta.base.engine.common.manager.ByteBufferAssetManager;
|
||||
import com.bartlomiejpluta.base.engine.world.image.asset.ImageAsset;
|
||||
|
||||
public interface ImageManager extends AssetManager<ImageAsset, Image>, Cleanable {
|
||||
public interface ImageManager extends AssetManager<ImageAsset, Image>, ByteBufferAssetManager<ImageAsset>, Cleanable {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user