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

Minor C++ programming question

Options
  • 23-03-2007 9:08pm
    #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

  • Closed Accounts Posts: 408 ✭✭Chada


    Is no-one able to help ?

    Im really stuck here cant see where ive went wrong!


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


    Try using the [code] tags to make it readable, people might be more willing to have a look then.


Advertisement