File: frost.cpp

package info (click to toggle)
libtcod 1.7.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,844 kB
  • sloc: ansic: 23,322; cpp: 20,694; python: 4,410; makefile: 182; sh: 67
file content (212 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <math.h>
#include <stdio.h>

#include "libtcod.hpp"
#define _SDL_main_h
#include "libtcod_int.h"


TCODColor frostCol[256];
int keys[4]={0,60,200,255};
TCODColor keyCols[4]={TCODColor::black,TCODColor::darkerBlue,TCODColor::lighterBlue,TCODColor::lightestBlue};

#define GROW 5000.0f
#define ANGLE_DELAY 0.2f 
#define FROST_LEVEL 0.8f
#define SMOOTH 0.3f
#define PIX_PER_FRAME 6
#define RANGE 10

struct Frost;

class FrostManager {
public :
	FrostManager(int w, int h);
	~FrostManager();
	void addFrost(int x, int y);
	void update(float elapsed);
	void render();
	void clear();
	inline float getValue(int cx, int cy) {
		TCOD_IFNOT(cx+cy*w >= 0 && cx+cy*w < w*h) return 0.0f;
		return grid[cx+cy*w];
	}
	inline void setValue(int cx, int cy,float v) {
		TCOD_IFNOT(cx+cy*w >= 0 && cx+cy*w < w*h) return;
		grid[cx+cy*w]=v;
	}	
protected :
	friend struct Frost;
	TCODList<Frost *> list;
	float *grid;
	TCODImage *img;
	int w,h;
};

struct Frost {
	int x,y,bestx,besty,rx,ry;
	int border;
	FrostManager *manager;
	float timer;
	float ra;
	float rr;
	Frost(int x, int y, FrostManager *manager);
	inline float getValue(int cx, int cy) {
		return manager->getValue(x-RANGE+cx,y-RANGE+cy);
	}
	inline void setValue(int cx, int cy,float v) {
		manager->setValue(x-RANGE+cx,y-RANGE+cy,v);
	}
	bool update(float elapsed);
	void render(TCODImage *img);
}; 


FrostManager::FrostManager(int w, int h) : w(w),h(h) {
	grid = new float[w*h];
	img=new TCODImage(w,h);
	clear();
}
FrostManager::~FrostManager() {
	delete [] grid;
	delete img;
}
void FrostManager::addFrost(int x, int y) {
	list.push(new Frost(x,y,this));
	setValue(x,y,1.0f);
}

void FrostManager::clear() {
	img->clear(TCODColor::black);
	memset(grid,0,sizeof(float)*w*h);
}

void FrostManager::update(float elapsed) {
	TCODList<Frost *>toRemove;
	for (Frost **it=list.begin();it!=list.end();it++) {
		if (!(*it)->update(elapsed)) toRemove.push(*it);
	}
	for (Frost **it=toRemove.begin();it!=toRemove.end();it++) {
		list.removeFast(*it);
	}
	toRemove.clearAndDelete();			
}

void FrostManager::render() {
	for (Frost **it=list.begin();it!=list.end();it++) {
		(*it)->render(img);
	}
	img->blit2x(TCODConsole::root,0,0);
}

Frost::Frost(int x, int y, FrostManager *manager) :x(x),y(y),manager(manager) {
	border=0;
	timer=0.0f;
}
bool Frost::update(float elapsed) {
	for (int i=PIX_PER_FRAME;i> 0; i--) {
		timer -= elapsed;
		if ( timer <= 0 ) {
			// find a new random frost direction
			ra = TCODRandom::getInstance()->getFloat(0.0f,2*3.1415926f);
			rr = TCODRandom::getInstance()->getFloat(0,2*RANGE);
			timer=ANGLE_DELAY;
			rx=(int)(RANGE + rr*cosf(ra));
			ry=(int)(RANGE + rr*sinf(ra));
			int minDist=100000;
			// find closest frost pixel
			for (int cx=1; cx < 2*RANGE; cx++) {
				if ( (unsigned)(x-RANGE+cx) < (unsigned)manager->w ) {
					for (int cy=1; cy < 2*RANGE; cy++) {
						if ( (unsigned)(y-RANGE+cy) < (unsigned)manager->h ) {
							float f=getValue(cx,cy);
							if ( f > FROST_LEVEL ) {
								int dist=(cx-rx)*(cx-rx)+(cy-ry)*(cy-ry);
								if ( dist < minDist ) {
									minDist=dist;
									bestx=cx;
									besty=cy;
								}
							}
						}
					}
				}
			}
		}
		// smoothing
		for (int cx=0; cx < 2*RANGE+1; cx++) {
			if ( x-RANGE+cx < manager->w-1 && x-RANGE+cx > 0 ) {
				for (int cy=0; cy < 2*RANGE+1; cy++) {
					if ( y-RANGE+cy < manager->h-1 && y-RANGE+cy > 0 ) {
						if ( getValue(cx,cy)< 1.0f) {
							float f=getValue(cx,cy);
							float oldf=f;
							f=MAX(f,getValue(cx+1,cy));
							f=MAX(f,getValue(cx-1,cy));
							f=MAX(f,getValue(cx,cy+1));
							f=MAX(f,getValue(cx,cy-1));
							setValue(cx,cy,oldf+(f-oldf)*SMOOTH*elapsed);
						}
					}
				}
			}
		}
		int curx=bestx;
		int cury=besty;
		// frosting
		TCODLine::init(curx,cury,rx,ry);
		TCODLine::step(&curx,&cury);
		if ((unsigned)(x-RANGE+curx) < (unsigned)manager->w && (unsigned)(y-RANGE+cury) < (unsigned)manager->h) { 
			float f=getValue(curx,cury);
			f+=GROW*elapsed;
			f=MIN(1.0f,f);
			setValue(curx,cury,f);
			if ( f==1.0f ) {
				bestx=curx;besty=cury;
				if ( bestx==rx && besty==ry ) timer=0.0f;
				timer=0.0f;
				if ( curx == 0 || curx == 2*RANGE || cury ==0 || cury == 2*RANGE ) {
					border++;
					if ( border==20 ) return false;
				}
			}
		} else timer=0.0f;
	}
	return true;
}
void Frost::render(TCODImage *img) {
	int w,h;
	img->getSize(&w,&h);
	for (int cx=x-RANGE; cx <= x+RANGE;cx++) {
		if ( (unsigned)cx < (unsigned)w ) { 
			for (int cy=y-RANGE; cy <= y+RANGE;cy++) {
				if ( (unsigned)cy < (unsigned)h ) {
					float f=getValue(cx - (x-RANGE), cy-(y-RANGE));
					int idx=(int)(f*255);
					idx=MIN(255,idx);
					img->putPixel(cx,cy,frostCol[idx]);
				}
			}
		}
	}
}

int main(int argc, char** argv) {
    TCODConsole::initRoot(80, 50, "frost test", false);
	TCOD_mouse_t mouse;
	TCOD_key_t key;
	TCODSystem::setFps(25);
	TCODColor::genMap(frostCol,sizeof(keys)/sizeof(int),keyCols,keys);
	FrostManager frostManager(160,100);
	while (! TCODConsole::isWindowClosed()) {
		frostManager.render();
		TCODConsole::flush();
		TCODSystem::checkForEvent(TCOD_EVENT_KEY_RELEASE|TCOD_EVENT_MOUSE,&key,&mouse);
		if ( key.vk == TCODK_BACKSPACE ) frostManager.clear();
		if ( mouse.lbutton ) {
			frostManager.addFrost(mouse.cx*2,mouse.cy*2);
		}
		frostManager.update(TCODSystem::getLastFrameLength());
	}
	return 0;
}