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

@@ -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; }