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

Last change on this file since 668 was 668, checked in by Sam Hocevar, 12 years ago

imlib: make JCFont use vec2i classes.

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