xref: /oneTBB/examples/common/gui/convideo.cpp (revision cf29ce81)
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 
93     delete[] g_pImg;
94     g_pImg = nullptr;
95 }
96 
97 video::~video() {
98     if (g_video)
99         terminate();
100 }
101 
102 //! Count and display FPS count in titlebar
103 bool video::next_frame() {
104 #if CALC_FPS_ENABLED
105     if (calc_fps) {
106         if (!g_fps) {
107 #if _WIN32 || _WIN64
108             g_msec = GetTickCount();
109 #else
110             struct timezone tz;
111             gettimeofday(&g_time, &tz);
112 #endif
113         }
114         g_fps++;
115     }
116 #endif
117     return running;
118 }
119 
120 //! Do standard loop
121 void video::main_loop() {
122     on_process();
123 }
124 
125 //! Change window title
126 void video::show_title() {}
127 
128 ///////////////////////////////////////////// public methods of video class ///////////////////////
129 
130 drawing_area::drawing_area(int x, int y, int sizex, int sizey)
131         : base_index(y * g_sizex + x),
132           max_index(g_sizex * g_sizey),
133           index_stride(g_sizex),
134           pixel_depth(24),
135           ptr32(g_pImg),
136           start_x(x),
137           start_y(y),
138           size_x(sizex),
139           size_y(sizey) {
140     assert(x < g_sizex);
141     assert(y < g_sizey);
142     assert(x + sizex <= g_sizex);
143     assert(y + sizey <= g_sizey);
144 
145     index = base_index; // current index
146 }
147 
148 void drawing_area::update() {}
149