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

View File

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

View File

@ -23,12 +23,12 @@ public class ResourceManager {
/** /**
* special end of application * special end of application
*/ */
public void applicationExiting() { public synchronized void applicationExiting() {
contextHasBeenDestroyed(); contextHasBeenDestroyed();
this.exiting = true; this.exiting = true;
} }
public void cleanInternalRemoved() { public synchronized void cleanInternalRemoved() {
//Log.info("remove object in Manager"); //Log.info("remove object in Manager");
updateContext(); updateContext();
// TODO ... // TODO ...
@ -43,7 +43,7 @@ public class ResourceManager {
/** /**
* This is to inform the resources manager that we have no more openGl context ... * 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) { for (final Resource it : this.resourceList) {
if (it.getCount() > 0) { if (it.getCount() > 0) {
it.removeContextToLate(); it.removeContextToLate();
@ -56,7 +56,7 @@ public class ResourceManager {
/** /**
* display in the log all the resources loaded ... * display in the log all the resources loaded ...
*/ */
public void display() { public synchronized void display() {
Log.info("Resources loaded : "); Log.info("Resources loaded : ");
// remove all resources ... // remove all resources ...
for (final Resource it : this.resourceList) { for (final Resource it : this.resourceList) {
@ -65,13 +65,13 @@ public class ResourceManager {
Log.info("Resources ---"); 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 // add at the end if no slot is free
this.resourceList.add(object); this.resourceList.add(object);
} }
// internal API to extent eResources in extern Soft // 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"); Log.verbose("KEEP (DEFAULT) : file : '" + filename + "' in " + this.resourceList.size() + " resources");
for (final Resource it : this.resourceList) { for (final Resource it : this.resourceList) {
if (it == null) { if (it == null) {
@ -91,7 +91,7 @@ public class ResourceManager {
return null; return null;
} }
public Resource localKeep(final Uri uri) { public synchronized Resource localKeep(final Uri uri) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return localKeep(uri.toString()); return localKeep(uri.toString());
} }
@ -100,7 +100,7 @@ public class ResourceManager {
* Reload all resources from files, and send there in openGL card if needed. * 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 * @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 -------------"); Log.info("------------- Resources re-loaded -------------");
// remove all resources ... // remove all resources ...
for (long jjj = 0; jjj < MAX_RESOURCE_LEVEL; jjj++) { 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) * remove all resources (un-init) out of the destructor (due to the system implementation)
*/ */
public void unInit() { public synchronized void unInit() {
display(); display();
this.resourceListToUpdate.clear(); this.resourceListToUpdate.clear();
// remove all resources ... // 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 ... * 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 * @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 // check if not added before
for (final Resource it : this.resourceListToUpdate) { for (final Resource it : this.resourceListToUpdate) {
if (it == object) { 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 .... * 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) { if (this.exiting) {
Log.error("Request update after application EXIT ..."); Log.error("Request update after application EXIT ...");
return; return;