xref: /oneTBB/examples/common/gui/convideo.cpp (revision 6caecf96)
1 /*
2     Copyright (c) 2005-2021 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 <cassert>
18 #include <stdio.h>
19 
20 #include "video.hpp"
21 
22 unsigned int *g_pImg = 0;
23 int g_sizex, g_sizey;
24 static video *g_video = 0;
25 static int g_fps = 0;
26 
27 #if _WIN32 || _WIN64
28 
29 static DWORD g_msec = 0;
30 
31 #ifdef _WINDOWS
32 HINSTANCE video::win_hInstance = 0;
33 int video::win_iCmdShow = 0;
34 void video::win_set_class(WNDCLASSEX &wcex) {}
35 void video::win_load_accelerators(int idc) {}
36 #endif //_WINDOWS
37 
38 #else
39 #include <sched.h>
40 #include <sys/time.h>
41 struct timeval g_time;
42 #endif //_WIN32||_WIN64
43 
44 #define CALC_FPS_ENABLED ((WINAPI_FAMILY != WINAPI_FAMILY_APP) && (!__ANDROID__))
45 
46 video::video()
47         // OpenGL* RGBA byte order for little-endian CPU
48         : depth(24),
49           red_shift(0),
50           green_shift(8),
51           blue_shift(16),
52           red_mask(0xff),
53           green_mask(0xff00),
54           blue_mask(0xff0000) {
55     assert(g_video == 0);
56     g_video = this;
57     title = "Video";
58     updating = calc_fps = false;
59 }
60 
61 bool video::init_window(int x, int y) {
62     g_sizex = x;
63     g_sizey = y;
64     g_pImg = new unsigned int[x * y];
65     running = true;
66     return false;
67 }
68 
69 bool video::init_console() {
70     running = true;
71     return true;
72 }
73 
74 void video::terminate() {
75 #if CALC_FPS_ENABLED
76     if (calc_fps) {
77         double fps = g_fps;
78 #if _WIN32 || _WIN64
79         fps /= (GetTickCount() - g_msec) / 1000.0;
80 #else
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 #endif
87         printf("%s: %.1f fps\n", title, fps);
88     }
89 #endif
90     g_video = 0;
91     running = false;
92     if (g_pImg) {
93         delete[] g_pImg;
94         g_pImg = 0;
95     }
96 }
97 
98 video::~video() {
99     if (g_video)
100         terminate();
101 }
102 
103 //! Count and display FPS count in titlebar
104 bool video::next_frame() {
105 #if CALC_FPS_ENABLED
106     if (calc_fps) {
107         if (!g_fps) {
108 #if _WIN32 || _WIN64
109             g_msec = GetTickCount();
110 #else
111             struct timezone tz;
112             gettimeofday(&g_time, &tz);
113 #endif
114         }
115         g_fps++;
116     }
117 #endif
118     return running;
119 }
120 
121 //! Do standard loop
122 void video::main_loop() {
123     on_process();
124 }
125 
126 //! Change window title
127 void video::show_title() {}
128 
129 ///////////////////////////////////////////// public methods of video class ///////////////////////
130 
131 drawing_area::drawing_area(int x, int y, int sizex, int sizey)
132         : base_index(y * g_sizex + x),
133           max_index(g_sizex * g_sizey),
134           index_stride(g_sizex),
135           pixel_depth(24),
136           ptr32(g_pImg),
137           start_x(x),
138           start_y(y),
139           size_x(sizex),
140           size_y(sizey) {
141     assert(x < g_sizex);
142     assert(y < g_sizey);
143     assert(x + sizex <= g_sizex);
144     assert(y + sizey <= g_sizey);
145 
146     index = base_index; // current index
147 }
148 
149 void drawing_area::update() {}
150