source: abuse/branches/lol/src/automap.cpp @ 732

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

build: SDL2 compilation fixes.

File size: 5.8 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[715]4 *  Copyright (c) 2005-2013 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
[732]11#if HAVE_CONFIG_H
[555]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 ;
[709]25  AImage *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();
[682]40  ivec2 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  {
[682]65   automap_window->m_surf->AddDirty(center, center + ivec2(1));
[2]66    if ((tick++)&4)
[670]67      automap_window->m_surf->PutPixel(center, 255);
[124]68    else
[670]69      automap_window->m_surf->PutPixel(center, 27);
[124]70    return ;
[2]71  }
72
73  old_dx=draw_xstart;
[124]74  old_dy=draw_ystart;
[2]75
76
[124]77  if (ex>=cur_lev->foreground_width())
[2]78    ex=cur_lev->foreground_width()-1;
79  if (ey>=cur_lev->foreground_height())
80    ey=cur_lev->foreground_height()-1;
81
82
[682]83  screen->Bar(ivec2(window_xstart, window_ystart),
84              ivec2(draw_xstart, window_yend), 0);
85  screen->Bar(ivec2(window_xstart, window_ystart),
86              ivec2(window_xend, draw_ystart), 0);
[2]87
[124]88
[2]89/*  if (ex>=cur_lev->foreground_width())
[124]90  {
[2]91    draw_xend=center
92    ex=foreground_width()-1; */
93
[670]94  // we are going to redraw the whole map, so make the dirty rect work
95  // easier by marking everything dirty
[682]96  screen->AddDirty(ivec2(window_xstart, window_ystart),
97                   ivec2(window_xend + 1, window_yend + 1));
[2]98
[644]99  // draw the tiles that will be around the border of the automap with PutImage
[2]100  // because it handles clipping, but for ths reason is slower, the rest
101  // we will slam on as fast as possible
102
[682]103  screen->SetClip(ivec2(window_xstart, window_ystart),
104                  ivec2(window_xend + 1, window_yend + 1));
[644]105#if 0
106  for (i=draw_xstart,j=draw_ystart,x=sx,y=sy; y<=ey; j+=AUTOTILE_HEIGHT,y++)
[682]107    screen->PutImage(foretiles[cur_lev->get_fg(x, y)]->micro_image, ivec2(i, j), 0);
[2]108
[494]109  for (i=draw_xstart+ex*AUTOTILE_WIDTH,j=draw_ystart,y=sy,x=ex; y<=ey; j+=AUTOTILE_HEIGHT,y++)
[682]110    screen->PutImage(foretiles[cur_lev->get_fg(x, y)]->micro_image, ivec2(i, j), 0);
[2]111
[494]112  for (i=draw_xstart,j=draw_ystart,x=sx,y=sy; x<=ex; i+=AUTOTILE_WIDTH,x++)
[682]113    screen->PutImage(foretiles[cur_lev->get_fg(x, y)]->micro_image, ivec2(i, j), 0);
[2]114
[494]115  for (i=draw_xstart,j=draw_ystart+ey*AUTOTILE_HEIGHT,x=sx,y=ex; x<=ex; i+=AUTOTILE_WIDTH,x++)
[682]116    screen->PutImage(foretiles[cur_lev->get_fg(x, y)]->micro_image, ivec2(i, j), 0);
[644]117#endif
[2]118
119  unsigned short *fgline;
[494]120  for (j=draw_ystart,y=sy; y<=ey; j+=AUTOTILE_HEIGHT,y++)
[124]121  {
[2]122    fgline=cur_lev->get_fgline(y)+sx;
[494]123    for (i=draw_xstart,x=sx; x<=ex; i+=AUTOTILE_WIDTH,x++,fgline++)
[2]124    {
125      if ((*fgline)&0x8000)
126      {
[124]127    int id=foretiles[ (*fgline)&0x7fff];
128    if (id>=0)
[682]129          screen->PutImage(cache.foret(id)->micro_image, ivec2(i, j), 0);
[124]130    else
[682]131          screen->PutImage(cache.foret(foretiles[0])->micro_image, ivec2(i, j), 0);
[2]132      }
133      else
[682]134        screen->Bar(ivec2(i, j),
135                    ivec2(i + AUTOTILE_WIDTH - 1, j + AUTOTILE_HEIGHT - 1), 0);
[2]136    }
[124]137  }
[2]138
[115]139  // draw the person as a dot, no need to add a dirty because we marked the
140  // whole screen already
[2]141  if ((tick++)&4)
[670]142    automap_window->m_surf->PutPixel(center, 255);
[124]143  else
[670]144    automap_window->m_surf->PutPixel(center, 27);
[2]145
146  // set the clip back to full window size because soemthing else could mess with the area
[719]147  automap_window->m_surf->SetClip(ivec2::zero, screen->Size());
[2]148}
149
150void automap::toggle_window()
151{
[111]152    if (automap_window)
[124]153    {
[111]154        wm->close_window(automap_window);
155        automap_window = NULL;
156    }
157    else
[124]158    {
[111]159        old_dx = -1000; // make sure the map gets drawn the first time
160        old_dy = -1000;
[2]161
[719]162        automap_window = wm->CreateWindow(ivec2::zero, ivec2(w * AUTOTILE_WIDTH,
[715]163                                          h * AUTOTILE_HEIGHT), "Map");
[682]164        automap_window->m_surf->Bar(ivec2(17, 1), ivec2(17 + 8 * 6 + 3, 6),
[124]165                                    wm->medium_color());
[682]166        wm->font()->PutString(automap_window->m_surf, ivec2(20, 2), "Automap",
[668]167                              wm->dark_color());
[124]168        draw();
169    }
[2]170}
171
172
[691]173automap::automap(Level *l, int width, int height)
[2]174{
175  w=width;
176  h=height;
[124]177
178  tick=0;
[2]179  cur_lev=l;
180  automap_window=NULL;
[124]181  toggle_window();
[2]182}
183
[643]184void automap::handle_event(Event &ev)
[2]185{
186
187  //only respond to stuff in our window or on the main screen
188  if (ev.window==NULL || ev.window==automap_window)
189  {
190    switch (ev.type)
191    {
192      case EV_KEY :
193        switch(ev.key)
[124]194    {
[494]195      case 'A' :
[124]196      case 'a' :
197          toggle_window();
[494]198        break;
[124]199    }
[494]200    break;
[2]201      case EV_CLOSE_WINDOW :
[106]202        wm->close_window(automap_window);
[124]203    automap_window=NULL;
[494]204    break;
[124]205    }
206  }
[2]207}
208
209
210
211
212
213
214
Note: See TracBrowser for help on using the repository browser.