@@ -3,12 +3,15 @@
import java.awt.Canvas;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.fenggui.binding.render.Graphics;
import org.fenggui.binding.render.IOpenGL;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.binding.render.lwjgl.LWJGLBinding;
import org.fenggui.binding.render.lwjgl.LWJGLOpenGL;
import org.fenggui.util.Color;
import org.fenggui.util.Dimension;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
@@ -17,25 +20,28 @@
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

import com.google.common.util.concurrent.Atomics;
import com.kowymaker.client.graphics.core.event.EventManager;
import com.kowymaker.spec.utils.debug.Debug;

public class ClientEngine implements Runnable
{
private final Configuration config;
private final EventManager eventManager;
private final Configuration config;
private final EventManager eventManager;

private final FPS fps = new FPS();
private final FPS fps = new FPS();

private Object context = null;
private boolean running = false;
private boolean useDisplay = false;
private boolean updateSize = false;
private Object context = null;
private boolean running = false;
private boolean useDisplay = false;
private AtomicReference<Dimension> newDimension = Atomics.newReference();

private LWJGLBinding binding = null;
private LWJGLOpenGL gl = null;
private LWJGLBinding binding = null;
private LWJGLOpenGL gl = null;

private final List<IChild> childs = new ArrayList<IChild>();
public static String text = "";

private final List<IChild> childs = new ArrayList<IChild>();

public ClientEngine(Configuration config, EventManager eventManager)
{
@@ -56,22 +62,24 @@ public void run()
{
loop();
}

Display.destroy();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
}

private void initGL() throws LWJGLException
public void initGL() throws LWJGLException
{
if (context != null)
{
if (context instanceof Canvas)
{
Display.setParent((Canvas) context);

Display.setVSyncEnabled(true);

Display.create();

Mouse.create();
@@ -85,7 +93,10 @@ private void initGL() throws LWJGLException
}
}

binding = new LWJGLBinding();
if (binding == null)
{
binding = new LWJGLBinding();
}
gl = (LWJGLOpenGL) binding.getOpenGL();

// PROJECTION
@@ -103,31 +114,45 @@ private void initGL() throws LWJGLException

gl.enableTexture2D(true);
gl.setTexEnvModeModulate();
GL11.glEnable(GL11.GL_BLEND);
gl.enable(IOpenGL.Attribute.BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}

private void loop()
Debug.Locker locker = Debug.createLocker("Loop", new Debug.CounterLocker(3));

public void loop()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

Debug.update("Loop");
update();
long update = Debug.diffAndUpdate("Loop");
render();
long render = Debug.diff("Loop");

if (locker.lock())
{
System.out.println("[Loop] Update : " + update + " ms.");
System.out.println(" Render : " + render + " ms.");
System.out.println(" Total : " + (update + render) + " ms.");
}

if (useDisplay)
{
Display.update();
}

if (updateSize)
Dimension dim;
if ((dim = newDimension.getAndSet(null)) != null)
{
config.setWidth(dim.getWidth());
config.setHeight(dim.getHeight());

gl.setProjectionMatrixMode();
gl.loadIdentity();

gl.setOrtho(0, config.width, 0, config.height, -1, 1);
gl.setViewPort(0, 0, config.width, config.height);

updateSize = false;
}

gl.setModelMatrixMode();
@@ -159,8 +184,13 @@ private void render()
Graphics g = binding.getGraphics();

g.setFont(ImageFont.getDefaultFont());
gl.color(Color.RED);
g.setColor(Color.RED);
g.drawString("FPS: " + fps.getFps(), 0, 0);

g.drawString(text, 0, config.getHeight()
- ImageFont.getDefaultFont().getHeight());

g.setColor(config.getBackground());
}

public void addChild(IChild child)
@@ -208,6 +238,11 @@ public LWJGLBinding getBinding()
return binding;
}

public void setBinding(LWJGLBinding binding)
{
this.binding = binding;
}

