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);
}
}
}
↧