fix for warnings from Sparse static source code checker, mostly BKE/BLI and python...
[blender.git] / source / blender / blenkernel / intern / world.c
1
2 /*  world.c
3  * 
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_world_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_texture_types.h"
40
41 #include "BKE_world.h"
42 #include "BKE_library.h"
43 #include "BKE_animsys.h"
44 #include "BKE_global.h"
45 #include "BKE_main.h"
46 #include "BKE_icons.h"
47
48 void free_world(World *wrld)
49 {
50         MTex *mtex;
51         int a;
52         
53         for(a=0; a<MAX_MTEX; a++) {
54                 mtex= wrld->mtex[a];
55                 if(mtex && mtex->tex) mtex->tex->id.us--;
56                 if(mtex) MEM_freeN(mtex);
57         }
58         BKE_previewimg_free(&wrld->preview);
59
60         BKE_free_animdata((ID *)wrld);
61
62         BKE_icon_delete((struct ID*)wrld);
63         wrld->id.icon_id = 0;
64 }
65
66
67 World *add_world(const char *name)
68 {
69         Main *bmain= G.main;
70         World *wrld;
71
72         wrld= alloc_libblock(&bmain->world, ID_WO, name);
73         
74         wrld->horr= 0.05f;
75         wrld->horg= 0.05f;
76         wrld->horb= 0.05f;
77         wrld->zenr= 0.01f;
78         wrld->zeng= 0.01f;
79         wrld->zenb= 0.01f;
80         wrld->skytype= 0;
81         wrld->stardist= 15.0f;
82         wrld->starsize= 2.0f;
83         
84         wrld->exp= 0.0f;
85         wrld->exposure=wrld->range= 1.0f;
86
87         wrld->aodist= 10.0f;
88         wrld->aosamp= 5;
89         wrld->aoenergy= 1.0f;
90         wrld->ao_env_energy= 1.0f;
91         wrld->ao_indirect_energy= 1.0f;
92         wrld->ao_indirect_bounces= 1;
93         wrld->aobias= 0.05f;
94         wrld->ao_samp_method = WO_AOSAMP_HAMMERSLEY;    
95         wrld->ao_approx_error= 0.25f;
96         
97         wrld->preview = NULL;
98         wrld->miststa = 5.0f;
99         wrld->mistdist = 25.0f;
100
101         return wrld;
102 }
103
104 World *copy_world(World *wrld)
105 {
106         World *wrldn;
107         int a;
108         
109         wrldn= copy_libblock(wrld);
110         
111         for(a=0; a<MAX_MTEX; a++) {
112                 if(wrld->mtex[a]) {
113                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
114                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
115                         id_us_plus((ID *)wrldn->mtex[a]->tex);
116                 }
117         }
118         
119         if (wrld->preview) wrldn->preview = BKE_previewimg_copy(wrld->preview);
120
121 #if 0 // XXX old animation system
122         id_us_plus((ID *)wrldn->ipo);
123 #endif // XXX old animation system
124         
125         return wrldn;
126 }
127
128 void make_local_world(World *wrld)
129 {
130         Main *bmain= G.main;
131         Scene *sce;
132         World *wrldn;
133         int local=0, lib=0;
134
135         /* - only lib users: do nothing
136                 * - only local users: set flag
137                 * - mixed: make copy
138                 */
139         
140         if(wrld->id.lib==NULL) return;
141         if(wrld->id.us==1) {
142                 wrld->id.lib= NULL;
143                 wrld->id.flag= LIB_LOCAL;
144                 new_id(NULL, (ID *)wrld, NULL);
145                 return;
146         }
147         
148         sce= bmain->scene.first;
149         while(sce) {
150                 if(sce->world==wrld) {
151                         if(sce->id.lib) lib= 1;
152                         else local= 1;
153                 }
154                 sce= sce->id.next;
155         }
156         
157         if(local && lib==0) {
158                 wrld->id.lib= 0;
159                 wrld->id.flag= LIB_LOCAL;
160                 new_id(NULL, (ID *)wrld, NULL);
161         }
162         else if(local && lib) {
163                 wrldn= copy_world(wrld);
164                 wrldn->id.us= 0;
165                 
166                 sce= bmain->scene.first;
167                 while(sce) {
168                         if(sce->world==wrld) {
169                                 if(sce->id.lib==NULL) {
170                                         sce->world= wrldn;
171                                         wrldn->id.us++;
172                                         wrld->id.us--;
173                                 }
174                         }
175                         sce= sce->id.next;
176                 }
177         }
178 }