[SDL] SDL_CreateRGBSurface

Edward Byard e_byard at yahoo.co.uk
Mon Sep 4 08:50:48 PDT 2006


Hello,

Has anyone got any examples of this in use they can share? I'm having problems with this - I don't understand how when you create a new struct of type Tiles, I can't get it working when passed to Load_Tiles...what am I doing wrong?

Tiles **Images;

Load_Tiles("filename.png",Images,xsize,ysize);

Help!!! I know it's a dumb coders question but I never have cause to use this much indirection!!!

Thanks
Ed


Torsten Giebl <wizard at syntheticsw.com> wrote: Hello !


> I have used two methods to splilt a large image loaded from disk(whith
> sprite images) into various frames of my SDL_Surface frames array. The
> first one I create a empty surface whith SDL_CreateRGBSurface whith
> dimensions of frames. Then i blit the large image with the calculated
> offsets in this image, after this i use SDL_Display format to past the
> image for a element of my array. The second is to load a template image
> with frames dimensions from disk, blit the larger image, then past it for
> my frames array.
>
> The first method, using SDL_CreateRBGSurface slow down the animation,
> why?


Look at the attached files.



CU


//
//    "$Id: tiles.cxx,v 1.5 2006-06-23 16:49:38 wizard Exp $"
//    
//    Turricane 4 Ever - New Leveleditor
//    Copyright (C) 2006  The T4E Crew & Synthetic Software Products
//
//    This program is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Library General Public
//    License as published by the Free Software Foundation; either
//    version 2 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Library General Public License for more details.
//
//    You should have received a copy of the GNU Library General Public
//    License along with this library; if not, write to the Free
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//    The T4E Crew
//    ( info at turricane.net )
//
//    Synthetic Software Products
//    ( info at syntheticsw.com )
//
//    General Information
//    ( info at turricane.net )
//


#include "tiles.h"


bool Load_Tiles  (char *Filename, Tiles **TILES_main,
     int Tile_Size_X, int Tile_Size_Y)
{
    bool status  = false;

    SDL_Surface *temp = NULL;
    
    SDL_Rect tile_rect;

    Uint32 index_tile = 0, index_x = 0, index_y = 0;
    Uint32 max_nr_tiles = 0, max_nr_tiles_x = 0, max_nr_tiles_y = 0;
    Uint32 r_mask = 0, g_mask = 0, b_mask = 0, a_mask = 0;

    if ( (*TILES_main) != NULL )       return status;
    if ( strlen (Filename) == 0 )      return status;
    if ( (Tile_Size_X == 0) || (Tile_Size_Y == 0) ) return status;

#if SDL_BYTEORDER == SDL_BIGENDIAN
    r_mask  = 0xff000000;
    g_mask  = 0x00ff0000;
    b_mask  = 0x0000ff00;
    a_mask  = 0x000000ff;
#else
    r_mask  = 0x000000ff;
    g_mask  = 0x0000ff00;
    b_mask  = 0x00ff0000;
    a_mask  = 0xff000000;
#endif

    temp    = SDL_LoadBMP (Filename);
    if (! temp) return status;

    if ( (! temp -> w) || (! temp -> h) ) return status;

    index_tile = 1;

    max_nr_tiles_x = (Uint32) ((temp -> w)    / Tile_Size_X);
    max_nr_tiles_y = (Uint32) ((temp -> h)    / Tile_Size_Y);
    
    if ( ((temp -> w) % Tile_Size_X) > 0 )
    {
 max_nr_tiles_x ++;
    }

    if ( ((temp -> h) % Tile_Size_Y) > 0 )
    {
 max_nr_tiles_y ++;
    }

    max_nr_tiles  = max_nr_tiles_x * max_nr_tiles_y;
    max_nr_tiles ++;

    (*TILES_main)      = new Tiles;   
    (*TILES_main) -> Nr_Tiles     = max_nr_tiles;
    
    (*TILES_main) -> Tile_Size_X    = Tile_Size_X;
    (*TILES_main) -> Tile_Size_Y    = Tile_Size_Y;

    (*TILES_main) -> Nr_Tiles_X     = max_nr_tiles_x;
    (*TILES_main) -> Nr_Tiles_Y     = max_nr_tiles_y;

    (*TILES_main) -> Orig_Width     = temp -> w;
    (*TILES_main) -> Orig_Height    = temp -> h;

    (*TILES_main) -> Tiles     = new SDL_Surface *[max_nr_tiles];

    (*TILES_main) -> Tiles  [0]     = NULL;
    
    for (index_y = 0; 
     index_y < (temp -> h); 
  index_y = index_y + ((*TILES_main) -> Tile_Size_Y))
    {
 for (index_x = 0; 
  index_x < (temp -> w); 
      index_x = index_x + ((*TILES_main) -> Tile_Size_X))
 {
     (*TILES_main) -> Tiles [index_tile] =
  SDL_CreateRGBSurface ( SDL_SWSURFACE, 
     (*TILES_main) -> Tile_Size_X,
     (*TILES_main) -> Tile_Size_Y,
     32,
     r_mask,
     g_mask,
     b_mask,
     a_mask    );
     
     tile_rect.x = index_x;
     tile_rect.y = index_y;
     tile_rect.w = (*TILES_main) -> Tile_Size_X;
     tile_rect.h = (*TILES_main) -> Tile_Size_Y;

     SDL_BlitSurface ( temp, & tile_rect, 
    (*TILES_main) -> Tiles [index_tile], NULL);

     index_tile ++;
 }
    }
    
    SDL_FreeSurface (temp);
    
    status = true;

    return status;
}

