| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "embedders/openglui/emulator/emulator_embedder.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 #include <sys/time.h> | |
| 9 #include <time.h> | |
| 10 #include "embedders/openglui/common/canvas_context.h" | |
| 11 #include "embedders/openglui/common/context.h" | |
| 12 #include "embedders/openglui/common/dart_host.h" | |
| 13 #include "embedders/openglui/common/events.h" | |
| 14 #include "embedders/openglui/common/input_handler.h" | |
| 15 #include "embedders/openglui/common/log.h" | |
| 16 #include "embedders/openglui/common/sound_handler.h" | |
| 17 #include "embedders/openglui/common/vm_glue.h" | |
| 18 #include "embedders/openglui/emulator/emulator_graphics_handler.h" | |
| 19 #include "embedders/openglui/emulator/emulator_resource.h" | |
| 20 | |
| 21 InputHandler* input_handler_ptr; | |
| 22 LifeCycleHandler* lifecycle_handler_ptr; | |
| 23 | |
| 24 struct timeval tvStart; | |
| 25 void tick(int data); | |
| 26 | |
| 27 Resource* MakePlatformResource(const char *path) { | |
| 28 return new EmulatorResource(path); | |
| 29 } | |
| 30 | |
| 31 void display() { | |
| 32 // Get number of msecs since last call. | |
| 33 struct timeval tvEnd; | |
| 34 gettimeofday(&tvEnd, NULL); | |
| 35 uint64_t now = (tvEnd.tv_usec + 1000000 * tvEnd.tv_sec); | |
| 36 | |
| 37 if (lifecycle_handler_ptr->OnStep() != 0) { | |
| 38 exit(-1); | |
| 39 } | |
| 40 // Schedule next call, trying to aim for 60fps. | |
| 41 uint64_t elapsed = now - (tvStart.tv_usec + 1000000 * tvStart.tv_sec); | |
| 42 int delay = 1000 / 60 - (elapsed / 1000); | |
| 43 if (delay < 0) delay = 0; | |
| 44 tvStart = tvEnd; | |
| 45 glutTimerFunc(delay, tick, 0); | |
| 46 } | |
| 47 | |
| 48 void tick(int data) { | |
| 49 display(); | |
| 50 } | |
| 51 | |
| 52 void reshape(int width, int height) { | |
| 53 glViewport(0, 0, width, height); | |
| 54 glMatrixMode(GL_PROJECTION); | |
| 55 glLoadIdentity(); | |
| 56 glOrtho(0, width, 0, height, -1, 1); | |
| 57 glMatrixMode(GL_MODELVIEW); | |
| 58 glutPostRedisplay(); | |
| 59 } | |
| 60 | |
| 61 void keyboard(unsigned char key, int x, int y) { | |
| 62 input_handler_ptr->OnKeyEvent(kKeyDown, time(0), key, false, false, false, 0); | |
| 63 input_handler_ptr->OnKeyEvent(kKeyUp, time(0), key, false, false, false, 0); | |
| 64 if (key == 'Q') { | |
| 65 lifecycle_handler_ptr->Pause(); | |
| 66 lifecycle_handler_ptr->Deactivate(); | |
| 67 lifecycle_handler_ptr->FreeAllResources(); | |
| 68 exit(0); | |
| 69 } else if (key == 'S') { | |
| 70 LOGI("Simulating suspend"); | |
| 71 lifecycle_handler_ptr->Pause(); | |
| 72 lifecycle_handler_ptr->Deactivate(); | |
| 73 } else if (key == 'R') { | |
| 74 LOGI("Simulating resume"); | |
| 75 lifecycle_handler_ptr->Activate(); | |
| 76 lifecycle_handler_ptr->Resume(); | |
| 77 } else if (key == '+') { | |
| 78 LOGI("Simulating focus gain"); | |
| 79 lifecycle_handler_ptr->Resume(); | |
| 80 } else if (key == '-') { | |
| 81 LOGI("Simulating focus loss"); | |
| 82 lifecycle_handler_ptr->Pause(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 void mouse(int button, int state, int x, int y) { | |
| 87 if (state == GLUT_UP) { | |
| 88 input_handler_ptr->OnMotionEvent(kMotionUp, time(0), x, y); | |
| 89 } else if (state == GLUT_DOWN) { | |
| 90 input_handler_ptr->OnMotionEvent(kMotionDown, time(0), x, y); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void motion(int x, int y) { | |
| 95 input_handler_ptr->OnMotionEvent(kMotionMove, time(0), x, y); | |
| 96 } | |
| 97 | |
| 98 DART_EXPORT void emulator_main(int argc, char** argv, const char* script) { | |
| 99 EmulatorGraphicsHandler graphics_handler(argc, argv); | |
| 100 if (argc > 0) { | |
| 101 int i = argc - 1; | |
| 102 size_t len = strlen(argv[i]); | |
| 103 if (len > 5 && strcmp(".dart", argv[i] + len - 5) == 0) { | |
| 104 script = argv[i]; | |
| 105 } | |
| 106 } | |
| 107 VMGlue vm_glue(&graphics_handler, ".", "gl.dart", script, 1); | |
| 108 InputHandler input_handler(&vm_glue); | |
| 109 input_handler_ptr = &input_handler; | |
| 110 SoundHandler sound_handler; | |
| 111 Timer timer; | |
| 112 Context app_context; | |
| 113 app_context.graphics_handler = &graphics_handler; | |
| 114 app_context.input_handler = &input_handler; | |
| 115 app_context.sound_handler = &sound_handler; | |
| 116 app_context.timer = &timer; | |
| 117 app_context.vm_glue = &vm_glue; | |
| 118 DartHost host(&app_context); | |
| 119 lifecycle_handler_ptr = &host; | |
| 120 glutReshapeFunc(reshape); | |
| 121 glutDisplayFunc(display); | |
| 122 glutKeyboardFunc(keyboard); | |
| 123 glutMouseFunc(mouse); | |
| 124 glutMotionFunc(motion); | |
| 125 lifecycle_handler_ptr->OnStart(); | |
| 126 lifecycle_handler_ptr->Activate(); | |
| 127 lifecycle_handler_ptr->Resume(); | |
| 128 gettimeofday(&tvStart, NULL); | |
| 129 glutTimerFunc(1, tick, 0); | |
| 130 glutMainLoop(); | |
| 131 } | |
| 132 | |
| OLD | NEW |