Working toward render timing

This commit is contained in:
Joshua Granick
2014-06-09 14:44:02 -07:00
parent 0f6adf39d7
commit e491460630
21 changed files with 498 additions and 33 deletions

View File

@@ -16,15 +16,17 @@ import lime.ui.WindowEvent;
import lime.system.System;
class Application implements IKeyEventListener implements IMouseEventListener implements ITouchEventListener implements IWindowEventListener {
class Application implements IKeyEventListener implements IMouseEventListener implements IRenderEventListener implements ITouchEventListener implements IUpdateEventListener implements IWindowEventListener {
public var handle:Dynamic;
private var lastUpdate:Int;
public function new () {
lastUpdate = 0;
}
@@ -32,17 +34,21 @@ class Application implements IKeyEventListener implements IMouseEventListener im
public function create (config:Config) {
#if (cpp || neko)
handle = lime_application_create ();
handle = lime_application_create (null);
#end
new KeyEventManager ();
new MouseEventManager ();
new RenderEventManager ();
new TouchEventManager ();
new UpdateEventManager ();
new WindowEventManager ();
KeyEventManager.addEventListener (this);
MouseEventManager.addEventListener (this);
RenderEventManager.addEventListener (this);
TouchEventManager.addEventListener (this);
UpdateEventManager.addEventListener (this);
WindowEventManager.addEventListener (this);
var window = new Window (this);
@@ -65,32 +71,19 @@ class Application implements IKeyEventListener implements IMouseEventListener im
public function onMouseDown (event:MouseEvent):Void {}
public function onMouseMove (event:MouseEvent):Void {}
public function onMouseUp (event:MouseEvent):Void {}
public function onRender (event:RenderEvent):Void {}
public function onTouchEnd (event:TouchEvent):Void {}
public function onTouchMove (event:TouchEvent):Void {}
public function onTouchStart (event:TouchEvent):Void {}
public function onUpdate (event:UpdateEvent):Void {}
public function onWindowActivate (event:WindowEvent):Void {}
public function onWindowDeactivate (event:WindowEvent):Void {}
public function render ():Void {
}
public function update ():Void {
}
#if (cpp || neko)
private static var lime_application_create = System.load ("lime", "lime_application_create", 0);
private static var lime_application_create = System.load ("lime", "lime_application_create", 1);
private static var lime_application_exec = System.load ("lime", "lime_application_exec", 1);
private static var lime_application_get_ticks = System.load ("lime", "lime_application_get_ticks", 0);
#end

View File

@@ -0,0 +1,10 @@
package lime.app;
interface IRenderEventListener {
function onRender (event:RenderEvent):Void;
}

View File

@@ -0,0 +1,10 @@
package lime.app;
interface IUpdateEventListener {
function onUpdate (event:UpdateEvent):Void;
}

31
lime/app/RenderEvent.hx Normal file
View File

@@ -0,0 +1,31 @@
package lime.app;
class RenderEvent {
public var type:RenderEventType;
public function new (type:RenderEventType = null) {
this.type = type;
}
public function clone ():RenderEvent {
return new RenderEvent (type);
}
}
@:enum abstract RenderEventType(Int) {
var RENDER = 0;
}

View File

@@ -0,0 +1,56 @@
package lime.app;
import lime.system.System;
class RenderEventManager {
private static var instance:RenderEventManager;
private var listeners:Array<IRenderEventListener>;
public function new () {
listeners = new Array ();
instance = this;
#if (cpp || neko)
lime_render_event_manager_register (handleEvent, new RenderEvent ());
#end
}
public static function addEventListener (listener:IRenderEventListener):Void {
if (instance != null) {
instance.listeners.push (listener);
}
}
private function handleEvent (event:RenderEvent):Void {
var event = event.clone ();
for (listener in listeners) {
listener.onRender (event);
}
}
#if (cpp || neko)
private static var lime_render_event_manager_register = System.load ("lime", "lime_render_event_manager_register", 2);
#end
}

View File

@@ -9,6 +9,8 @@ class Renderer {
public var handle:Dynamic;
private static var instance:Renderer;
public function new (window:Window) {
@@ -16,11 +18,21 @@ class Renderer {
handle = lime_renderer_create (window.handle);
#end
instance = this;
}
public static function flip ():Void {
lime_renderer_flip (instance.handle);
}
#if (cpp || neko)
private static var lime_renderer_create = System.load ("lime", "lime_renderer_create", 1);
private static var lime_renderer_flip = System.load ("lime", "lime_renderer_flip", 1);
#end

33
lime/app/UpdateEvent.hx Normal file
View File

@@ -0,0 +1,33 @@
package lime.app;
class UpdateEvent {
public var deltaTime:Int;
public var type:UpdateEventType;
public function new (type:UpdateEventType = null, deltaTime:Int = 0) {
this.type = type;
this.deltaTime = deltaTime;
}
public function clone ():UpdateEvent {
return new UpdateEvent (type, deltaTime);
}
}
@:enum abstract UpdateEventType(Int) {
var UPDATE = 0;
}

View File

@@ -0,0 +1,56 @@
package lime.app;
import lime.system.System;
class UpdateEventManager {
private static var instance:UpdateEventManager;
private var listeners:Array<IUpdateEventListener>;
public function new () {
listeners = new Array ();
instance = this;
#if (cpp || neko)
lime_update_event_manager_register (handleEvent, new UpdateEvent ());
#end
}
public static function addEventListener (listener:IUpdateEventListener):Void {
if (instance != null) {
instance.listeners.push (listener);
}
}
private function handleEvent (event:UpdateEvent):Void {
var event = event.clone ();
for (listener in listeners) {
listener.onUpdate (event);
}
}
#if (cpp || neko)
private static var lime_update_event_manager_register = System.load ("lime", "lime_update_event_manager_register", 2);
#end
}

View File

@@ -48,6 +48,8 @@
</section>
<file name="src/app/RenderEvent.cpp" />
<file name="src/app/UpdateEvent.cpp" />
<file name="src/ui/KeyEvent.cpp" />
<file name="src/ui/MouseEvent.cpp" />
<file name="src/ui/TouchEvent.cpp" />

View File

@@ -2,6 +2,9 @@
#define LIME_APP_APPLICATION_H
#include <hx/CFFI.h>
namespace lime {
@@ -10,6 +13,10 @@ namespace lime {
public:
static double GetTicks ();
static AutoGCRoot* callback;
virtual int Exec () = 0;

View File

@@ -0,0 +1,37 @@
#ifndef LIME_APP_RENDER_EVENT_H
#define LIME_APP_RENDER_EVENT_H
#include <hx/CFFI.h>
namespace lime {
enum RenderEventType {
RENDER
};
class RenderEvent {
public:
static AutoGCRoot* callback;
static AutoGCRoot* eventObject;
RenderEvent ();
static void Dispatch (RenderEvent* event);
RenderEventType type;
};
}
#endif

View File

@@ -13,6 +13,8 @@ namespace lime {
public:
virtual void Flip () = 0;
Window* currentWindow;

View File

@@ -0,0 +1,38 @@
#ifndef LIME_APP_UPDATE_EVENT_H
#define LIME_APP_UPDATE_EVENT_H
#include <hx/CFFI.h>
namespace lime {
enum UpdateEventType {
UPDATE
};
class UpdateEvent {
public:
static AutoGCRoot* callback;
static AutoGCRoot* eventObject;
UpdateEvent ();
static void Dispatch (UpdateEvent* event);
int deltaTime;
UpdateEventType type;
};
}
#endif

View File

@@ -9,8 +9,10 @@
#include <hx/CFFI.h>
#include <app/Application.h>
#include <app/Window.h>
#include <app/Renderer.h>
#include <app/RenderEvent.h>
#include <app/UpdateEvent.h>
#include <app/Window.h>
#include <ui/KeyEvent.h>
#include <ui/MouseEvent.h>
#include <ui/TouchEvent.h>
@@ -20,9 +22,10 @@
namespace lime {
value lime_application_create () {
value lime_application_create (value callback) {
Application* app = CreateApplication ();
Application::callback = new AutoGCRoot (callback);
return alloc_int ((intptr_t)app);
}
@@ -36,6 +39,13 @@ namespace lime {
}
value lime_application_get_ticks (value application) {
return alloc_float (Application::GetTicks ());
}
value lime_key_event_manager_register (value callback, value eventObject) {
KeyEvent::callback = new AutoGCRoot (callback);
@@ -80,6 +90,15 @@ namespace lime {
}
value lime_render_event_manager_register (value callback, value eventObject) {
RenderEvent::callback = new AutoGCRoot (callback);
RenderEvent::eventObject = new AutoGCRoot (eventObject);
return alloc_null ();
}
value lime_renderer_create (value window) {
Renderer* renderer = CreateRenderer ((Window*)(intptr_t)val_int (window));
@@ -88,6 +107,14 @@ namespace lime {
}
value lime_renderer_flip (value renderer) {
((Renderer*)(intptr_t)renderer)->Flip ();
return alloc_null ();
}
value lime_touch_event_manager_register (value callback, value eventObject) {
TouchEvent::callback = new AutoGCRoot (callback);
@@ -97,6 +124,15 @@ namespace lime {
}
value lime_update_event_manager_register (value callback, value eventObject) {
UpdateEvent::callback = new AutoGCRoot (callback);
UpdateEvent::eventObject = new AutoGCRoot (eventObject);
return alloc_null ();
}
value lime_window_create (value application) {
Window* window = CreateWindow ((Application*)(intptr_t)val_int (application));
@@ -114,14 +150,18 @@ namespace lime {
}
DEFINE_PRIM (lime_application_create, 0);
DEFINE_PRIM (lime_application_create, 1);
DEFINE_PRIM (lime_application_exec, 1);
DEFINE_PRIM (lime_application_get_ticks, 0);
DEFINE_PRIM (lime_key_event_manager_register, 2);
DEFINE_PRIM (lime_lzma_encode, 1);
DEFINE_PRIM (lime_lzma_decode, 1);
DEFINE_PRIM (lime_mouse_event_manager_register, 2);
DEFINE_PRIM (lime_renderer_create, 1);
DEFINE_PRIM (lime_renderer_flip, 1);
DEFINE_PRIM (lime_render_event_manager_register, 2);
DEFINE_PRIM (lime_touch_event_manager_register, 2);
DEFINE_PRIM (lime_update_event_manager_register, 2);
DEFINE_PRIM (lime_window_create, 1);
DEFINE_PRIM (lime_window_event_manager_register, 2);

View File

@@ -0,0 +1,43 @@
#include <hx/CFFI.h>
#include <app/RenderEvent.h>
namespace lime {
AutoGCRoot* RenderEvent::callback = 0;
AutoGCRoot* RenderEvent::eventObject = 0;
//static int id_type;
//static bool init = false;
RenderEvent::RenderEvent () {
type = RENDER;
}
void RenderEvent::Dispatch (RenderEvent* event) {
if (RenderEvent::callback) {
//if (!init) {
//id_type = val_id ("type");
//}
value object = (RenderEvent::eventObject ? RenderEvent::eventObject->get () : alloc_empty_object ());
//alloc_field (object, id_type, alloc_int (event->type));
val_call1 (RenderEvent::callback->get (), object);
}
}
}

View File

@@ -0,0 +1,47 @@
#include <hx/CFFI.h>
#include <app/UpdateEvent.h>
namespace lime {
AutoGCRoot* UpdateEvent::callback = 0;
AutoGCRoot* UpdateEvent::eventObject = 0;
static int id_deltaTime;
//static int id_type;
static bool init = false;
UpdateEvent::UpdateEvent () {
deltaTime = 0;
type = UPDATE;
}
void UpdateEvent::Dispatch (UpdateEvent* event) {
if (UpdateEvent::callback) {
if (!init) {
id_deltaTime = val_id ("deltaTime");
//id_type = val_id ("type");
}
value object = (UpdateEvent::eventObject ? UpdateEvent::eventObject->get () : alloc_empty_object ());
alloc_field (object, id_deltaTime, alloc_int (event->deltaTime));
//alloc_field (object, id_type, alloc_int (event->type));
val_call1 (UpdateEvent::callback->get (), object);
}
}
}

View File

@@ -4,6 +4,16 @@
namespace lime {
AutoGCRoot* Application::callback = 0;
double Application::GetTicks () {
return SDL_GetTicks ();
}
SDLApplication::SDLApplication () {
SDL_Init (SDL_INIT_VIDEO);
@@ -22,23 +32,43 @@ namespace lime {
SDL_Event event;
active = true;
bool firstTime = true;
lastUpdate = SDL_GetTicks ();
Uint32 currentUpdate = 0;
int deltaTime = 0;
while (active) {
while (active && SDL_WaitEvent (&event)) {
HandleEvent (&event);
event.type = -1;
if (!active) break;
}
event.type = -1;
while (active && SDL_PollEvent (&event)) {
while (active && (firstTime || SDL_WaitEvent (&event))) {
firstTime = false;
HandleEvent (&event);
event.type = -1;
if (!active) break;
while (active && SDL_PollEvent (&event)) {
HandleEvent (&event);
event.type = -1;
if (!active) break;
}
currentUpdate = SDL_GetTicks ();
deltaTime = currentUpdate - lastUpdate;
if (deltaTime > 16) {
updateEvent.deltaTime = deltaTime;
UpdateEvent::Dispatch (&updateEvent);
RenderEvent::Dispatch (&renderEvent);
}
}
}
@@ -92,7 +122,11 @@ namespace lime {
ProcessWindowEvent (event);
break;
case SDL_WINDOWEVENT_EXPOSED: /*poll*/ break;
case SDL_WINDOWEVENT_EXPOSED:
RenderEvent::Dispatch (&renderEvent);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED: /*resize*/ break;
case SDL_WINDOWEVENT_FOCUS_GAINED: /*focus in*/ break;
case SDL_WINDOWEVENT_FOCUS_LOST: /*focus out*/ break;

View File

@@ -4,6 +4,8 @@
#include <SDL.h>
#include <app/Application.h>
#include <app/RenderEvent.h>
#include <app/UpdateEvent.h>
#include <ui/KeyEvent.h>
#include <ui/MouseEvent.h>
#include <ui/TouchEvent.h>
@@ -32,8 +34,11 @@ namespace lime {
bool active;
KeyEvent keyEvent;
Uint32 lastUpdate;
MouseEvent mouseEvent;
RenderEvent renderEvent;
TouchEvent touchEvent;
UpdateEvent updateEvent;
WindowEvent windowEvent;
};

View File

@@ -23,6 +23,13 @@ namespace lime {
}
void SDLRenderer::Flip () {
SDL_RenderPresent (sdlRenderer);
}
Renderer* CreateRenderer (Window* window) {
return new SDLRenderer (window);

View File

@@ -16,6 +16,8 @@ namespace lime {
SDLRenderer (Window* window);
~SDLRenderer ();
virtual void Flip ();
SDL_Renderer* sdlRenderer;
};

View File

@@ -15,7 +15,7 @@
#ifdef DECLARE_EXTENSION
namespace lime { extern void *OpenGLBindings::handle; }
namespace lime { void *OpenGLBindings::handle; }
#define OGL_EXT(func,ret,args) \
namespace lime { ret (CALLING_CONVENTION *func)args; }