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

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

imlib: started refactoring the dirty rectangle system.

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