[DEV] better application state manager

This commit is contained in:
Edouard DUPIN 2021-03-29 00:11:46 +02:00
parent 84fc01f773
commit 438b8aeee4
4 changed files with 39 additions and 24 deletions

View File

@ -12,7 +12,6 @@ import org.atriasoft.gale.key.KeyStatus;
import org.atriasoft.gale.key.KeyType;
public class Application {
public boolean canDraw = false;
private boolean needRedraw = true;
private String title = "gale";
private Uri iconName = null;

View File

@ -575,6 +575,7 @@ public class OpenGL {
* remove the current matrix and get the last one from the matrix stack.
*/
public static void pop() {
Log.verbose("Pop OpenGl Matrix: " + MATRIX_LIST.size());
if (MATRIX_LIST.size() <= 1) {
Log.error("set matrix list is not corect size in the stack : " + MATRIX_LIST.size());
MATRIX_LIST.clear();
@ -851,6 +852,7 @@ public class OpenGL {
* store current matrix in the matrix stack.
*/
public static void push() {
Log.verbose("push OpenGl Matrix: " + MATRIX_LIST.size());
if (MATRIX_LIST.size() == 0) {
Log.error("set matrix list is not corect size in the stack : " + MATRIX_LIST.size());
MATRIX_LIST.add(Matrix4f.IDENTITY);
@ -870,9 +872,7 @@ public class OpenGL {
}
reader.close();
} catch (final IOException e) {
Log.error("Could not read the file!");
e.printStackTrace();
System.exit(-1);
Log.critical("Could not read the file!");
}
return fileSource;
}
@ -882,8 +882,7 @@ public class OpenGL {
try {
final InputStream inputStream = Uri.getStream(name);
if (inputStream == null) {
Log.error("Could not read the file! " + name);
System.exit(-1);
Log.critical("Could not read the file! " + name);
}
final Reader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())));
int c = 0;

View File

@ -2,6 +2,7 @@ package org.atriasoft.gale.context;
import java.util.Vector;
import org.atriasoft.etk.Color;
import org.atriasoft.etk.ThreadAbstract;
import org.atriasoft.etk.Uri;
import org.atriasoft.etk.math.Vector2f;
@ -21,6 +22,10 @@ interface ActionToDoInAsyncLoop {
public void run(Context context);
}
enum ApplicationState {
UNDEFINED, CREATE, RUNNING, DIED
};
public abstract class Context {
protected static final int MAX_MANAGE_INPUT = 15;
private static Context globalContext = null;
@ -41,6 +46,7 @@ public abstract class Context {
protected ThreadAbstract periodicThread;;
protected Application application; //!< Application handle
protected ApplicationState applicationState = ApplicationState.UNDEFINED; // state of the application
private final CommandLine commandLine = new CommandLine(); //!< Start command line information;
private final ResourceManager resourceManager = new ResourceManager(); //!< global resources Manager
// simulation area:
@ -58,6 +64,7 @@ public abstract class Context {
public Context(final Application application, final String[] args) {
// set a basic
this.application = application;
this.applicationState = ApplicationState.CREATE;
setContext(this);
Thread.currentThread().setName("galeThread");
if (this.application == null) {
@ -104,12 +111,13 @@ public abstract class Context {
postAction((context) -> {
final Application appl = context.getApplication();
if (appl == null) {
this.applicationState = ApplicationState.UNDEFINED;
return;
}
appl.onCreate(context);
appl.onStart(context);
appl.onResume(context);
appl.canDraw = true;
this.applicationState = ApplicationState.RUNNING;
});
// force a recalculation
@ -278,7 +286,7 @@ public abstract class Context {
if (countMemeCheck++ >= 10 * 16) {
countMemeCheck = 0;
}
//Log.verbose("Call draw");
Log.verbose("Call draw");
final long currentTime = System.currentTimeMillis();
//echrono::Time currentTime2 = echrono::Time::now();
//Log.warning("Time = " << currentTime << " " << currentTime2);
@ -316,10 +324,14 @@ public abstract class Context {
*/
if (this.application != null) {
if (this.applicationState == ApplicationState.RUNNING) {
// Redraw all needed elements
//Log.debug("Regenerate Display");
this.application.onRegenerateDisplay(this);
needRedraw = this.application.isDrawingNeeded();
} else {
needRedraw = true;
}
}
if (this.displayFps) {
this.fpsSystemEvent.incrementCounter();
@ -354,8 +366,13 @@ public abstract class Context {
this.fpsSystem.incrementCounter();
// set the current interface :
lockContext();
if (this.application.canDraw) {
if (this.applicationState == ApplicationState.RUNNING) {
this.application.onDraw(this);
} else {
OpenGL.setViewPort(new Vector2f(0, 0), this.application.getSize());
final Color bgColor = new Color(0.8f, 0.5f, 0.8f, 1.0f);
OpenGL.clearColor(bgColor);
Log.info("==> appl clear ==> not created ...");
}
unLockContext();
hasDisplayDone = true;

View File

@ -23,12 +23,12 @@ public class ResourceManager {
/**
* special end of application
*/
public void applicationExiting() {
public synchronized void applicationExiting() {
contextHasBeenDestroyed();
this.exiting = true;
}
public void cleanInternalRemoved() {
public synchronized void cleanInternalRemoved() {
//Log.info("remove object in Manager");
updateContext();
// TODO ...
@ -43,7 +43,7 @@ public class ResourceManager {
/**
* This is to inform the resources manager that we have no more openGl context ...
*/
public void contextHasBeenDestroyed() {
public synchronized void contextHasBeenDestroyed() {
for (final Resource it : this.resourceList) {
if (it.getCount() > 0) {
it.removeContextToLate();
@ -56,7 +56,7 @@ public class ResourceManager {
/**
* display in the log all the resources loaded ...
*/
public void display() {
public synchronized void display() {
Log.info("Resources loaded : ");
// remove all resources ...
for (final Resource it : this.resourceList) {
@ -65,13 +65,13 @@ public class ResourceManager {
Log.info("Resources ---");
}
public void localAdd(final Resource object) {
public synchronized void localAdd(final Resource object) {
// add at the end if no slot is free
this.resourceList.add(object);
}
// internal API to extent eResources in extern Soft
public Resource localKeep(final String filename) {
public synchronized Resource localKeep(final String filename) {
Log.verbose("KEEP (DEFAULT) : file : '" + filename + "' in " + this.resourceList.size() + " resources");
for (final Resource it : this.resourceList) {
if (it == null) {
@ -91,7 +91,7 @@ public class ResourceManager {
return null;
}
public Resource localKeep(final Uri uri) {
public synchronized Resource localKeep(final Uri uri) {
// TODO Auto-generated method stub
return localKeep(uri.toString());
}
@ -100,7 +100,7 @@ public class ResourceManager {
* Reload all resources from files, and send there in openGL card if needed.
* @note If file is reference at THEMEXXX:///filename if the Theme change the file will reload the newOne
*/
public void reLoadResources() {
public synchronized void reLoadResources() {
Log.info("------------- Resources re-loaded -------------");
// remove all resources ...
for (long jjj = 0; jjj < MAX_RESOURCE_LEVEL; jjj++) {
@ -127,7 +127,7 @@ public class ResourceManager {
/**
* remove all resources (un-init) out of the destructor (due to the system implementation)
*/
public void unInit() {
public synchronized void unInit() {
display();
this.resourceListToUpdate.clear();
// remove all resources ...
@ -141,7 +141,7 @@ public class ResourceManager {
* Call by the system to send all the needed data on the graphic card chen they change ...
* @param object The resources that might be updated
*/
public void update(final Resource object) {
public synchronized void update(final Resource object) {
// check if not added before
for (final Resource it : this.resourceListToUpdate) {
if (it == object) {
@ -156,7 +156,7 @@ public class ResourceManager {
/**
* Call by the system chen the openGL Context has been unexpectially removed == > This reload all the texture, VBO and other ....
*/
public void updateContext() {
public synchronized void updateContext() {
if (this.exiting) {
Log.error("Request update after application EXIT ...");
return;