public LWJGLOpenGL getGl()
{
return gl;
@@ -225,10 +260,7 @@ public FPS getFps()

public void resize(int width, int height)
{
config.width = width;
config.height = height;

updateSize = true;
newDimension.set(new Dimension(width, height));
}

public static class Configuration
@@ -29,25 +29,20 @@

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.kowymaker.client.graphics.core.ClientApplet;

public class EventManager
{
private final ClientApplet applet;
private Display display = null;
protected Display display = null;

private final Map<Integer, List<IKeyListener>> keyListeners = Maps.newLinkedHashMap();
private final Map<Integer, LWJGLEventBinder.State> keyStates = Maps.newLinkedHashMap();
private final Map<Integer, Character> keyCharValues = Maps.newLinkedHashMap();
private final Set<Key> modifiers = Sets.newTreeSet();
protected final Map<Integer, List<IKeyListener>> keyListeners = Maps.newLinkedHashMap();
protected final Map<Integer, LWJGLEventBinder.State> keyStates = Maps.newLinkedHashMap();
protected final Map<Integer, Character> keyCharValues = Maps.newLinkedHashMap();
protected final Set<Key> modifiers = Sets.newTreeSet();

private final Map<MouseButton, List<IMouseListener>> mouseListeners = Maps.newEnumMap(MouseButton.class);
private final Map<MouseButton, LWJGLEventBinder.State> mouseStates = Maps.newEnumMap(MouseButton.class);
protected final Map<MouseButton, List<IMouseListener>> mouseListeners = Maps.newEnumMap(MouseButton.class);
protected final Map<MouseButton, LWJGLEventBinder.State> mouseStates = Maps.newEnumMap(MouseButton.class);

public EventManager(ClientApplet applet)
{
this.applet = applet;
}
protected int scrollAmount = 0;

public void registerKeyListener(int key, IKeyListener keyListener)
{
@@ -96,6 +91,28 @@ public void update()
final long current = System.currentTimeMillis();
final long diff = current - state.getLastPressed();

if (button == MouseButton.WHEEL)
{
int scroll = Mouse.getDWheel();
scrollAmount += scroll;
if (display != null)
{
if (!display.fireMouseWheel(Mouse.getX(), Mouse.getY(),
(scroll > 0), scroll, scrollAmount))
{
listener.mouseWheel(new MouseWheelEvent(null, Mouse
.getX(), Mouse.getY(), (scroll > 0), scroll,
scrollAmount, modifiers));
}
}
else
{
listener.mouseWheel(new MouseWheelEvent(null, Mouse.getX(),
Mouse.getY(), (scroll > 0), scroll, scrollAmount,
modifiers));
}
}

if (Mouse.isButtonDown(button.getCode()))
{
if (!state.isPressed())
@@ -343,11 +360,6 @@ public char getChar(int code)
return keyCharValues.get(code);
}

public ClientApplet getApplet()
{
return applet;
}

public Display getDisplay()
{
return display;
@@ -358,7 +370,7 @@ public void setDisplay(Display display)
this.display = display;
}

private class ListKeyListener implements IKeyListener
public class ListKeyListener implements IKeyListener
{
private final List<IKeyListener> keyListeners;

@@ -393,7 +405,7 @@ public void keyTyped(KeyTypedEvent event)

}

private class ListMouseListener implements IMouseListener
public class ListMouseListener implements IMouseListener
{
private final List<IMouseListener> mouseListeners;

@@ -3,6 +3,7 @@
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.theme.DefaultTheme;

import com.kowymaker.client.graphics.core.ClientApplet;
import com.kowymaker.client.graphics.core.ClientEngine;
import com.kowymaker.client.graphics.core.IChild;
@@ -51,7 +52,7 @@ private void init(ClientEngine engine)
display = new Display(engine.getBinding());
FengGUI.setTheme(new DefaultTheme());

applet.getEventManager().setDisplay(display);
engine.getEventManager().setDisplay(display);
}

public void resize(int width, int height)