JNR
laFame.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 //
31 // FILE: laFame.cpp
32 //
33 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
34 //
35 #include "stdafx.h"
36 #include "Core.h"
37 
38 laFame::laFame(void)
39 {
40 }
41 
42 laFame::~laFame(void)
43 {
44 }
45 
46 void laFame::load(char *strFameFile)
47 {
48  laFileParser fp;
49  if(fp.fileOpen(strFameFile)==NULL)
50  {
51  for(unsigned i=0; i<10; i++)
52  {
53  strcpy(_arNames[i], "(empty)");
54  _arPlayTimes[i] = 0;
55  }
56  return;
57  }
58 
59  for(unsigned i=0; i<10; i++)
60  {
61  fp.readText(_arNames[i]);
62  fp.readDouble(_arPlayTimes+i);
63  }
64 }
65 
66 void laFame::ctlSave(char *strFameFile)
67 {
68  FILE *f = fopen(strFameFile, "w");
69  for(unsigned i=0; i<10; i++)
70  {
71  fprintf(f, "name: '%s'\n", _arNames[i]);
72  fprintf(f, "play-time: %.2f\n\n", (float)_arPlayTimes[i]);
73  }
74  fclose(f);
75 }
76 
77 void laFame::ctlUpdate(char* strName, double dPlayTime)
78 {
79  M_BOOL bUnsorted = M_FALSE;
80  M_BOOL bFound = M_FALSE;
81 
82  for(unsigned i=0; i<10; i++)
83  {
84  if(!strcmp(_arNames[i], strName))
85  {
86  if(dPlayTime<_arPlayTimes[i])
87  {
88  _arPlayTimes[i] = dPlayTime;
89  bUnsorted = M_TRUE;
90  }
91  bFound = M_TRUE;
92  break;
93  }
94  }
95 
96  if(!bFound)
97  {
98  for(unsigned i=0; i<10; i++)
99  {
100  if( (_arPlayTimes[i]>dPlayTime) || (!strcmp(_arNames[i], "(empty)")))
101  {
102  for(unsigned j=9; j<=i; j--)
103  {
104  strcpy(_arNames[j], _arNames[j-1]);
105  _arPlayTimes[j] = _arPlayTimes[j-1];
106  }
107  strcpy(_arNames[i], strName);
108  _arPlayTimes[i] = dPlayTime;
109  break;
110  }
111  }
112  }
113 
114  if(bUnsorted)
115  {
116  unsigned nMin, nLastSorted;
117  char strTemp[128];
118  double dTemp;
119 
120  for(nLastSorted=0; nLastSorted<10; nLastSorted++)
121  {
122  nMin = nLastSorted;
123 
124  for(unsigned i=nLastSorted+1; i<10; i++){
125  if( (_arPlayTimes[i]<_arPlayTimes[nMin]) && strcmp(_arNames[i], "(empty)") )
126  {
127  nMin = i;
128  }
129  }
130 
131  if(nMin!=nLastSorted)
132  {
133  strcpy(strTemp, _arNames[nLastSorted]);
134  strcpy(_arNames[nLastSorted], _arNames[nMin]);
135  strcpy(_arNames[nMin], strTemp);
136 
137  dTemp = _arPlayTimes[nLastSorted];
138  _arPlayTimes[nLastSorted] = _arPlayTimes[nMin];
139  _arPlayTimes[nMin] = dTemp;
140  }
141  }
142  }
143 }
144 
145 unsigned laFame::getMin(unsigned i)
146 {
147  return (unsigned)_arPlayTimes[i]/60;
148 }
149 
150 unsigned laFame::getSec(unsigned i)
151 {
152  unsigned min = (unsigned)_arPlayTimes[i]/60;
153  return (unsigned)_arPlayTimes[i] - min*60;
154 }
File Parser.
Definition: laFileParser.h:41