void
TileDescription::load(TileFactory* factory)
{  
  if (debug)
    std::cout << "Loading tiles: " << filename << std::endl;

  SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
  if(!image) 
    {
      std::ostringstream msg;
      msg << "Couldn't load image '" << filename << "': " << SDL_GetError();
      throw std::runtime_error(msg.str());
    }
  else
    {
      try 
        {
          int num_tiles = width * height; //(image->w/TILE_RESOLUTION) * (image->h/TILE_RESOLUTION);
          if (int(colmap.size()) != num_tiles)
            {
              std::ostringstream str;
              str << "'colmap' information and num_tiles mismatch (" 
                  << colmap.size() << " != " << num_tiles << ") for image '" << filename << "'";
              throw std::runtime_error(str.str());
            }

          if (int(ids.size()) != num_tiles)
            {
              std::ostringstream str;
              str << "'ids' information and num_tiles mismatch (" 
                  << ids.size() << " != " << num_tiles << ") for image '" << filename << "'";
              throw std::runtime_error(str.str());
            }
    
          int i = 0;
          for (int y = 0; y < height*TILE_RESOLUTION; y += TILE_RESOLUTION)
            {
              for (int x = 0; x < width*TILE_RESOLUTION; x += TILE_RESOLUTION)
                {
                  if(ids[i] != -1)
                    {
                      factory->pack(ids[i], colmap[i], image,
                                    Rect(x, y, x+TILE_RESOLUTION, y+TILE_RESOLUTION));
                    }

                  i += 1; 
                }
            }
        } 
      catch(...) 
        {
          SDL_FreeSurface(image);
          throw;
        }
      SDL_FreeSurface(image);
    }
}
TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
{
  SDL_Surface *image = nullptr;

  Surfaces::iterator i = m_surfaces.find(filename);
  if (i != m_surfaces.end())
  {
    image = i->second;
  }
  else
  {
    image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
    if (!image)
    {
      std::ostringstream msg;
      msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
      throw std::runtime_error(msg.str());
    }

    m_surfaces[filename] = image;
  }

  SDL_PixelFormat* format = image->format;
  if(format->Rmask == 0 && format->Gmask == 0 && format->Bmask == 0 && format->Amask == 0) {
    log_debug << "Wrong surface format for image " << filename << ". Compensating." << std::endl;
    image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGBA8888, 0);
  }

  SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
                                                  rect.top * image->pitch +
                                                  rect.left * image->format->BytesPerPixel,
                                                  rect.get_width(), rect.get_height(),
                                                  image->format->BitsPerPixel,
                                                  image->pitch,
                                                  image->format->Rmask,
                                                  image->format->Gmask,
                                                  image->format->Bmask,
                                                  image->format->Amask));
  if (!subimage)
  {
    throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
  }

#ifdef OLD_SDL
  if (image->format->palette)
  { // copy the image palette to subimage if present
    SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
  }
