Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL,vector, with reference question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 08 September 2012 - 12:45 PM

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 ?
	
}


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9596

Like
0Likes
Like

Posted 08 September 2012 - 12:51 PM

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

#3 SimLabDevelopments   Members   -  Reputation: 205

Like
1Likes
Like

Posted 08 September 2012 - 01:07 PM

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?

#4 BaneTrapper   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 08 September 2012 - 01:17 PM

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.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#5 BaneTrapper   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 08 September 2012 - 01:18 PM


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?

Good point, i should not write next to code

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#6 SiCrane   Moderators   -  Reputation: 9596

Like
0Likes
Like

Posted 08 September 2012 - 01:32 PM

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.

#7 BaneTrapper   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 08 September 2012 - 01:45 PM

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);

Edited by BaneTrapper, 08 September 2012 - 02:00 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#8 rip-off   Moderators   -  Reputation: 8216

Like
0Likes
Like

Posted 08 September 2012 - 01:55 PM

Therefore your argument is invalid.

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.

#9 SiCrane   Moderators   -  Reputation: 9596

Like
0Likes
Like

Posted 08 September 2012 - 02:03 PM

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.

#10 BaneTrapper   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 08 September 2012 - 02:10 PM

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.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#11 SiCrane   Moderators   -  Reputation: 9596

Like
0Likes
Like

Posted 08 September 2012 - 02:15 PM

One way to get a new surface is to call SDL_CreateRGBSurface.

#12 rip-off   Moderators   -  Reputation: 8216

Like
0Likes
Like

Posted 08 September 2012 - 05:04 PM

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

The implementation I found is apparently SDL 1.3. The implementation you posted seems to be SDL 1.2.

Also, the API documentation doesn't say anything about ignoring a null destination pointer.

SDL is fairly defensively programmed from what I remember. It is usually pretty good at letting you know when you've done something wrong without crashing on you (where it can). From the above it certainly seems they are moving towards being more defensive. It would be nice for this to be documented behaviour rather than a nice bonus.

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

A high level description is:

I want to be able to draw around 10 random boxes (between 1 and 100 pixels).

Talking about SDL_Surfaces is too low level. Why? Because there might be other approaches. For example, to solve this higher level goal, you might see that you don't actually need to store surfaces - you could store rectangles and use SDL_FillRect on the screen surface directly.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS