code cleanup:
[blender.git] / source / gameengine / VideoTexture / FilterBase.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Copyright (c) 2007 The Zdeno Ash Miklas
19  *
20  * This source file is part of VideoTexture library
21  *
22  * Contributor(s):
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file gameengine/VideoTexture/FilterBase.cpp
28  *  \ingroup bgevideotex
29  */
30
31
32 #include "FilterBase.h"
33
34 #include "PyObjectPlus.h"
35 #include <structmember.h>
36
37
38 // FilterBase class implementation
39
40 // constructor
41 FilterBase::FilterBase (void) : m_previous(NULL) {}
42
43
44 // destructor
45 FilterBase::~FilterBase (void)
46 {
47         // release Python objects, if not released yet
48         release();
49 }
50
51
52 // release python objects
53 void FilterBase::release (void)
54 {
55         // release previous filter object
56         setPrevious(NULL);
57 }
58
59
60 // set new previous filter
61 void FilterBase::setPrevious (PyFilter * filt, bool useRefCnt)
62 {
63         // if reference counting has to be used
64         if (useRefCnt)
65         {
66                 // reference new filter
67                 if (filt != NULL) Py_INCREF(filt);
68                 // release old filter
69                 Py_XDECREF(m_previous);
70         }
71         // set new previous filter
72         m_previous = filt;
73 }
74
75
76 // find first filter
77 FilterBase * FilterBase::findFirst (void)
78 {
79         // find first filter in chain
80         FilterBase * frst;
81         for (frst = this; frst->m_previous != NULL; frst = frst->m_previous->m_filter) {};
82         // set first filter
83         return frst;
84 }
85
86
87
88 // list offilter types
89 PyTypeList pyFilterTypes;
90
91
92
93 // functions for python interface
94
95
96 // object allocation
97 PyObject *Filter_allocNew (PyTypeObject *type, PyObject *args, PyObject *kwds)
98 {
99         // allocate object
100         PyFilter *self = reinterpret_cast<PyFilter*>(type->tp_alloc(type, 0));
101         // initialize object structure
102         self->m_filter = NULL;
103         // return allocated object
104         return reinterpret_cast<PyObject*>(self);
105 }
106
107 // object deallocation
108 void Filter_dealloc(PyFilter *self)
109 {
110         // release object attributes
111         if (self->m_filter != NULL)
112         {
113                 self->m_filter->release();
114                 delete self->m_filter;
115                 self->m_filter = NULL;
116         }
117 }
118
119
120 // get previous pixel filter object
121 PyObject *Filter_getPrevious (PyFilter *self, void *closure)
122 {
123         // if filter object is available
124         if (self->m_filter != NULL)
125         {
126                 // pixel filter object
127                 PyObject * filt = reinterpret_cast<PyObject*>(self->m_filter->getPrevious());
128                 // if filter is present
129                 if (filt != NULL)
130                 {
131                         // return it
132                         Py_INCREF(filt);
133                         return filt;
134                 }
135         }
136         // otherwise return none
137         Py_RETURN_NONE;
138 }
139
140
141 // set previous pixel filter object
142 int Filter_setPrevious(PyFilter *self, PyObject *value, void *closure)
143 {
144         // if filter object is available
145         if (self->m_filter != NULL)
146         {
147                 // check new value
148                 if (value == NULL || !pyFilterTypes.in(Py_TYPE(value)))
149                 {
150                         // report value error
151                         PyErr_SetString(PyExc_TypeError, "Invalid type of value");
152                         return -1;
153                 }
154                 // set new value
155                 self->m_filter->setPrevious(reinterpret_cast<PyFilter*>(value));
156         }
157         // return success
158         return 0;
159 }