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 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 #include <cstdio>
47 #include <cstdlib>
48 #include <cstring>
49 
50 #include "types.hpp"
51 #include "api.hpp" /* The ray tracing library API */
52 #include "ui.hpp"
53 #include "util.hpp"
54 #include "tachyon_video.hpp"
55 
56 extern SceneHandle global_scene;
57 extern char *global_window_title;
58 extern bool global_usegraphics;
59 
on_process()60 void tachyon_video::on_process() {
61     char buf[8192];
62     flt runtime;
63     scenedef *scene = (scenedef *)global_scene;
64     updating_mode = scene->displaymode == RT_DISPLAY_ENABLED;
65     recycling = false;
66     pausing = false;
67     do {
68         updating = updating_mode;
69         timer start_timer = gettimer();
70         rt_renderscene(global_scene);
71         timer end_timer = gettimer();
72         runtime = timertime(start_timer, end_timer);
73         sprintf(buf, "%s: %.3f seconds", global_window_title, runtime);
74         rt_ui_message(MSG_0, buf);
75         title = buf;
76         show_title(); // show time spent for rendering
77         if (!updating) {
78             updating = true;
79             drawing_memory dm = get_drawing_memory();
80             drawing_area drawing(0, 0, dm.sizex, dm.sizey); // invalidate whole screen
81         }
82         rt_finalize();
83         title = global_window_title;
84         show_title(); // reset title to default
85     } while (recycling && running);
86 }
87 
on_key(int key)88 void tachyon_video::on_key(int key) {
89     key &= 0xff;
90     recycling = true;
91     if (key == esc_key)
92         running = false;
93     else if (key == ' ') {
94         if (!updating) {
95             updating = true;
96             drawing_memory dm = get_drawing_memory();
97             drawing_area drawing(0, 0, dm.sizex, dm.sizey); // invalidate whole screen
98         }
99         updating = updating_mode = !updating_mode;
100     }
101     else if (key == 'p') {
102         pausing = !pausing;
103         if (pausing) {
104             title = "Press ESC to exit or 'p' to continue after rendering completion";
105             show_title();
106         }
107     }
108 }
109 
rt_finalize(void)110 void rt_finalize(void) {
111     timer t0, t1;
112     t0 = gettimer();
113     if (global_usegraphics)
114         do {
115             rt_sleep(1);
116             t1 = gettimer();
117         } while ((timertime(t0, t1) < 10 || video->pausing) && video->next_frame());
118 #ifdef _WINDOWS
119     else
120         rt_sleep(10000);
121 #endif
122 }
123