SDL,vector, with reference question

Started by
10 comments, last by rip-off 11 years, 8 months ago
Hello.
I am currently working on my game project, i came across a problem/a thing i did not know
If i make something like this :

std::vector <SDL_Surface*> SurfaceList;

And use function to get member of "SurfaceList" like

SDL_Surface& GETR_SurfaceList(int which);
//Which does
SDL_Surface& class::GETR_SurfaceList(int which)
{
return SurfaceList[which];
}//I know the pros and cons of vector and i am aware of issues that this may cause

/*But this will return Reference to this SDL_Surface*
so can i inside my main do next */
int main(int argc, char**)
{
//Function to load/populate/ADD $HIT TO SurfaceList is called
SDL_Surface* tempSurf = GETR_SurfaceList[0]; // is this valid ?

}

Advertisement
Given that it wouldn't compile, no it's not valid.

I am currently working on my game project, i came across a problem/a thing i did not know
If i make something like this :

Is there a question here somewhere?
SimLab Developments
[twitter]simlabdev[/twitter]

Given that it wouldn't compile, no it's not valid.

Let me help that lack of imagination.

#include <vector>
#include <SDL.h>
class T
{
public:
std::vector<SDL_Surface*> SurfaceList;
int HowManySurfaces;
void InitSurfaceList(SDL_Surface*& Screen);
};
void T::InitSurfaceList(SDL_Surface*& Screen)
{
HowManySurfaces = 1;
SDL_Surface* tempSurface = NULL;
SDL_Rect tempRect;
tempRect.x = 0;
tempRect.y = 0;
tempRect.w = 10;
tempRect.h = 10;
SDL_FillRect(tempSurface, &tempRect, SDL_MapRGB(Screen->format, 255, 255, 255));
SurfaceList.push_back(tempSurface);
}
int main(int argc, char**)
{
SDL_Surface* Screen;
SDL_Init(SDL_INIT_EVERYTHING);
Screen = SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);
T obj_T;
obj_T.InitSurfaceList(Screen);
if(obj_T.SurfaceList[0] == NULL)
{
SDL_Quit();
return -1;
}
SDL_BlitSurface(obj_T.SurfaceList[0], NULL, Screen, NULL);
SDL_Flip(Screen);
SDL_Delay(2000);
if(obj_T.SurfaceList[0])
SDL_FreeSurface(obj_T.SurfaceList[0]);
SDL_Quit();
return 0;
}


I am in middle of nowhere. Thanks to my neightborhood i could use his computer to download some IDE.
Tho this is not valid and is returning value of 3.

[quote name='BaneTrapper' timestamp='1347129944' post='4978061']
I am currently working on my game project, i came across a problem/a thing i did not know
If i make something like this :

Is there a question here somewhere?
[/quote]
Good point, i should not write next to code
Given that you assign tempSurface a null pointer and then proceed to try use it, then your code still isn't valid. Note that this problem has nothing to do with vectors or references.

Given that you assign tempSurface a null pointer and then proceed to try use it, then your code still isn't valid. Note that this problem has nothing to do with vectors or references.

Excuse you.
In T::InitSurfaceList(SDL_Surface** Screen)
There is a call to SDL_FillRect(which_rect_to_fill, what_rectangle_to_fill, what_color_to_use);
Therefore your argument is invalid.

EDIT: its bean a long day... and i may not think straight. But it does not mother if i set a SDL_Surface* to null at creation, since its pointing to "lets call it random crap"
SDL_FillRect dose not use return to give you a variable, instead it changes it directly as it does it stuff, Saves copy time. Therefore
SDL_FillRect(What_surface_to_modify, What_rect_to_draw_to_surface, What_color_to_use);

Therefore your argument is invalid.
[/quote]
SiCrane is incorrect, because SDL_FillRect() checks for null (at least the copy I found online did) and returns an error code. The program, in the absence of a SDL initialisation/video mode failure, does not invoke undefined behaviour.

However you appear to think that this will magically create a surface. It will not, and by the rules of C++ cannot - SDL_FillRect takes a pointer by value, it cannot "write back" to initialise the pointer in the calling code.

It is unclear what your question is, or what you are trying to achieve. If you can give us a higher level description of the problem you are wrestling with, it would help. Also, I would advise you re-consider your tone. We are taking our free time to try to understand and help you.

SiCrane is incorrect, because SDL_FillRect() checks for null (at least the copy I found online did) and returns an error code. The program, in the absence of a SDL initialisation/video mode failure, does not invoke undefined behaviour.

The copy on my computer begins like this:

int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
{
SDL_VideoDevice *video = current_video;
SDL_VideoDevice *this = current_video;
int x, y;
Uint8 *row;

/* This function doesn't work on surfaces < 8 bpp */
if ( dst->format->BitsPerPixel < 8 ) {
switch(dst->format->BitsPerPixel) {

Here it dereferences dst without a check for null. Also, the API documentation doesn't say anything about ignoring a null destination pointer.

SDL_FillRect() doesn't create a surface, it tries to write to a surface denoted by the first argument, which is a null pointer in that call.

i apologize, as i recently said its bean a fragmenting day for me.


Now what i am trying to achieve is :

Create a vector list of SDL_Surfaces
there will be around 10 SDL_Surfaces, Each is supposed to be a random box
Width and height is random 1-100pixels

Problem is, how to create a valid SDL_Surface* something, that i can use function SDL_FillRect()
To create a rectangle in it. Since you are stating SDL_FillRect() does not take a NULL parameter/surface.
What em i supposed to do, to the surface before calling SDL_FillRect() for the surface to create a rectangle.

This topic is closed to new replies.

Advertisement