source: abuse/trunk/src/automap.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: 5.7 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
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
[481]13#include "automap.h"
14#include "game.h"
[2]15
16automap *current_automap=0;
17
18void automap::draw()
19{
20  if (!automap_window) return ;
21  image *screen=automap_window->screen;
[124]22
[2]23  long sx,ex,sy,ey,x,y,window_xstart,window_ystart,
24                       window_xend,window_yend,centerx,centery,
25                       draw_xstart,draw_ystart,
26                       i,j;
[124]27
[2]28  x=the_game->first_view->x_center();
29  y=the_game->first_view->y_center();
30
[124]31
32  window_xstart=automap_window->x1();
[2]33  window_ystart=automap_window->y1();
[124]34  window_xend=automap_window->x2();
[2]35  window_yend=automap_window->y2();
36  centerx=(window_xstart+window_xend)/2;
37  centery=(window_ystart+window_yend)/2;
[124]38
39  sx=x/f_wid-w/2;                // start drawing with this foretile
[2]40  sy=y/f_hi-h/2;
[124]41  ex=sx+w;
42  ey=sy+h;
[2]43
44  if (sx<0)                       // does the map scroll past the left side ?
[124]45  { sx=0;                         // yes, start drawing at 0
[2]46    draw_xstart=centerx-(x*AUTOTILE_WIDTH/f_wid);
47  }
48  else
49    draw_xstart=centerx-(x*AUTOTILE_WIDTH/f_wid-sx*AUTOTILE_WIDTH);
[124]50
51  if (sy<0)
52  {
53    sy=0;
[2]54    draw_ystart=centery-(y*AUTOTILE_HEIGHT/f_hi);
55  }
56  else
57    draw_ystart=centery-(y*AUTOTILE_HEIGHT/f_hi-sy*AUTOTILE_HEIGHT);
58
59  // if view position hasn't changed, only update the binking dot and return
[124]60  if (draw_xstart==old_dx && draw_ystart==old_dy)
[2]61  {
[115]62   automap_window->screen->lock();
[2]63   automap_window->screen->add_dirty(centerx,centery,centerx,centery);
64    if ((tick++)&4)
65      automap_window->screen->putpixel(centerx,centery,255);
[124]66    else
[2]67      automap_window->screen->putpixel(centerx,centery,27);
[115]68   automap_window->screen->unlock();
[124]69    return ;
[2]70  }
71
72  old_dx=draw_xstart;
[124]73  old_dy=draw_ystart;
[2]74
75
[124]76  if (ex>=cur_lev->foreground_width())
[2]77    ex=cur_lev->foreground_width()-1;
78  if (ey>=cur_lev->foreground_height())
79    ey=cur_lev->foreground_height()-1;
80
81
82  screen->bar(window_xstart,window_ystart,draw_xstart,window_yend,0);
83  screen->bar(window_xstart,window_ystart,window_xend,draw_ystart,0);
84
[124]85
[2]86/*  if (ex>=cur_lev->foreground_width())
[124]87  {
[2]88    draw_xend=center
89    ex=foreground_width()-1; */
90
91
92
[124]93
[2]94  // we are going to redraw the whole map, so make the dirty rect work easier by marking
95  // everything dirty
96  screen->add_dirty(window_xstart,window_ystart,window_xend,window_yend);
97
98
99
[124]100
[2]101  // draw the tiles that will be around the border of the automap with put_image
102  // because it handles clipping, but for ths reason is slower, the rest
103  // we will slam on as fast as possible
104
105  screen->set_clip(window_xstart,window_ystart,window_xend,window_yend);
[494]106/*  for (i=draw_xstart,j=draw_ystart,x=sx,y=sy; y<=ey; j+=AUTOTILE_HEIGHT,y++)
[2]107    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0);
108
[494]109  for (i=draw_xstart+ex*AUTOTILE_WIDTH,j=draw_ystart,y=sy,x=ex; y<=ey; j+=AUTOTILE_HEIGHT,y++)
[2]110    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0);
111
[494]112  for (i=draw_xstart,j=draw_ystart,x=sx,y=sy; x<=ex; i+=AUTOTILE_WIDTH,x++)
[2]113    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0);
114
[494]115  for (i=draw_xstart,j=draw_ystart+ey*AUTOTILE_HEIGHT,x=sx,y=ex; x<=ex; i+=AUTOTILE_WIDTH,x++)
[2]116    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0); */
117
118
[124]119
[2]120  unsigned short *fgline;
[494]121  for (j=draw_ystart,y=sy; y<=ey; j+=AUTOTILE_HEIGHT,y++)
[124]122  {
[2]123    fgline=cur_lev->get_fgline(y)+sx;
[494]124    for (i=draw_xstart,x=sx; x<=ex; i+=AUTOTILE_WIDTH,x++,fgline++)
[2]125    {
126      if ((*fgline)&0x8000)
127      {
[124]128    int id=foretiles[ (*fgline)&0x7fff];
129    if (id>=0)
[123]130          cache.foret(id)->micro_image->put_image(screen,i,j,0);
[124]131    else
[123]132          cache.foret(foretiles[0])->micro_image->put_image(screen,i,j,0);
[2]133      }
134      else
135        screen->bar(i,j,i+AUTOTILE_WIDTH-1,j+AUTOTILE_HEIGHT-1,0);
136
137
138    }
139
[124]140  }
[2]141
142
[115]143  // draw the person as a dot, no need to add a dirty because we marked the
144  // whole screen already
145  automap_window->screen->lock();
[2]146  if ((tick++)&4)
147    automap_window->screen->putpixel(centerx,centery,255);
[124]148  else
[2]149    automap_window->screen->putpixel(centerx,centery,27);
[115]150  automap_window->screen->unlock();
[2]151
152  // set the clip back to full window size because soemthing else could mess with the area
[124]153  automap_window->screen->set_clip(0,0,screen->width()-1,screen->height()-1);
[2]154}
155
156
157void automap::toggle_window()
158{
[111]159    if (automap_window)
[124]160    {
[111]161        wm->close_window(automap_window);
162        automap_window = NULL;
163    }
164    else
[124]165    {
[111]166        old_dx = -1000; // make sure the map gets drawn the first time
167        old_dy = -1000;
[2]168
[111]169        automap_window = wm->new_window(0, 0, w * AUTOTILE_WIDTH,
170                                        h * AUTOTILE_HEIGHT, NULL, "Map");
171        automap_window->screen->bar(17, 1, 17 + 8 * 6 + 3, 6,
[124]172                                    wm->medium_color());
[111]173        wm->font()->put_string(automap_window->screen, 20, 2, "Automap",
174                               wm->dark_color());
[124]175        draw();
176    }
[2]177}
178
179
180automap::automap(level *l, int width, int height)
181{
182  w=width;
183  h=height;
[124]184
185  tick=0;
[2]186  cur_lev=l;
187  automap_window=NULL;
[124]188  toggle_window();
[2]189}
190
191void automap::handle_event(event &ev)
192{
193
194  //only respond to stuff in our window or on the main screen
195  if (ev.window==NULL || ev.window==automap_window)
196  {
197    switch (ev.type)
198    {
199      case EV_KEY :
200        switch(ev.key)
[124]201    {
[494]202      case 'A' :
[124]203      case 'a' :
204          toggle_window();
[494]205        break;
[124]206    }
[494]207    break;
[2]208      case EV_CLOSE_WINDOW :
[106]209        wm->close_window(automap_window);
[124]210    automap_window=NULL;
[494]211    break;
[124]212    }
213  }
[2]214}
215
216
217
218
219
220
221
Note: See TracBrowser for help on using the repository browser.