Discussion:
SDL_Surface & malloc
Marcos Prieto Gonzalez
2003-02-22 22:12:48 UTC
Permalink
Hi, i´m triying to develop a tile system for a game. I start with a
example program that display some random tiles on the screen. I don´t
know if use a surface for each tile is the corret way...

I have a compiler error here ( i ´m a C newbie)

//Declarations
SDL_Surface *tiles, *screen;
...

//Dinamic Memory for the array of surfaces
n_tiles = 4;
tiles = (SDL_Surface *) malloc (n_tiles * sizeof(SDL_Surface));
if(tiles == NULL){ printf("Error de memoria\n"); return -1; }
...

//Images load
for(i = 0; i <= n_tiles; i++){
sprintf(ruta,"%s%d%s","tiles",i,".png");
printf("%s", ruta);
tiles[i] = SDL_LoadBMP(ruta); /*In this line the compiler says
uncompatible types assignament, with &tiles[i] = ... i have an error
too.*/
if(&tiles[i]==NULL) printf("Error al abrir una imagen\n");
}

what ´s the problem? Thanks.

(Sorry for my english, i hope you can understand me)

---Publicidad--------------------------------------------------------
Juega con Ventura24.es, lotería inteligente y multiplica tus
posibilidades!! http://www.iespana.es/_reloc/email.ventura
Marcos
2003-02-22 23:28:03 UTC
Permalink
Hi, i´m triying to develop a tile system for a game. I start with a
example program that display some random tiles on the screen. I don´t
know if use a surface for each tile is the corret way...

I have a compiler error here ( i ´m a C newbie)

//Declarations
SDL_Surface *tiles, *screen;
...

//Dinamic Memory for the array of surfaces
n_tiles = 4;
tiles = (SDL_Surface *) malloc (n_tiles * sizeof(SDL_Surface));
if(tiles == NULL){ printf("Error de memoria\n"); return -1; }
...

//Images load
for(i = 0; i <= n_tiles; i++){
sprintf(ruta,"%s%d%s","tiles",i,".png");
printf("%s", ruta);
tiles[i] = SDL_LoadBMP(ruta); /*In this line the compiler says
uncompatible types assignament, with &tiles[i] = ... i have an error
too.*/
if(&tiles[i]==NULL) printf("Error al abrir una imagen\n");
}

what ´s the problem? Thanks.

(Sorry for my english, i hope you can understand me)


---Publicidad--------------------------------------------------------
Únete a los miles de sin pareja en Meetic... ¡te vas a enamorar!
http://www.iespana.es/_reloc/email.meetic
steve
2003-02-23 00:21:08 UTC
Permalink
if(&tiles[i]==NULL) printf("Error al abrir una imagen\n");

should read if(tiles[i] == NULL) { ... }

Best place to read up on this is in the C Faq (Steve Summit)
Sami Näätänen
2003-02-23 00:33:41 UTC
Permalink
Post by Marcos Prieto Gonzalez
Hi, i´m triying to develop a tile system for a game. I start with a
example program that display some random tiles on the screen. I don´t
know if use a surface for each tile is the corret way...
I have a compiler error here ( i ´m a C newbie)
//Declarations
SDL_Surface *tiles, *screen;
...
//Dinamic Memory for the array of surfaces
n_tiles = 4;
tiles = (SDL_Surface *) malloc (n_tiles * sizeof(SDL_Surface));
if(tiles == NULL){ printf("Error de memoria\n"); return -1; }
...
//Images load
for(i = 0; i <= n_tiles; i++){
sprintf(ruta,"%s%d%s","tiles",i,".png");
printf("%s", ruta);
tiles[i] = SDL_LoadBMP(ruta); /*In this line the compiler says
uncompatible types assignament, with &tiles[i] = ... i have an error
too.*/
if(&tiles[i]==NULL) printf("Error al abrir una imagen\n");
}
what ´s the problem? Thanks.
Looks like your tiles variable is pointer to SDL_Surface instead being a
pointer to pointer to SDL_Surface. So the error is correct, because you
are trying to set in SDL_Surface struct value that is pointer to
SDL_Surface struct, instead being SDL_Surface.

So use one * more in declaration and casting of the malloc.

PS. sprintf(ruta,"tiles%d.png",i); is litle bit more readable than
sprintf(ruta,"%s%d%s","tiles",i,".png");
Marcos
2003-01-23 00:46:35 UTC
Permalink
Two times? Sorry.
----- Original Message -----
From: "Marcos" <***@iespana.es>
To: <***@libsdl.org>
Sent: Sunday, February 23, 2003 12:28 AM
Subject: [SDL] SDL_Surface & malloc
Post by Marcos Prieto Gonzalez
Hi, i´m triying to develop a tile system for a game. I start with a
example program that display some random tiles on the screen. I don´t
know if use a surface for each tile is the corret way...
I have a compiler error here ( i ´m a C newbie)
//Declarations
SDL_Surface *tiles, *screen;
...
//Dinamic Memory for the array of surfaces
n_tiles = 4;
tiles = (SDL_Surface *) malloc (n_tiles * sizeof(SDL_Surface));
if(tiles == NULL){ printf("Error de memoria\n"); return -1; }
...
//Images load
for(i = 0; i <= n_tiles; i++){
sprintf(ruta,"%s%d%s","tiles",i,".png");
printf("%s", ruta);
tiles[i] = SDL_LoadBMP(ruta); /*In this line the compiler says
uncompatible types assignament, with &tiles[i] = ... i have an error
too.*/
if(&tiles[i]==NULL) printf("Error al abrir una imagen\n");
}
what ´s the problem? Thanks.
(Sorry for my english, i hope you can understand me)
---Publicidad--------------------------------------------------------
Únete a los miles de sin pareja en Meetic... ¡te vas a enamorar!
http://www.iespana.es/_reloc/email.meetic
_______________________________________________
SDL mailing list
http://www.libsdl.org/mailman/listinfo/sdl
---Publicidad--------------------------------------------------------
Únete a los miles de sin pareja en Meetic... ¡te vas a enamorar!
http://www.iespana.es/_reloc/email.meetic
---Publicidad--------------------------------------------------------
Únete a los miles de sin pareja en Meetic... ¡te vas a enamorar!
http://www.iespana.es/_reloc/email.meetic
Max Watson
2003-02-23 08:15:02 UTC
Permalink
Post by Marcos Prieto Gonzalez
Hi, i´m triying to develop a tile system for a game. I start with a
example program that display some random tiles on the screen. I don´t
know if use a surface for each tile is the corret way...
I have a compiler error here ( i ´m a C newbie)
Post by Marcos Prieto Gonzalez
//Declarations
SDL_Surface *tiles, *screen;
This is just declaring a pointer to an SDL_Surface. If you want a dynamic
array of surfaces, it should be:
SDL_Surface **tiles;
Post by Marcos Prieto Gonzalez
Post by Marcos Prieto Gonzalez
//Dinamic Memory for the array of surfaces
n_tiles = 4;
tiles = (SDL_Surface *) malloc (n_tiles * sizeof(SDL_Surface));
This bit here should allocate memory for the *pointers* to your surfaces, not
actual memory for the surfaces themselves. The SDL_LoadBMP later will
allocate the memory for the actual surface. Try:
tiles = (SDL_Surface **) malloc (n_tiles * sizeof(SDL_Surface*));

Don't worry, it takes some practice to get the hang of pointers in C. Good
luck.

Max Watson <***@blackholesun.com>

Loading...