1 /*
2 * Holotz's Castle
3 * Copyright (C) 2004 Juan Carlos Seijo P�rez
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * 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., 59
17 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Juan Carlos Seijo P�rez
20 * jacob@mainreactor.net
21 */
22
23 /** Level timer for holotz's castle.
24 * @file HCTimer.cpp
25 * @author Juan Carlos Seijo P�rez
26 * @date 04/10/2004
27 * @version 0.0.1 - 4/10/2004 - First version.
28 */
29
30 #include <HCTimer.h>
31
Build(u16 t,u8 r,u8 g,u8 b)32 bool HCTimer::Build(u16 t, u8 r, u8 g, u8 b)
33 {
34 char str[32];
35 sprintf(str, "%d", t);
36
37 SDL_Color c = {r, g, b, 0};
38
39 Destroy();
40 if ((img = font->RenderTextBlended(str, c)))
41 {
42 return true;
43 }
44
45 return false;
46 }
47
Init(u16 t,JFont * f)48 bool HCTimer::Init(u16 t, JFont *f)
49 {
50 if (!f)
51 {
52 fprintf(stderr, "HCTimer: No hay fuente\n");
53 return false;
54 }
55
56 cycleTime = 1000;
57 maxTime = t * 1000;
58 font = f;
59
60 return Build(t, 255, 255, 255);
61 }
62
Draw()63 void HCTimer::Draw()
64 {
65 img->Draw((s32)pos.x, (s32)pos.y);
66 }
67
Update()68 s32 HCTimer::Update()
69 {
70 s32 t = TimeRemaining();
71
72 if (Changed() > 0)
73 {
74 // Turns red as time goes by
75 u8 r = 255;
76 u8 g = 255 * t/maxTime;
77 u8 b = 255 * t/maxTime;
78
79 if (t < 0)
80 {
81 t = 0;
82 Pause();
83 }
84 else
85 {
86 t /= 1000;
87 }
88
89 Build(t, r, g, b);
90 }
91
92 return t;
93 }
94