| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "embedders/openglui/emulator/emulator_embedder.h" | 5 #include "embedders/openglui/emulator/emulator_embedder.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 #include <sys/time.h> |
| 9 #include "embedders/openglui/common/canvas_context.h" |
| 9 #include "embedders/openglui/common/context.h" | 10 #include "embedders/openglui/common/context.h" |
| 10 #include "embedders/openglui/common/dart_host.h" | 11 #include "embedders/openglui/common/dart_host.h" |
| 11 #include "embedders/openglui/common/events.h" | 12 #include "embedders/openglui/common/events.h" |
| 12 #include "embedders/openglui/common/input_handler.h" | 13 #include "embedders/openglui/common/input_handler.h" |
| 13 #include "embedders/openglui/common/sound_handler.h" | 14 #include "embedders/openglui/common/sound_handler.h" |
| 14 #include "embedders/openglui/common/vm_glue.h" | 15 #include "embedders/openglui/common/vm_glue.h" |
| 15 #include "embedders/openglui/emulator/emulator_graphics_handler.h" | 16 #include "embedders/openglui/emulator/emulator_graphics_handler.h" |
| 16 | 17 |
| 17 InputHandler* input_handler_ptr; | 18 InputHandler* input_handler_ptr; |
| 18 LifeCycleHandler* lifecycle_handler_ptr; | 19 LifeCycleHandler* lifecycle_handler_ptr; |
| 19 | 20 |
| 21 struct timeval tvStart; |
| 22 void tick(int data); |
| 23 |
| 20 void display() { | 24 void display() { |
| 21 glClearColor(1.0, 1.0, 1.0, 1.0); | 25 // Get number of msecs since last call. |
| 22 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 26 struct timeval tvEnd; |
| 23 lifecycle_handler_ptr->OnStep(); | 27 gettimeofday(&tvEnd, NULL); |
| 24 glutSwapBuffers(); | 28 uint64_t elapsed = (tvEnd.tv_usec + 1000000 * tvEnd.tv_sec) - |
| 29 (tvStart.tv_usec + 1000000 * tvStart.tv_sec); |
| 30 |
| 31 if (lifecycle_handler_ptr->OnStep() != 0) { |
| 32 exit(-1); |
| 33 } |
| 34 // Schedule next call, trying to aim for 60fps. |
| 35 int delay = 1000 / 60 - (elapsed / 1000); |
| 36 if (delay < 0) delay = 0; |
| 37 tvStart = tvEnd; |
| 38 glutTimerFunc(delay, tick, 0); |
| 39 } |
| 40 |
| 41 void tick(int data) { |
| 42 display(); |
| 25 } | 43 } |
| 26 | 44 |
| 27 void reshape(int width, int height) { | 45 void reshape(int width, int height) { |
| 28 glViewport(0, 0, width, height); | 46 glViewport(0, 0, width, height); |
| 29 glMatrixMode(GL_PROJECTION); | 47 glMatrixMode(GL_PROJECTION); |
| 30 glLoadIdentity(); | 48 glLoadIdentity(); |
| 31 glOrtho(0, width, 0, height, -1, 1); | 49 glOrtho(0, width, 0, height, -1, 1); |
| 32 glMatrixMode(GL_MODELVIEW); | 50 glMatrixMode(GL_MODELVIEW); |
| 33 glutPostRedisplay(); | 51 glutPostRedisplay(); |
| 34 } | 52 } |
| 35 | 53 |
| 36 void keyboard(unsigned char key, int x, int y) { | 54 void keyboard(unsigned char key, int x, int y) { |
| 37 input_handler_ptr->OnKeyEvent(kKeyDown, time(0), 0, key, 0, 0); | 55 input_handler_ptr->OnKeyEvent(kKeyDown, time(0), 0, key, 0, 0); |
| 38 input_handler_ptr->OnKeyEvent(kKeyUp, time(0), 0, key, 0, 0); | 56 input_handler_ptr->OnKeyEvent(kKeyUp, time(0), 0, key, 0, 0); |
| 39 if (key == 27) { | 57 if (key == 27) { |
| 40 exit(0); | 58 exit(0); |
| 41 } | 59 } |
| 42 } | 60 } |
| 43 | 61 |
| 44 DART_EXPORT void emulator_main(int argc, char** argv, const char* script) { | 62 DART_EXPORT void emulator_main(int argc, char** argv, const char* script) { |
| 45 EmulatorGraphicsHandler graphics_handler(argc, argv); | 63 EmulatorGraphicsHandler graphics_handler(argc, argv); |
| 46 if (argc > 0) { | 64 if (argc > 0) { |
| 47 int i = argc - 1; | 65 int i = argc - 1; |
| 48 size_t len = strlen(argv[i]); | 66 size_t len = strlen(argv[i]); |
| 49 if (len > 5 && strcmp(".dart", argv[i] + len - 5) == 0) { | 67 if (len > 5 && strcmp(".dart", argv[i] + len - 5) == 0) { |
| 50 script = argv[i]; | 68 script = argv[i]; |
| 51 } | 69 } |
| 52 } | 70 } |
| 53 VMGlue vm_glue(&graphics_handler, ".", "gl.dart", script); | 71 VMGlue vm_glue(&graphics_handler, ".", "gl.dart", script); |
| 54 InputHandler input_handler(&vm_glue); | 72 InputHandler input_handler(&vm_glue); |
| 55 input_handler_ptr = &input_handler; | 73 input_handler_ptr = &input_handler; |
| 56 SoundHandler sound_handler; | 74 SoundHandler sound_handler; |
| 57 Timer timer; | 75 Timer timer; |
| 58 Context app_context; | 76 Context app_context; |
| 59 app_context.graphics_handler = &graphics_handler; | 77 app_context.graphics_handler = &graphics_handler; |
| 60 app_context.input_handler = &input_handler; | 78 app_context.input_handler = &input_handler; |
| 61 app_context.sound_handler = &sound_handler; | 79 app_context.sound_handler = &sound_handler; |
| 62 app_context.timer = &timer; | 80 app_context.timer = &timer; |
| 63 app_context.vm_glue = &vm_glue; | 81 app_context.vm_glue = &vm_glue; |
| 64 DartHost host(&app_context); | 82 DartHost host(&app_context); |
| 65 lifecycle_handler_ptr = &host; | 83 lifecycle_handler_ptr = &host; |
| 66 lifecycle_handler_ptr->OnActivate(); | |
| 67 glutReshapeFunc(reshape); | 84 glutReshapeFunc(reshape); |
| 68 glutDisplayFunc(display); | 85 glutDisplayFunc(display); |
| 69 glutKeyboardFunc(keyboard); | 86 glutKeyboardFunc(keyboard); |
| 70 glutIdleFunc(display); | 87 lifecycle_handler_ptr->OnActivate(); |
| 88 gettimeofday(&tvStart, NULL); |
| 89 glutTimerFunc(1, tick, 0); |
| 71 glutMainLoop(); | 90 glutMainLoop(); |
| 72 } | 91 } |
| 73 | 92 |
| OLD | NEW |