Sign in to follow this  
Afr0m@n

Putting data into arrays?

Recommended Posts

Ok, I'm trying to accomplish animation in SDL, and I need to load a bunch of images into an array. How do I do that? Here's what I'm doing so far:
SDL_Surface *player_imgs[7];
player_imgs[0] = SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp");
This gives me these errors:
--------------------Configuration: SDL DrawImage test - Win32 Debug--------------------
Compiling...
main.cpp
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(20) : error C2466: cannot allocate an array of constant size 0
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(20) : error C2234: '<Unknown>' : arrays of references are illegal
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(20) : error C2501: 'player_imgs' : missing storage-class or type specifiers
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(20) : error C2440: 'initializing' : cannot convert from 'struct SDL_Surface *' to 'int *[]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(93) : warning C4018: '>' : signed/unsigned mismatch
Error executing cl.exe.

SDL DrawImage test.exe - 4 error(s), 1 warning(s)

What's wrong? :S

Share this post


Link to post
Share on other sites
If I had to guess, I'd say that the error actually occurs before the line where you define the player_imgs array, and the error propogation just made it seem like the error is on that line. Try posting a more complete source dump.

Share this post


Link to post
Share on other sites
I think that you have to use the "new" keyword to create the objects. I'm not familiar with SDL, but it may be something like:

player_imgs[0] = new SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp");

Share this post


Link to post
Share on other sites
Quote:
Original post by TyroWorks
I think that you have to use the "new" keyword to create the objects. I'm not familiar with SDL, but it may be something like:

player_imgs[0] = new SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp");


The documentation counters with:
Quote:
SDL_LoadBMP
Return Value
Returns the new surface, or NULL if there was an error.



jfl.

Share this post


Link to post
Share on other sites
yea in SDL you can use the keyword new.

i havnt exactly tried doing animations yet but

ik beleive that sprites have to places into a rect because they need to be clipped and such

try

SDL_Rect animation[4];

then just do

animation[0] = SDL_LoadBMP("image.bmp");
animation[1] = SDL_LoadBMP("image2.bmp");
ect..

and im sure you know blitting

//edit

here look at this it may help

http://lazyfooproductions.com/SDL_tutorials/lesson06/index.php

Share this post


Link to post
Share on other sites
Quote:
Original post by willthiswork89
yea in SDL you can use the keyword new.

i havnt exactly tried doing animations yet but

ik beleive that sprites have to places into a rect because they need to be clipped and such

try

SDL_Rect animation[4];

then just do

animation[0] = SDL_LoadBMP("image.bmp");
animation[1] = SDL_LoadBMP("image2.bmp");
ect..

and im sure you know blitting

//edit

here look at this it may help

http://lazyfooproductions.com/SDL_tutorials/lesson06/index.php



nope, they should be SDL_Surface pointers.
SDL handles Clipping itself.

SiCrane is probably right, it is more likely an error on the line before it thats confusing the compiler.

Share this post


Link to post
Share on other sites
I suspect you're actually trying to make the assignment at global scope rather than in a function. Variable definitions and variable definitions with assignment are legal at global scope. Assignments are not.
SDL_Surface *player_imgs1[7]; // OK, variable definition
SDL_Surface *player_imgs2[7] = {SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp")}; // OK, variable definition with initialisation
player_imgs1[0] = SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp"); // ERROR, attempted assignment at global scope

void function()
{
player_imgs1[0] = SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp"); // OK, assignment within a function
}


Enigma

Share this post


Link to post
Share on other sites
Ok tahnks guys! Sorry for bieng so slow to rsepond, but I'm a little drunk.

None of your tips has wokrked out so far, but TyroWorks' tip decreases the number of errors to 3 in all;

--------------------Configuration: SDL DrawImage test - Win32 Debug--------------------
Compiling...
main.cpp
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(21) : error C2501: 'player_imgs' : missing storage-class or type specifiers
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(21) : error C2371: 'player_imgs' : redefinition; different basic types
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(19) : see declaration of 'player_imgs'
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(21) : error C2440: 'initializing' : cannot convert from 'struct SDL_Surface ** ' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(94) : warning C4018: '>' : signed/unsigned mismatch
Error executing cl.exe.

SDL DrawImage test.exe - 3 error(s), 1 warning(s)



Here's the whole code.. I don't think there's anything wrong on the line above:

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <windows.h>
#include "SDLWrapper_Memory.h"
#include "list.h"

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;
SDL_Event event;

linkedList *EnemyList;
int timer;

//These are used to animate the playership.
int frame_timer = SDL_GetTicks();
int frame_count = 0;
SDL_Surface player_imgs;
//player_imgs[0] = SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp");
player_imgs = new SDL_Surface *[7];

BITFIELD_CREATE(KeyboardDown, SDLK_LAST);

void DrawImage(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

bool KeyDown(uint32 uiAscii)
{
return (BITFIELD_GET(KeyboardDown, uiAscii) != 0);
}

///////////////////////////// GAME STRUCTS ///////////////////////////////////////
typedef struct _PLAYER
{
int ypos;
int xpos;
SDL_Surface *img;
} player;

typedef struct _ENEMY
{
int ypos;
int xpos;
SDL_Surface *img;
} enemy;
////////////////////////////////////////////////////////////////////////////////////

player p;
linkedList *list;

int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}

screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}

