1 /* 2 Copyright (c) 2005-2022 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "video.hpp" 18 #include <cassert> 19 #include <stdio.h> 20 #include <iostream> 21 #include <pthread.h> 22 23 unsigned int *g_pImg = nullptr; 24 int g_sizex = 0, g_sizey = 0; 25 static video *g_video = nullptr; 26 static int g_fps = 0; 27 char *window_title = nullptr; 28 #define WINDOW_TITLE_SIZE 256 29 int cocoa_update = 0; 30 31 #include <sched.h> 32 #include <sys/time.h> 33 struct timeval g_time; 34 35 video::video() 36 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ 37 : depth(24), 38 red_shift(0), 39 green_shift(8), 40 blue_shift(16), 41 red_mask(0xff), 42 green_mask(0xff00), 43 blue_mask(0xff0000) 44 #else 45 : depth(24), 46 red_shift(16), 47 green_shift(8), 48 blue_shift(0), 49 red_mask(0xff0000), 50 green_mask(0xff00), 51 blue_mask(0xff) 52 #endif 53 { 54 assert(g_video == nullptr); 55 g_video = this; 56 title = "Video"; 57 cocoa_update = 1; 58 updating = true; 59 calc_fps = false; 60 } 61 62 bool video::init_window(int x, int y) { 63 g_sizex = x; 64 g_sizey = y; 65 g_pImg = new unsigned int[x * y]; 66 if (window_title == nullptr) 67 window_title = (char *)malloc(WINDOW_TITLE_SIZE); 68 strncpy(window_title, title, WINDOW_TITLE_SIZE - 1); 69 running = true; 70 return true; 71 } 72 73 bool video::init_console() { 74 running = true; 75 return true; 76 } 77 78 void video::terminate() { 79 if (calc_fps) { 80 double fps = g_fps; 81 struct timezone tz; 82 struct timeval end_time; 83 gettimeofday(&end_time, &tz); 84 fps /= (end_time.tv_sec + 1.0 * end_time.tv_usec / 1000000.0) - 85 (g_time.tv_sec + 1.0 * g_time.tv_usec / 1000000.0); 86 printf("%s: %.1f fps\n", title, fps); 87 } 88 g_video = nullptr; 89 running = false; 90 91 delete[] g_pImg; 92 g_pImg = nullptr; 93 } 94 95 video::~video() { 96 if (g_video) 97 terminate(); 98 } 99 100 //! Count and display FPS count in titlebar 101 bool video::next_frame() { 102 if (calc_fps) { 103 if (!g_fps) { 104 struct timezone tz; 105 gettimeofday(&g_time, &tz); 106 } 107 g_fps++; 108 } 109 struct timezone tz; 110 struct timeval now_time; 111 gettimeofday(&now_time, &tz); 112 double sec = ((now_time.tv_sec + 1.0 * now_time.tv_usec / 1000000.0) - 113 (g_time.tv_sec + 1.0 * g_time.tv_usec / 1000000.0)); 114 if (sec > 1) { 115 if (calc_fps) { 116 memcpy(&g_time, &now_time, sizeof(g_time)); 117 int fps; 118 fps = g_fps / sec; 119 cocoa_update = (int)updating; 120 snprintf(window_title, 121 WINDOW_TITLE_SIZE, 122 "%s%s: %d fps", 123 title, 124 updating ? "" : " (no updating)", 125 int(fps)); 126 g_fps = 0; 127 } 128 } 129 return running; 130 } 131 132 void *thread_func(void *) { 133 g_video->on_process(); 134 exit(EXIT_SUCCESS); 135 } 136 137 extern "C" void on_mouse_func(int x, int y, int k) { 138 g_video->on_mouse(x, y, k); 139 return; 140 } 141 142 extern "C" void on_key_func(int x) { 143 g_video->on_key(x); 144 return; 145 } 146 147 extern "C" int cocoa_main(int argc, char *argv[]); 148 //! Do standard loop 149 void video::main_loop() { 150 pthread_t handle; 151 pthread_attr_t attr; 152 pthread_attr_init(&attr); 153 pthread_create(&handle, &attr, &thread_func, (void *)nullptr); 154 pthread_detach(handle); 155 cocoa_main(0, nullptr); 156 } 157 158 //! Change window title 159 void video::show_title() { 160 if (title) 161 strncpy(window_title, title, WINDOW_TITLE_SIZE); 162 return; 163 } 164 165 ///////////////////////////////////////////// public methods of video class /////////////////////// 166 167 drawing_area::drawing_area(int x, int y, int sizex, int sizey) 168 : base_index(y * g_sizex + x), 169 max_index(g_sizex * g_sizey), 170 index_stride(g_sizex), 171 pixel_depth(24), 172 ptr32(g_pImg), 173 start_x(x), 174 start_y(y), 175 size_x(sizex), 176 size_y(sizey) { 177 assert(x < g_sizex); 178 assert(y < g_sizey); 179 assert(x + sizex <= g_sizex); 180 assert(y + sizey <= g_sizey); 181 182 index = base_index; // current index 183 } 184 185 void drawing_area::update() { 186 //nothing to do, updating via timer in cocoa part. 187 } 188