Jump to content
  • Advertisement
Sign in to follow this  
chosenkill6

SDL_Surface in a struct

This topic is 2634 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

I am trying to put an SDL_Surface into a struct but i keep getting error saying it forbids initialization of member "variable name", plus many more errors...
How would i go about doing this. Does it have something to do with the fact that an SDL_Surface is a struct? I am lost, some help would be greatly appreciated.
here is the struct i am trying to make:
struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image = NULL;
};

Share this post


Link to post
Share on other sites
Advertisement

I am trying to put an SDL_Surface into a struct but i keep getting error saying it forbids initialization of member "variable name", plus many more errors...
How would i go about doing this. Does it have something to do with the fact that an SDL_Surface is a struct? I am lost, some help would be greatly appreciated.
here is the struct i am trying to make:
struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image = NULL;
};



struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};


You can't set *image to NULL like that. Should solve your issue, I believe.

Share this post


Link to post
Share on other sites

[quote name='chosenkill6' timestamp='1317079048' post='4866258']
I am trying to put an SDL_Surface into a struct but i keep getting error saying it forbids initialization of member "variable name", plus many more errors...
How would i go about doing this. Does it have something to do with the fact that an SDL_Surface is a struct? I am lost, some help would be greatly appreciated.
here is the struct i am trying to make:
struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image = NULL;
};



struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};


You can't set *image to NULL like that. Should solve your issue, I believe.
[/quote]

Yeah, that worked like a charm.Thanks :D
Just wondering, why is that so? Whenever i declare a surface in any function i set it to NULL, why is that not possible here?

Share this post


Link to post
Share on other sites

[quote name='CornyKorn21' timestamp='1317079185' post='4866259']
[quote name='chosenkill6' timestamp='1317079048' post='4866258']
I am trying to put an SDL_Surface into a struct but i keep getting error saying it forbids initialization of member "variable name", plus many more errors...
How would i go about doing this. Does it have something to do with the fact that an SDL_Surface is a struct? I am lost, some help would be greatly appreciated.
here is the struct i am trying to make:
struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image = NULL;
};



struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};


You can't set *image to NULL like that. Should solve your issue, I believe.
[/quote]

Yeah, that worked like a charm.Thanks :D
Just wondering, why is that so? Whenever i declare a surface in any function i set it to NULL, why is that not possible here?
[/quote]

Someone else may need to chip in here, I'm not sure of the exact reasoning behind not allowing default values in stuct/class definitions. Default values should be set through a constructor. I think the only exception for that are const members.

Share this post


Link to post
Share on other sites
Lol, need some more help :P

Here is the program that i made just to test the struct in and as soon as it runs, it stops responding. Some help please

#include <SDL.h>
#include <SDL_image.h>
#include <string>

SDL_Surface *screen = NULL;

SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loaded_image = NULL;
SDL_Surface *optimized_image = NULL;

loaded_image = IMG_Load(filename.c_str());
optimized_image = SDL_DisplayFormatAlpha(loaded_image);
SDL_FreeSurface(loaded_image);
return optimized_image;
}

struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};

int main(int argc, char *args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
SDL_WM_SetCaption("Pong", NULL);
object left_paddle, right_paddle, ball;
ball.image = load_image("ball.png");
left_paddle.image = load_image("left_paddle.png");
right_paddle.image = load_image("right_paddle.png");
SDL_BlitSurface(left_paddle.image, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(5000);
SDL_Quit();
return 0;
}

Share this post


Link to post
Share on other sites

Lol, need some more help :P

Here is the program that i made just to test the struct in and as soon as it runs, it stops responding. Some help please

#include <SDL.h>
#include <SDL_image.h>
#include <string>

SDL_Surface *screen = NULL;

SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loaded_image = NULL;
SDL_Surface *optimized_image = NULL;

loaded_image = IMG_Load(filename.c_str());
optimized_image = SDL_DisplayFormatAlpha(loaded_image);
SDL_FreeSurface(loaded_image);
return optimized_image;
}

struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};

int main(int argc, char *args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
SDL_WM_SetCaption("Pong", NULL);
object left_paddle, right_paddle, ball;
ball.image = load_image("ball.png");
left_paddle.image = load_image("left_paddle.png");
right_paddle.image = load_image("right_paddle.png");
SDL_BlitSurface(left_paddle.image, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(5000);
SDL_Quit();
return 0;
}



Need a little more information. What do you mean it stops responding? Do you know where it stops responding if it actualy does? From what I can see it looks like it is set to render the left paddle on screen for approximately 5 seconds and then quit.

Share this post


Link to post
Share on other sites

yeah, like as soon as i compile and run it, the window pops up and says that it is not responding...


have you tried running it through a debugger.

and as for your first question, the reason you can't have "= NULL" in the struct definition. When you have image = NULL, you're trying to initialize the value. and that can't be done from within a definition of a struct or class.

you should instead use the constructor to initialize starting values, or another function.


struct object
{
object()
: x_coordinate(0), y_coordinate(0), x_speed(0), y_speed(0), image(NULL) {

};

float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};



and just to be nit picky, you should consider using vectors rather than single values. ie instead of using "x, and y". Use "vector2 position". and "vector2 speed". That way you can just do math like "position += speed".

Share this post


Link to post
Share on other sites
Yeah, i have run it through the codeblocks debugger and i dont see anything that should cause the program to crash.
I have used this type of program many times to test out something, in this case the struct, and usually it runs fine. I think it may have something to do with my code
As for the vectors, i have thought of using those, but they seem to complex

Share this post


Link to post
Share on other sites

Yeah, i have run it through the codeblocks debugger and i dont see anything that should cause the program to crash.
I have used this type of program many times to test out something, in this case the struct, and usually it runs fine. I think it may have something to do with my code
As for the vectors, i have thought of using those, but they seem to complex


Try this:


#include <SDL.h>
#include <SDL_image.h>
#include <string>

SDL_Surface *screen = NULL;

SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loaded_image = NULL;
SDL_Surface *optimized_image = NULL;

loaded_image = IMG_Load(filename.c_str());
optimized_image = SDL_DisplayFormatAlpha(loaded_image);
SDL_FreeSurface(loaded_image);
return optimized_image;
}

struct object
{
float x_coordinate, y_coordinate;
float x_speed, y_speed;
SDL_Surface *image;
};

int main(int argc, char *args[])
{
SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
SDL_WM_SetCaption("Pong", NULL);
object left_paddle, right_paddle, ball;
ball.image = load_image("ball.png");
left_paddle.image = load_image("left_paddle.png");
right_paddle.image = load_image("right_paddle.png");
SDL_BlitSurface(left_paddle.image, NULL, screen, NULL);
SDL_Flip(screen);
do
{
SDL_PollEvent(&event)
}
while (event.type != SDL_QUIT);
SDL_Quit();
return 0;
}


Your program stopped responding because you didn't have a proper main loop to process messages coming from the operating system. I replaced SDL_Delay with a very simple loop, and now you should be able to move and close window normally.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!