Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[oweals/minetest.git] / src / content_cso.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "content_cso.h"
21 #include <IBillboardSceneNode.h>
22 #include "tile.h"
23 #include "environment.h"
24 #include "gamedef.h"
25 #include "log.h"
26
27 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
28                 float txs, float tys, int col, int row)
29 {
30         video::SMaterial& material = bill->getMaterial(0);
31         core::matrix4& matrix = material.getTextureMatrix(0);
32         matrix.setTextureTranslate(txs*col, tys*row);
33         matrix.setTextureScale(txs, tys);
34 }
35
36 class SmokePuffCSO: public ClientSimpleObject
37 {
38         float m_age;
39         scene::IBillboardSceneNode *m_spritenode;
40 public:
41         SmokePuffCSO(scene::ISceneManager *smgr,
42                         ClientEnvironment *env, v3f pos, v2f size):
43                 m_age(0),
44                 m_spritenode(NULL)
45         {
46                 infostream<<"SmokePuffCSO: constructing"<<std::endl;
47                 m_spritenode = smgr->addBillboardSceneNode(
48                                 NULL, v2f(1,1), pos, -1);
49                 m_spritenode->setMaterialTexture(0,
50                                 env->getGameDef()->tsrc()->getTextureRaw("smoke_puff.png"));
51                 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
52                 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
53                 //m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
54                 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
55                 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
56                 m_spritenode->setColor(video::SColor(255,0,0,0));
57                 m_spritenode->setVisible(true);
58                 m_spritenode->setSize(size);
59                 /* Update brightness */
60                 u8 light = 64;
61                 try{
62                         MapNode n = env->getMap().getNode(floatToInt(pos, BS));
63                         light = decode_light(n.getLightBlend(env->getDayNightRatio(),
64                                         env->getGameDef()->ndef()));
65                 }
66                 catch(InvalidPositionException &e){}
67                 video::SColor color(255,light,light,light);
68                 m_spritenode->setColor(color);
69         }
70         virtual ~SmokePuffCSO()
71         {
72                 infostream<<"SmokePuffCSO: destructing"<<std::endl;
73                 m_spritenode->remove();
74         }
75         void step(float dtime)
76         {
77                 m_age += dtime;
78                 if(m_age > 1.0){
79                         m_to_be_removed = true;
80                 }
81         }
82 };
83
84 ClientSimpleObject* createSmokePuff(scene::ISceneManager *smgr,
85                 ClientEnvironment *env, v3f pos, v2f size)
86 {
87         return new SmokePuffCSO(smgr, env, pos, size);
88 }
89