source: abuse/tags/pd/abuse/src/statbar.c

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