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

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

style: remove trailing spaces, fix copyright statements.

File size: 5.7 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 "automap.h"
14#include "game.h"
15
16automap *current_automap=0;
17
18void automap::draw()
19{
20  if (!automap_window) return ;
21  image *screen=automap_window->screen;
22
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;
27
28  x=the_game->first_view->x_center();
29  y=the_game->first_view->y_center();
30
31
32  window_xstart=automap_window->x1();
33  window_ystart=automap_window->y1();
34  window_xend=automap_window->x2();
35  window_yend=automap_window->y2();
36  centerx=(window_xstart+window_xend)/2;
37  centery=(window_ystart+window_yend)/2;
38
39  sx=x/f_wid-w/2;                // start drawing with this foretile
40  sy=y/f_hi-h/2;
41  ex=sx+w;
42  ey=sy+h;
43
44  if (sx<0)                       // does the map scroll past the left side ?
45  { sx=0;                         // yes, start drawing at 0
46    draw_xstart=centerx-(x*AUTOTILE_WIDTH/f_wid);
47  }
48  else
49    draw_xstart=centerx-(x*AUTOTILE_WIDTH/f_wid-sx*AUTOTILE_WIDTH);
50
51  if (sy<0)
52  {
53    sy=0;
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
60  if (draw_xstart==old_dx && draw_ystart==old_dy)
61  {
62   automap_window->screen->lock();
63   automap_window->screen->add_dirty(centerx,centery,centerx,centery);
64    if ((tick++)&4)
65      automap_window->screen->putpixel(centerx,centery,255);
66    else
67      automap_window->screen->putpixel(centerx,centery,27);
68   automap_window->screen->unlock();
69    return ;
70  }
71
72  old_dx=draw_xstart;
73  old_dy=draw_ystart;
74
75
76  if (ex>=cur_lev->foreground_width())
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
85
86/*  if (ex>=cur_lev->foreground_width())
87  {
88    draw_xend=center
89    ex=foreground_width()-1; */
90
91
92
93
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
100
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);
106/*  for (i=draw_xstart,j=draw_ystart,x=sx,y=sy; y<=ey; j+=AUTOTILE_HEIGHT,y++)
107    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0);
108
109  for (i=draw_xstart+ex*AUTOTILE_WIDTH,j=draw_ystart,y=sy,x=ex; y<=ey; j+=AUTOTILE_HEIGHT,y++)
110    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0);
111
112  for (i=draw_xstart,j=draw_ystart,x=sx,y=sy; x<=ex; i+=AUTOTILE_WIDTH,x++)
113    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0);
114
115  for (i=draw_xstart,j=draw_ystart+ey*AUTOTILE_HEIGHT,x=sx,y=ex; x<=ex; i+=AUTOTILE_WIDTH,x++)
116    foretiles[cur_lev->get_fg(x,y)]->micro_image->put_image(screen,i,j,0); */
117
118
119
120  unsigned short *fgline;
121  for (j=draw_ystart,y=sy; y<=ey; j+=AUTOTILE_HEIGHT,y++)
122  {
123    fgline=cur_lev->get_fgline(y)+sx;
124    for (i=draw_xstart,x=sx; x<=ex; i+=AUTOTILE_WIDTH,x++,fgline++)
125    {
126      if ((*fgline)&0x8000)
127      {
128    int id=foretiles[ (*fgline)&0x7fff];
129    if (id>=0)
130          cache.foret(id)->micro_image->put_image(screen,i,j,0);
131    else
132          cache.foret(foretiles[0])->micro_image->put_image(screen,i,j,0);
133      }
134      else
135        screen->bar(i,j,i+AUTOTILE_WIDTH-1,j+AUTOTILE_HEIGHT-1,0);
136
137
138    }
139
140  }
141
142
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();
146  if ((tick++)&4)
147    automap_window->screen->putpixel(centerx,centery,255);
148  else
149    automap_window->screen->putpixel(centerx,centery,27);
150  automap_window->screen->unlock();
151
152  // set the clip back to full window size because soemthing else could mess with the area
153  automap_window->screen->set_clip(0,0,screen->width()-1,screen->height()-1);
154}
155
156
157void automap::toggle_window()
158{
159    if (automap_window)
160    {
161        wm->close_window(automap_window);
162        automap_window = NULL;
163    }
164    else
165    {
166        old_dx = -1000; // make sure the map gets drawn the first time
167        old_dy = -1000;
168
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,
172                                    wm->medium_color());
173        wm->font()->put_string(automap_window->screen, 20, 2, "Automap",
174                               wm->dark_color());
175        draw();
176    }
177}
178
179
180automap::automap(level *l, int width, int height)
181{
182  w=width;
183  h=height;
184
185  tick=0;
186  cur_lev=l;
187  automap_window=NULL;
188  toggle_window();
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)
201    {
202      case 'A' :
203      case 'a' :
204          toggle_window();
205        break;
206    }
207    break;
208      case EV_CLOSE_WINDOW :
209        wm->close_window(automap_window);
210    automap_window=NULL;
211    break;
212    }
213  }
214}
215
216
217
218
219
220
221
Note: See TracBrowser for help on using the repository browser.