Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

src/tilefield.c

Go to the documentation of this file.
00001 /* {{{
00002  * CalcRogue, a roguelike game for PCs, calculators and PDAs
00003  * Copyright (C) 2003 Jim Babcock
00004  * 
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  * }}} */
00019 // tilefield.c
00022 
00023 #include "crogue.h"
00024 
00025 static void _UI_TF_DrawTile(int x, int y, UI_TF_TileInfo tile);
00026 
00027 static UI_Tilefield tf;
00028 
00029 //{{{
00030 void UI_TF_MoveCamera(int x, int y)
00031 {
00032     int oldx=tf.viewoffset.x, oldy=tf.viewoffset.y;
00033         // Previous x,y offset, to compare later to see if it's changed
00034     uint threshhold_x, threshhold_y;
00035     
00036     threshhold_x = tf.window.x / 3 - 1;
00037     threshhold_y = tf.window.y / 3 - 1;
00038     
00039     // Scroll X
00040     if( x-threshhold_x <= tf.viewoffset.x || x+threshhold_x-tf.viewoffset.x >= tf.window.x )
00041     {
00042         tf.viewoffset.x = max(x - ((sint)tf.window.x)/2, 0);
00043         if(tf.viewoffset.x + tf.window.x > tf.size.x)
00044             tf.viewoffset.x = tf.size.x - tf.window.x;
00045     }
00046     
00047     // Scroll Y
00048     if( y-threshhold_y <= tf.viewoffset.y || y+threshhold_y-tf.viewoffset.y >= tf.window.y )
00049     {
00050         tf.viewoffset.y = max(y - ((sint)tf.window.y)/2, 0);
00051         if(tf.viewoffset.y + tf.window.y > tf.size.y)
00052             tf.viewoffset.y = tf.size.y - tf.window.y;
00053     }
00054     
00055     if( tf.viewoffset.x != oldx || tf.viewoffset.y != oldy ) UI_TF_FullRedraw();
00056 }
00057 //}}}
00058 //{{{
00059 void UI_TF_PlaceCamera(int x, int y)
00060 {
00061     tf.viewoffset.x = max(x - ((sint)tf.window.x)/2, 0);
00062     tf.viewoffset.y = max(y - ((sint)tf.window.y)/2, 0);
00063     if(tf.viewoffset.x + tf.window.x > tf.size.x)
00064         tf.viewoffset.x = tf.size.x - tf.window.x;
00065     if(tf.viewoffset.y + tf.window.y > tf.size.y)
00066         tf.viewoffset.y = tf.size.y - tf.window.y;
00067     return;
00068 }
00069 //}}}
00070 //{{{
00071 void UI_TF_Scroll(int x, int y)
00072 {
00073     signed int new_x, new_y;
00074     
00075     if(w->time == 0)
00076     {
00077         message(gettext("Use the number keys (1-9) to move."));
00078         return;
00079     }
00080     
00081     new_x = tf.viewoffset.x + x;
00082     new_y = tf.viewoffset.y + y;
00083     if(new_x<0) new_x = 0;
00084     if(new_y<0) new_y = 0;
00085     if(new_x + tf.window.x > tf.size.x) new_x = tf.size.x - tf.window.x;
00086     if(new_y + tf.window.y > tf.size.y) new_y = tf.size.y - tf.window.y;
00087     
00088     tf.viewoffset.x = new_x;
00089     tf.viewoffset.y = new_y;
00090     
00091     UI_TF_FullRedraw();
00092 }
00093 //}}}
00094 //{{{
00095 void UI_TF_Init(coord size)
00096 {
00097     tf.size = size;
00098     tf.viewoffset = COORD(0,0);
00099     
00100     // Recalculate pos and window
00101     tf.window = tf.full_window;
00102     tf.pos = tf.full_pos;
00103     if(tf.window.x > tf.size.x) {
00104         tf.window.x = tf.size.x;
00105         tf.pos.x += (tf.full_window.x - tf.size.x)/2;
00106     }
00107     if(tf.window.y > tf.size.y) {
00108         tf.window.y = tf.size.y;
00109         tf.pos.y += (tf.full_window.y - tf.size.y)/2;
00110     }
00111 }
00112 //}}}
00113 //{{{
00114 void UI_TF_Place(coord pos, coord window, const uchar *graphics,
00115     uchar tileheight, uchar tilewidth)
00116 {
00117     tf.full_pos = pos;
00118     tf.full_window = window;
00119     tf.graphics = graphics;
00120     tf.tileheight = tileheight;
00121     tf.tilewidth = tilewidth;
00122     
00123     UI_TF_Init(tf.size);
00124 }
00125 //}}}
00126 //{{{
00127 void UI_TF_Deinit(void)
00128 {
00129 }
00130 //}}}
00131 //{{{
00132 void UI_TF_PutTile(int x, int y, uchar tnum, uchar lighting)
00133 {
00134     int tx, ty;
00135     UI_TF_TileInfo tile;
00136     tx = x - tf.viewoffset.x;
00137     ty = y - tf.viewoffset.y;
00138     
00139     tile.tile = tnum;
00140     tile.lighting = lighting;
00141     
00142     if(tx<0 || ty<0 || tx>=tf.window.x || ty>=tf.window.y) return;
00143     
00144     _UI_TF_DrawTile(tx, ty, tile);
00145 }
00146 //}}}
00147 #ifdef SUPPORT_COLOR
00148 //{{{
00149 void UI_TF_PutTile_Color(int x, int y, uchar tnum, uchar lighting, colorinfo color)
00150 {
00151     int tx, ty;
00152     UI_TF_TileInfo tile;
00153     tx = x - tf.viewoffset.x;
00154     ty = y - tf.viewoffset.y;
00155     
00156     tile.tile = tnum;
00157     tile.lighting = lighting;
00158     tile.color = color;
00159     
00160     if(tx<0 || ty<0 || tx>=tf.window.x || ty>=tf.window.y) return;
00161     
00162     _UI_TF_DrawTile(tx, ty, tile);
00163 }
00164 //}}}
00165 #endif
00166 //{{{
00167 void UI_TF_FullRedraw()
00168 {
00169     int i, ii;
00170     
00171     for(i=0; i<tf.window.y; i++)
00172     {
00173 #ifdef REALCOMPUTER
00174         clear_line(i+tf.pos.y);
00175 #endif
00176         for(ii=0; ii<tf.window.x; ii++)
00177             draw_tile(ii+tf.viewoffset.x, i+tf.viewoffset.y);
00178     }
00179 }
00180 //}}}
00181 #if 0
00182 //{{{
00183 const uchar tiles[128] = {
00184 0,   ' ', 2,   '.', '.', '#', '~', '.', '.', '^', '^', '-', '|', '\\','/', 't',
00185 '%', ' ', '}', '*', '*', 21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  
00186 32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  
00187 48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  
00188 64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,  
00189 80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  
00190 96,  97,  98,  99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 
00191 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
00192 }; 
00193 //}}}
00194 #endif
00195 //{{{
00196 static void _UI_TF_DrawTile(int x, int y, UI_TF_TileInfo tile)
00197 {
00198 #ifdef IS_CALCULATOR
00199     void *light;
00200     void *dark;
00201     const uchar *lighttile;
00202     const uchar *darktile;
00203     
00204     if(w->options[OPTION_GRAYSCALE] == OPTION_GRAY_ON)
00205     {
00206         light = GrayGetPlane(LIGHT_PLANE);
00207         dark = GrayGetPlane(DARK_PLANE);
00208         switch(tile.lighting)
00209         {
00210             case LIGHTING_LIT:
00211                 lighttile = darktile = tf.graphics + tile.tile*tf.tileheight;
00212                 break;
00213             case LIGHTING_DARK:
00214                 lighttile = tf.graphics + (tf.tileheight * blacktile);
00215                 darktile = tf.graphics + tile.tile*tf.tileheight;
00216                 break;
00217             case LIGHTING_UNEXPLORED:
00218                 lighttile = tf.graphics + (tf.tileheight * whitetile);
00219                 darktile = tf.graphics + (tf.tileheight * blacktile);
00220                 break;
00221             default:
00222                 return;
00223         }
00224         GraySprite8_BLIT(tf.pos.x + x * tf.tilewidth,
00225             tf.pos.y + y * tf.tileheight,
00226             tf.tileheight,
00227             lighttile,
00228             darktile,
00229             0xFF >> tf.tileheight,
00230             light,
00231             dark);
00232     } else {
00233         switch(tile.lighting)
00234         {
00235             case LIGHTING_LIT:
00236                 if(tile.tile == 3) tile.tile = 4;
00237             case LIGHTING_DARK:
00238                 darktile = tf.graphics + tile.tile * tf.tileheight;
00239                 break;
00240             case LIGHTING_UNEXPLORED:
00241                 darktile = tf.graphics + 17 * tf.tileheight;
00242                 break;
00243             default:
00244                 return;
00245         }
00246         GraySprite8_BLIT(tf.pos.x + x * tf.tilewidth,
00247             tf.pos.y + y * tf.tileheight,
00248             tf.tileheight,
00249             darktile,
00250             darktile,
00251             0xFF >> tf.tileheight,
00252             LCD_MEM,
00253             LCD_MEM);
00254     }
00255 #endif
00256 #ifdef USE_CURSES
00257     uchar target_color;
00258     if(tile.lighting == LIGHTING_LIT)
00259         target_color = tile.color.lit;
00260     else
00261         target_color = tile.color.dark;
00262     attroff(COLOR_PAIR(DEFAULT_COLOR));
00263     if(target_color & COLOR_BOLD)
00264         attron(A_BOLD);
00265     attron(COLOR_PAIR((target_color&~COLOR_BOLD)+1));
00266     mvaddch(y+tf.pos.y, x+tf.pos.x, tiles[tile.tile]);
00267     if(target_color & COLOR_BOLD)
00268         attroff(A_BOLD);
00269     attroff(COLOR_PAIR((target_color&~COLOR_BOLD)+1));
00270     attron(COLOR_PAIR(DEFAULT_COLOR));
00271 #endif
00272 #ifdef IS_PALM
00273     Graph_ClearRect(tf.pos.x + x*tf.tilewidth, tf.pos.y + y*tf.tileheight,
00274                     tf.tilewidth, tf.tileheight);
00275     Graph_DrawChar(tf.pos.x + x*tf.tilewidth, tf.pos.y + y*tf.tileheight,
00276                    tiles[tile.tile]);
00277 #endif
00278 }
00279 //}}}
00280 
00281 #ifdef REALCOMPUTER
00282 //{{{
00283 void UI_TF_PlaceCursor(int x, int y)
00284 {
00285     move(y+tf.pos.y-tf.viewoffset.y, x+tf.pos.x-tf.viewoffset.x);
00286 }
00287 //}}}
00288 #endif
00289 //{{{
00290 void UI_TF_DrawEffect(sshort x, sshort y, ushort tnum)
00291 {
00292     UI_TF_TileInfo t;
00293     t.tile = tnum;
00294     if(!(w->t[y][x].flags & TFLAG_EXPLORED))
00295         t.lighting = LIGHTING_UNEXPLORED;
00296     else if(w->t[y][x].flags & TFLAG_LIT)
00297         t.lighting = LIGHTING_LIT;
00298     else
00299         t.lighting = LIGHTING_DARK;
00300     
00301 #ifdef SUPPORT_COLOR
00302     t.color.lit = t.color.dark = COLOR_BOLD + 7;
00303 #endif
00304 #ifdef USE_CURSES
00305     UI_TF_PlaceCursor(x, y);
00306 #endif
00307     x -= tf.viewoffset.x;
00308     y -= tf.viewoffset.y;
00309     _UI_TF_DrawTile(x, y, t);
00310 }
00311 //}}}
00312 //{{{
00313 void UI_TF_ClearEffects(void)
00314 {
00315     UI_TF_FullRedraw();
00316 }
00317 //}}}
00318 

Generated on Thu May 20 13:12:11 2004 for CalcRogue by doxygen 1.3.6