Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Mouse selection problems

Options
  • 28-03-2007 6:40pm
    #1
    Closed Accounts Posts: 408 ✭✭


    Hey guys,

    I'm after some assistance if possible to do with a selection class im creating for a real time strategy game (2d).

    This might be a bit long code wise sorry! :
    #ifndef SELECTION_H
    #define SELECTION_H
    
    #include "SDL/SDL.h"
    
    struct Point2D
    {
    double getX() const {return mX;}
    double getY() const {return mY;}
    void setX(double x) {mX = x;}
    void setY(double y) {mY = y;}
    void setXY(double x, double y) {setX(x); setY(y);}
    
    private:
    
    double mX, mY;
    
    }; // end point2d struct
    
    
    struct Area
    {
    
    Area() { setAll(0, 0, 0, 0); }
    
    void setLeft(int left)
    {
    mLeft = left;
    if(mLeft > mRight)
    swap(mLeft, mRight);
    }
    
    void setTop(int top)
    {
    mTop = top;
    if(mTop > mBottom)
    swap(mTop, mBottom);
    }
    
    void setRight(int right)
    {
    mRight = right;
    if(mRight < mLeft)
    swap(mRight, mLeft);
    }
    
    void setBottom(int bottom)
    {
    mBottom = bottom;
    if(mBottom < mTop)
    swap(mBottom, mTop);
    }
    
    void setAll(int left, int top, int right, int bottom)
    {
    mLeft = left;
    mTop = top;
    setRight(right);
    setBottom(bottom);
    }
    
    
    int left() const { return mLeft; }
    int top() const { return mTop; }
    int right() const { return mRight; }
    int bottom() const { return mBottom; }
    
    private:
    
    int mLeft, mTop, mRight, mBottom;
    
    void swap(int& a, int& b)
    {
    int c = a;
    a = b;
    b = c;
    }
    };
    
    class Selection
    {
    
    private:
    
    enum SelectionStates { NOT_SELECTING, SELECTING };
    SelectionStates state;
    //int mouseX, mouseY;
    Uint8 mouseState;
    
    
    public:
    
    Area selection;
    Point2D location;
    Selection();
    bool update();
    void render( SDL_Surface *screen );
    
    
    }; // end selection.h
    
    #endif
    
    OK thats my header file! Heres my selection.cpp >>>
    #include "selection.h"
    #include <iostream>
    #include <stdlib.h>
    #include <SDL/SDL.h>
    #include <SDL/SDL_gfxPrimitives.h>
    
    Selection::Selection()
    {
    state = NOT_SELECTING;
    }
    
    
    bool Selection::update()
    {
    int mouseX, mouseY;
    Uint8 mouseState = SDL_GetMouseState( &mouseX, &mouseY );
    if( state == SELECTING ) {
    if( !( mouseState & SDL_BUTTON(1) ) ) {
    state = NOT_SELECTING;
    return true;
    } else {
    selection.setLeft( mouseX );
    selection.setTop( mouseY );
    }
    } else {
    if( mouseState & SDL_BUTTON(1) ) {
    state = SELECTING;
    selection.setLeft( mouseX );
    selection.setTop( mouseY );
    selection.setRight( mouseX );
    selection.setBottom( mouseY );
    
    
    }
    }
    
    return false;
    
    }
    
    
    void Selection::render( SDL_Surface *screen )
    {
    if( state == SELECTING )
    {
    rectangleColor( screen, selection.left(), selection.top(), selection.right(), selection.bottom(), 0xfe00008b);
    }
    }
    
    
    >>> THe problem im experiencing is that my rectangleColor() function (which is a preset from sdl_gfx library) is drawing a rectangle which is good, but the initial x, y coordinates (i.e. left and top) are stuck at (0,0) and don't move no matter where i click, but the right and bottom coordinates are correctly drawing as the bottom right hand corner of the rectangle ( as specified when i click the mouse button). Basically when i click on the mouse intially the x and y coords of the mouse sud be left() and top() but they are turning into right() and bottom() and the left and top are stuck at 0,0.

    Am i missing out on some really basic mathematical mistake ? My brain is just fried and if anyone can lend a hand and point out my mistake i would be greatly appreciated!


    Sorry for long post,

    dan.


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Heh well I suggested the code tags in the other thread so that it would be indented properly and easier to read! That's a bit better though.

    Looks to me like your selection update logic is a bit screwed. You want to track where the mouse is on every update, then the first time it's clicked capture that position as left & top, and then every subsequent update set bottom & right to the mouse position until the button is released again. You have it almost right, you just need to rethink when you're setting left, top, right & bottom.

    You'll also want do double-check whether SDL's rectangle drawing function expects the last two parameters to be the width & height of the rectangle, or the coordinates of the bottom right-hand corner. If it's the former, you'll have to calculate that too (not difficult).


  • Closed Accounts Posts: 408 ✭✭Chada


    Hey man,

    Cheers for the input, sorry for the new post for some reason it wouldn't let me edit my original post.

    The rectangleColor function as dictated in sdl_gfxprimitives documentation is:

    int rectangleColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);

    So basically im passing the x and y coordinates of the top left and bottom right corners as far as i can see.

    As for my logic im sure your right and its flawed but i really cant see where im going wrong.

    Any ideas as to a solution to this problem, i wouldn't post if i hadn't have fried my brains over it :)

    Cheers for any input,

    dAn


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    No help on gamedev.net either eh?!

    Hint:
    selection.setLeft( mouseX );
    selection.setTop( mouseY );
    selection.setRight( mouseX );
    selection.setBottom( mouseY );
    
    ...does that really look right to you, setting top/bottom and left/right to the same values?

    I've pointed out what you need to change (not just the above), just think about it for a while. If you can't figure this out by yourself, doing a whole strategy game might be a bit much...


  • Closed Accounts Posts: 408 ✭✭Chada


    Yeah i figured that setting mouseX and mouseY to my 4 x, y pos looks odd but there are calculations set in place to swap the variables about.

    The only thing i can think of possibely creating another instance of Point2D, like this:
    
    Area s1, s2;
    
    

    And then setting s1.setTop(mouseY), and s1.setLeft(mouseX) and then possibely s2.setTop(mouseY) etc. but then it gets confusing because of the way the area struct swaps left and right if left is greater than right etc.

    I could always go back to the original s1.minX(), s2.maxX() sort of way of thinking but i like the functionality of the Area struct.

    What would you recommend? Am i on the right lines ?

    Its my first game btw so go easy on me :P

    Cheers,

    daN


  • Closed Accounts Posts: 408 ✭✭Chada


    Looks to me like your selection update logic is a bit screwed. You want to track where the mouse is on every update, then the first time it's clicked capture that position as left & top, and then every subsequent update set bottom & right to the mouse position until the button is released again. You have it almost right, you just need to rethink when you're setting left, top, right & bottom.

    Ok so, i know what i need to do, but im a bit confused at how to do it. For example if i follow what you have said my code should look like this(i think):
    bool Selection::update() {
    	int mouseX, mouseY;
    	Uint8 mouseState = SDL_GetMouseState( &mouseX, &mouseY );
    
    	if( state == SELECTING ) {
                    
    		if( !( mouseState & SDL_BUTTON(1) ) ) {
    			state = NOT_SELECTING;
    			return true;
    		} else {
                             if ((SDL_BUTTON(1)){
                   
    			selection.setLeft( mouseX );
    			selection.setTop( mouseY );
    		}
    	} else {
            if( mouseState & SDL_BUTTON(1) )
            {
                state = SELECTING;
                selection.setRight( mouseX );
                selection.setBottom( mouseY );
    		}
        	
    	}
    	
    	return false;
    	
    }
    

    Is this anywhere closer ? I'm finding guides on how to handle mouse events without SDL_Event usage a bit difficult to come by.

    Cheers,

    dan


  • Advertisement
  • Registered Users Posts: 1,481 ✭✭✭satchmo


    That's it - although it would make more sense to have the initial click to be top/left and then move bottom/right around as the mouse is dragged, that's how selection boxes usually work. Although I'm not sure what you're trying to do with the swapping, so maybe you've worked that out already. Looks good otherwise (although you left out the 'mouseState &' bit in the first else statement).


  • Closed Accounts Posts: 408 ✭✭Chada


    Hey many thanks for the input so far,

    I tried out the code i had typed previously and it didnt fix any of my problems.

    When you say i missed out a mousestate, that would mean that both if statements wud depend on the same click and drag action to call the set methods.

    I want it so that the intial click sets the left and top and then when the mouse button is held down it will set the x, y coords of the mouse to right and bottom.

    The problem im still having is referring to this inital click. AFAIK mouseState & SDL_BUTTON(1) is saying if there is a x,y value for the mouse and the left mouse button is being clicked set watever.

    So how do i differientate between just clicks and draggin without using SDL_events are already present in my code for the key presses.

    By the way the setLeft functions calculate whether the left x is greater than the right x coord and thus swaps the values if this is true. Same with top and bottom so thats how i got round the selection box issue you mentioned .

    Any ideas on how to fix the problem ?

    Any help appreciated,

    dan


Advertisement