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 "oneapi/tbb/global_control.h"
18 
19 #include "seismic_video.hpp"
20 #include "universe.hpp"
21 
22 const char *const SeismicVideo::titles[2] = { "Seismic Simulation: Serial",
23                                               "Seismic Simulation: Parallel" };
on_mouse(int x,int y,int key)24 void SeismicVideo::on_mouse(int x, int y, int key) {
25     if (key == 1) {
26         u_.TryPutNewPulseSource(x, y);
27     }
28 }
29 
on_key(int key)30 void SeismicVideo::on_key(int key) {
31     key &= 0xff;
32     if (char(key) == ' ')
33         initIsParallel = !initIsParallel;
34     else if (char(key) == 'p')
35         initIsParallel = true;
36     else if (char(key) == 's')
37         initIsParallel = false;
38     else if (char(key) == 'e')
39         updating = true;
40     else if (char(key) == 'd')
41         updating = false;
42     else if (key == 27)
43         running = false;
44     title = titles[initIsParallel ? 1 : 0];
45 }
46 
on_process()47 void SeismicVideo::on_process() {
48     oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism,
49                                   threadsHigh);
50     for (int frames = 0; numberOfFrames_ == 0 || frames < numberOfFrames_; ++frames) {
51         if (initIsParallel)
52             u_.ParallelUpdateUniverse();
53         else
54             u_.SerialUpdateUniverse();
55         if (!next_frame())
56             break;
57     }
58 }
59 
60 #if defined(_WINDOWS) && !defined(_CONSOLE)
61 #include "resource.hpp"
62 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
63 SeismicVideo *gVideo = nullptr;
64 #endif
65 
SeismicVideo(Universe & u,int number_of_frames,int threads_high,bool init_is_parallel)66 SeismicVideo::SeismicVideo(Universe &u,
67                            int number_of_frames,
68                            int threads_high,
69                            bool init_is_parallel)
70         : numberOfFrames_(number_of_frames),
71           initIsParallel(init_is_parallel),
72           u_(u),
73           threadsHigh(threads_high) {
74     title = titles[initIsParallel ? 1 : 0];
75 #if defined(_WINDOWS) && !defined(_CONSOLE)
76     gVideo = this;
77     LoadStringA(video::win_hInstance, IDC_SEISMICSIMULATION, szWindowClass, MAX_LOADSTRING);
78     memset(&wcex, 0, sizeof(wcex));
79     wcex.lpfnWndProc = (WNDPROC)WndProc;
80     wcex.hIcon = LoadIcon(video::win_hInstance, MAKEINTRESOURCE(IDI_SEISMICSIMULATION));
81     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
82     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
83     wcex.lpszMenuName = LPCTSTR(IDC_SEISMICSIMULATION);
84     wcex.lpszClassName = szWindowClass;
85     wcex.hIconSm = LoadIcon(video::win_hInstance, MAKEINTRESOURCE(IDI_SMALL));
86     win_set_class(wcex); // ascii convention here
87     win_load_accelerators(IDC_SEISMICSIMULATION);
88 #endif
89 }
90 
91 #if defined(_WINDOWS) && !defined(_CONSOLE)
92 
93 //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
94 
95 //  PURPOSE:  Processes messages for the main window.
96 
97 //  WM_COMMAND  - process the application menu
98 //  WM_PAINT    - Paint the main window
99 //  WM_DESTROY  - post a quit message and return
100 
About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)101 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
102     switch (message) {
103         case WM_INITDIALOG: return TRUE;
104         case WM_COMMAND:
105             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
106                 EndDialog(hDlg, LOWORD(wParam));
107                 return TRUE;
108             }
109             break;
110     }
111     return FALSE;
112 }
113 
WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)114 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
115     int wmId;
116     switch (message) {
117         case WM_COMMAND:
118             wmId = LOWORD(wParam);
119             // Parse the menu selections:
120             switch (wmId) {
121                 case IDM_ABOUT:
122                     DialogBox(
123                         video::win_hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About);
124                     break;
125                 case IDM_EXIT: PostQuitMessage(0); break;
126                 case ID_FILE_PARALLEL: gVideo->on_key('p'); break;
127                 case ID_FILE_SERIAL: gVideo->on_key('s'); break;
128                 case ID_FILE_ENABLEGUI: gVideo->on_key('e'); break;
129                 case ID_FILE_DISABLEGUI: gVideo->on_key('d'); break;
130                 default: return DefWindowProc(hWnd, message, wParam, lParam);
131             }
132             break;
133         default: return DefWindowProc(hWnd, message, wParam, lParam);
134     }
135     return 0;
136 }
137 
138 #endif
139