From e59d6c22a55309cb610ab4944982ab7c7d9b6a5c Mon Sep 17 00:00:00 2001 From: Antoine Date: Thu, 3 Jun 2021 22:48:54 +0200 Subject: [PATCH] Optimisation setNewText --- src/engine/object/Text.java | 60 ++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/src/engine/object/Text.java b/src/engine/object/Text.java index 7d2e0aa..894be31 100644 --- a/src/engine/object/Text.java +++ b/src/engine/object/Text.java @@ -30,19 +30,32 @@ public class Text { private void addCharListInEngine(){ int i = 0; - for (ObjectGl c : this.charList){ - c.translate(new Vector3f(i * 10.0f * this.size, 0.0f, 0.0f)); - this.engine.add_objectGl(c); + for (ObjectGl obj : this.charList){ + addCharInEngine(i, obj); i++; } } + private void addCharInEngine(int i, ObjectGl obj){ + obj.translate(new Vector3f(i * 10.0f * this.size, 0.0f, 0.0f)); + this.engine.add_objectGl(obj); + } + public void setNewText(String text){ - // TODO NE PAS REGENERER L'OBJECTGL DE ZERO A CHAQUE FOIS - this.remove(); - this.charList = new ArrayList<>(); - textToArrayObjectGl(text); - this.addCharListInEngine(); + int i = 0; + for (ObjectGl obj : this.charList) { + ObjectGlSetCharWrap(text.charAt(i), obj); + i++; + if (i >= text.length()) break; + } + while (i < text.length()){ + ObjectGl obj = this.charToObjectGl(text.charAt(i)); + this.charList.add(obj); + addCharInEngine(i, obj); + i++; + } + if (i < this.charList.size()) removeFromIndexToEnd(i); + } public void remove(){ @@ -51,19 +64,32 @@ public class Text { } } + private void removeFromIndexToEnd(int i){ + int j = 0; + for (ObjectGl objectGl : this.charList){ + if (j >= i) this.engine.remove_objectGl(objectGl); + j++; + } + this.charList = this.charList.subList(0, i); + } + private ObjectGl charToObjectGl(char a){ ObjectGl objectGl = new ObjectGl(this.zPos, 1.0f, 1.0f, this.size, "textures/dejavu10x10_gs_tc.png", null); objectGl.setShader("shaders/StylishShaders/BasicVert.glsl","shaders/StylishShaders/TextFrag.glsl"); - if (a < 132 && a > 96){ - objectGl.setTextureWrap(0.0f + (a - 97) * 10.0f,40.0f,10.0f,10.0f, ObjectGl.DEFAULT); - } - else if (a < 91 && a > 64){ - objectGl.setTextureWrap(0.0f + (a - 65) * 10.0f,30.0f,10.0f,10.0f, ObjectGl.DEFAULT); - } - else if (a < 64 && a > 31){ - objectGl.setTextureWrap(0.0f + (a - 32) * 10.0f,0.0f,10.0f,10.0f, ObjectGl.DEFAULT); - } + ObjectGlSetCharWrap(a, objectGl); return objectGl; } + private void ObjectGlSetCharWrap(char a, ObjectGl obj){ + if (a < 132 && a > 96){ + obj.setTextureWrap(0.0f + (a - 97) * 10.0f,40.0f,10.0f,10.0f, ObjectGl.DEFAULT); + } + else if (a < 91 && a > 64){ + obj.setTextureWrap(0.0f + (a - 65) * 10.0f,30.0f,10.0f,10.0f, ObjectGl.DEFAULT); + } + else if (a < 64 && a > 31){ + obj.setTextureWrap(0.0f + (a - 32) * 10.0f,0.0f,10.0f,10.0f, ObjectGl.DEFAULT); + } + } + }