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:
/////////////-------------------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.)
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;
}
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;
}
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.