Jump to content
  • Advertisement

Dzuvan

Member
  • Content Count

    10
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Dzuvan

  • Rank
    Member

Personal Information

Social

  • Twitter
    Dzuvan
  • Github
    https://github.com/Dzuvan
  • Twitch
    https://www.twitch.tv/dzuvan
  • Steam
    dzuvan
  1. Dzuvan

    Project Management

    Great article! I myself struggle with estimates, so the thing I do is multiply by 3 my estimate time. Works most of the time. XD
  2. So, this is the solution I've come up with, It kinda works but sometimes pieces still go together. Z values are going from one to n of pieces. So first piece has z value 1, second 2 and so on... std::vector<GameObject*> overlaps; std::vector<int> zs; //heights void Game::update() { // Grab pieces under button and push them in vector. if (InputHandler::Instance()->getMouseButtonState(LEFT)){ for(int i = 0; i < pieces.size(); i++) { if (intersects(pieces[i]->getPosition().getX(),pieces[i]->getPosition().getY(), 100, InputHandler::Instance()->getMouseButtonPosition()->getX(), InputHandler::Instance()->getMouseButtonPosition()->getY())) { overlaps.push_back(pieces[i]); } } } if (InputHandler::Instance()->getMouseButtonState(LEFT)) { for (GameObject* o : overlaps) { zs.push_back(o->getZ()); int it = *std::max_element(std::begin(zs), std::end(zs)); if (o->getZ() == it) { o->setSelected(true); } } } else { for (GameObject* o : overlaps) { o->setSelected(false); overlaps.clear(); zs.clear(); } } for(int i = 0; i < pieces.size(); i++) { pieces[i]->update(); } }
  3. I have only 7 pieces, but yeah I agree, that should be put elsewhere. I treat y as height since sdl2 doesn't have z value. I dunno how to invent that(unless it can be anything I want like index in the array or something like that).
  4. So I've been trying to make this work, but I only got it to work for shape that has highest y value. Missing something, can't pin point what >.< This is modified update method: std::vector<int> heights; void Game::update() { for(int i = 0; i < pieces.size(); i++) { heights.push_back(pieces[i]->getPosition().getY()); if (InputHandler::Instance()->getMouseButtonState(LEFT)) { if (intersects(pieces[i]->getPosition().getX(), pieces[i]->getPosition().getY(), 100, InputHandler::Instance()->getMouseButtonPosition()->getX(), InputHandler::Instance()->getMouseButtonPosition()->getY())) { int it = *std::max_element(std::begin(heights), std::end(heights)); if (pieces[i]->getPosition().getY() == it) { pieces[i]->setSelected(true); } if (!InputHandler::Instance()->getMouseButtonState(LEFT)){ pieces[i]->setSelected(false); } } } else { if(InputHandler::Instance()->getMouseButtonState(LEFT)){ pieces[i]->setSelected(true); } if(!InputHandler::Instance()->getMouseButtonState(LEFT)){ pieces[i]->setSelected(false); } } heights.clear(); heights.push_back(pieces[i]->getPosition().getY()); pieces[i]->update(); } }
  5. Dzuvan

    Wierd bug

    Hi guys, I managed to fix the problem. State machine was the culprit here. Changing state from pause to main menu didn't remove play state from stack. Once i poped the state while going to main menu problem went away. Thank you all.
  6. Dzuvan

    Wierd bug

    I do have code on github(I've fucked up code so many times in the past ) : https://github.com/Dzuvan/Puzzle Well I'm gonna try to test it the way you described, thanks. Yeah everything in that vector si live data. I'm only handling pieces in PlayState.
  7. Dzuvan

    Wierd bug

    Well I'm calling clear on vector that holds data. bool PlayState::onExit() { for (unsigned int i = 0; i < m_pieces.size(); i++) { m_pieces[i]->clean(); // doesn't do anything. It's empty void function m_pieces[i]->setOffset(Vec2(0, 0)); } m_pieces.clear(); std::cout<<"Exiting playState\n"<<std::endl; return true; // Here is state machine I'm Using #include "InputHandler.h" void GameStateMachine::pushState(GameState* pState) { m_gameStates.push_back(pState); m_gameStates.back()->onEnter(); } void GameStateMachine::popState() { if (!m_gameStates.empty()) { if (m_gameStates.back()->onExit()) { delete m_gameStates.back(); m_gameStates.pop_back(); } } } void GameStateMachine::changeState(GameState* pState) { if (!m_gameStates.empty()) { if (m_gameStates.back()->getStateID() == pState->getStateID()) { return; } if (m_gameStates.back()->getIsValid()) { m_gameStates.back()->setIsValid(false); // Mark the state as invalid } } m_gameStates.push_back(pState); m_gameStates.back()->onEnter(); } void GameStateMachine::update(){ if (!m_gameStates.empty()) { m_gameStates.back()->update(); } } void GameStateMachine::render() { if (!m_gameStates.empty()) { m_gameStates.back()->render(); } } void GameStateMachine::dequeState() { if (!m_gameStates.empty()) { // If the state is invalid we proceed to dequeue the state if (!m_gameStates[0]->getIsValid() && m_gameStates[0]->onExit()) { delete m_gameStates[0]; m_gameStates.erase(m_gameStates.begin()); // Reset the Input handler buttons state // This line is extremely important, fixes an issue with the "State traveling" // when a button is in the position of another button in another state // this will prevent the accident of traveling 2 states with 1 click. InputHandler::Instance()->reset(); } } } // Here is the call from MainMenuState void MainMenuState::s_menuToPlay() { std::cout << "Play button clicked\n"<<std::endl; Game::Instance()->getStateMachine()->changeState(new PlayState()); } bool MainMenuState::onExit() { for (unsigned int i = 0; i < m_buttons.size(); i++) { m_buttons[i]->clean(); } m_buttons.clear(); TextureManager::Instance()->clearFromTextureMap("playbutton"); TextureManager::Instance()->clearFromTextureMap("exitbutton"); std::cout << "Exiting menu state\n"<<std::endl; return true; } As for the static cast, I saw post on stackoverflow, since I thought rand was causing the issue. I changed that part of code.
  8. Dzuvan

    Wierd bug

    Well I'm using constructor like this: Piece(): m_position(Vec2(0,0)),m_position2(Vec2(0,0)), m_dimension(Vec2(0, 0)), m_dimension2(Vec2(0,0)), offset(Vec2(0,0)), offset2(Vec2(0,0)) {} My c++ is quite bad so I'm 100% missing something.
  9. Dzuvan

    Wierd bug

    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.
  10. Hi there fellas, I'm the new guy in need of help. I'm writing a simple tangram puzzle using c++ and sdl2. Goal of the game is to fit pieces on the grid. Piece movement is done via mouse drag and drop. The problem I'm having is when the cursor is over overlapping objects and I click and drag every shape under it gets dragged. I need some kind of check for object on top or something like that (I'm thinking it has to do something with height). Any help would be greatly appreciated. I've made Vec2 class that holds two int values for anything that has x and y values. Here is GameObject code that I use for puzzle pieces : void GameObject::render() { SDL_SetRenderDrawColor(Game::Instance()->getRenderer(), m_color.r, m_color.g, m_color.b, m_color.a); SDL_Rect rects[] = { m_position.getX(), m_position.getY(), m_dimension.getX(), m_dimension.getY(), m_position2.getX(), m_position2.getY(), m_dimension2.getX(), m_dimension2.getY() }; SDL_RenderFillRects(Game::Instance()->getRenderer(), rects, 2); SDL_RenderDrawRects(Game::Instance()->getRenderer(),rects, 2); } void GameObject::update() { if (InputHandler::Instance()->getMouseButtonState(0) && intersects(m_position, m_dimension, *InputHandler::Instance()->getMouseButtonPosition()) || intersects(m_position2, m_dimension2, *InputHandler::Instance()->getMouseButtonPosition())) { offset = (*InputHandler::Instance()->getMouseButtonPosition() - m_position); offset2 = (*InputHandler::Instance()->getMouseButtonPosition() - m_position2); dragging = true; } else if (!InputHandler::Instance()->getMouseButtonState(0) && dragging) { offset = Vec2(0,0); offset2 = Vec2(0,0); dragging = false; } if(dragging){ m_position = *InputHandler::Instance()->getMousePosition() - offset; m_position2 = *InputHandler::Instance()->getMousePosition() - offset2; } } bool GameObject::intersects(Vec2 object, Vec2 dimensions, Vec2 mouse) { if(mouse.getX() < object.getX() || mouse.getY() < object.getY()) { return false; } if(mouse.getX() > object.getX() + dimensions.getX() || mouse.getY() > object.getY() + dimensions.getY()) { return false; } return true; } And here is it being called in the game singleton. void Game::render() { SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 0); SDL_RenderClear(m_pRenderer); for (int i = 0; i < m_gameObjects.size(); i++) { m_gameObjects[i]->render(); } SDL_RenderPresent(m_pRenderer); } void Game::update() { for (int i = m_gameObjects.size()-1; i >= 0; i--) { m_gameObjects[i]->update(); } } And here is input handler, also a singleton: void InputHandler::update() { SDL_Event event; while (SDL_PollEvent(&event)) { m_keystates = SDL_GetKeyboardState(0); if (event.type == SDL_QUIT) { Game::Instance()->quit(); } if (event.type == SDL_MOUSEBUTTONDOWN) { m_mouseButtonPosition->setX(event.button.x); m_mouseButtonPosition->setY(event.button.y); if (event.button.button == SDL_BUTTON_LEFT) { m_mouseButtonStates[LEFT] = true; } if (event.button.button == SDL_BUTTON_MIDDLE) { m_mouseButtonStates[MIDDLE] = true; } if (event.button.button == SDL_BUTTON_RIGHT) { m_mouseButtonStates[RIGHT] = true; } } if (event.type == SDL_MOUSEBUTTONUP) { if (event.button.button == SDL_BUTTON_LEFT) { m_mouseButtonStates[LEFT] = false; } if (event.button.button == SDL_BUTTON_MIDDLE) { m_mouseButtonStates[MIDDLE] = false; } if (event.button.button == SDL_BUTTON_RIGHT) { m_mouseButtonStates[RIGHT] = false; } } if (event.type == SDL_MOUSEMOTION) { m_mousePosition->setX(event.motion.x); m_mousePosition->setY(event.motion.y); } } }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!