source: golgotha/src/golg/objs/cloud.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 4.0 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "object_definer.hh"
10#include "math/random.hh"
11#include "map_vert.hh"
12#include "map_man.hh"
13#include "tick_count.hh"
14#include "objs/model_draw.hh"
15#include "loaders/load.hh"
16#include "image/image.hh"
17#include "map_vert.hh"
18#include "objs/model_id.hh"
19#include "time/profile.hh"
20
21static i4_profile_class pf_cloud_shadow("Cloud Shadow");
22
23i4_image_class *cloud_im=0;
24int cloud_ref=0;
25
26g1_model_ref cloud_model("cloud2");
27
28inline float interp(float c1, float c2, float r)
29{
30  return (c2-c1)*r+c1;
31}
32
33static float xfract(float x)
34{
35  return x-(int)x;
36}
37
38class g1_cloud_class :public g1_object_class
39{
40public:
41  int ticks_till_move;
42  static int cloud_count;
43
44
45  float x_offset;
46  float y_offset;
47
48  float cloud_x() { return x*2-g1_map_width/2.0; }
49  float cloud_y() { return y*2; }
50
51
52  g1_cloud_class(g1_object_type id, g1_loader_class *fp)
53    : g1_object_class(id,fp)
54  {   
55
56    ticks_till_move=0;
57    draw_params.setup("lightbulb");
58    draw_params.flags|=g1_model_draw_parameters::NO_LIGHTING;
59   
60    if (!cloud_ref)
61    {
62      cloud_im=i4_load_image("bitmaps/cloud.tga");
63      if (!cloud_im) i4_error("couldn't load cload.tga");
64    }
65
66
67    y_offset=x_offset=0;
68
69    cloud_ref++;
70    draw_params.setup(cloud_model.id());
71  }
72
73  ~g1_cloud_class()
74  {
75    cloud_ref--;
76    if (!cloud_ref)
77    {
78      delete cloud_im;
79      cloud_im=0;
80    }
81  }
82
83  void apply_shadow()
84  {
85    pf_cloud_shadow.start();   
86    int iw=cloud_im->width(), ih=cloud_im->height();
87    int mw=g1_map_width, mh=g1_map_height;
88    float rx=xfract(x_offset), ry=xfract(y_offset);
89   
90    g1_map_vertex_class *v=g1_verts;
91
92
93
94    int dy=(int)(y_offset);
95    w32 *d=(w32 *)cloud_im->data;
96
97
98    for (int vy=0; vy<=mh; vy++)
99    {
100
101      int dx=(int)x_offset;
102      w32 *d=(w32 *)cloud_im->data + dx + dy*65;
103
104      for (int vx=0; vx<=mw; vx++, v++)
105      {
106        float x1=interp(d[0]&0xff, d[1]&0xff, rx);
107        float x2=interp(d[iw]&0xff, d[iw+1]&0xff, rx);
108        int yc=(int)interp(x1,x2, ry);
109
110        v->shadow_subtract=yc;
111        v->light_sum|=0x80000000;
112
113        if (dx==iw-1)
114        {
115          dx=0;
116          d-=iw-1;
117        }
118        else
119        {
120          dx++;
121          d++;
122        }
123      }
124
125      dy++;
126      if (dy==ih-1)
127        dy=0;
128
129    }
130
131    pf_cloud_shadow.stop();
132  }
133
134  i4_bool occupy_location()
135  {
136    if (cloud_count)
137      return i4_F;
138
139    if (g1_object_class::occupy_location_center())
140    {
141      cloud_count++;
142      apply_shadow();
143      return i4_T;
144    }
145    else return i4_F;
146  }
147
148
149
150  void unoccupy_location()
151  {
152    cloud_count--;
153
154    g1_map_vertex_class *v=g1_verts;
155    int mw=g1_map_width, mh=g1_map_height;
156
157    for (int vy=0; vy<mh; vy++)
158    {
159      for (int vx=0; vx<mw; vx++, v++)
160      {
161        v->shadow_subtract=0;
162        v->light_sum|=0x80000000;
163      }
164    }
165
166    g1_object_class::unoccupy_location();
167  }
168
169
170  void think()
171  {
172    if (!ticks_till_move)
173    {
174      ticks_till_move=0;
175      g1_map_vertex_class *v=g1_verts;
176
177
178      y_offset+=1.0/8.0;
179      if (y_offset>=64)
180        y_offset-=64;
181
182      apply_shadow();
183    }
184    else ticks_till_move--;
185
186    request_think();
187  }
188
189  void draw(g1_draw_context_class *context)
190  { 
191    // don't do anything because this is done by draw_sky
192  }
193
194
195
196};
197
198
199int g1_cloud_class::cloud_count=0;
200
201
202g1_object_definer<g1_cloud_class>
203g1_cloud_def("cloud_shadow", g1_object_definition_class::EDITOR_SELECTABLE);
204
Note: See TracBrowser for help on using the repository browser.