source: abuse/branches/lol/src/statbar.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

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