JNR
laProgressiveTask.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "laParser.h"
32 #include "Core.h"
33 #include "Core.h"
34 
35 laProgressiveTask::laProgressiveTask(void)
36 {
37  _strCurrentLabel[0] = '\0';
38  _dValue = 0;
39  _dRange = 1;
40  _pST = NULL;
41 
42  ::InitializeCriticalSection(&cs);
43 }
44 
45 laProgressiveTask::~laProgressiveTask(void)
46 {
47  ::DeleteCriticalSection(&cs);
48 }
49 
50 void laProgressiveTask::progressIncrease(double val, char *str, ...)
51 {
52  ::EnterCriticalSection(&cs);
53  va_list ap;
54 
55  _dValue = M_MIN(1, (_dValue+val));
56 
57  if(str)
58  {
59  va_start(ap, str);
60  vsnprintf(_strCurrentLabel, 255, str, ap);
61  va_end(ap);
62 
63  ::laSystemIntegrator::getEnvironment()->message(M_TRUE, _strCurrentLabel);
64  }
65  ::LeaveCriticalSection(&cs);
66 }
67 
68 void laProgressiveTask::progressReset(char *str, ...)
69 {
70  ::EnterCriticalSection(&cs);
71  va_list ap;
72  _dValue = 0;
73 
74  if(str)
75  {
76  va_start(ap, str);
77  vsnprintf(_strCurrentLabel, 255, str, ap);
78  va_end(ap);
79 
80  ::laSystemIntegrator::getEnvironment()->message(M_TRUE, _strCurrentLabel);
81  }
82 
83  ::LeaveCriticalSection(&cs);
84 }
85 
86 void laProgressiveTask::progressSubtask(laProgressiveTask* pts, double dRange)
87 {
88  ::EnterCriticalSection(&cs);
89 
90  //Adding a subtask
91  if(pts)
92  {
93  ASSERT(!_pST, "Subtask already present.");
94 
95  _pST = pts;
96  _pST->_dRange = dRange;
97  }
98  //Removing a subtask
99  else
100  {
101  ASSERT(_pST, "Nil subtask.");
102 
103  _dValue += (_pST->progress()) * (_pST->_dRange);
104  strcpy(_strCurrentLabel, _pST->status());
105 
106  _pST = NULL;
107  }
108 
109  ::LeaveCriticalSection(&cs);
110 }
111 
112 double laProgressiveTask::progress()
113 {
114  ::EnterCriticalSection(&cs);
115 
116  double v = _dValue;
117  if(_pST) v += (_pST->progress()) * (_pST->_dRange);
118 
119  ::LeaveCriticalSection(&cs);
120 
121  return v;
122 }
123 
124 char* laProgressiveTask::status()
125 {
126  if(_pST) return _pST->status();
127  else return _strCurrentLabel;
128 }
Progressive Task.