Skip to content

Commit

Permalink
Initial implementation of a basic file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaskelly committed Apr 23, 2020
1 parent 865f8ff commit fb0ebc8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.interrupt.dungeoneer.editor;

import com.badlogic.gdx.*;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
Expand Down Expand Up @@ -73,8 +74,11 @@
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.file.*;
import java.util.HashMap;

import static java.nio.file.StandardWatchEventKinds.*;

public class EditorApplication implements ApplicationListener {
public JFrame frame;

Expand Down Expand Up @@ -348,6 +352,8 @@ protected Decal newObject () {

Vector3 rayOutVector = new Vector3();

private Thread fileWatcher;

public EditorApplication() {
frame = new JFrame("DelvEdit");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Expand Down Expand Up @@ -375,6 +381,8 @@ public void windowClosing(WindowEvent e) {
config.addIcon("icon-16.png", Files.FileType.Internal); // 16x16 icon (Windows)

new LwjglApplication(this, config);

startFileWatcher();
}

public void init(){
Expand Down Expand Up @@ -410,6 +418,11 @@ public void dispose() {
gameApp.dispose();
}

try {
fileWatcher.interrupt();
}
catch (Exception ignore) {}

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}

Expand Down Expand Up @@ -4351,4 +4364,44 @@ private void vizualizePicking() {
public void setTitle(String title) {
Gdx.graphics.setTitle(title + " - DelvEdit");
}

private void startFileWatcher() {
// Super basic file watcher that refreshes the assets when any file changes.
fileWatcher = new Thread() {
public void run() {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
java.nio.file.Path path = Paths.get(Gdx.files.getLocalStoragePath());
path.register(watchService, ENTRY_CREATE, ENTRY_MODIFY);

boolean running = true;
while(running) {
WatchKey key = watchService.take();
for (WatchEvent<?> ignored : key.pollEvents()) {
refreshAssets();
break;
}

running = key.reset();
}
}
catch (Exception ignore) {}
}
};
fileWatcher.setPriority(Thread.MIN_PRIORITY);
fileWatcher.start();
}

private void refreshAssets() {
try {
// Give the asset a chance to finish writing.
Thread.sleep(125);

Art.refresh();
for (Entity e : level.entities) {
e.drawable.refresh();
}
}
catch (Exception ignored) {}
}
}
44 changes: 44 additions & 0 deletions Dungeoneer/src/com/interrupt/dungeoneer/Art.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ArrayMap;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.game.CachePools;
import com.interrupt.dungeoneer.game.Game;
import com.interrupt.dungeoneer.gfx.Bitmap;
import com.interrupt.dungeoneer.gfx.GlRenderer;
import com.interrupt.dungeoneer.gfx.Tesselator;
import com.interrupt.dungeoneer.gfx.TextureAtlas;
import com.interrupt.dungeoneer.ui.UiSkin;

Expand Down Expand Up @@ -205,4 +208,45 @@ static public void KillCache() {
// Might have some pooled meshes to clear
CachePools.clearMeshPool();
}

public static void refresh() {
try {
Art.KillCache();

Game.instance.loadManagers();
GameManager.renderer.initTextures();
GameManager.renderer.initShaders();

GlRenderer.staticMeshPool.resetAndDisposeAllMeshes();
Tesselator.tesselatorMeshPool.resetAndDisposeAllMeshes();

// reset all drawables now that we've reset stuff
for (Entity e : Game.GetLevel().entities) {
if(e.drawable != null) {
e.drawable.refresh();
e.drawable.update(e);
}
}
for (Entity e : Game.GetLevel().static_entities) {
if(e.drawable != null) {
e.drawable.refresh();
e.drawable.update(e);
}
}
for (Entity e : Game.GetLevel().non_collidable_entities) {
if(e.drawable != null) {
e.drawable.refresh();
e.drawable.update(e);
}
}

GameManager.renderer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Game.GetLevel().isDirty = true;

UiSkin.loadSkin();
}
catch(Exception ex) {
Gdx.app.log("Delver", "Could not refresh: " + ex.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
Expand All @@ -15,16 +14,12 @@
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Array;
import com.interrupt.dungeoneer.Art;
import com.interrupt.dungeoneer.GameManager;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.entities.Item;
import com.interrupt.dungeoneer.entities.Monster;
import com.interrupt.dungeoneer.entities.Player;
import com.interrupt.dungeoneer.entities.items.*;
import com.interrupt.dungeoneer.game.Game;
import com.interrupt.dungeoneer.gfx.GlRenderer;
import com.interrupt.dungeoneer.gfx.Tesselator;
import com.interrupt.dungeoneer.ui.UiSkin;
import com.interrupt.managers.ItemManager;

public class DebugOverlay extends WindowOverlay {
Expand Down Expand Up @@ -357,7 +352,7 @@ protected void addRefreshItem(Table table) {
@Override
public void clicked(InputEvent event, float x, float y) {
OverlayManager.instance.remove(thisOverlay);
refreshData();
Art.refresh();
}

@Override
Expand Down Expand Up @@ -765,46 +760,4 @@ public void clicked(InputEvent event, float x, float y) {

return contentTable;
}

// Re-load all the key assets
public void refreshData() {
try {
Art.KillCache();

Game.instance.loadManagers();
GameManager.renderer.initTextures();
GameManager.renderer.initShaders();

GlRenderer.staticMeshPool.resetAndDisposeAllMeshes();
Tesselator.tesselatorMeshPool.resetAndDisposeAllMeshes();

// reset all drawables now that we've reset stuff
for (Entity e : Game.GetLevel().entities) {
if(e.drawable != null) {
e.drawable.refresh();
e.drawable.update(e);
}
}
for (Entity e : Game.GetLevel().static_entities) {
if(e.drawable != null) {
e.drawable.refresh();
e.drawable.update(e);
}
}
for (Entity e : Game.GetLevel().non_collidable_entities) {
if(e.drawable != null) {
e.drawable.refresh();
e.drawable.update(e);
}
}

GameManager.renderer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Game.GetLevel().isDirty = true;

UiSkin.loadSkin();
}
catch(Exception ex) {
Gdx.app.log("Delver", "Could not refresh: " + ex.getMessage());
}
}
}

0 comments on commit fb0ebc8

Please sign in to comment.