source: abuse/tags/pd/macabuse/src/statbar.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1#include "sbar.hpp"
2#include "view.hpp"
3#include "lisp.hpp"
4#include "cache.hpp"
5#include "demo.hpp"
6#include "chars.hpp"
7#include "objects.hpp"
8#include "game.hpp"
9#include "clisp.hpp"
10#include "dprint.hpp"
11
12status_bar sbar;
13
14status_bar::status_bar()
15{
16  v=NULL;
17  need_rf=1;
18  changed_cursor=0;
19  icon_in_selection=-1;  // the weapon the mouse cursor is on top of, -1 if none
20  currently_selected_weapon=-1;
21}
22
23// defined in dev.c
24void scale_put_trans(image *im, image *screen, int x, int y, short new_width, short new_height);
25void scale_put(image *im, image *screen, int x, int y, short new_width, short new_height);
26extern image *small_render;
27
28
29void status_bar::load()
30{
31  char sbname[100];
32  char iname[20];
33  void *l_name=make_find_symbol("sbar_file");
34  if (symbol_value(l_name)!=l_undefined)
35    strcpy(sbname,lstring_value(symbol_value(l_name)));
36  else strcpy(sbname,"art/statbar.spe");
37
38  int i;
39  for (i=0;i<TOTAL_WEAPONS;i++)
40  {
41    sprintf(iname,"bweap%04d.pcx",i+1);
42    bweap[i]=cash.reg(sbname,iname,SPEC_IMAGE);
43
44    sprintf(iname,"dweap%04d.pcx",i+1);
45    dweap[i]=cash.reg(sbname,iname,SPEC_IMAGE);   
46  }
47
48  for (i=0;i<30;i++)
49  {
50    sprintf(iname,"bnum%02d",i);
51    bnum[i]=cash.reg(sbname,iname,SPEC_IMAGE);
52  }
53
54 
55  sbar=cash.reg(sbname,"sbar",SPEC_IMAGE);
56  sbar_select=cash.reg(sbname,"sbar_select",SPEC_IMAGE);
57  sbar_numpad=cash.reg(sbname,"sbar_numpad",SPEC_IMAGE);
58}
59
60void status_bar::draw_num(image *screen, int x, int y, int num, int *offset)
61{
62  if (num<0 || num>999)
63  {
64    dprintf("bad number for statbar\n");
65    return ;
66  }
67
68  image *im=cash.img(*offset);
69  int dw=small_render ? im->width()*2 : im->width();
70  int dh=small_render ? im->height()*2 : im->height();
71
72  int n=num/100;   
73  scale_put(cash.img(offset[n]),screen,x,y,dw,dh);
74  num-=n*100;
75
76  x+=dw; n=num/10;
77  scale_put(cash.img(offset[n]),screen,x,y,dw,dh);
78  num-=n*10;
79
80  x+=dw;
81  scale_put(cash.img(offset[num]),screen,x,y,dw,dh);
82}
83
84
85void status_bar::redraw(image *screen)
86{
87  if (xres>320)
88    return ;
89
90  need_rf=0;
91  if (!v) return ;
92
93  if (total_weapons)
94  {
95    if (!playing_state(the_game->state)) return ;
96
97    image *sb=cash.img(sbar);
98   
99    // status bar width & height
100    int sb_w=(small_render ? sb->width()*2 : sb->width()),
101    sb_h=(small_render ? sb->height()*2 : sb->height());
102
103    // status bar x & y position
104    int sx=xres/2-sb_w/2,sy=yres-sb_h;
105
106    // weapon x offset, and x add increment
107    int wx=small_render ? 80 : 40,wa=small_render ? 34*2 : 34;   
108
109    // weapon icon width & height
110    int ww=small_render ? cash.img(bweap[0])->width()*2 : cash.img(bweap[0])->width();
111    int wh=small_render ? cash.img(bweap[0])->height()*2 : cash.img(bweap[0])->height();
112   
113
114    // numpad y offset
115    int np_yo=small_render ? 42 : 21;
116    int np_w=small_render ? cash.img(sbar_numpad)->width()*2 : cash.img(sbar_numpad)->width();
117    int np_h=small_render ? cash.img(sbar_numpad)->height()*2 : cash.img(sbar_numpad)->height();
118
119    // selection bar width * height
120    int sel_w=small_render ? cash.img(sbar_select)->width()*2 : cash.img(sbar_select)->width();
121    int sel_h=small_render ? cash.img(sbar_select)->height()*2 : cash.img(sbar_select)->height();
122
123    int sel_off=small_render ?  8 : 4;
124    scale_put(sb,screen,sx,sy,sb_w,sb_h);
125
126    if (v->focus)
127      draw_num(screen,sx+(small_render ? 17*2 : 17),sy+(small_render ? 11*2 : 11),v->focus->hp(),bnum);
128
129    int ammo_x,ammo_y;
130    if (small_render)
131    {
132      ammo_x=sx+52*2;
133      ammo_y=sy+25*2;
134    } else {ammo_x=sx+52; ammo_y=sy+25; }
135   
136    int i,x_on=sx+wx,t=TOTAL_WEAPONS;
137    if (t>=total_weapons) t=total_weapons;
138    for (i=0;i<t;i++,x_on+=wa,ammo_x+=wa)
139    {
140      if (v->has_weapon(i))
141      {
142        if (v->current_weapon==i)
143        scale_put_trans(cash.img(bweap[i]),screen,x_on,sy,ww,wh);
144        else
145        scale_put_trans(cash.img(dweap[i]),screen,x_on,sy,ww,wh);
146       
147        scale_put_trans(cash.img(sbar_numpad),screen,x_on-2,sy+np_yo,np_w,np_h);
148
149        if (v->current_weapon==i)
150          draw_num(screen,ammo_x,ammo_y,v->weapon_total(i),bnum+20);
151        else
152          draw_num(screen,ammo_x,ammo_y,v->weapon_total(i),bnum+10);
153       
154        if (i==icon_in_selection)
155        scale_put_trans(cash.img(sbar_select),screen,x_on+sel_off,sy,sel_w,sel_h);
156      }
157    }
158  }
159}
160
161void status_bar::area(int &x1, int &y1, int &x2, int &y2)
162{
163  if (sbar<=0 || !total_weapons)
164  {
165    x2=xres;
166    y2=yres;
167    x1=x2;
168    y1=y2;
169    return ;
170  }
171
172  image *sb=cash.img(sbar); 
173
174  // status bar width & height
175  int sb_w=sb->width(),
176      sb_h=sb->height();
177
178  if (small_render) { sb_w*=2; sb_h*=2; }
179
180  x1=xres/2-sb_w/2;
181  x2=xres/2+sb_w/2;
182  y1=yres-sb_h;
183  y2=yres;
184}
185
186
187void status_bar::draw_health(image *screen,int amount)
188{
189  if (total_weapons)
190  {
191    int x1,y1,x2,y2;
192    area(x1,y1,x2,y2);
193    draw_num(screen,x1+(small_render ? 17*2 : 17),y1+(small_render ? 11*2 : 11),amount,bnum); 
194  }
195}
196
197
198void status_bar::draw_ammo(image *screen, int weapon_num, int amount, int light)
199{
200  if (total_weapons)
201  {
202    int x1,y1,x2,y2;
203    area(x1,y1,x2,y2);
204    draw_num(screen,
205                x1+(small_render ? 52*2+weapon_num*34*2 : 52+weapon_num*34),
206                y1+(small_render ? 25*2 : 25),amount,bnum+(light ? 20 : 10)); 
207  }
208}
209
210
211int status_bar::mouse_in_area()
212{
213  if (!v) return 0;
214  int x1,y1,x2,y2;
215  area(x1,y1,x2,y2);
216
217  int mx,my;
218  if (small_render)
219  {
220    mx=(v->pointer_x-v->cx1)*2+v->cx1;
221    my=(v->pointer_y-v->cy1)*2+v->cy1;
222  } else
223  {
224    mx=v->pointer_x;
225    my=v->pointer_y;
226  }
227
228  if (mx>=x1 && my>=y1 && mx<=x2 && my<=y2)
229    return 1;
230  else return 0;
231}
232
233
234void status_bar::draw_update()
235{
236  if (total_weapons && v)
237  {
238    if (DEFINEDP(symbol_value(l_mouse_can_switch)) && symbol_value(l_mouse_can_switch) &&
239        mouse_in_area())
240    {
241      if ((current_level->tick_counter()&4)==0)
242        eh->set_mouse_shape(cash.img(c_mouse1)->copy(),4,4);
243      else eh->set_mouse_shape(cash.img(c_mouse2)->copy(),4,4);   
244      changed_cursor=1;
245    } else if (changed_cursor)
246    {
247      if (!(dev&EDIT_MODE))
248        eh->set_mouse_shape(cash.img(c_target)->copy(),8,8);
249      else
250        eh->set_mouse_shape(cash.img(c_normal)->copy(),1,1);
251      changed_cursor=0;     
252    }
253
254    if (need_rf)
255      redraw(screen);
256  }
257}
258
259
260void status_bar::step()
261{
262  if (!v) return ;
263  if (!DEFINEDP(symbol_value(l_mouse_can_switch)) || !symbol_value(l_mouse_can_switch)) return ;
264 
265  int sb_w,sb_h;
266  if (sbar>0 && total_weapons)
267  {
268    image *sb=cash.img(sbar); 
269
270    // status bar width & height
271    sb_w=sb->width();
272    sb_h=sb->height();
273  }
274
275  // see if the mouse is in the sbar region (demo_x already corrected for small_render)
276  int sx1,sy1,sx2,sy2;
277  area(sx1,sy1,sx2,sy2);
278
279  int view_y2=small_render ? (v->cy2-v->cy1+1)*2+v->cy1 : v->cy2;
280  if (sy1<view_y2)     // tell view to shrink if it is overlapping the status bar
281  {
282    /*    v->suggest.send_view=1;
283    v->suggest.cx1=v->cx1;
284    v->suggest.cy1=v->cy1;
285    v->suggest.cx2=v->cx2;
286    v->suggest.cy2=small_render ? (sy1-v->cy1-2)/2+v->cy1 : sy1-2;     */
287  }
288
289  if (sbar<=0 || !total_weapons) return ;
290
291
292  int mx=small_render ? (last_demo_mx-v->cx1)*2+v->cx1 : last_demo_mx;
293  int my=small_render ? (last_demo_my-v->cy1)*2+v->cy1 : last_demo_my;
294
295
296  if (mx>sx1 && my>sy1 && mx<sx2 && my<sy2)
297  {
298
299    int new_target;
300
301    mx-=sx1;   
302    if (small_render) mx/=2;
303
304
305    mx-=47;
306    if (mx<0) new_target=0;
307    else
308    {
309      new_target=mx/33;
310      if (new_target>=TOTAL_WEAPONS)
311        new_target=TOTAL_WEAPONS-1;
312      if (new_target>=total_weapons)
313        new_target=total_weapons-1;
314    }     
315
316    if (v->has_weapon(new_target) && new_target!=icon_in_selection)
317    {
318      icon_in_selection=new_target;
319      need_refresh();
320    }
321   
322    if (last_demo_mbut==2 && icon_in_selection!=v->current_weapon &&
323        icon_in_selection!=-1) // the user requested a weapon change
324    {
325      v->suggest.send_weapon_change=1;
326      v->suggest.new_weapon=icon_in_selection;
327    }
328
329  } else
330  {
331    if (icon_in_selection!=-1)
332    {
333      icon_in_selection=-1;
334      need_refresh();
335    }
336  }
337
338  // see if a new weapon has been selected other than the one
339  // we think is selected, if so redraw the status bar
340  if (currently_selected_weapon!=v->current_weapon)
341  {
342    currently_selected_weapon=v->current_weapon;
343    need_refresh();
344  }
345 
346 
347}
348
349
350
351
352
Note: See TracBrowser for help on using the repository browser.