Skip to content

Commit

Permalink
attempt to cleanly separate drawcalls from logic, not yet succeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhael-Danilov committed Dec 25, 2024
1 parent 3530d9d commit aa5d2fc
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 159 deletions.
2 changes: 1 addition & 1 deletion RemixedDungeon/src/main/assets/scripts/services/scene.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local function levelsTestModeOnStep(self, scene)
autoTestAi.step()
end

if framesOnLevel > 10000 then
if framesOnLevel > 1000 then
currentLevel = currentLevel + 1

if currentLevel < levelsSize then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ public void onFrame() {
}

if (framesSinceInit > 2 && !Game.softPaused && loadingOrSaving.get() == 0) {
if(stepExecutor.getQueue().isEmpty()) {
stepExecutor.submit(() -> {
update();
}
});
}

NoosaScript.get().resetCamera();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.nyrds.platform.compatibility.RectF;
import com.nyrds.platform.gfx.BitmapData;
import com.nyrds.platform.gl.Texture;

import org.jetbrains.annotations.NotNull;

public class SmartTexture extends Texture {
Expand Down Expand Up @@ -49,7 +50,7 @@ public SmartTexture(@NotNull BitmapData bitmap, int filtering, int wrapping ) {
public void bitmap( BitmapData bitmap ) {
width = bitmap.getWidth();
height = bitmap.getHeight();
handMade(bitmap, true );
super.bitmap( bitmap );
}

public RectF uvRect( int left, int top, int right, int bottom ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import com.nyrds.platform.gl.Texture;
import com.nyrds.util.ModdingMode;
import com.watabou.noosa.TextureFilm;
import lombok.Synchronized;
import lombok.val;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

import lombok.Synchronized;
import lombok.val;

public class TextureCache {

private static final Map<Object, SmartTexture> all = new HashMap<>();
Expand Down Expand Up @@ -77,7 +79,7 @@ public static SmartTexture get(@NotNull Object src) {
BitmapData bmp = ModdingMode.getBitmapData((String) src);
SmartTexture tx = new SmartTexture(bmp);
all.put(src, tx);
bmp.dispose();
//bmp.dispose();
return tx;

}
Expand Down
2 changes: 1 addition & 1 deletion RemixedDungeon/src/main/java/com/watabou/noosa/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static Font colorMarked(String tex_src, String chars ) {
SmartTexture tex = TextureCache.get( bmp );
Font font = new Font( tex );
font.splitByAlpha( bmp, chars );
bmp.dispose();
//bmp.dispose();
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private class FogTexture extends SmartTexture {

public FogTexture() {
super(toDispose = BitmapData.createBitmap(width2, height2));
toDispose.dispose();
//toDispose.dispose();
filter(Texture.LINEAR, Texture.LINEAR);
TextureCache.add(FogOfWar.class, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.watabou.gltextures.TextureCache;
import com.watabou.noosa.InterstitialPoint;
import com.watabou.noosa.Scene;

import org.jetbrains.annotations.NotNull;

import java.io.InputStream;
Expand All @@ -32,6 +33,7 @@ public class Game implements ApplicationListener, InputProcessor {
public Iap iap = new Iap();

public PlayGames playGames = new PlayGames();
public long renderThreadId;

public Game(Class<? extends Scene> c) {
super();
Expand Down Expand Up @@ -123,6 +125,8 @@ public void render() {
Gl.blendSrcAlphaOneMinusAlpha();
Gdx.gl20.glEnable(Gdx.gl20.GL_SCISSOR_TEST);

renderThreadId = Thread.currentThread().getId();

gameLoop.onFrame();
}

Expand Down Expand Up @@ -222,6 +226,7 @@ public static boolean deleteFile(String path) {
}

public final void runOnUiThread(Runnable action) {

action.run();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.nyrds.platform.gfx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.nyrds.pixeldungeon.game.GamePreferences;
import com.nyrds.platform.game.Game;
import com.nyrds.platform.storage.FileSystem;
import com.nyrds.platform.util.StringsManager;
import com.watabou.glwrap.Matrix;
Expand All @@ -17,15 +19,16 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class SystemText extends Text {
private static FreeTypeFontGenerator generator;
private static Map<String, BitmapFont> fontCache = new HashMap<>();
private static final Map<String, BitmapFont> fontCache = new HashMap<>();

private final FreeTypeFontParameter fontParameters;
private final BitmapFont font;
private final GlyphLayout glyphLayout;
private final GlyphLayout spaceLayout; // New instance to measure space width
private final GlyphLayout spaceLayout;
private boolean multiline = false;

private final ArrayList<ArrayList<String>> lines = new ArrayList<>();
Expand All @@ -45,39 +48,66 @@ public SystemText(float baseLine) {
fontParameters = getFontParameters(baseLine);

String fontKey = getFontKey(fontParameters);
synchronized (fontCache) {
if (!fontCache.containsKey(fontKey)) {
fontCache.put(fontKey, generator.generateFont(fontParameters));
}
font = fontCache.get(fontKey);
}
font = generateFontOnRenderThread(fontKey, fontParameters);

glyphLayout = new GlyphLayout();
spaceLayout = new GlyphLayout(); // Initialize spaceLayout
spaceLayout.setText(font, " "); // Measure the width of a space
spaceLayout = new GlyphLayout();
spaceLayout.setText(font, " ");
}

public SystemText(final String text, float size, boolean multiline) {
super(0, 0, 0, 0);
fontParameters = getFontParameters(size);

String fontKey = getFontKey(fontParameters);
synchronized (fontCache) {
if (!fontCache.containsKey(fontKey)) {
fontCache.put(fontKey, generator.generateFont(fontParameters));
}
font = fontCache.get(fontKey);
}
font = generateFontOnRenderThread(fontKey, fontParameters);

glyphLayout = new GlyphLayout();
spaceLayout = new GlyphLayout(); // Initialize spaceLayout
spaceLayout.setText(font, " "); // Measure the width of a space
spaceLayout = new GlyphLayout();
spaceLayout.setText(font, " ");
this.text(text);
this.multiline = multiline;
wrapText();
}

private BitmapFont generateFontOnRenderThread(String fontKey, FreeTypeFontParameter params) {
// Check if the current thread is the render thread
if (Thread.currentThread().getId() == Game.instance().renderThreadId) {
// Already on the render thread, generate the font directly
synchronized (fontCache) {
if (!fontCache.containsKey(fontKey)) {
fontCache.put(fontKey, generator.generateFont(params));
}
return fontCache.get(fontKey);
}
} else {
// Not on the render thread, use postRunnable and CountDownLatch
final BitmapFont[] generatedFont = new BitmapFont[1];
final CountDownLatch latch = new CountDownLatch(1);

Gdx.app.postRunnable(() -> {
synchronized (fontCache) {
if (!fontCache.containsKey(fontKey)) {
fontCache.put(fontKey, generator.generateFont(params));
}
generatedFont[0] = fontCache.get(fontKey);
}
latch.countDown();
});

try {
latch.await(); // Wait for the font generation to complete
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Font generation interrupted", e);
}

return generatedFont[0];
}
}

public static void updateFontScale() {
float scale = 0.5f + 0.01f * GamePreferences.fontScale();

scale *= 1.2f;

if (scale < 0.1f) {
Expand All @@ -93,14 +123,12 @@ public static void updateFontScale() {
}

private FreeTypeFontParameter getFontParameters(float baseLine) {
if (fontScale != fontScale) {
if (Float.isNaN(fontScale)) {
updateFontScale();
}

final FreeTypeFontParameter fontParameters;
fontParameters = new FreeTypeFontParameter();
final FreeTypeFontParameter fontParameters = new FreeTypeFontParameter();
fontParameters.characters = FreeTypeFontGenerator.DEFAULT_CHARS + StringsManager.getAllCharsAsString();
//PUtil.slog("font", "characters " + fontParameters.characters);
fontParameters.size = (int) (baseLine * oversample * fontScale);
fontParameters.borderColor = Color.BLACK;
fontParameters.borderWidth = oversample * fontScale;
Expand Down Expand Up @@ -128,7 +156,6 @@ private void wrapText() {
}

int index = 0;
// Perform wrapping based on maxWidth
String[] paragraphs = text.split("\n");
for (String paragraph : paragraphs) {
if (paragraph.isEmpty()) {
Expand Down Expand Up @@ -163,7 +190,6 @@ private void wrapText() {
@Override
public void destroy() {
super.destroy();
// No need to dispose font here as it's managed by the cache
}

@Override
Expand Down Expand Up @@ -202,10 +228,9 @@ public void draw() {
if (wordMask == null || wordMask.get(wi)) {
font.draw(batch, glyphLayout, x, y);
}
x += glyphLayout.width + spaceLayout.width; // Use spaceLayout.width
x += glyphLayout.width + spaceLayout.width;
wi++;
}

y += glyphLayout.height + 5;
}
}
Expand All @@ -226,15 +251,14 @@ protected void measure() {
wrapText();
}

// Calculate total height and maximum width for wrapped lines
float totalHeight = 0;
float maxWidth = 0;
for (ArrayList<String> line : lines) {
if (!line.isEmpty()) {
float line_width = 0;
for (String word : line) {
glyphLayout.setText(font, word);
line_width += glyphLayout.width + spaceLayout.width; // Use spaceLayout.width
line_width += glyphLayout.width + spaceLayout.width;
}
if (line_width > maxWidth) {
maxWidth = line_width;
Expand All @@ -261,11 +285,7 @@ public static void invalidate() {
generator.dispose();
}

//if (GamePreferences.classicFont()) {
// generator = new FreeTypeFontGenerator(FileSystem.getInternalStorageFileHandle("fonts/pixel_font.ttf"));
//} else {
generator = new FreeTypeFontGenerator(FileSystem.getInternalStorageFileHandle("fonts/LXGWWenKaiScreen.ttf"));
//}
generator = new FreeTypeFontGenerator(FileSystem.getInternalStorageFileHandle("fonts/LXGWWenKaiScreen.ttf"));

synchronized (fontCache) {
for (BitmapFont font : fontCache.values()) {
Expand Down
Loading

0 comments on commit aa5d2fc

Please sign in to comment.