Jump to content
  • Advertisement
Sign in to follow this  
Afr0m@n

Putting data into arrays?

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!