Skip to content

Commit

Permalink
1.12.2-0.3.1k
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuxelus committed Jan 27, 2024
1 parent a4b7425 commit 3772d09
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle_old
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}
apply plugin: 'net.minecraftforge.gradle.forge'

version = "1.12.2-0.3.1j"
version = "1.12.2-0.3.1k"
group= "com.zuxelus.energycontrol"
archivesBaseName = "EnergyControl"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.zuxelus.energycontrol.renderers;

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

import net.minecraft.client.model.PositionTextureVertex;
import net.minecraft.client.model.TexturedQuad;
import net.minecraft.client.renderer.BufferBuilder;
Expand All @@ -15,6 +18,8 @@
public class CubeRenderer {
public static final CubeRenderer MODEL = new CubeRenderer(0, 0, 0, 32, 32, 32, 128, 128, 0, 0, false);
public static final CubeRenderer DESTROY = new CubeRenderer(0, 0, 0, 32, 32, 32, 32, 32, 0, 0, false);
public static Map<Long, CubeRenderer> LIBRARY = new HashMap<Long, CubeRenderer>();
public static Map<Long, CubeRenderer> LIBRARY_FACE = new HashMap<Long, CubeRenderer>();

private boolean compiled;
private int displayList;
Expand Down Expand Up @@ -274,4 +279,22 @@ public static void rotateBlockText(EnumFacing facing, EnumFacing rotation) {
break;
}
}

public static CubeRenderer getModel(RotationOffset offset) {
long hash = (long) (offset.leftTop * 1000000 + offset.leftBottom * 10000 + offset.rightTop * 100 + offset.rightBottom);
if (LIBRARY.containsKey(hash))
return LIBRARY.get(hash);
CubeRenderer model = new CubeRenderer(0, 0, offset, false);
LIBRARY.put(hash, model);
return model;
}

public static CubeRenderer getFaceModel(RotationOffset offset, int textureId) {
long hash = (long) (textureId * 100000000 + offset.leftTop * 1000000 + offset.leftBottom * 10000 + offset.rightTop * 100 + offset.rightBottom);
if (LIBRARY_FACE.containsKey(hash))
return LIBRARY_FACE.get(hash);
CubeRenderer model = new CubeRenderer(textureId / 4 * 32, textureId % 4 * 32, offset, true);
LIBRARY_FACE.put(hash, model);
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public void render(TileEntityAdvancedInfoPanelExtender te, double x, double y, d
} else {
Screen screen = te.getScreen();
if (screen != null) {
RotationOffset offset = new RotationOffset(thickness * 2, rotateHor, rotateVert);
RotationOffset offset = new RotationOffset(thickness * 2, rotateHor, rotateVert).addOffset(screen, te.getPos(), te.getFacing(), te.getRotation());
if (destroyStage > -1)
new CubeRenderer(0.0F, 0.0F, 0.0F, 32, 32, 32, 32, 32, 0, 0, offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation()), false).render(0.03125F);
new CubeRenderer(0.0F, 0.0F, 0.0F, 32, 32, 32, 32, 32, 0, 0, offset, false).render(0.03125F);
else {
new CubeRenderer(0, 0, offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation()), false).render(0.03125F);
CubeRenderer.getModel(offset).render(0.03125F);
bindTexture(TileEntityInfoPanelRenderer.SCREEN);
new CubeRenderer(textureId / 4 * 32, textureId % 4 * 32, offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation()), true).render(0.03125F, color);
CubeRenderer.getFaceModel(offset, textureId).render(0.03125F, color);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void render(TileEntityAdvancedInfoPanel te, double x, double y, double z,
RotationOffset offset = new RotationOffset(thickness * 2, rotateHor, rotateVert);
Screen screen = te.getScreen();
if (screen != null) {
RotationOffset offsetScreen = offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation());
if (thickness == 16 && rotateHor == 0 && rotateVert == 0) {
if (destroyStage > -1)
CubeRenderer.DESTROY.render(0.03125F);
Expand All @@ -71,11 +72,11 @@ public void render(TileEntityAdvancedInfoPanel te, double x, double y, double z,
}
} else {
if (destroyStage > -1)
new CubeRenderer(0, 0, 0, 32, 32, 32, 32, 32, 0, 0, offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation()), false).render(0.03125F);
new CubeRenderer(0, 0, 0, 32, 32, 32, 32, 32, 0, 0, offsetScreen, false).render(0.03125F);
else {
new CubeRenderer(0, 0, offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation()), false).render(0.03125F);
CubeRenderer.getModel(offsetScreen).render(0.03125F);
bindTexture(TileEntityInfoPanelRenderer.SCREEN);
new CubeRenderer(textureId / 4 * 32, textureId % 4 * 32, offset.addOffset(screen, te.getPos(), te.getFacing(), te.getRotation()), true).render(0.03125F, color);
CubeRenderer.getFaceModel(offsetScreen, textureId).render(0.03125F, color);
}
}

Expand Down

2 comments on commit 3772d09

@TheNullablePrototype
Copy link

@TheNullablePrototype TheNullablePrototype commented on 3772d09 Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this will not change the case much, but still, I would do it this way:

    public static final TLongObjectMap<CubeRenderer> LIBRARY = new TLongObjectHashMap<>();
    public static final TLongObjectMap<CubeRenderer> LIBRARY_FACE = new TLongObjectHashMap<>();
    
   // ...

    public static CubeRenderer getModel(RotationOffset offset) {
        final long hash = (long) (offset.leftTop * 1000000 + offset.leftBottom * 10000 + offset.rightTop * 100 + offset.rightBottom);
        CubeRenderer renderer = LIBRARY.get(hash);
        if (renderer == null)
            LIBRARY.put(hash, renderer = new CubeRenderer(0, 0, offset, false));
        return renderer;
    }

    public static CubeRenderer getFaceModel(RotationOffset offset, int textureId) {
        final long hash = (long) (textureId * 100000000L + offset.leftTop * 1000000 + offset.leftBottom * 10000 + offset.rightTop * 100 + offset.rightBottom);
        CubeRenderer renderer = LIBRARY_FACE.get(hash);
        if (renderer == null)
            LIBRARY_FACE.put(hash, renderer = new CubeRenderer(textureId / 4 * 32, textureId % 4 * 32, offset, true));
        return renderer;
    }

In case for some reason you don't want to use TCollections, you can redo it at least on computeIfAbsent.
Fast example:

Map<Long, CubeRenderer> LIBRARY = ...;

return LIBRARY.computeIfAbsent(hash, key -> new CubeRenderer(0, 0, offset, false));

Unfortunately, the current version of TCollections does not have such a method.

Also, you are indefinitely holding compiled models, it is also a memory leak, since they are not being unloaded. You need to implement a system for tracking the use of the model and if it has not been used for a certain time, delete it. To do this, you will have to rework the current implementation.

Example delete:

    public void delete() {
       // And don't forget removed from maps
        if (compiled) {
            GLAllocation.deleteDisplayLists(this.displayList);
        }
    }

@Zuxelus
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right about the TLongObjectHashMap. It should be a little bit faster. And yes - holding models forever without tracking is not the best idea.
But now I'm very busy and don't have time to maintain the mod at all. I know about all such bugs and plan to fix them somehow in future.

Please sign in to comment.