Skip to content

Commit 1604d3d

Browse files
Implement services
- Service support. - Optiminze import.
1 parent 9adea3c commit 1604d3d

28 files changed

Lines changed: 199 additions & 28 deletions

res/layout/main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
padding="20dp"/>
3030
<View width="10dp" height="10dp"/>
3131
<ImageView
32+
id="panda"
3233
width="200dp"
3334
height="200dp"
3435
background="?colorPrimary"

src/br/nullexcept/mux/C.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import java.util.Arrays;
88

99
public class C {
10+
public static int VIEW_COUNT = 0;
1011
public static long VG_CONTEXT = -1;
1112
public static long GLFW_CONTEXT = 0;
1213
public static BitmapFactory BITMAP_FACTORY;
1314

1415
public static class Flags {
1516
public static final boolean FULL_DRAW;
17+
public static final boolean DEBUG_OVERLAY;
1618
public static final boolean DISABLE_AUTO_REDRAW;
1719

1820
static {
@@ -23,6 +25,7 @@ public static class Flags {
2325
}
2426

2527
FULL_DRAW = flags.contains("FULL_DRAW");
28+
DEBUG_OVERLAY = flags.contains("DEBUG_OVERLAY");
2629
DISABLE_AUTO_REDRAW = flags.contains("DISABLE_AUTO_REDRAW");
2730
}
2831
}

src/br/nullexcept/mux/app/Application.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import br.nullexcept.mux.C;
44
import br.nullexcept.mux.core.texel.TexelAPI;
5-
import br.nullexcept.mux.lang.Function;
65
import br.nullexcept.mux.lang.Valuable;
7-
import br.nullexcept.mux.res.AssetsManager;
8-
import br.nullexcept.mux.res.Resources;
96
import br.nullexcept.mux.view.Window;
107
import org.lwjgl.opengles.GLES;
118

9+
import java.util.HashMap;
10+
1211
import static org.lwjgl.glfw.GLFW.*;
1312

1413
public class Application {
1514
private static long lastGc = System.currentTimeMillis();
15+
private static HashMap<String, Service> services = new HashMap<>();
1616
private static int RUNNING_ACTIVITIES = 0;
1717

1818
public static void initialize(Valuable<Activity> creator){
@@ -93,4 +93,24 @@ static void boot(Window window, Activity activity) {
9393
public static void stop(){
9494
Looper.getMainLooper().stop();
9595
}
96+
97+
public static <T extends Service> T beginService(Class<T> clazz, Valuable<T> service) {
98+
String name = clazz.getName();
99+
if (services.containsKey(name)) {
100+
return (T) services.get(name);
101+
}
102+
Looper looper = new Looper();
103+
T sv = service.get();
104+
sv.myLooper = looper;
105+
106+
services.put(name, sv);
107+
new Thread(()->{
108+
looper.post(sv::onCreate);
109+
looper.initialize();
110+
looper.loop();
111+
services.remove(name);
112+
sv.onDestroy();
113+
}).start();
114+
return sv;
115+
}
96116
}

src/br/nullexcept/mux/app/Context.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package br.nullexcept.mux.app;
22

33
import br.nullexcept.mux.core.texel.TexelAPI;
4+
import br.nullexcept.mux.lang.Valuable;
45
import br.nullexcept.mux.res.AssetsManager;
56
import br.nullexcept.mux.res.LayoutInflater;
67
import br.nullexcept.mux.res.Resources;
@@ -18,6 +19,20 @@ public Context(){
1819
applets.putAll(TexelAPI.obtainApplets());
1920
}
2021

22+
public <T extends Service> T startService(Class<T> clazz) {
23+
return startService(clazz, ()-> {
24+
try {
25+
return clazz.newInstance();
26+
} catch (Exception e) {
27+
throw new IllegalArgumentException(e);
28+
}
29+
});
30+
}
31+
32+
public <T extends Service> T startService(Class<T> clazz, Valuable<T> service) {
33+
return Application.beginService(clazz, service);
34+
}
35+
2136
public String getString(String id) {
2237
return mResource.getString(id);
2338
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package br.nullexcept.mux.app;
2+
3+
public class Service {
4+
Looper myLooper;
5+
6+
protected void onCreate() {
7+
8+
}
9+
10+
protected void finish() {
11+
myLooper.stop();
12+
}
13+
14+
public Looper getLooper() {
15+
return myLooper;
16+
}
17+
18+
protected void onDestroy() {
19+
20+
}
21+
}

src/br/nullexcept/mux/core/texel/GLTexel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import br.nullexcept.mux.graphics.Color;
44
import org.lwjgl.BufferUtils;
5-
import org.lwjgl.opengles.OESDrawBuffersIndexed;
65

76
import java.nio.FloatBuffer;
87

src/br/nullexcept/mux/core/texel/GlfwEventManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import br.nullexcept.mux.graphics.Point;
44
import br.nullexcept.mux.input.*;
5-
import org.lwjgl.glfw.*;
5+
import org.lwjgl.glfw.GLFW;
66

77
import java.util.HashMap;
88

src/br/nullexcept/mux/core/texel/GlfwWindow.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package br.nullexcept.mux.core.texel;
22

33
import br.nullexcept.mux.C;
4-
import br.nullexcept.mux.app.Activity;
54
import br.nullexcept.mux.app.Looper;
65
import br.nullexcept.mux.graphics.Point;
7-
import br.nullexcept.mux.input.CharEvent;
6+
import br.nullexcept.mux.graphics.fonts.Typeface;
87
import br.nullexcept.mux.hardware.GLES;
8+
import br.nullexcept.mux.input.CharEvent;
99
import br.nullexcept.mux.input.KeyEvent;
1010
import br.nullexcept.mux.input.MotionEvent;
1111
import br.nullexcept.mux.input.MouseEvent;
1212
import br.nullexcept.mux.view.View;
13+
import br.nullexcept.mux.view.ViewGroup;
1314
import br.nullexcept.mux.view.Window;
1415
import org.lwjgl.glfw.GLFW;
16+
import org.lwjgl.nanovg.NVGColor;
17+
import org.lwjgl.nanovg.NanoVG;
1518
import org.lwjgl.opengles.GLES20;
1619

20+
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
21+
1722
class GlfwWindow extends Window {
1823
private final long window;
1924
private final GlfwEventManager eventManager;
@@ -54,7 +59,7 @@ private void refresh() {
5459
position.set(buffer[0][0], buffer[1][0]);
5560
}
5661

57-
private int frames = 0;
62+
private long[] times = new long[8];
5863
private long last = System.currentTimeMillis();
5964

6065
private void update() {
@@ -64,13 +69,18 @@ private void update() {
6469

6570
if (System.currentTimeMillis() - last >= 1000) {
6671
last = System.currentTimeMillis();
67-
frames = 0;
72+
times[5] = times[0];
73+
times[6] = times[1] / times[0];
74+
75+
times[0] = 0;
76+
times[1] = 0;
6877
}
69-
frames++;
78+
times[0]++;
7079
eventManager.runFrame();
7180

7281
long time = System.currentTimeMillis();
7382
if (isVisible() && container != null) {
83+
long begin = System.currentTimeMillis();
7484
int ow = windowSize.x;
7585
int oh = windowSize.y;
7686
refresh();
@@ -80,10 +90,17 @@ private void update() {
8090
container.drawFrame();
8191
GLFW.glfwSwapBuffers(C.GLFW_CONTEXT);
8292
GLFW.glfwMakeContextCurrent(window);
93+
glfwSwapInterval(0); //NEED THAT FOR NOT SLOW MAIN LOOP
8394
GLES.glViewport(0, 0, ow, oh);
8495
GLES.glClearColor(0,0,0,1);
8596
GLES.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_STENCIL_BUFFER_BIT);
8697
GLTexel.drawTexture(0, 0, ow, oh, container.getCanvas().getFramebuffer().getTexture());
98+
99+
times[1] += System.currentTimeMillis() - begin;
100+
if (C.Flags.DEBUG_OVERLAY) {
101+
drawDebug();
102+
}
103+
87104
GLFW.glfwSwapBuffers(window);
88105
GLFW.glfwMakeContextCurrent(C.GLFW_CONTEXT);
89106
}
@@ -93,13 +110,48 @@ private void update() {
93110
time = System.currentTimeMillis() - time;
94111
// Window with focus = 60fps | without focus = 24fps
95112
time = Math.max(0, (isFocused() ? 16 : 41) - time);
113+
if(C.Flags.FULL_DRAW) {
114+
time = 1;
115+
}
96116
if (!destroyed) {
97117
Looper.getMainLooper().postDelayed(this::update, time);
98118
} else {
99119
running = false;
100120
}
101121
}
102122

123+
private final NVGColor color1 = NVGColor.create();
124+
private final NVGColor color2 = NVGColor.create();
125+
private void drawDebug() {
126+
NanoVG.nvgBeginFrame(C.VG_CONTEXT,windowSize.x, windowSize.y,1);
127+
NanoVG.nvgFontFaceId(C.VG_CONTEXT, Typeface.DEFAULT.hashCode());
128+
NanoVG.nvgFontSize(C.VG_CONTEXT, 16);
129+
130+
NanoVG.nvgBeginPath(C.VG_CONTEXT);
131+
NanoVG.nvgRGBAf(0,0,0,.5f, color2);
132+
NanoVG.nvgFillColor(C.VG_CONTEXT, color2);
133+
NanoVG.nvgRect(C.VG_CONTEXT, 10, 10, 160,72);
134+
NanoVG.nvgFill(C.VG_CONTEXT);
135+
NanoVG.nvgClosePath(C.VG_CONTEXT);
136+
137+
NanoVG.nvgRGBAf(1,1,1,1, color1);
138+
NanoVG.nvgFillColor(C.VG_CONTEXT, color1);
139+
NanoVG.nvgText(C.VG_CONTEXT,15,32,"FPS: "+times[5]);
140+
NanoVG.nvgText(C.VG_CONTEXT,15,48,"DRAW TIME: "+times[6]+"ms");
141+
NanoVG.nvgText(C.VG_CONTEXT,15,64,"VIEWS: "+viewCount(container));
142+
NanoVG.nvgEndFrame(C.VG_CONTEXT);
143+
}
144+
145+
private int viewCount(ViewGroup container) {
146+
int count = 0;
147+
for (View view: container.getChildren()) {
148+
if (view instanceof ViewGroup)
149+
count += viewCount((ViewGroup) view);
150+
count++;
151+
}
152+
return count;
153+
}
154+
103155
private void onResize(int width, int height) {
104156
if (container != null) {
105157
container.resize(width, height);

src/br/nullexcept/mux/core/texel/VgTexel.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import br.nullexcept.mux.graphics.Path;
77
import br.nullexcept.mux.graphics.Point;
88
import br.nullexcept.mux.utils.Log;
9-
import org.lwjgl.nanovg.*;
9+
import org.lwjgl.nanovg.NVGColor;
10+
import org.lwjgl.nanovg.NVGPaint;
11+
import org.lwjgl.nanovg.NanoVG;
12+
import org.lwjgl.nanovg.NanoVGGLES2;
1013

1114
import static org.lwjgl.nanovg.NanoVG.*;
1215

src/br/nullexcept/mux/core/texel/ViewRenderer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package br.nullexcept.mux.core.texel;
22

33
import br.nullexcept.mux.C;
4-
import br.nullexcept.mux.graphics.Color;
54
import br.nullexcept.mux.graphics.Paint;
65
import br.nullexcept.mux.graphics.Rect;
7-
import br.nullexcept.mux.hardware.GLES;
86
import br.nullexcept.mux.view.View;
97
import br.nullexcept.mux.view.ViewGroup;
108
import org.lwjgl.nanovg.NanoVG;

0 commit comments

Comments
 (0)