bool Delete_Tiles (Tiles **TILES_main)
{
    bool status  = false;

    Uint32 index_tile = 0;

    if ( (*TILES_main) == NULL )    return status;
 
    for (index_tile = 0;
     index_tile < (*TILES_main) -> Nr_Tiles;
         index_tile ++)
    {
 if ( (*TILES_main) -> Tiles [index_tile] != NULL )
 {
     SDL_FreeSurface ( (*TILES_main) -> Tiles [index_tile] );
     (*TILES_main) -> Tiles [index_tile] = NULL;
 }
    }

    delete[]     (*TILES_main)   -> Tiles;
    delete     (*TILES_main);
    (*TILES_main)      = NULL;

    status = true;

    return status;
}


//
// End of "$Id: tiles.cxx,v 1.5 2006-06-23 16:49:38 wizard Exp $".
//
//
//    "$Id: tiles.h,v 1.5 2006-06-23 16:49:38 wizard Exp $"
//
//    Turricane 4 Ever - New Leveleditor
//    Copyright (C) 2006  The T4E Crew & Synthetic Software Products
//
//    This program is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Library General Public
//    License as published by the Free Software Foundation; either
//    version 2 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Library General Public License for more details.
//
//    You should have received a copy of the GNU Library General Public
//    License along with this library; if not, write to the Free
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//    The T4E Crew
//    ( info at turricane.net )
//
//    Synthetic Software Products
//    ( info at syntheticsw.com )
//
//    General Information
//    ( info at turricane.net )
//


#ifndef _TILES_H
#define _TILES_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include 
#include 
#include "SDL.h"


typedef struct
{
    SDL_Surface **Tiles;

    Uint32 Nr_Tiles;

    Uint32 Tile_Size_X;
    Uint32 Tile_Size_Y;

    Uint32 Nr_Tiles_X;
    Uint32 Nr_Tiles_Y;

    Uint32 Orig_Width;
    Uint32 Orig_Height;
}
Tiles;

extern bool Load_Tiles     (char *Filename, Tiles **TILES_main,
     int Tile_Size_X, int Tile_Size_Y);

extern bool Delete_Tiles    (Tiles **TILES_main);


#endif     /* _TILES_H */


//
// End of "$Id: tiles.h,v 1.5 2006-06-23 16:49:38 wizard Exp $".
//
_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


 		
---------------------------------
 Yahoo! Messenger - with free PC-PC calling and photo sharing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060904/5a7d718e/attachment-0007.htm>


More information about the SDL mailing list