FS2_Open
Open source remastering of the Freespace 2 engine
fishtank.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #include "anim/animplay.h"
13 #include "anim/packunpack.h"
14 #include "freespace2/freespace.h"
16 #include "menuui/fishtank.h"
17 
18 
19 
20 // fish
21 typedef struct fish {
22  float x, y; // x and y coords
23  float x_speed, y_speed; // x and y speed
24  int left; // left or right
25  anim_instance *a; // tha animation
26  int onscreen;
27  int swimming; // whee
28 } fish;
29 #define MAX_FISH 12
31 
32 // fish anim name
33 #define FISH_LEFT_ANIM_NAME "f_left.ani"
34 #define FISH_RIGHT_ANIM_NAME "f_right.ani"
35 
36 #define FISH_ANIM_WIDTH 100
37 #define FISH_ANIM_HEIGHT 30
38 
41 
42 int Fish_inited = 0;
43 
45 {
46  fish *f;
47  int idx;
48 
49  if(!Fish_inited){
50  return;
51  }
52 
53  // bogus anims
54  if((Fish_left_anim == NULL) || (Fish_right_anim == NULL)){
55  return;
56  }
57 
58  // find a free fish
59  f = NULL;
60  for(idx=0; idx<MAX_FISH; idx++){
61  if(!Fish[idx].swimming){
62  f = &Fish[idx];
63  }
64  }
65 
66  // no fish left
67  if(f == NULL){
68  return;
69  }
70 
71  // left or right
72  f->left = frand_range(0.0f, 1.0f) < 0.5f ? 0 : 1;
73 
74  // start location
75  if(f->left){
76  f->x = gr_screen.max_w_unscaled_zoomed + frand_range(0.0f, 50.0f);
77  } else {
78  f->x = frand_range(0.0f, -50.0f) - FISH_ANIM_WIDTH;
79  }
80  f->y = frand_range(-40.0f, (float)gr_screen.max_h_unscaled_zoomed + 40.0f);
81 
82  // speed
83  if(f->left){
84  f->x_speed = frand_range(-1.0f, -15.0f);
85  } else {
86  f->x_speed = frand_range(1.0f, 15.0f);
87  }
88  f->y_speed = frand_range(0.0f, 1.0f) < 0.5f ? frand_range(1.0f, 4.0f) : frand_range(-1.0f, -4.0f);
89 
90  // all fish start out offscreen
91  f->onscreen = 0;
92 
93  // he's swimming
94  f->swimming = 1;
95 
96  // anim instance
97  anim_play_struct aps;
98 
99  if(f->left){
100  anim_play_init(&aps, Fish_left_anim, (int)f->x, (int)f->y);
101  f->a = anim_play(&aps);
102 
103  // doh. cancel him
104  if(f->a == NULL){
105  f->swimming = 0;
106  } else {
108  f->a->looped = 1;
109  f->a->framerate_independent = 1;
110  }
111  } else {
112  anim_play_init(&aps, Fish_right_anim, (int)f->x, (int)f->y);
113  f->a = anim_play(&aps);
114 
115  // doh. cancel him
116  if(f->a == NULL){
117  f->swimming = 0;
118  } else {
120  f->a->looped = 1;
121  f->a->framerate_independent = 1;
122  }
123  }
124 }
125 
127 {
128  // bogus
129  if(f == NULL){
130  return;
131  }
132 
133  // release his render instance
134  if(f->a != NULL){
136  f->a = NULL;
137  }
138 
139  // no longer swimming
140  f->swimming = 0;
141 }
142 
144 {
145  int idx;
146 
147  if(Fish_inited){
148  return;
149  }
150 
151  // try and load the fish anim
152  Fish_left_anim = anim_load(FISH_LEFT_ANIM_NAME);
153  if(Fish_left_anim == NULL){
154  return;
155  }
156  Fish_right_anim = anim_load(FISH_RIGHT_ANIM_NAME);
157  if(Fish_right_anim == NULL){
158  return;
159  }
160 
161  // no anim instances
162  for(idx=0; idx<MAX_FISH; idx++){
163  Fish[idx].a = NULL;
164  Fish[idx].swimming = 0;
165  }
166 
167  Fish_inited = 1;
168 
169  // generate a random # of fish
170  int count = (int)frand_range(1.0f, (float)(MAX_FISH - 1));
171  for(idx=0; idx<count; idx++){
172  fish_generate();
173  }
174 }
175 
177 {
178  int idx;
179 
180  if(!Fish_inited){
181  return;
182  }
183 
184  // release stuff
185  for(idx=0; idx<MAX_FISH; idx++){
186  if(Fish[idx].a != NULL){
187  anim_release_render_instance(Fish[idx].a);
188  Fish[idx].a = NULL;
189  }
190  Fish[idx].swimming = 0;
191  }
192  if(Fish_left_anim != NULL){
193  anim_free(Fish_left_anim);
194  Fish_left_anim = NULL;
195  }
196  if(Fish_right_anim != NULL){
197  anim_free(Fish_right_anim);
198  Fish_right_anim = NULL;
199  }
200 
201  Fish_inited = 0;
202 }
203 
205 {
206  int idx, onscreen;
207  fish *f;
208 
209  if(!Fish_inited){
210  return;
211  }
212 
213  // process all fish
214  for(idx=0; idx<MAX_FISH; idx++){
215  f = &Fish[idx];
216 
217  // not swimming?
218  if(!f->swimming){
219  continue;
220  }
221 
222  // move him along
223  f->x += f->x_speed * flFrametime;
224  f->y += f->y_speed * flFrametime;
225 
226  // is he currently onscreen ?
227  onscreen = 0;
228  if( (f->x < (float)gr_screen.max_w_unscaled_zoomed) && ((f->x + FISH_ANIM_WIDTH) >= 0.0f) &&
229  (f->y < (float)gr_screen.max_h_unscaled_zoomed) && ((f->y + FISH_ANIM_HEIGHT) >= 0.0f) ){
230  onscreen = 1;
231  }
232 
233  // if he was onscreen before, but is no longer, flush him and make a new fish
234  if(f->onscreen && !onscreen){
235  fish_flush(f);
236 
237  fish_generate();
238  continue;
239  }
240 
241  // otherwise just mark his current status
242  f->onscreen = onscreen;
243 
244  // render
245  if(f->onscreen){
246  // set coords
247  f->a->x = (int)f->x;
248  f->a->y = (int)f->y;
249 
251  }
252  }
253 }
254 
int framerate_independent
Definition: packunpack.h:85
float frand_range(float min, float max)
Return a floating point number in the range min..max.
Definition: floating.cpp:50
float flFrametime
Definition: fredstubs.cpp:22
int left
Definition: fishtank.cpp:24
fish Fish[MAX_FISH]
Definition: fishtank.cpp:30
GLclampf f
Definition: Glext.h:7097
anim * Fish_left_anim
Definition: fishtank.cpp:39
struct fish fish
void fishtank_process()
Definition: fishtank.cpp:204
float x_speed
Definition: fishtank.cpp:23
typedef int(SCP_EXT_CALLCONV *SCPDLL_PFVERSION)(SCPDLL_Version *)
#define FISH_ANIM_HEIGHT
Definition: fishtank.cpp:37
GLboolean GLboolean GLboolean GLboolean a
Definition: Glext.h:5781
void anim_release_render_instance(anim_instance *instance)
Free a particular animation instance that is on the anim_render_list. Do not call this function to fr...
Definition: animplay.cpp:538
int anim_free(anim *ptr)
Free an animation that was loaded with anim_load().
Definition: animplay.cpp:813
int swimming
Definition: fishtank.cpp:27
float y_speed
Definition: fishtank.cpp:23
int idx
Definition: multiui.cpp:761
int Fish_inited
Definition: fishtank.cpp:42
anim_instance * anim_play(anim_play_struct *aps)
Will add an anim instance to the anim_render_list. This will cause the anim to be played at the x...
Definition: animplay.cpp:143
void fishtank_start()
Definition: fishtank.cpp:143
int max_w_unscaled_zoomed
Definition: 2d.h:362
anim * Fish_right_anim
Definition: fishtank.cpp:40
#define FISH_LEFT_ANIM_NAME
Definition: fishtank.cpp:33
#define MAX_FISH
Definition: fishtank.cpp:29
void anim_play_init(anim_play_struct *aps, anim *a_info, int x, int y, int base_w, int base_h)
Setup an anim_play_struct for passing into anim_play().
Definition: animplay.cpp:117
void fishtank_stop()
Definition: fishtank.cpp:176
#define FISH_RIGHT_ANIM_NAME
Definition: fishtank.cpp:34
float x
Definition: fishtank.cpp:22
screen gr_screen
Definition: 2d.cpp:46
anim_instance * a
Definition: fishtank.cpp:25
GLint GLsizei count
Definition: Gl.h:1491
void fish_flush(fish *f)
Definition: fishtank.cpp:126
int onscreen
Definition: fishtank.cpp:26
void fish_generate()
Definition: fishtank.cpp:44
void anim_render_one(int screen_id, anim_instance *ani, float frametime)
Display the frames for the passed animation.
Definition: animplay.cpp:93
int max_h_unscaled_zoomed
Definition: 2d.h:362
anim * anim_load(char *real_filename, int cf_dir_type, int file_mapped)
Load an animation. This stores the compressed data, which instances of the animation can reference...
Definition: animplay.cpp:687
#define FISH_ANIM_WIDTH
Definition: fishtank.cpp:36
float y
Definition: fishtank.cpp:22