Skip to content

Commit

Permalink
relief scenario blink
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Jan 26, 2025
1 parent d24306d commit 3462eaf
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 112 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/teammoeg/chorda/Chorda.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
Expand Down Expand Up @@ -62,7 +63,7 @@ public Chorda() {
// Config
LOGGER.info(COMMON_INIT, "Loading Config");
ChordaConfig.register();
CClientDataStorage.load();

// Init
LOGGER.info(COMMON_INIT, "Initializing " + MODNAME);

Expand All @@ -83,6 +84,7 @@ public Chorda() {
mod.addListener(this::processIMC);
mod.addListener(this::enqueueIMC);
mod.addListener(this::loadComplete);
mod.addListener(this::clientSetup);

// Client setup
LOGGER.info(COMMON_INIT, "Proceeding to Client Initialization");
Expand All @@ -98,7 +100,9 @@ public Chorda() {
private void setup(final FMLCommonSetupEvent event) {
ChordaNetwork.register();
}

public void clientSetup(final FMLClientSetupEvent ev) {
CClientDataStorage.load();
}
/**
* Enqueue Inter-Mod Communication
* @param event The event
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/teammoeg/chorda/ChordaClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import net.minecraftforge.event.TickEvent.RenderTickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

@Mod.EventBusSubscriber(modid = Chorda.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE,value=Dist.CLIENT)
public class ChordaClientEvents {
@SubscribeEvent
public static void clientTick(RenderTickEvent tick) {
CClientDataStorage.checkAndSave();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,43 @@
import com.teammoeg.frostedheart.content.scenario.client.gui.layered.gl.TypedDynamicTexture;

public class LayerManager extends GLLayerContent {
Map<String, OrderedRenderableContent> names = new LinkedHashMap<>();
PriorityQueue<OrderedRenderableContent> pq;
PriorityQueue<OrderedRenderableContent> opq;
GLImageContent oglc=new GLImageContent(null,0,0, 1f, 1f, 0, 0, 1024*FHConfig.CLIENT.getScenarioScale(), 576*FHConfig.CLIENT.getScenarioScale(), 1024*FHConfig.CLIENT.getScenarioScale(), 576*FHConfig.CLIENT.getScenarioScale());
GLImageContent nglc=new GLImageContent(null,0,0, 1f, 1f, 0, 0, 1024*FHConfig.CLIENT.getScenarioScale(), 576*FHConfig.CLIENT.getScenarioScale(), 1024*FHConfig.CLIENT.getScenarioScale(), 576*FHConfig.CLIENT.getScenarioScale());
private static ExecutorService executor;
private Future <PrerenderParams> rendering;
public static <T> Future<T> submitRenderTask(Runnable r,T result) {
if(executor==null) {
AtomicInteger id=new AtomicInteger(0);
executor=Executors.newFixedThreadPool(FHConfig.CLIENT.scenarioRenderThread.get(),rn->new Thread(rn,"frostedheart-scenario-prerender-"+id.getAndAdd(1)));
private static record RerenderRequest(TransitionFunction trans,int ticks){}
private static record LayerContext(PriorityQueue<OrderedRenderableContent> pq,GLImageContent glc) {
public void render(RenderParams params) {
glc.render(params);
if (pq != null)
for(OrderedRenderableContent i:pq) {
i.render(params);
}
}
return executor.submit(r,result);
}
int transTicks;
int maxTransTicks;
TransitionFunction trans;

public LayerManager() {
public void close() {
glc.texture.release();
}

}
private static class TransitionInfo{
int transTicks=0;
int maxTransTicks;
TransitionFunction trans;
public TransitionInfo(int maxTransTicks, TransitionFunction trans) {
super();
this.maxTransTicks = maxTransTicks;
this.trans = trans;
}
}
Map<String, OrderedRenderableContent> names = new LinkedHashMap<>();
volatile LayerContext nextlayer;
volatile LayerContext current;
volatile TransitionInfo trans;

RerenderRequest rrq;

public LayerManager() {
}
public LayerContext createContext() {
return new LayerContext(new PriorityQueue<>(Comparator.comparingInt(OrderedRenderableContent::getZ).thenComparing(OrderedRenderableContent::getOrder)),new GLImageContent(null,0,0, 1f, 1f, 0, 0, 1024*FHConfig.CLIENT.getScenarioScale(), 576*FHConfig.CLIENT.getScenarioScale(), 1024*FHConfig.CLIENT.getScenarioScale(), 576*FHConfig.CLIENT.getScenarioScale()));
}
public LayerManager(Map<String, OrderedRenderableContent> names) {
super();
this.names = names;
Expand All @@ -70,13 +86,17 @@ public LayerManager(int x, int y, int w, int h) {

@Override
public void tick() {
if(maxTransTicks>0) {
transTicks++;
if(trans!=null&&trans.maxTransTicks>0) {
trans.transTicks++;

if (transTicks >= maxTransTicks) {
maxTransTicks = transTicks = 0;
if (trans.transTicks >= trans.maxTransTicks) {
LayerContext ol=current;
current=nextlayer;
trans = null;
opq = null;
nextlayer=null;
if(ol!=null)
ol.close();

}
}
for (RenderableContent i : names.values()) {
Expand All @@ -102,113 +122,80 @@ public LayerManager getLayer(String name) {
return new LayerManager();
}
public void close() {
if(oglc.texture!=null){
oglc.texture.close();
oglc.texture=null;

if(nextlayer!=null){
nextlayer.close();
nextlayer=null;
}
if(nglc.texture!=null) {
nglc.texture.close();
nglc.texture=null;
if(current!=null) {
current.close();
current=null;
}
for(OrderedRenderableContent ctx:names.values())
if(ctx instanceof LayerManager) {
((LayerManager) ctx).close();
}
}
boolean prerenderRequested;
public synchronized void commitChanges(TransitionFunction t, int ticks) {

if (t != null) {
this.trans = t;
this.transTicks = 0;
this.maxTransTicks = ticks;
opq = pq;

if(oglc.texture!=null) {
oglc.texture.close();
oglc.texture=null;
}
oglc.texture=nglc.texture;
nglc.texture=null;
}else close();
pq=null;
public void commitChanges(TransitionFunction t, int ticks) {
rrq=new RerenderRequest(t,ticks);
}
public void renderPrerendered(TransitionFunction t, int ticks) {
LayerContext inext=this.createContext();
if (!names.isEmpty()) {
pq = new PriorityQueue<>(Comparator.comparingInt(OrderedRenderableContent::getZ).thenComparing(OrderedRenderableContent::getOrder));
int i = 0;
for (OrderedRenderableContent r : names.values()) {
r.setOrder(i);
pq.add(r);
inext.pq.add(r);
i++;
}
final PrerenderParams prerender=new PrerenderParams();

pq.forEach(s->s.prerender(prerender));

inext.pq.forEach(s->s.prerender(prerender));
inext.glc.texture=prerender.loadTexture();

TypedDynamicTexture tex=prerender.loadTexture();

//System.out.println("Loading tex");
if(nglc.texture!=null) {
nglc.texture.close();
nglc.texture=null;
}
nglc.texture=tex;
}
if (t != null) {
this.nextlayer=inext;
this.trans = new TransitionInfo(ticks,t);
}else {
LayerContext otex=current;
current=inext;
if(otex!=null)
otex.close();
}
}

@Override
public synchronized void renderContents(RenderParams params) {
/*if(rendering!=null) {
Future<PrerenderParams> rendering=this.rendering;
this.rendering=null;
PrerenderParams rendered;
try {
rendered = rendering.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("scenario rendering failed");
} catch (ExecutionException e) {
e.getCause().printStackTrace();
throw new RuntimeException("scenario rendering failed");
public void renderContents(RenderParams params) {
if(rrq!=null)
synchronized(this) {
if(rrq!=null) {
RerenderRequest trrq=rrq;
rrq=null;
renderPrerendered(trrq.trans(),trrq.ticks());
}
}
TypedDynamicTexture tex=rendered.loadTexture();
//System.out.println("Loading tex");
if(nglc.texture!=null) {
nglc.texture.close();
nglc.texture=null;
}
nglc.texture=tex;
}*/

if (trans != null) {
RenderParams prev = params.copyWithCurrent(this);
RenderParams next = params.copyWithCurrent(this);
float val = (transTicks + params.partialTicks) / maxTransTicks;
float val = (trans.transTicks) / trans.maxTransTicks;
// System.out.println(val);
trans.compute(prev, next, val);
trans.trans.compute(prev, next, val);

if (prev.forceFirst) {
nglc.render(next);
if (pq != null)
pq.forEach(t -> t.render(next));
oglc.render(prev);
if (opq != null)
opq.forEach(t -> t.render(prev));
if(nextlayer!=null)
nextlayer.render(next);
if(current!=null)
current.render(prev);
} else {
oglc.render(prev);
if (opq != null)
opq.forEach(t -> t.render(prev));
nglc.render(next);
if (pq != null)
pq.forEach(t -> t.render(next));
if(current!=null)
current.render(prev);
if(nextlayer!=null)
nextlayer.render(next);
}
} else {
nglc.render(params);
if (pq != null)
pq.forEach(t -> t.render(params));
if(current!=null)
current.render(params);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.teammoeg.frostedheart.content.scenario.client.gui.layered.gl;

import java.util.concurrent.atomic.AtomicInteger;

import com.mojang.blaze3d.platform.NativeImage;
import com.teammoeg.frostedheart.FHMain;
import com.teammoeg.chorda.client.ClientUtils;
Expand All @@ -34,15 +36,29 @@ public class TypedDynamicTexture {
DynamicTexture texture;
RenderType renderType;
ResourceLocation resourceLocation;
boolean isClosed = false;
AtomicInteger refCount=new AtomicInteger(1);
public TypedDynamicTexture(NativeImage texture) {
this.texture = new DynamicTexture(texture);
resourceLocation = FHMain.rl("fhscenario/generated_"+this.hashCode());
ClientUtils.mc().textureManager.register(resourceLocation, this.texture);
this.renderType= CGuiHelper.RenderStateAccess.createTempType(resourceLocation);
this.texture = new DynamicTexture(texture);
resourceLocation = FHMain.rl("fhscenario/generated_" + this.hashCode());
ClientUtils.mc().textureManager.register(resourceLocation, this.texture);
this.renderType = CGuiHelper.RenderStateAccess.createTempType(resourceLocation);
}

public synchronized void close() {
if(!isClosed) {
isClosed = true;
texture.close();
ClientUtils.mc().textureManager.release(resourceLocation);
}
}
public void close() {
texture.close();
ClientUtils.mc().textureManager.release(resourceLocation);
public void addRef() {
refCount.incrementAndGet();
}
public void release() {
int ncount=refCount.decrementAndGet();
if(ncount<=0)
close();
}
public void draw(GuiGraphics graphics,int x,int y,int w,int h,int uOffset,int vOffset,int uWidth,int vHeight,float alpha) {
/*Matrix4f matrix4f = graphics.pose().last().pose();
Expand All @@ -58,8 +74,12 @@ public void draw(GuiGraphics graphics,int x,int y,int w,int h,int uOffset,int vO
vertexconsumer.vertex(matrix4f, x+w, y+h, 0F).color(1, 1, 1, alpha).uv(u2, v2).endVertex();
vertexconsumer.vertex(matrix4f, x+w, y, 0F).color(1, 1, 1, alpha).uv(u2,v1).endVertex();
vertexconsumer.vertex(matrix4f, x, y, 0F).color(1, 1, 1, alpha).uv(u1, v1).endVertex();*/
CGuiHelper.bindTexture(resourceLocation);
CGuiHelper.blit(graphics.pose(), x, y, w, h, uOffset, vOffset, uWidth, vHeight, texture.getPixels().getWidth(), texture.getPixels().getHeight(), alpha);
}
if(!isClosed){
addRef();
CGuiHelper.bindTexture(resourceLocation);
CGuiHelper.blit(graphics.pose(), x, y, w, h, uOffset, vOffset, uWidth, vHeight, texture.getPixels().getWidth(), texture.getPixels().getHeight(), alpha);
release();
}
}

}

0 comments on commit 3462eaf

Please sign in to comment.