ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/dclient/src/frontend/client/widgets/map_widget.cpp
Revision: 1.2
Committed: Mon Oct 18 14:11:49 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.1: +13 -2 lines
Log Message:
check and log but do not die when ncurses stuff fails

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "dclient/widgets/map.h"
2     #include "dclient/render_glyph.h"
3    
4     #include "deliantra/data/glyph.h"
5     #include "deliantra/data/map.h"
6    
7     #include <ndk++/application.hh>
8     #include <ndk++/color.hh>
9     #include <ndk++/pen.hh>
10    
11 sf-pippijn 1.2 #include <ncurses/assert.hh>
12    
13 sf-pippijn 1.1 map_widget::map_widget (ndk::panel *parent)
14     : frame ("", parent)
15     , map (nullptr)
16     {
17     }
18    
19     void
20     map_widget::update (data::map const &map)
21     {
22     this->map = &map;
23     }
24    
25    
26     static u16
27     select_face (u16 const (&face)[3], int layer)
28     {
29     if (layer == -1)
30     return face[2]
31     ? face[2]
32     : face[1]
33     ? face[1]
34     : face[0]
35     ;
36     return face[layer];
37     }
38    
39    
40     void
41     map_widget::draw ()
42     {
43     frame::draw ();
44    
45     if (!map)
46     return;
47    
48     static int const layer = -1;
49    
50     int const W = this->width () - 2;
51     int const H = this->height () - 2;
52     ndk::pen pen (*this);
53    
54     static data::glyph const space = { 0, 0, " " };
55 sf-pippijn 1.2 #if 1
56 sf-pippijn 1.1 static data::glyph const player = { 0, 0, "@" };
57 sf-pippijn 1.2 #endif
58 sf-pippijn 1.1
59     int const zerox = -(W - map->w) / 2;
60     int const zeroy = -(H - map->h) / 2;
61    
62 sf-pippijn 1.2 #if 1
63 sf-pippijn 1.1 int const playerx = (W + 1 + (zerox > 0)) / 2;
64     int const playery = (H + 1 + (zeroy > 0)) / 2;
65 sf-pippijn 1.2 #endif
66 sf-pippijn 1.1
67     for (int y = zeroy; y < H + zeroy; y++)
68     {
69     int const mapy = y + map->y;
70     if (!map->rows.has (mapy))
71     continue;
72     auto const &row = map->rows.at (mapy);
73     for (int x = zerox; x < W + zerox; x++)
74     {
75     int const mapx = x + map->x;
76     if (!row.has (mapx))
77     continue;
78     data::map::cell const &cell = row.at (mapx);
79     u32 const face = select_face (cell.face, layer);
80    
81     data::glyph const *glyph;
82    
83     int const winy = y - zeroy + 1;
84     int const winx = x - zerox + 1;
85    
86 sf-pippijn 1.2 #if 1
87 sf-pippijn 1.1 if (winx == playerx && winy == playery)
88     glyph = &player;
89 sf-pippijn 1.2 #else
90     if (0)
91     ;
92     #endif
93 sf-pippijn 1.1 else if (face && face < data::glyphs.size ()) // TODO: warn about face >= size
94     glyph = &data::glyphs[face];
95     else
96     glyph = &space;
97    
98 sf-pippijn 1.2 return_unless (pen.move (winx, winy));
99 sf-pippijn 1.1 write (pen, *glyph, cell.darkness);
100     }
101     }
102    
103 sf-pippijn 1.2 #if 1
104 sf-pippijn 1.1 // 19:15 < n0nsense> pippijn: kannst du nethack-mäßig noch den cursor auf den player setzen?
105     pen.move (playerx, playery);
106     ndk::app ().cursor_on ();
107     #endif
108    
109     if (getenv ("DCLIENT_ONESHOT"))
110     {
111     delete &ndk::app ();
112     exit (0);
113     }
114     }