Hello, I'm having a rather strange bug. I'm making puzzle game.
When the game starts(PlayState) for the first time pieces are indeed correctly placed, but after that sometimes they appear fine and most of the time only half of each piece is present ( I'm composing them out of 2 rectangles). The other parts appear at coordinates like this : Part2: X[1] : 842150923, Y[1]: 842150691. It happens even if I don' t use rand().
I'm using a state machine:
PlayState can go to:
- pause state
PauseState can go to:
- Resume
- Main Menu stateMain
MenuState can go to:
- PlayState
- Exit
I'm following a book "SDL Game Development" by Shaun Mitchell (btw It's full of bugs).
bool PlayState::onEnter() {
int left_x = 600 + (rand() % static_cast<int>(750 - 600 + 1));
int left_y = 500 + (rand() % static_cast<int>(500 - 100 + 1));
Vec2 dimension = Vec2(100, 200);
int right_x = left_x + 100;
int right_y = left_y;
Vec2 dimension_2 = Vec2(100, 200);
SDL_Color color1 = { 255, 0, 0, 0 };
Piece* piece = new Piece(Vec2(left_x, left_y), Vec2(right_x, right_y), dimension, dimension_2, color1);
// Piece 2
int i_left_x = 600 + (rand() % static_cast<int>(750 - 600 + 1));
int i_left_y = 500 + (rand() % static_cast<int>(500 - 100 + 1));
Vec2 dimension_i1 = Vec2(100, 100);
int i_right_x = i_left_x;
int i_right_y = i_left_y + 100;
Vec2 dimension_i2 = Vec2(100, 100);
SDL_Color color3 = { 0, 0, 255, 0 };
Piece* piece3 = new Piece(Vec2(i_left_x, i_left_y), Vec2(i_right_x, i_right_y), dimension_i1, dimension_i2, color3);
// and so on until I have 7 pieces.
}
// ...
void PlayState::update() {
if (InputHandler::Instance()->isKeyDown(SDL_SCANCODE_ESCAPE)) {
Game::Instance()->getStateMachine()->pushState(new PauseState());
}
for (unsigned int i = 0; i < m_pieces.size(); i++){
m_pieces[i]->update();
}
The issue occurs when I go from MainMenuState to PlayState after PauseState.
I'll provide additional info if needed. Thanks in advance.
↧