Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

OSPRITE.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

OSPRITE.CPP

Go to the documentation of this file.
00001 //Filename    : OSprite.h
00002 //Description : Sprite Class Declaration
00003 //Owner       : Fred
00004 
00005 #include <memory.h>
00006 #include <ALL.H>
00007 #include <GAMEDEF.H>
00008 #include <OMISC.H>
00009 #include <ORECT.H>
00010 #include <OSPRITE.H>
00011 #include <OSPRTRES.H>
00012 #include <OWORLD.H>
00013 #include <OWORLDMT.H>
00014 #include <OMATRIX.H>
00015 
00016 // for this->draw()
00017 #include <OVGA.H>
00018 //#include <OSTR.H>
00019 //#include <OFONT.H>
00020 
00021 //-------- Begin of function Sprite::Sprite --------//
00022 Sprite::Sprite() {
00023     // preserve virtual pointer
00024     memset( (char *)this + sizeof(void *), 0, sizeof(Sprite) - sizeof(void *) );
00025 }
00026 
00027 //--------- End of function Sprite::Sprite --------//
00028 
00029 //-------- Begin of function Sprite::~Sprite --------//
00030 Sprite::~Sprite() {
00031     deinit();
00032 }
00033 
00034 //--------- End of function Sprite::~Sprite --------//
00035 
00036 //--------- Begin of function Sprite::init ---------//
00037 void Sprite::init(short spriteId, short startXLoc, short startYLoc, char startSubXLoc, char startSubYLoc) {
00038     sprite_id = spriteId;
00039 
00040     go_x = next_x = cur_x = startXLoc;
00041     go_y = next_y = cur_y = startYLoc;
00042 
00043     /*
00044       cur_attack = 0;
00045       cur_action = SPRITE_IDLE;
00046       cur_dir     = m.random(GameDef::MAX_SPRITE_DIR_TYPE);     // facing any of the eight directions
00047       cur_frame  = 1;
00048       final_dir  = cur_dir;
00049     */
00050 
00051     //----- clone vars from sprite_res for fast access -----//
00052     sprite_id = 1;                                  //temp
00053     err_here();
00054     //990511            sprite_info = sprite_res[sprite_id];
00055 
00056     sprite_info->load_bitmap_res();
00057 
00058     //----- set the sprite's location & absolute positions on the map -----//
00059 
00060     set_abs(ZOOM_SMALL , MAP_LOC_WIDTH   , MAP_LOC_HEIGHT);
00061     set_abs(ZOOM_MEDIUM, ZOOM1_LOC_WIDTH, ZOOM1_LOC_HEIGHT);
00062     set_abs(ZOOM_LARGE , ZOOM2_LOC_WIDTH, ZOOM2_LOC_HEIGHT);
00063 
00064     will_not_move_next_process = 0;
00065 }
00066 
00067 //----------- End of function Sprite::init -----------//
00068 
00069 //--------- Begin of function Sprite::deinit ---------//
00070 void Sprite::deinit() {
00071     world.get_loc(cur_x, cur_y)->remove_sprite();
00072 }
00073 
00074 //----------- End of function Sprite::deinit -----------//
00075 
00076 //--------- Begin of function Sprite::set_abs ---------//
00078 void Sprite::set_abs(int zoomLevel, int locWidth, int locHeight) {
00079 }
00080 
00081 //----------- End of function Sprite::set_abs ---------//
00082 
00083 //--------- Begin of function Sprite::cur_sprite_frame ---------//
00086 SpriteFrame* Sprite::cur_sprite_frame(int *needMirror) {
00087     /*
00088       UCHAR curDir = display_dir();
00089       SpriteStop *stopAction= sprite_info->stop_array +curDir;
00090 
00091       return sprite_frame_res[0];       //temp
00092 
00093       if ( cur_frame > stopAction->frame_count )
00094       return sprite_frame_res[stopAction->frame_recno];       // first frame
00095       else            // only few sprite has stopAction->frame_count > 1
00096       return sprite_frame_res[stopAction->frame_recno+cur_frame-1];
00097     */
00098     return NULL;                                    //temp
00099 
00100     /*UCHAR curDir = display_dir();
00101       if( needMirror)
00102       *needMirror = need_mirror(curDir);
00103 
00104       // do not update cur_dir as curDir
00105       err_when(curDir<0 || curDir>=3*MAX_SPRITE_DIR_TYPE);
00106 
00107       switch( cur_action )
00108       {
00109       case SPRITE_MOVE:
00110       case SPRITE_SHIP_EXTRA_MOVE:
00111       if( guard_count)
00112       {
00113       if( curDir >= MAX_SPRITE_DIR_TYPE)
00114       {
00115       err_here();
00116       curDir %= MAX_SPRITE_DIR_TYPE;
00117       }
00118       return sprite_frame_res[sprite_info->guard_move_array[curDir].first_frame_recno+cur_frame-1];
00119       }
00120       else
00121       return sprite_frame_res[sprite_info->move_array[curDir].first_frame_recno+cur_frame-1];
00122 
00123       case SPRITE_ATTACK:
00124       err_when(curDir<0 || curDir>=MAX_SPRITE_DIR_TYPE);
00125       return sprite_frame_res[sprite_info->attack_array[cur_attack][curDir].first_frame_recno+cur_frame-1];
00126 
00127       case SPRITE_TURN:
00128       case SPRITE_IDLE:
00129       case SPRITE_WAIT:
00130       // air unit needs it own stop frames to float on air
00131       {
00132       if( guard_count )
00133       {
00134       if( curDir >= MAX_SPRITE_DIR_TYPE)
00135       {
00136       // if the sprite is turning, adjust direction to next
00137       if( turn_delay > 0)
00138       curDir ++;
00139       curDir %= MAX_SPRITE_DIR_TYPE;
00140       }
00141 
00142       SpriteGuardStop *guardStopAction = sprite_info->guard_stop_array + curDir;
00143       return sprite_frame_res[guardStopAction->first_frame_recno+
00144       min(guard_count,guardStopAction->frame_count)-1];
00145       }
00146       else
00147       {
00148       SpriteStop *stopAction= sprite_info->stop_array +curDir;
00149       if(cur_frame > stopAction->frame_count)
00150       return sprite_frame_res[stopAction->frame_recno];       // first frame
00151       else            // only few sprite has stopAction->frame_count > 1
00152       return sprite_frame_res[stopAction->frame_recno+cur_frame-1];
00153       }
00154       }
00155 
00156       case SPRITE_DIE:
00157       if(sprite_info->die.first_frame_recno)                    // only if this sprite has dying frame
00158       {
00159       if( needMirror)
00160       *needMirror = 0;                  // no need to mirror at any direction
00161       return sprite_frame_res[sprite_info->die.first_frame_recno+cur_frame-1];
00162       }
00163 
00164       default:
00165       return sprite_frame_res[sprite_info->move_array[curDir].first_frame_recno+cur_frame-1];
00166       }*/
00167 }
00168 
00169 //----------- End of function Sprite::cur_sprite_frame -----------//
00170 
00171 //--------- Begin of function Sprite::update_abs_pos ---------//
00174 void Sprite::update_abs_pos() {                   //
00175 
00176     /*
00177       SpriteFrame       *spriteFrame;   //temp
00178       if(!spriteFrame)
00179       spriteFrame = cur_sprite_frame();
00180     */
00181 
00182     /*
00183       abs_x1 = sub_cur_x + spriteFrame->offset_x - world.zoom_matrix->top_x_loc*Location::SUB_LOC_WIDTH*GameDef::ZOOM_LOC_WIDTH;
00184       abs_y1 = sub_cur_y + spriteFrame->offset_y - world.zoom_matrix->top_y_loc*Location::SUB_LOC_HEIGHT*GameDef::ZOOM_LOC_HEIGHT;
00185 
00186       abs_x2 = abs_x1 + spriteFrame->width  - 1;
00187       abs_y2 = abs_y1 + spriteFrame->height - 1;
00188     */
00189 }
00190 
00191 //----------- End of function Sprite::update_abs_pos -----------//
00192 
00193 /*
00194 
00195 short Sprite::cur_x_loc()
00196 { return sub_cur_x>>GameDef::ZOOM_X_SHIFT_COUNT; }
00197 short   Sprite::cur_y_loc()
00198 { return sub_cur_y>>GameDef::ZOOM_Y_SHIFT_COUNT; }
00199 
00200 short   Sprite::next_x_loc()
00201 { return sub_next_x>>GameDef::ZOOM_X_SHIFT_COUNT; }
00202 short   Sprite::next_y_loc()
00203 { return sub_next_y>>GameDef::ZOOM_Y_SHIFT_COUNT; }
00204 
00205 short Sprite::go_x_loc()
00206 { return sub_go_x>>GameDef::ZOOM_X_SHIFT_COUNT; }
00207 short Sprite::go_y_loc()
00208 { return sub_go_y>>GameDef::ZOOM_Y_SHIFT_COUNT; }
00209 
00210 //------------------------------------------------------//
00211 
00212 short Sprite::cur_sub_x_loc()
00213 { return sub_cur_x>>GameDef::ZOOM_SUB_X_SHIFT_COUNT; }
00214 short   Sprite::cur_sub_y_loc()
00215 { return sub_cur_y>>GameDef::ZOOM_SUB_Y_SHIFT_COUNT; }
00216 
00217 short   Sprite::next_sub_x_loc()
00218 { return sub_next_x>>GameDef::ZOOM_SUB_X_SHIFT_COUNT; }
00219 short   Sprite::next_sub_y_loc()
00220 { return sub_next_y>>GameDef::ZOOM_SUB_Y_SHIFT_COUNT; }
00221 
00222 short Sprite::go_sub_x_loc()
00223 { return sub_go_x>>GameDef::ZOOM_SUB_X_SHIFT_COUNT; }
00224 short Sprite::go_sub_y_loc()
00225 { return sub_go_y>>GameDef::ZOOM_SUB_Y_SHIFT_COUNT; }
00226 */
00227 
00228 //--------- Begin of function Sprite::draw ----------//
00232 void Sprite::draw(Matrix* matrixPtr) {
00233     //--------- draw sprite on the zoom window ---------//
00234 
00235     //int needMirror;
00236 
00237     //SpriteFrame* spriteFrame = cur_sprite_frame(&needMirror);
00238 
00239     //-------- check if the firm is within the view area --------//
00240 
00241     int zoomLevel = matrixPtr->zoom_level;
00242     int bmpWidth  = sprite_info->bitmap_width[zoomLevel];
00243     int bmpHeight = sprite_info->bitmap_height[zoomLevel];
00244 
00245     //char* bitmapPtr = sprite_info->res_bitmap.read_imported(spriteFrame->bitmap_offset);
00246     err_when( !sprite_info->res_bitmap[zoomLevel].init_flag );
00247     char* bmpPtr  = sprite_info->bitmap_ptr[zoomLevel];
00248 
00249     //update_abs_rect(zoomLevel);
00250 
00251     int x1 = abs_rect[zoomLevel].x1 - matrixPtr->abs_top_x;
00252 
00253     // out of the view area, not even a slight part of it appears in the view area
00254     if( x1 <= -bmpWidth || x1 >= matrixPtr->win_width )
00255         return;
00256 
00257     int y1 = abs_rect[zoomLevel].y1 - matrixPtr->abs_top_y;
00258 
00259     if( y1 <= -bmpHeight || y1 >= matrixPtr->win_height )
00260         return;
00261 
00262     int x2 = abs_rect[zoomLevel].x2 - matrixPtr->abs_top_x;
00263     int y2 = abs_rect[zoomLevel].y2 - matrixPtr->abs_top_y;
00264 
00265     if( x2 < 0 || y2 < 0 )
00266         return;
00267 
00268     //---- only portion of the sprite is inside the view area ------//
00269 
00270     if( x1 < 0 || x2 >= matrixPtr->win_width || y1 < 0 || y2 >= matrixPtr->win_height ) {
00271         vga_back.put_bitmap_area_trans_decompress( x1+matrixPtr->win_x1, y1+matrixPtr->win_y1, bmpPtr,
00272                                                    max(0,x1)-x1, max(0,y1)-y1, min(matrixPtr->win_width-1,x2)-x1, min(matrixPtr->win_height-1,y2)-y1);
00273     }
00274 
00275     //---- the whole sprite is inside the view area ------//
00276 
00277     else {
00278         vga_back.put_bitmap_trans_decompress( x1+matrixPtr->win_x1, y1+matrixPtr->win_y1, bmpPtr);
00279 
00280         /*
00281           if( zoomLevel == 1 )
00282           {
00283           String str;
00284           str  = "(";
00285           str += loc_x1;
00286           str += ",";
00287           str += loc_y1;
00288           str += ")";
00289 
00290           vga.use_back();
00291           font_san.put(x1+matrixPtr->win_x1, y1+matrixPtr->win_y1, str);
00292           vga.use_front();
00293           }
00294         */
00295     }
00296 }
00297 
00298 //--------- End of function Sprite::draw -----------//
00299 
00300 //--------- Begin of function Sprite::process ---------//
00301 void Sprite::process() {
00302     if ( will_not_move_next_process ) {
00303         will_not_move_next_process = (will_not_move_next_process+1)%10;
00304         return;
00305     }
00306     else
00307         will_not_move_next_process = 1;
00308 
00309     // randomly move along walkable locations (road)
00310     //
00311     go_x = cur_x;
00312     go_y = cur_y;
00313 
00314     go_x += m.random(3) - 1;
00315     go_y += m.random(3) - 1;
00316 
00317     Location* locPtr = world.get_valid_loc(go_x, go_y);
00318 
00319     if ( !locPtr || !locPtr->is_walkable() || locPtr->has_sprite() )
00320         return;
00321 
00322     world.get_loc(cur_x, cur_y)->remove_sprite();
00323     cur_x = go_x; cur_y = go_y;
00324     locPtr->set_sprite(sprite_recno);
00325 
00326     set_abs(ZOOM_SMALL , MAP_LOC_WIDTH   , MAP_LOC_HEIGHT);
00327     set_abs(ZOOM_MEDIUM, ZOOM1_LOC_WIDTH, ZOOM1_LOC_HEIGHT);
00328     set_abs(ZOOM_LARGE , ZOOM2_LOC_WIDTH, ZOOM2_LOC_HEIGHT);
00329 
00330 }
00331 
00332 //----------- End of function Sprite::process -----------//

Generated on Fri Aug 23 01:38:23 2002 for VirtualU by doxygen1.2.17