Advertisement

SDL games, use of vector causing crash

Started by July 15, 2011 02:03 PM
5 comments, last by grechzoo 13 years, 4 months ago
Okay, having a little problem in a basic platformer im trying to make after studying some awesome lazyfoo stuff.

My problem is, for the creation of the level im trying to make a vector filled with platforms of different sizes and positions. I set up the class like this shown below:


#include "SDL/SDL.h"

#include "globals.h"

#include <vector>

#include "player.h"


class Level

{

public:

Level();

void setPlatform();

void draw();

protected:

std::vector<SDL_Rect> platform;

};



Then in the level.cpp set the platforms, using the set platform functions:



#include "level.h"

Level::Level()
{

}

void Level::setPlatform()
{
platform[0].x = 200;
platform[0].y = 300;
platform[0].w = 200;
platform[0].h = 50;

platform[1].x = 400;
platform[1].y = 200;
platform[1].w = 100;
platform[1].h = 50;
}


So in the main.cpp with the gameloop, and with the level.h included, I run the main game loop.


int main (int argc, char*args[])
{
bool quit = false;

initFunc();

Timer fps;

Player myPlayer;

Level myLevel;

while (quit == false)
{
fps.start();



while (SDL_PollEvent (&event))

{

myPlayer.handleInput();



if (event.type == SDL_QUIT)

{

quit = true;

}

}



myPlayer.move();

SDL_FillRect( screen,&screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

myLevel.setPlatform(); <------------------------- CRASH!!!!!!!

myPlayer.show();

/////////////-------------------rest of loop not necessary (code crashes on above line every time--------///////////////////


so without the setPlatform function used in the main loop, the game boots up as normal and works perfectly,

however when it is used in the main loop, the SDL program simply flashed a black screen for half a second and closes again.

I have no idea whats going on and REALLY hope someone can see an error? it cant be SDL because Lazyfoo using this exact vector in his per pixel collision tutorial.




ps:( for the record: all the values are safely in bounds, and i tried this with a normal array and it worked fine. however i need the features of the vector in order to do some things i have planned, so really want to get it working.)
In your code you never seem to set the vector's size.
Advertisement
i thought vectors dont need to be assigned a size, and are as big as the definitions amount.

Lazfoo's code doesn't seem to define it.

but if this might cause the error to go away can you please tell me what exactly i need to add? just a [ "number" ] ? on the end of the definition?
A vector is initially empty. Vector will grow when you add to it, e.g. using push_back(). When you use operator, you are saying "I know you're at least size i + 1, give me the object at position i".

For your code, you just need to push_back() instead:

SDL_Rect make_rect(int x, int y, int w, int h)
{
SDL_Rect rect = { x, y, w, h };
return rect;
}

void Level::setPlatform()
{
platform.push_back(make_rect(200, 300, 200, 50));
platform.push_back(make_rect(400, 200, 100, 50));
}

Alternatively you can resize() the vector, and then access any element in the resized range. Be careful that you don't call reserve(), which is a similar method which makes space but does not allow you to access additional elements. I see calls to std::vector::resize() in some Lazy Foo tutorial I just checked.

If you see some code where Lazy Foo is accessing an element beyond the size() of the vector, it is possibly a bug in the code in the tutorial.

A vector is initially empty. Vector will grow when you add to it, e.g. using push_back(). When you use operator, you are saying "I know you're at least size i + 1, give me the object at position i".

For your code, you just need to push_back() instead:

SDL_Rect make_rect(int x, int y, int w, int h)
{
SDL_Rect rect = { x, y, w, h };
return rect;
}

void Level::setPlatform()
{
platform.push_back(make_rect(200, 300, 200, 50));
platform.push_back(make_rect(400, 200, 100, 50));
}

Alternatively you can resize() the vector, and then access any element in the resized range. Be careful that you don't call reserve(), which is a similar method which makes space but does not allow you to access additional elements. I see calls to std::vector::resize() in some Lazy Foo tutorial I just checked.

If you see some code where Lazy Foo is accessing an element beyond the size() of the vector, it is possibly a bug in the code in the tutorial.


I love you!!!!

seriously. i thought this was gonna delay me for a day, but instead it was 30 minutes.

people like you make the world a much better place to learn how to code!!!!!! ALSO great thansk to Sircrane :)

it is odd how i saw no code regarding these necessary vector commands in lazofoo's code though.

anyway who care now. thanks!!!!!!
lazyfoo calls box.resize(11) in the Dot constructor so it's not a bug in the tutorial.
Advertisement

lazyfoo calls box.resize(11) in the Dot constructor so it's not a bug in the tutorial.


ahh thanks :) knew it wasn't a bug really, as it worked for me. just totally missed than line with my STUPID eyes :)

sorry if this was such a simple error. im just trying to learn by doing and basically doing ym best impression of a sponge :)

This topic is closed to new replies.

Advertisement