| Index: runtime/embedders/openglui/emulator/emulator_embedder.cc
|
| ===================================================================
|
| --- runtime/embedders/openglui/emulator/emulator_embedder.cc (revision 17737)
|
| +++ runtime/embedders/openglui/emulator/emulator_embedder.cc (working copy)
|
| @@ -5,7 +5,8 @@
|
| #include "embedders/openglui/emulator/emulator_embedder.h"
|
|
|
| #include <string.h>
|
| -
|
| +#include <sys/time.h>
|
| +#include "embedders/openglui/common/canvas_context.h"
|
| #include "embedders/openglui/common/context.h"
|
| #include "embedders/openglui/common/dart_host.h"
|
| #include "embedders/openglui/common/events.h"
|
| @@ -17,27 +18,44 @@
|
| InputHandler* input_handler_ptr;
|
| LifeCycleHandler* lifecycle_handler_ptr;
|
|
|
| +struct timeval tvStart;
|
| +void tick(int data);
|
| +
|
| void display() {
|
| - glClearColor(1.0, 1.0, 1.0, 1.0);
|
| - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
| - lifecycle_handler_ptr->OnStep();
|
| - glutSwapBuffers();
|
| + // Get number of msecs since last call.
|
| + struct timeval tvEnd;
|
| + gettimeofday(&tvEnd, NULL);
|
| + uint64_t elapsed = (tvEnd.tv_usec + 1000000 * tvEnd.tv_sec) -
|
| + (tvStart.tv_usec + 1000000 * tvStart.tv_sec);
|
| +
|
| + if (lifecycle_handler_ptr->OnStep() != 0) {
|
| + exit(-1);
|
| + }
|
| + // Schedule next call, trying to aim for 60fps.
|
| + int delay = 1000 / 60 - (elapsed / 1000);
|
| + if (delay < 0) delay = 0;
|
| + tvStart = tvEnd;
|
| + glutTimerFunc(delay, tick, 0);
|
| }
|
|
|
| +void tick(int data) {
|
| + display();
|
| +}
|
| +
|
| void reshape(int width, int height) {
|
| - glViewport(0, 0, width, height);
|
| - glMatrixMode(GL_PROJECTION);
|
| - glLoadIdentity();
|
| - glOrtho(0, width, 0, height, -1, 1);
|
| - glMatrixMode(GL_MODELVIEW);
|
| - glutPostRedisplay();
|
| + glViewport(0, 0, width, height);
|
| + glMatrixMode(GL_PROJECTION);
|
| + glLoadIdentity();
|
| + glOrtho(0, width, 0, height, -1, 1);
|
| + glMatrixMode(GL_MODELVIEW);
|
| + glutPostRedisplay();
|
| }
|
|
|
| void keyboard(unsigned char key, int x, int y) {
|
| input_handler_ptr->OnKeyEvent(kKeyDown, time(0), 0, key, 0, 0);
|
| input_handler_ptr->OnKeyEvent(kKeyUp, time(0), 0, key, 0, 0);
|
| if (key == 27) {
|
| - exit(0);
|
| + exit(0);
|
| }
|
| }
|
|
|
| @@ -63,11 +81,12 @@
|
| app_context.vm_glue = &vm_glue;
|
| DartHost host(&app_context);
|
| lifecycle_handler_ptr = &host;
|
| - lifecycle_handler_ptr->OnActivate();
|
| glutReshapeFunc(reshape);
|
| glutDisplayFunc(display);
|
| glutKeyboardFunc(keyboard);
|
| - glutIdleFunc(display);
|
| + lifecycle_handler_ptr->OnActivate();
|
| + gettimeofday(&tvStart, NULL);
|
| + glutTimerFunc(1, tick, 0);
|
| glutMainLoop();
|
| }
|
|
|
|
|