void CMapHandler::initObjectRects() { //initializing objects / rects for(size_t f=0; f < map->objects.size(); ++f) { const CGObjectInstance *obj = map->objects[f]; if( !obj || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero) //boat with hero (hero graphics is used) || !obj->defInfo ) { continue; } if (!graphics->getDef(obj)) //try to load it processDef(obj->defInfo); if (!graphics->getDef(obj)) // stil no graphics? exit continue; const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap; for(int fx=0; fx<bitmap->w>>5; ++fx) //bitmap->w/32 { for(int fy=0; fy<bitmap->h>>5; ++fy) //bitmap->h/32 { SDL_Rect cr; cr.w = 32; cr.h = 32; cr.x = fx<<5; //fx*32 cr.y = fy<<5; //fy*32 std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr); if( (obj->pos.x + fx - bitmap->w/32+1) >= 0 && (obj->pos.x + fx - bitmap->w/32+1) < ttiles.size() - frameW && (obj->pos.y + fy - bitmap->h/32+1) >= 0 && (obj->pos.y + fy - bitmap->h/32+1) < ttiles[0].size() - frameH ) { //TerrainTile2 & curt = // ttiles // [obj->pos.x + fx - bitmap->w/32] //[obj->pos.y + fy - bitmap->h/32] //[obj->pos.z]; ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd); } } // for(int fy=0; fy<bitmap->h/32; ++fy) } //for(int fx=0; fx<bitmap->w/32; ++fx) } // for(int f=0; f<map->objects.size(); ++f) for(int ix=0; ix<ttiles.size()-frameW; ++ix) { for(int iy=0; iy<ttiles[0].size()-frameH; ++iy) { for(int iz=0; iz<ttiles[0][0].size(); ++iz) { stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo); } } } }
void CMapHandler::initObjectRects() { //initializing objects / rects for(auto & elem : map->objects) { const CGObjectInstance *obj = elem; if( !obj || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used) { continue; } if (!graphics->getDef(obj)) //try to load it processDef(obj->appearance); if (!graphics->getDef(obj)) // stil no graphics? exit continue; const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap; for(int fx=0; fx < obj->getWidth(); ++fx) { for(int fy=0; fy < obj->getHeight(); ++fy) { int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z); SDL_Rect cr; cr.w = 32; cr.h = 32; cr.x = bitmap->w - fx * 32 - 32; cr.y = bitmap->h - fy * 32 - 32; std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr); if( map->isInTheMap(currTile) && // within map cr.x + cr.w > 0 && // image has data on this tile cr.y + cr.h > 0 && obj->coveringAt(currTile.x, currTile.y) // object is visible here ) { ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd); } } } } for(int ix=0; ix<ttiles.size()-frameW; ++ix) { for(int iy=0; iy<ttiles[0].size()-frameH; ++iy) { for(int iz=0; iz<ttiles[0][0].size(); ++iz) { stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo); } } } }
// Update map window screen // top_tile top left tile to draw. Not necessarily visible. // extRect, extRect = map window on screen // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31. void CMapHandler::terrainRect( int3 top_tile, ui8 anim, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, bool otherHeroAnim, ui8 heroAnim, SDL_Surface * extSurf, const SDL_Rect * extRect, int moveX, int moveY, bool puzzleMode, int3 grailPosRel ) const { // Width and height of the portion of the map to process. Units in tiles. ui32 dx = tilesW; ui32 dy = tilesH; // Basic rectangle for a tile. Should be a const but conflicts with SDL headers SDL_Rect rtile = { 0, 0, 32, 32 }; // Absolute coords of the first pixel in the top left corner int srx_init = offsetX + extRect->x; int sry_init = offsetY + extRect->y; int srx, sry; // absolute screen coordinates in pixels // If moving, we need to add an extra column/line if (moveX != 0) { dx++; srx_init += moveX; if (moveX > 0) { // Moving right. We still need to draw the old tile on the // left, so adjust our referential top_tile.x --; srx_init -= 32; } } if (moveY != 0) { dy++; sry_init += moveY; if (moveY > 0) { // Moving down. We still need to draw the tile on the top, // so adjust our referential. top_tile.y --; sry_init -= 32; } } // Reduce sizes if we go out of the full map. if (top_tile.x < -frameW) top_tile.x = -frameW; if (top_tile.y < -frameH) top_tile.y = -frameH; if (top_tile.x + dx > sizes.x + frameW) dx = sizes.x + frameW - top_tile.x; if (top_tile.y + dy > sizes.y + frameH) dy = sizes.y + frameH - top_tile.y; if(!otherHeroAnim) heroAnim = anim; //the same, as it should be SDL_Rect prevClip; SDL_GetClipRect(extSurf, &prevClip); SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf); const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf); //const BlitterWithRotationAndAlphaVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf); // printing terrain srx = srx_init; for (int bx = 0; bx < dx; bx++, srx+=32) { // Skip column if not in map if (top_tile.x+bx < 0 || top_tile.x+bx >= sizes.x) continue; sry = sry_init; for (int by=0; by < dy; by++, sry+=32) { int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position // Skip tile if not in map if (pos.y < 0 || pos.y >= sizes.y) continue; const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z]; const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z)); SDL_Rect sr; sr.x=srx; sr.y=sry; sr.h=sr.w=32; //blit terrain with river/road if(tile.terbitmap) { //if custom terrain graphic - use it SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0); CSDL_Ext::blitSurface(tile.terbitmap, &temp_rect, extSurf, &sr); } else //use default terrain graphic { blitterWithRotation(terrainGraphics[tinfo.terType][tinfo.terView],rtile, extSurf, sr, tinfo.extTileFlags%4); } if(tinfo.riverType) //print river if present { blitterWithRotationAndAlpha(staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,rtile, extSurf, sr, (tinfo.extTileFlags>>2)%4); } //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD) { //part from top tile const TerrainTile &topTile = map->getTile(int3(pos.x, pos.y-1, pos.z)); Rect source(0, 16, 32, 16); Rect dest(sr.x, sr.y, sr.w, sr.h/2); blitterWithRotationAndAlpha(roadDefs[topTile.roadType - 1]->ourImages[topTile.roadDir].bitmap, source, extSurf, dest, (topTile.extTileFlags>>4)%4); } if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile { Rect source(0, 0, 32, 32); Rect dest(sr.x, sr.y+16, sr.w, sr.h/2); blitterWithRotationAndAlpha(roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap, source, extSurf, dest, (tinfo.extTileFlags>>4)%4); } //blit objects const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects; for(int h=0; h < objects.size(); ++h) { const CGObjectInstance *obj = objects[h].first; if (!graphics->getDef(obj)) processDef(obj->defInfo); PlayerColor color = obj->tempOwner; //checking if object has non-empty graphic on this tile if(obj->ID != Obj::HERO && !obj->coveringAt(top_tile.x + bx - obj->pos.x, top_tile.y + by - obj->pos.y)) continue; static const int notBlittedInPuzzleMode[] = {124}; //don't print flaggable objects in puzzle mode if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //? continue; SDL_Rect sr2(sr); SDL_Rect pp = objects[h].second; pp.h = sr.h; pp.w = sr.w; const CGHeroInstance * themp = (obj->ID != Obj::HERO ? NULL : static_cast<const CGHeroInstance*>(obj)); //print hero / boat and flag if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat { const int IMGVAL = 8; //frames per group of movement animation ui8 dir; std::vector<Cimage> * iv = NULL; std::vector<CDefEssential *> Graphics::*flg = NULL; SDL_Surface * tb = nullptr; //surface to blitted if(themp) //hero { if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero? { logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!"; continue; } dir = themp->moveDir; //pick graphics of hero (or boat if hero is sailing) if (themp->boat) iv = &graphics->boatAnims[themp->boat->subID]->ourImages; else if (themp->sex) iv = &graphics->heroAnims[themp->type->heroClass->imageMapFemale]->ourImages; else iv = &graphics->heroAnims[themp->type->heroClass->imageMapMale]->ourImages; //pick appropriate flag set if(themp->boat) { switch (themp->boat->subID) { case 0: flg = &Graphics::flags1; break; case 1: flg = &Graphics::flags2; break; case 2: flg = &Graphics::flags3; break; default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID; } } else { flg = &Graphics::flags4; } } else //boat { const CGBoat *boat = static_cast<const CGBoat*>(obj); dir = boat->direction; iv = &graphics->boatAnims[boat->subID]->ourImages; } if(themp && !themp->isStanding) //hero is moving { size_t gg; for(gg=0; gg<iv->size(); ++gg) { if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true)) { tb = (*iv)[gg+heroAnim%IMGVAL].bitmap; break; } } CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2); //printing flag pp.y+=IMGVAL*2-32; sr2.y-=16; CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2); } else //hero / boat stands still { size_t gg; for(gg=0; gg < iv->size(); ++gg) { if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false)) { tb = (*iv)[gg].bitmap; break; } } CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2); //printing flag if(flg && obj->pos.x == top_tile.x + bx && obj->pos.y == top_tile.y + by) { SDL_Rect bufr = sr2; bufr.x-=2*32; bufr.y-=1*32; bufr.h = 64; bufr.w = 96; if(bufr.x-extRect->x>-64) CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, NULL, extSurf, &bufr); } } } else //blit normal object { const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages; SDL_Surface *bitmap = ourImages[(anim+getPhaseShift(obj))%ourImages.size()].bitmap; //setting appropriate flag color if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL) CSDL_Ext::setPlayerColor(bitmap, color); if( obj->hasShadowAt(top_tile.x + bx - obj->pos.x, top_tile.y + by - obj->pos.y) ) CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2); else CSDL_Ext::blitSurface(bitmap,&pp,extSurf,&sr2); } } //objects blitted //X sign if(puzzleMode) { if(bx == grailPosRel.x && by == grailPosRel.y) { CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, NULL, extSurf, &sr); } } }