00001 #ifndef VIEWGEOM_H 00002 #define VIEWGEOM_H 00003 00004 #include "tiledgnbuf.h" 00005 00006 struct screen_cell_t 00007 { 00008 screen_buffer_t glyph; 00009 screen_buffer_t colour; 00010 screen_buffer_t flash_colour; 00011 #ifdef USE_TILE 00012 packed_cell tile; 00013 #endif 00014 }; 00015 00016 class crawl_view_buffer 00017 { 00018 public: 00019 crawl_view_buffer(); 00020 crawl_view_buffer(const coord_def &sz); 00021 ~crawl_view_buffer(); 00022 00023 coord_def size() const { return m_size; } 00024 void resize(const coord_def &sz); 00025 bool empty() const; 00026 00027 operator screen_cell_t * () { return (m_buffer); } 00028 operator const screen_cell_t * () const { return (m_buffer); } 00029 const crawl_view_buffer & operator = (const crawl_view_buffer &rhs); 00030 00031 void clear(); 00032 void draw(); 00033 private: 00034 coord_def m_size; 00035 screen_cell_t *m_buffer; 00036 }; 00037 00038 struct crawl_view_geometry 00039 { 00040 public: 00041 coord_def termp; // Left-top pos of terminal. 00042 coord_def termsz; // Size of the terminal. 00043 coord_def viewp; // Left-top pos of viewport. 00044 coord_def viewsz; // Size of the viewport (play area). 00045 coord_def hudp; // Left-top pos of status area. 00046 coord_def hudsz; // Size of the status area. 00047 coord_def msgp; // Left-top pos of the message pane. 00048 coord_def msgsz; // Size of the message pane. 00049 coord_def mlistp; // Left-top pos of the monster list. 00050 coord_def mlistsz; // Size of the monster list. 00051 00052 crawl_view_buffer vbuf; // Buffer for drawing the main game map. 00053 00054 coord_def vgrdc; // What grid pos is at the centre of the view 00055 // usually you.pos(). 00056 00057 coord_def viewhalfsz; 00058 00059 coord_def glos1, glos2; // LOS limit grid coords (inclusive) 00060 coord_def vlos1, vlos2; // LOS limit viewport coords (inclusive) 00061 00062 coord_def mousep; // Where the mouse is. 00063 00064 private: 00065 coord_def last_player_pos; 00066 00067 public: 00068 crawl_view_geometry(); 00069 void init_geometry(); 00070 00071 void init_view(); 00072 void set_player_at(const coord_def &c, bool force_centre = false); 00073 // Set new location, but preserve scrolling as if the player didn't move. 00074 void shift_player_to(const coord_def &c); 00075 // Recalculate vlos1 and vlos2. 00076 void calc_vlos(); 00077 00078 inline coord_def view2grid(const coord_def &pos) const 00079 { 00080 return (pos - viewhalfsz + vgrdc - coord_def(1, 1)); 00081 } 00082 00083 inline coord_def grid2view(const coord_def &pos) const 00084 { 00085 return (pos - vgrdc + viewhalfsz + coord_def(1, 1)); 00086 } 00087 00088 inline coord_def view2show(const coord_def &pos) const 00089 { 00090 return (pos - vlos1); 00091 } 00092 00093 inline coord_def show2view(const coord_def &pos) const 00094 { 00095 return (pos + vlos1); 00096 } 00097 00098 inline coord_def grid2show(const coord_def &pos) const 00099 { 00100 return (view2show(grid2view(pos))); 00101 } 00102 00103 inline coord_def show2grid(const coord_def &pos) const 00104 { 00105 return (view2grid(show2view(pos))); 00106 } 00107 00108 inline coord_def screen2view(const coord_def& pos) const 00109 { 00110 return (pos - viewp + termp); 00111 } 00112 00113 inline coord_def view2screen(const coord_def& pos) const 00114 { 00115 return (pos + viewp - termp); 00116 } 00117 00118 inline coord_def screen2grid(const coord_def& pos) const 00119 { 00120 return view2grid(screen2view(pos)); 00121 } 00122 00123 inline coord_def grid2screen(const coord_def& pos) const 00124 { 00125 return view2screen(grid2view(pos)); 00126 } 00127 00128 coord_def glosc() const 00129 { 00130 return (glos1 + glos2) / 2; 00131 } 00132 00133 bool in_los_bounds_g(const coord_def &c) const 00134 { 00135 return (c.x >= glos1.x && c.x <= glos2.x 00136 && c.y >= glos1.y && c.y <= glos2.y); 00137 } 00138 00139 bool in_los_bounds_v(const coord_def &c) const 00140 { 00141 return in_los_bounds_g(view2grid(c)); 00142 } 00143 00144 bool in_viewport_v(const coord_def &c) const 00145 { 00146 return (c.x > 0 && c.y > 0 00147 && c.x <= viewsz.x && c.y <= viewsz.y); 00148 } 00149 00150 bool in_viewport_s(const coord_def &c) const 00151 { 00152 return in_viewport_v(screen2view(c)); 00153 } 00154 00155 bool in_viewport_g(const coord_def &c) const 00156 { 00157 return in_viewport_v(grid2view(c)); 00158 } 00159 }; 00160 00161 extern crawl_view_geometry crawl_view; 00162 00163 inline coord_def view2grid(const coord_def &pos) 00164 { 00165 return crawl_view.view2grid(pos); 00166 } 00167 00168 inline coord_def grid2view(const coord_def &pos) 00169 { 00170 return crawl_view.grid2view(pos); 00171 } 00172 00173 inline coord_def view2show(const coord_def &pos) 00174 { 00175 return crawl_view.view2show(pos); 00176 } 00177 00178 inline coord_def show2view(const coord_def &pos) 00179 { 00180 return crawl_view.show2view(pos); 00181 } 00182 00183 inline coord_def grid2show(const coord_def &pos) 00184 { 00185 return crawl_view.grid2show(pos); 00186 } 00187 00188 inline coord_def show2grid(const coord_def &pos) 00189 { 00190 return crawl_view.show2grid(pos); 00191 } 00192 00193 inline bool in_los_bounds_v(const coord_def& pos) 00194 { 00195 return crawl_view.in_los_bounds_v(pos); 00196 } 00197 00198 inline bool in_los_bounds_g(const coord_def& pos) 00199 { 00200 return crawl_view.in_los_bounds_g(pos); 00201 } 00202 00203 #endif