• 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
Lewis De Angelo

Vectors

1 post in this topic

Hey guys, I've been having a little trouble with vectors recently. I've used this same code on another file, and it complies flawlessly. However, here it does compile but the program ends immediately.

[code]
#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<cstdlib>
#include<ctime>
#include<sstream>
#include"string"
#include<vector>
//Applies a sprite to the screen
int apply_sprite( SDL_Surface* src, SDL_Rect* offsets = NULL, SDL_Rect* clip = NULL )
{
//Directly applies the sprite to the screen and check for errors
if( SDL_BlitSurface( src, clip, SDL_GetVideoSurface(), offsets ) == -1 )
{
return 0;
}

return 1;
}
//Opens a sprite/spritesheet and returns it
SDL_Surface* open_sprite( std::string path )
{
//Opens the image and stores it in a temp surface
SDL_Surface* temp = IMG_Load( path.c_str() );

//If the image didn't load
if( temp == NULL )
{
return NULL;
}

//Optimises the temp surface
SDL_Surface* optimized = SDL_DisplayFormat( temp );

//Map the color key
Uint32 colorkey = SDL_MapRGB( optimized->format, 255, 0, 255 );

//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey( optimized, SDL_SRCCOLORKEY, colorkey );


//Free's the memory consumed by temp
SDL_FreeSurface( temp );

return optimized;

}

class present
{
private:

int x, y, w, h, yVel;

SDL_Rect offsets;

SDL_Surface* sprite;

public:

present();

~present();

void move();

void show();

};
present::~present()
{
SDL_FreeSurface( sprite );
}
present::present():
w( 51 ),
h( 48 ),
yVel( 1 ),
x( rand() % SDL_GetVideoSurface()->clip_rect.w ),
y( 48 )

{

std::stringstream temp;
temp << "Pres" << (rand() % 5) + 1 << ".bmp";

sprite = open_sprite( temp.str().c_str() );
}

void present::move()
{
y += yVel;

if( (y + h) >= 589 )
{
y += -yVel;
}
}

void present::show()
{
offsets.x = x;
offsets.y = y;

apply_sprite( sprite, &amp;amp;offsets );
}


int main( int argv, char* argc[] )
{
srand( static_cast<unsigned int>(time(0)));

SDL_Init( SDL_INIT_EVERYTHING );

SDL_Surface* screen = SDL_SetVideoMode( 500, 657, 32, SDL_SWSURFACE );

if( screen == NULL )
{
return 1;
}

SDL_Surface* backround = open_sprite( "Backround.bmp" );

if( backround == NULL )
{
return 1;
}

SDL_WM_SetCaption( "Merry Christmas!", NULL );

bool quit = false;

int maximum_presents = 2;

std::vector<present> presents;
std::vector<present>::iterator it;

// Begin OP's bolded code
for( int i = 0; i < maximum_presents; i++ )
{
present temp;
presents.push_back( temp );
}
// End OP's bolded code

SDL_Event event;

int time = SDL_GetTicks();

while( !quit )
{


while( SDL_PollEvent(&amp;amp;event) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}

SDL_FillRect( screen, &amp;amp;screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255) );

if( !apply_sprite( backround ) )
{
return 1;
}


for( it = presents.begin(); it != presents.end(); it++ )
{
it->move();
}


for( it = presents.begin(); it != presents.end(); it++ )
{
it->show();
}

SDL_Flip( screen );
}

SDL_Quit();
SDL_FreeSurface( backround );

return 0;
}
[/code]
I've highlighted the problematic lines.
If you could help, I'd be grateful. Edited by JTippetts
Code tags
0

Share this post


Link to post
Share on other sites
Please use [ code ][ /code ] (without the spaces) tags when posting code in order to give it pretty formatting.

You need to follow the [url="http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)"]Rule of Three[/url]. What I suspect is happening is that when you [font=courier new,courier,monospace]push_back(temp)[/font], [font=courier new,courier,monospace]temp[/font] then goes out of scope and gets destroyed (and it frees its [font=courier new,courier,monospace]sprite[/font]). However, because you aren't following the Rule of Three, the [font=courier new,courier,monospace]present[/font] that just got added to the vector happens to have the same [font=courier new,courier,monospace]sprite[/font] inside of it, when means that a) it now is invalid because [font=courier new,courier,monospace]temp[/font] destroyed the [font=courier new,courier,monospace]sprite[/font] that the two objects were sharing, and b) it too will try to free the [font=courier new,courier,monospace]sprite[/font] when it gets destroyed, resulting in a double free.
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