#endif

  return VideoSystem::current()->new_texture(subimage.get());
}
TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename)
{
  SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
  if (!image)
  {
    std::ostringstream msg;
    msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
    throw std::runtime_error(msg.str());
  }
  else
  {
    TexturePtr texture = VideoSystem::current()->new_texture(image.get());
    image.reset(NULL);
    return texture;
  }
}
ImageTexture*
TextureManager::create_image_texture(const std::string& filename)
{
  SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
  if(image == NULL) {
    std::ostringstream msg;
    msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
    throw std::runtime_error(msg.str());
  }

  int texture_w = next_power_of_two(image->w);
  int texture_h = next_power_of_two(image->h);

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
      texture_w, texture_h, 32,
      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
  SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
      texture_w, texture_h, 32,
      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif

  if(convert == 0)
    throw std::runtime_error("Couldn't create texture: out of memory");

  SDL_SetAlpha(image, 0, 0);
  SDL_BlitSurface(image, 0, convert, 0);

  ImageTexture* result = NULL;
  try {
    result = new ImageTexture(convert);
    result->filename = filename;
    result->image_width = image->w;
    result->image_height = image->h;
  } catch(...) {
    delete result;
    SDL_FreeSurface(convert);
    throw;
  }

  SDL_FreeSurface(convert);
  return result;
}
Beispiel #5
0
void
Main::init_video()
{
  SDL_SetWindowTitle(VideoSystem::current()->get_renderer().get_window(), PACKAGE_NAME " " PACKAGE_VERSION);

  const char* icon_fname = "images/engine/icons/supertux-256x256.png";
  SDL_Surface* icon = IMG_Load_RW(get_physfs_SDLRWops(icon_fname), true);
  if (!icon)
  {
    log_warning << "Couldn't load icon '" << icon_fname << "': " << SDL_GetError() << std::endl;
  }
  else
  {
    SDL_SetWindowIcon(VideoSystem::current()->get_renderer().get_window(), icon);
    SDL_FreeSurface(icon);
  }
  SDL_ShowCursor(0);

  log_info << (g_config->use_fullscreen?"fullscreen ":"window ")
           << " Window: "     << g_config->window_size
           << " Fullscreen: " << g_config->fullscreen_size << "@" << g_config->fullscreen_refresh_rate
           << " Area: "       << g_config->aspect_size << std::endl;
}
void 
Font::loadFontSurface(
  const std::string &glyphimage,
  const std::string &shadowimage,
  const std::vector<std::string> &chars,
  GlyphWidth glyph_width,
  int char_width
  )
{
  SurfacePtr glyph_surface  = Surface::create("images/engine/fonts/" + glyphimage);
  SurfacePtr shadow_surface = Surface::create("images/engine/fonts/" + shadowimage);

  int surface_idx = glyph_surfaces.size();
  glyph_surfaces.push_back(glyph_surface);
  shadow_surfaces.push_back(shadow_surface);

  int row=0, col=0;
  int wrap = glyph_surface->get_width() / char_width;
 
  SDL_Surface *surface = NULL;
  
  if( glyph_width == VARIABLE ) {
    //this does not work:
    // surface = ((SDL::Texture *)glyph_surface.get_texture())->get_texture();
    surface = IMG_Load_RW(get_physfs_SDLRWops("images/engine/fonts/"+glyphimage), 1);
    if(surface == NULL) {
      std::ostringstream msg;
      msg << "Couldn't load image '" << glyphimage << "' :" << SDL_GetError();
      throw std::runtime_error(msg.str());
    }
    SDL_LockSurface(surface);
  }

  for( unsigned int i = 0; i < chars.size(); i++) {
    for(UTF8Iterator chr(chars[i]); !chr.done(); ++chr) {
      int y = row * char_height;
      int x = col * char_width;
      if( ++col == wrap ) { col=0; row++; }
      if( *chr == 0x0020 && glyphs[0x20].surface_idx != -1) continue;
        
      Glyph glyph;
      glyph.surface_idx   = surface_idx;
      
      if( glyph_width == FIXED ) 
      {
        glyph.rect    = Rectf(x, y, x + char_width, y + char_height);
        glyph.offset  = Vector(0, 0);
        glyph.advance = char_width;
      }
      else 
      {
        int left = x;
        while (left < x + char_width && vline_empty(surface, left, y, y + char_height, 64))
          left += 1;
        int right = x + char_width - 1;
        while (right > left && vline_empty(surface, right, y, y + char_height, 64))
          right -= 1;
          
        if (left <= right) 
        {
          glyph.offset  = Vector(x-left, 0);
          glyph.advance = right - left + 1 + 1; // FIXME: might be useful to make spacing configurable
        } 
        else 
        { // glyph is completly transparent
          glyph.offset  = Vector(0, 0);
          glyph.advance = char_width + 1; // FIXME: might be useful to make spacing configurable
        }

        glyph.rect = Rectf(x,  y, x + char_width, y + char_height);
      }

      glyphs[*chr] = glyph;
    }
    if( col>0 && col <= wrap ) { 
      col = 0;
      row++;
    }
  }
  
  if( surface != NULL ) {
    SDL_UnlockSurface(surface);
    SDL_FreeSurface(surface);
  }
}
Beispiel #7
0
void init_video()
{
  if(texture_manager != NULL)
    texture_manager->save_textures();
  
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  
  int flags = SDL_OPENGL;
  if(config->use_fullscreen)
    flags |= SDL_FULLSCREEN;
  int width = config->screenwidth;
  int height = config->screenheight;
  int bpp = 0;

  screen = SDL_SetVideoMode(width, height, bpp, flags);
  if(screen == 0) {
    std::stringstream msg;
    msg << "Couldn't set video mode (" << width << "x" << height
        << "-" << bpp << "bpp): " << SDL_GetError();
    throw std::runtime_error(msg.str());
  }

  SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);

  // set icon
  SDL_Surface* icon = IMG_Load_RW(
      get_physfs_SDLRWops("images/engine/icons/supertux.xpm"), true);
  if(icon != 0) {
    SDL_WM_SetIcon(icon, 0);
    SDL_FreeSurface(icon);
  }
#ifdef DEBUG
  else {
    log_warning << "Couldn't find icon 'images/engine/icons/supertux.xpm'" << std::endl;
  }
#endif

  // setup opengl state and transform
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_CULL_FACE);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glViewport(0, 0, screen->w, screen->h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  // logical resolution here not real monitor resolution
  glOrtho(0, 800, 600, 0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0, 0, 0);

  check_gl_error("Setting up view matrices");

  if(texture_manager != NULL)
    texture_manager->reload_textures();
  else
    texture_manager = new TextureManager();
}