924324b90a8c4ff38cc9f4b6e3aad9eb397f4925
[oweals/minetest.git] / src / utility.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "utility.h"
25 #include "irrlichtwrapper.h"
26 #include "gettime.h"
27
28 TimeTaker::TimeTaker(const char *name, u32 *result)
29 {
30         m_name = name;
31         m_result = result;
32         m_running = true;
33         m_time1 = getTimeMs();
34 }
35
36 u32 TimeTaker::stop(bool quiet)
37 {
38         if(m_running)
39         {
40                 u32 time2 = getTimeMs();
41                 u32 dtime = time2 - m_time1;
42                 if(m_result != NULL)
43                 {
44                         (*m_result) += dtime;
45                 }
46                 else
47                 {
48                         if(quiet == false)
49                                 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
50                 }
51                 m_running = false;
52                 return dtime;
53         }
54         return 0;
55 }
56
57 const v3s16 g_26dirs[26] =
58 {
59         // +right, +top, +back
60         v3s16( 0, 0, 1), // back
61         v3s16( 0, 1, 0), // top
62         v3s16( 1, 0, 0), // right
63         v3s16( 0, 0,-1), // front
64         v3s16( 0,-1, 0), // bottom
65         v3s16(-1, 0, 0), // left
66         // 6
67         v3s16(-1, 1, 0), // top left
68         v3s16( 1, 1, 0), // top right
69         v3s16( 0, 1, 1), // top back
70         v3s16( 0, 1,-1), // top front
71         v3s16(-1, 0, 1), // back left
72         v3s16( 1, 0, 1), // back right
73         v3s16(-1, 0,-1), // front left
74         v3s16( 1, 0,-1), // front right
75         v3s16(-1,-1, 0), // bottom left
76         v3s16( 1,-1, 0), // bottom right
77         v3s16( 0,-1, 1), // bottom back
78         v3s16( 0,-1,-1), // bottom front
79         // 18
80         v3s16(-1, 1, 1), // top back-left
81         v3s16( 1, 1, 1), // top back-right
82         v3s16(-1, 1,-1), // top front-left
83         v3s16( 1, 1,-1), // top front-right
84         v3s16(-1,-1, 1), // bottom back-left
85         v3s16( 1,-1, 1), // bottom back-right
86         v3s16(-1,-1,-1), // bottom front-left
87         v3s16( 1,-1,-1), // bottom front-right
88         // 26
89 };
90
91 static unsigned long next = 1;
92
93 /* RAND_MAX assumed to be 32767 */
94 int myrand(void)
95 {
96    next = next * 1103515245 + 12345;
97    return((unsigned)(next/65536) % 32768);
98 }
99
100 void mysrand(unsigned seed)
101 {
102    next = seed;
103 }
104
105