source: abuse/trunk/src/statbar.cpp @ 494

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

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