bool SdlBatcher::append(const Picture& pic, const Rect& srcrect, const Rect& dstRect, Rect *clip )
{
  if( !pic.isValid() )
    return true;

  Rect rclip = clip ? *clip : Rect();
  if( !_d->onwork->texture.isValid() )
  {
    _d->onwork->texture = pic;
    _d->onwork->clip = rclip;
  }

  bool batched = true;
  bool textureSwitched = _d->onwork->texture.texture() != pic.texture();
  bool clipSwitched = (_d->onwork->clip != rclip);

  if( textureSwitched || clipSwitched)
  {
    std::swap( _d->batched, _d->onwork );

    reset();
    _d->onwork->texture = pic;
    _d->onwork->clip = rclip;
    batched = false;
  }

  _d->onwork->srcrects.push_back( Rect( pic.originRect().lefttop() + srcrect.lefttop(), srcrect.size() ) );
  _d->onwork->dstrects.push_back( Rect( dstRect.lefttop() + Point( pic.offset().x(), -pic.offset().y() ), dstRect.size() ) );

  return batched;
}
bool SdlBatcher::append(const Picture &pic, const Rects &srcrects, const Rects &dstrects, Rect *clip)
{
  if( !pic.isValid() )
    return true;

  Rect rclip = clip ? *clip : Rect();
  if( !_d->onwork->texture.isValid() )
  {
    _d->onwork->texture = pic;
    _d->onwork->clip = rclip;
  }

  bool batched = true;
  bool textureSwitched = _d->onwork->texture.texture() != pic.texture();
  bool clipSwitched = (_d->onwork->clip != rclip);

  if( textureSwitched || clipSwitched)
  {
    std::swap( _d->batched, _d->onwork );

    reset();
    _d->onwork->texture = pic;
    _d->onwork->clip = rclip;
    batched = false;
  }

  _d->onwork->srcrects.insert( _d->onwork->srcrects.end(), srcrects.begin(), srcrects.end() );
  _d->onwork->dstrects.insert( _d->onwork->dstrects.end(), dstrects.begin(), dstrects.end() );

  return batched;
}
Beispiel #3
0
void SdlEngine::draw(const Picture &picture, const int dx, const int dy, Rect* clipRect )
{
  if( !picture.isValid() )
      return;

  int t = DateTime::elapsedTime();
  _d->drawCall++;

  if( clipRect != 0 )
  {
    SDL_Rect r = { clipRect->left(), clipRect->top(), clipRect->width(), clipRect->height() };
    SDL_RenderSetClipRect( _d->renderer, &r );
  }

  const Impl::MaskInfo& mask = _d->mask;
  SDL_Texture* ptx = picture.texture();
  const Rect& orect = picture.originRect();
  Size picSize = orect.size();
  const Point& offset = picture.offset();

  if( mask.enabled )
  {
    SDL_SetTextureColorMod( ptx, mask.red >> 16, mask.green >> 8, mask.blue );
    SDL_SetTextureAlphaMod( ptx, mask.alpha >> 24 );
  }
Beispiel #4
0
void Font::draw( Picture& dstpic, const std::string &text, const int dx, const int dy, bool useAlpha, bool updatextTx )
{
  if( !_d->ttfFont || !dstpic.isValid() )
    return;

#if defined(CAESARIA_PLATFORM_EMSCRIPTEN)
  SDL_Surface* sText = TTF_RenderText_Solid( _d->ttfFont, text.c_str(), _d->color );
#else
  SDL_Surface* sText = TTF_RenderUTF8_Blended( _d->ttfFont, text.c_str(), _d->color );
#endif

  if( sText )
  {

    if( useAlpha )
    {
#if SDL_MAJOR_VERSION>1
      SDL_SetSurfaceBlendMode( sText, SDL_BLENDMODE_NONE );
#else
      SDL_SetAlpha( sText, 0, 0 );
#endif
    }

    if( !dstpic.surface() )
    {
      Logger::warning("Font::draw dstpic surface is null");
      return;
    }

    SDL_Rect srcRect, dstRect;

    srcRect.x = 0;
    srcRect.y = 0;
    srcRect.w = sText->w;
    srcRect.h = sText->h;
    dstRect.x = dx;
    dstRect.y = dy;
    dstRect.w = sText->w;
    dstRect.h = sText->h;

    SDL_BlitSurface( sText, &srcRect, dstpic.surface(), &dstRect );
    SDL_FreeSurface( sText );
  }

  if( updatextTx )
    dstpic.update();
}       
Picture& PictureBank::getPicture(const std::string &name)
{
  const unsigned int hash = Hash( name );
  //Logger::warning( "PictureBank getpic " + name );

  Impl::ItPicture it = _d->resources.find( hash );
  if( it == _d->resources.end() )
  {
    //can't find image in valid resources, try load from hdd
    Picture pic = _d->tryLoadPicture( name );

    if( pic.isValid() ) { setPicture( name, pic );  }
    else{ _d->resources[ hash ] = pic; }

    return _d->resources[ hash ];
  }
  return it->second;
}
Beispiel #6
0
void Font::draw(Picture& dstpic, const std::string &text, const int dx, const int dy, bool useAlpha )
{
  if( !_d->ttfFont || !dstpic.isValid() )
    return;

  SDL_Surface* sText = TTF_RenderUTF8_Blended( _d->ttfFont, text.c_str(), _d->color );
  if( sText && useAlpha )
  {
    SDL_SetAlpha( sText, 0, 0 );
  }

  if( sText )
  {
    Picture pic;
    pic.init( sText, Point( 0, 0 ) );
    dstpic.draw( pic, dx, dy);
  }

  SDL_FreeSurface( sText );
}       
void RenderContext::load_image( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready )
{
  litehtml::tstring url;
  make_url(src, baseurl, url);
  if(m_images.find(url.c_str()) == m_images.end())
  {
    try
    {
      Picture img = get_image(url.c_str(), true);
      if(img.isValid())
      {
        m_images[url] = img;
      }
    }
    catch(...)
    {
      int iii=0;
      iii++;
    }
  }
}
Beispiel #8
0
bool C3Sav::Impl::loadCity( std::fstream& f, Game& game )
{
    uint32_t tmp;

    // need to rewrite better
    std::vector<short int> graphicGrid;
    graphicGrid.resize( 26244, 0 );
    std::vector<unsigned char> edgeGrid;
    edgeGrid.resize( 26244, 0 );
    std::vector<short int> terrainGrid;
    terrainGrid.resize( 26244, 0 );
    std::vector<unsigned char> rndmTerGrid;
    rndmTerGrid.resize(26244, 0);
    std::vector<unsigned char> randomGrid;
    randomGrid.resize( 26244, 0 );
    std::vector<unsigned char> zeroGrid;
    zeroGrid.resize( 26244, 0 );

    if( !f.is_open() )
    {
        Logger::warning( "GameLoaderC3Sav: can't open file " );
        return false;
    }

    f.read( (char*)&tmp, 4); // read dummy

    std::string cityName = LoaderHelper::getDefaultCityName( tmp );
    game.city()->setName( cityName );

    f.read((char*)&tmp, 4); // read scenario flag

    try
    {
        f.read((char*)&tmp, 4); // read length of compressed chunk
        Logger::warning( "GameLoaderC3Sav: length of compressed ids is %d", tmp );
        PKWareInputStream *pk = new PKWareInputStream(&f, false, tmp);
        for (int i = 0; i < 162 * 162; i++)
        {
            graphicGrid[i] = pk->readShort();
        }
        pk->empty();
        delete pk;

        f.read((char*)&tmp, 4); // read length of compressed chunk
        Logger::warning( "GameLoaderC3Sav: length of compressed egdes is %d", tmp );
        pk = new PKWareInputStream(&f, false, tmp);
        for (int i = 0; i < 162 * 162; i++)
        {
            edgeGrid[i] = pk->readByte();
        }
        pk->empty();
        delete pk;

        SkipCompressed(f); // skip building ids

        f.read((char*)&tmp, 4); // read length of compressed chunk
        Logger::warning( "GameLoaderC3Sav: length of compressed terraindata is %d", tmp );
        pk = new PKWareInputStream(&f, false, tmp);
        for (int i = 0; i < 162 * 162; i++)
        {
            terrainGrid[i] = pk->readShort();
        }
        pk->empty();
        delete pk;

        SkipCompressed(f);
        SkipCompressed(f);
        SkipCompressed(f);
        SkipCompressed(f);

        f.read((char*)&randomGrid[0], 26244);

        SkipCompressed(f);
        SkipCompressed(f);
        SkipCompressed(f);
        SkipCompressed(f);
        SkipCompressed(f);

        // here goes walkers array
        f.read((char*)&tmp, 4); // read length of compressed chunk
        Logger::warning( "GameLoaderC3Sav: length of compressed walkers data is %d", tmp );
        pk = new PKWareInputStream(&f, false, tmp);
        for (int j = 0; j < 1000; j++)
        {
            pk->skip(10);
            pk->readShort();
            pk->skip(8);
            pk->readByte();
            pk->readByte();
            pk->skip(106);
        }
        pk->empty();
        delete pk;
        int length;
        f.read((char*)&length, 4); // read next length :-)

        if (length <= 0)
            f.seekg(1200, std::ios::cur);
        else
            f.seekg(length, std::ios::cur);

        SkipCompressed(f);
        SkipCompressed(f);

        // 3x int
        f.read((char*)&tmp, 4);
        f.read((char*)&tmp, 4);
        f.read((char*)&tmp, 4);
        SkipCompressed(f);
        f.seekg(70, std::ios::cur);
        SkipCompressed(f); // skip building list
        f.seekg(208, std::ios::cur);
        SkipCompressed(f); // skip unknown
        f.seekg(788, std::ios::cur); // skip unused data
        f.read((char*)&tmp, 4); //mapsize

        int size = tmp;
        PlayerCityPtr oCity = game.city();
        Tilemap& oTilemap = oCity->tilemap();

        oCity->resize(size);
        oCity->setCameraPos( TilePos( 0, 0 ) );

        initEntryExit( f, game.city() );

        f.seekg(1312, std::ios::cur);
        char climate;
        f.read(&climate, 1);
        oCity->setClimate((ClimateType)climate);

        // here goes the WORK!


        // loads the graphics map
        int border_size = (162 - size) / 2;

        std::map< int, std::map< int, unsigned char > > edgeData;

        game.city()->setCameraPos( TilePos( size/2, size/2 ) );

        for (int itA = 0; itA < size; ++itA)
        {
            for (int itB = 0; itB < size; ++itB)
            {
                int i = itB;
                int j = size - itA - 1;

                int index = 162 * (border_size + itA) + border_size + itB;

                Tile& tile = oTilemap.at(i, j);

                unsigned int imgId = graphicGrid[index];
                Picture pic = imgid::toPicture( imgId );

                if( pic.isValid() )
                {
                    tile.setPicture( pic );
                    tile.setOriginalImgId( imgId );
                }
                else
                {
                    TileOverlay::Type ovType = LoaderHelper::convImgId2ovrType( imgId );
                    if( ovType == constants::objects::unknown )
                    {
                        Logger::warning( "!!! GameLoaderC3Sav: Unknown building %x at [%d,%d]", imgId, i, j );
                    }

                    baseBuildings[ tile.pos() ] = imgId;
                    pic = Picture::load( ResourceGroup::land1a, 230 + math::random( 57 ) );
                    tile.setPicture( pic );
                    tile.setOriginalImgId( imgid::fromResource( pic.name() ) );
                }

                edgeData[ i ][ j ] = edgeGrid[index];
                tile::decode( tile, terrainGrid[index] );
                tile::fixPlateauFlags( tile );
            }
        }

        for (int i = 0; i < size; ++i)
        {
            for (int j = 0; j < size; ++j)
            {
                unsigned char ed = edgeData[ i][ j ];
                if( ed == 0x00)
                {
                    int size = 1;

                    {
                        int dj;
                        try
                        {
                            // find size, 5 is maximal size for building
                            for (dj = 0; dj < 5; ++dj)
                            {
                                int edd = edgeData[ i ][ j - dj ];
                                // find bottom left corner
                                if (edd == 8 * dj + 0x40)
                                {
                                    size = dj + 1;
                                    break;
                                }
                            }
                        }
                        catch(...)
                        {
                            size = dj + 1;
                        }
                    }

                    Tile& master = oTilemap.at(i, j - size + 1);

                    //Logger::warning( "Master will be at (%d,%d)", master.i(), master.j() );
                    for (int di = 0; di < size; ++di)
                    {
                        for (int dj = 0; dj < size; ++dj)
                        {
                            oTilemap.at(master.i() + di, master.j() + dj).setMasterTile(&master);
                        }
                    }
                }

                // Check if it is building and type of building
                //if (ttile.getMasterTile() == NULL)
                std::map<TilePos, unsigned int>::iterator bbIt = baseBuildings.find( TilePos( i, j ) );
                unsigned int bbImgId = bbIt == baseBuildings.end() ? 0 : bbIt->second;

                Tile& tile = oTilemap.at( i, j );
                Tile* masterTile = tile.masterTile();
                if( !masterTile )
                    masterTile = &tile;

                if( masterTile->overlay().isNull() )
                {
                    LoaderHelper::decodeTerrain( *masterTile, oCity, bbImgId );
                }
            }
        }

    }
    catch(PKException)
    {
        THROW("fatal error when unpacking");
    }

    return true;
}
Beispiel #9
0
void Decorator::drawLine( Picture& dstpic, const Point& p1, const Point& p2, NColor color)
{
  if( dstpic.isValid() )
    lineColor( dstpic.surface(), p1.x(), p1.y(), p2.x(), p2.y(), color.rgba() );
}