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

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

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 8.4 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[555]11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
[56]14
[512]15#include "common.h"
16
[481]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"
[2]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];
[492]48  void *l_name = LSymbol::FindOrCreate("sbar_file");
[2]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;
[494]54  for (i=0; i<TOTAL_WEAPONS; i++)
[2]55  {
56    sprintf(iname,"bweap%04d.pcx",i+1);
[123]57    bweap[i]=cache.reg(sbname,iname,SPEC_IMAGE);
[2]58
59    sprintf(iname,"dweap%04d.pcx",i+1);
[124]60    dweap[i]=cache.reg(sbname,iname,SPEC_IMAGE);
[2]61  }
62
[494]63  for (i=0; i<30; i++)
[2]64  {
65    sprintf(iname,"bnum%02d",i);
[123]66    bnum[i]=cache.reg(sbname,iname,SPEC_IMAGE);
[2]67  }
68
[124]69
[123]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);
[2]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");
[124]80    return ;
[2]81  }
82
[123]83  image *im=cache.img(*offset);
[512]84  int dw=small_render ? im->Size().x*2 : im->Size().x;
85  int dh=small_render ? im->Size().y*2 : im->Size().y;
[2]86
[124]87  int n=num/100;
[123]88  scale_put(cache.img(offset[n]),screen,x,y,dw,dh);
[2]89  num-=n*100;
90
91  x+=dw; n=num/10;
[123]92  scale_put(cache.img(offset[n]),screen,x,y,dw,dh);
[2]93  num-=n*10;
94
95  x+=dw;
[123]96  scale_put(cache.img(offset[num]),screen,x,y,dw,dh);
[2]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
[123]108    image *sb=cache.img(sbar);
[124]109
[2]110    // status bar width & height
[512]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);
[2]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
[124]118    int wx=small_render ? 80 : 40,wa=small_render ? 34*2 : 34;
[2]119
120    // weapon icon width & height
[512]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;
[2]123
[124]124
[2]125    // numpad y offset
126    int np_yo=small_render ? 42 : 21;
[512]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;
[2]129
130    // selection bar width * height
[512]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;
[2]133
[124]134    int sel_off=small_render ?  8 : 4;
[2]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;
[494]145    } else { ammo_x=sx+52; ammo_y=sy+25; }
[124]146
[2]147    int i,x_on=sx+wx,t=TOTAL_WEAPONS;
148    if (t>=total_weapons) t=total_weapons;
[494]149    for (i=0; i<t; i++,x_on+=wa,ammo_x+=wa)
[2]150    {
151      if (v->has_weapon(i))
152      {
[124]153    if (v->current_weapon==i)
[123]154        scale_put_trans(cache.img(bweap[i]),screen,x_on,sy,ww,wh);
[124]155    else
[123]156        scale_put_trans(cache.img(dweap[i]),screen,x_on,sy,ww,wh);
[494]157
[124]158    scale_put_trans(cache.img(sbar_numpad),screen,x_on-2,sy+np_yo,np_w,np_h);
[2]159
[124]160    if (v->current_weapon==i)
[2]161          draw_num(screen,ammo_x,ammo_y,v->weapon_total(i),bnum+20);
[124]162    else
[2]163          draw_num(screen,ammo_x,ammo_y,v->weapon_total(i),bnum+10);
[124]164
165    if (i==icon_in_selection)
[123]166        scale_put_trans(cache.img(sbar_select),screen,x_on+sel_off,sy,sel_w,sel_h);
[2]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
[124]183  image *sb=cache.img(sbar);
[2]184
185  // status bar width & height
[512]186  int sb_w=sb->Size().x,
187      sb_h=sb->Size().y;
[2]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);
[124]204    draw_num(screen,x1+(small_render ? 17*2 : 17),y1+(small_render ? 11*2 : 11),amount,bnum);
[2]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,
[124]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));
[2]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) &&
[124]250    mouse_in_area())
[2]251    {
252      if ((current_level->tick_counter()&4)==0)
[123]253        wm->set_mouse_shape(cache.img(c_mouse1)->copy(),4,4);
[124]254      else wm->set_mouse_shape(cache.img(c_mouse2)->copy(),4,4);
[2]255      changed_cursor=1;
256    } else if (changed_cursor)
257    {
258      if (!(dev&EDIT_MODE))
[123]259        wm->set_mouse_shape(cache.img(c_target)->copy(),8,8);
[2]260      else
[123]261        wm->set_mouse_shape(cache.img(c_normal)->copy(),1,1);
[124]262      changed_cursor=0;
[2]263    }
264
265    if (need_rf)
266      redraw(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 ;
[124]275
[2]276  int sb_w,sb_h;
277  if (sbar>0 && total_weapons)
278  {
[124]279    image *sb=cache.img(sbar);
[2]280
281    // status bar width & height
[512]282    sb_w=sb->Size().x;
283    sb_h=sb->Size().y;
[2]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;
[124]297    v->suggest.cy2=small_render ? (sy1-v->cy1-2)/2+v->cy1 : sy1-2;
[2]298  }
299
300  if (sbar<=0 || !total_weapons) return ;
301
302
303  int mx=small_render ? (last_demo_mx-v->cx1)*2+v->cx1 : last_demo_mx;
304  int my=small_render ? (last_demo_my-v->cy1)*2+v->cy1 : last_demo_my;
305
306
307  if (mx>sx1 && my>sy1 && mx<sx2 && my<sy2)
308  {
309
310    int new_target;
311
[124]312    mx-=sx1;
[2]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)
[124]322        new_target=TOTAL_WEAPONS-1;
[2]323      if (new_target>=total_weapons)
324        new_target=total_weapons-1;
[124]325    }
[2]326
327    if (v->has_weapon(new_target) && new_target!=icon_in_selection)
328    {
329      icon_in_selection=new_target;
330      need_refresh();
331    }
[124]332
[2]333    if (last_demo_mbut==2 && icon_in_selection!=v->current_weapon &&
[124]334    icon_in_selection!=-1) // the user requested a weapon change
[2]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  }
[124]356
357
[2]358}
359
360
361
362
363
Note: See TracBrowser for help on using the repository browser.