int done=0;
p.img = SDL_LoadBMP("Graphics/Ships/Player/plyshp01a.bmp");
p.xpos = 250;
p.ypos = 200;

while(done == 0)
{

while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) {done = 1;}
if (event.type == SDL_KEYDOWN)
{
BITFIELD_SET( KeyboardDown, event.key.keysym.sym);
}
if (event.type == SDL_KEYUP)
{
BITFIELD_RESET( KeyboardDown, event.key.keysym.sym);
}
}

//This shit right here enables us to generate enemies every half second.
if(timeGetTime() > timer + 500)
{
timer = timeGetTime();
listNode *enemy;
}

if(KeyDown(SDLK_ESCAPE))
{
done = 1;
}

if(KeyDown(SDLK_UP))
{
if(p.ypos > 0)
{
p.ypos = p.ypos - 2;
}
}

if(KeyDown(SDLK_DOWN))
{
if(p.ypos < 426)
{
p.ypos = p.ypos + 2;
}

}

if(KeyDown(SDLK_LEFT))
{
if(p.xpos > 0)
{
p.xpos = p.xpos - 2;
}
}

if(KeyDown(SDLK_RIGHT))
{
if(p.xpos < 576)
{
p.xpos = p.xpos + 2;
}
}

SDL_FillRect(screen, NULL, SDL_MapRGB(screen -> format, 0, 0, 0));
DrawImage(p.img, p.xpos, p.ypos);
SDL_Flip(screen);
}

return 0;
}

Share this post


Link to post
Share on other sites
Replace
SDL_Surface player_imgs;
//player_imgs[0] = SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp");
player_imgs = new SDL_Surface *[7];

with
SDL_Surface * player_imgs[7] = {SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp")};

Assignments are only legal at global scope when they are part of a variable definition with initialisation.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by TyroWorks
How about this to create the array of objects:

player_imgs = new SDL_Surface *[7];

See this thread:
LINK


He should only need to do that if he is allocating an array of non-constant size.

Share this post


Link to post
Share on other sites
Thanks guys now there's only one error remaining! :) This is what the compiler says:

[source lang ="cpp"]--------------------Configuration: SDL DrawImage test - Win32 Debug--------------------
Compiling...
main.cpp
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(19) : error C2440: 'initializing' : cannot convert from 'struct SDL_Surface *' to 'unsigned int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Programs\C++\SDL\SDL DrawImage test\main.cpp(92) : warning C4018: '>' : signed/unsigned mismatch
Error executing cl.exe.

SDL DrawImage test.exe - 1 error(s), 1 warning(s)



This error is really freakign me uot since I've loaded bmps into SDL_Surfaces before! :S

Share this post


Link to post
Share on other sites
The type of the variable you're initializing has to be filled in (or it defaults to int) and match what you're initializing with (SDL_Surface*).

Enigma has it correct:

SDL_Surface * player_imgs[7] = {SDL_LoadBMP("Graphics/Ships/Player/anim/pl_left01.bmp")};


That is an array of pointers, which is initialized (not assigned to) using the result of the LoadBMP() call (that result is a pointer of the type you want) for element 0 and null pointers for the others.

BTW, that signed/unsigned mismatch could be a severe problem - it may result in comparisons not behaving the way you expect. There's a good chance there's a variable that you should be declaring as unsigned whereas it is currently signed (the default for short/int/long) or vice-versa.

Share this post


Link to post
Share on other sites
I would not recommend calling SDL_LoadBMP at initialisation time. This function will be called before you initialise SDL. In this particular case, SDL_LoadBMP probably doesn't require SDL to be initialised, so it runs okay, but it's a bad habit to get into.

More info: here and here.

cheers
sam

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this