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

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

game: convert a few integer pairs to vec2i variables.

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