• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BaneTrapper

SDL,vector, with reference question

11 posts in this topic

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 :
[CODE]
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 ?

}

[/CODE]
0

Share this post


Link to post
Share on other sites
[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 :
[/quote]
Is there a question here somewhere?
1

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1347130304' post='4978064']
Given that it wouldn't compile, no it's not valid.
[/quote]
Let me help that lack of imagination.
[CODE]
#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;
}

[/CODE]
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.
0

Share this post


Link to post
Share on other sites
[quote name='SimLabDevelopments' timestamp='1347131245' post='4978068']
[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 :
[/quote]
Is there a question here somewhere?
[/quote]
Good point, i should not write next to code
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1347132739' post='4978076']
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.
[/quote]
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
0

Share this post


Link to post
Share on other sites
[quote]
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.
0

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1347134154' post='4978081']
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.
[/quote]
The copy on my computer begins like this:
[code]
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) {
[/code]
Here it dereferences dst without a check for null. Also, the [url=http://www.libsdl.org/docs/html/sdlfillrect.html]API documentation[/url] doesn't say anything about ignoring a null destination pointer.
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1347134278' post='4978082']
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.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
One way to get a new surface is to call [url=http://www.libsdl.org/docs/html/sdlcreatergbsurface.html]SDL_CreateRGBSurface[/url].
0

Share this post


Link to post
Share on other sites
[quote]
Here it dereferences dst without a check for null. Also, the API documentation doesn't say anything about ignoring a null destination pointer.
[/quote]
The implementation I found is apparently [url="http://hg.libsdl.org/SDL/file/e54cc2d4e89a/src/video/SDL_fillrect.c#l312"]SDL 1.3[/url]. The implementation you posted seems to be [url="http://hg.libsdl.org/SDL/file/ec7ccf5ee3dd/src/video/SDL_surface.c#l553"]SDL 1.2[/url].

[quote]
Also, the API documentation doesn't say anything about ignoring a null destination pointer.
[/quote]
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.

[quote]
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
[/quote]
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.
0

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  
Followers 0