Caching images

Hi

I have a problem getting the SDL_Surfaces work as I want them. I need to
cache some images and therefore needs to store some SDL_Surfaces for later
use. The problem is this, I first create some surfaces using
SDL_DisplayFormat and then set a entry in the cache to this value
(something like cache[i].image = image, where cache[i].image and image
are SDL_Surface *). When I want to draw something from the cache later all
images are the same.
Is it not possible to store a lot of SDL_Surfaces or do I need to do
something special in order to not overwrite the previos (not
freed) surfaces ?

I’ve done something similar before. I think your problem might be that by
using the straight ‘=’ here between two pointers, you’re in effect making
’cache[i].image’ an alias for ‘image’, therefore when ‘image’ is freed or
changed, so is ‘cache[i].image’ because they point to the same memory
location. You would want create a new surface for ‘cache[i].image’ and copy
the memory contents from ‘image’ over to ‘cache[i].image’.
I dunno, I might be way off here. If I had a bit more code, I might have a
better idea of what you’re trying to accomplish.

Dave “PenguinDude” Archbold
davea at radiks.net (home)
@Archbold_David_W (work)> -----Original Message-----

From: Tim Johansson [SMTP:tim at pellefant.campus.luth.se]
Sent: Thursday, July 27, 2000 6:15 AM
To: sdl at lokigames.com
Subject: [SDL] Caching images

Hi

I have a problem getting the SDL_Surfaces work as I want them. I need to
cache some images and therefore needs to store some SDL_Surfaces for later
use. The problem is this, I first create some surfaces using
SDL_DisplayFormat and then set a entry in the cache to this value
(something like cache[i].image = image, where cache[i].image and image
are SDL_Surface *). When I want to draw something from the cache later all
images are the same.
Is it not possible to store a lot of SDL_Surfaces or do I need to do
something special in order to not overwrite the previos (not
freed) surfaces ?

The problem is this, I first create some surfaces using
SDL_DisplayFormat and then set a entry in the cache to this value
(something like cache[i].image = image, where cache[i].image and image
are SDL_Surface *). When I want to draw something from the cache later all
images are the same.

You are doing something wrong. Remember that SDL_DisplayFormat() creates
a copy of the surface you give to it

Is it not possible to store a lot of SDL_Surfaces or do I need to do
something special in order to not overwrite the previos (not
freed) surfaces ?

nothing special, except writing bug-free code :slight_smile: