# SDL game programming

This topic is 3989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there, basically i'm looking for some help with a 2d real time strategy game i'm currently working on using C++ and SDL. I've got a basic understanding of SDL and how to use it, but im struggling with how to assign variables to the mouses X and Y location and hence assign a location for a unit to move to a destination specified by the mouse. If anyone has any SDL tutorials on real time strategy games it would be greatly appreciated! I'm also having problems with drawing a GUI, sort of warcraft style with the minmap and build options when i select my building. If i seem too vague i apologise, when it boils down to it, all i'm really after is a stepping stone guide to get my game off the ground and i can learn from there. Any help greatly appreciated, sykth.

##### Share on other sites
I don't know of any websites or tutorials about strategy games, but I can give you some ideas.

Get mouse position:
int x,y; // variables to hold the current position of the mouseSDL_GetMouseState(&x,&y); // get mouse xy

Input and some walking code:
// struct to hold the destinationstruct Destination {    int x;    int y;};// this structure can for example be a temporary struct which you later copy// into a member structure in a player classDestination dest;// input codeif (event.type == SDL_MOUSEBUTTONDOWN) {    // if left mouse button is pressed    if (event.button.button == SDL_BUTTON_LEFT) {        dest.x = mousex;        dest.y = mousey;    }}// and then in the actual player move code you can have something that// checks if the player haven't reached the current destination// you have specified by pressing on the screen, like below// these if statements will simply check if the player x and y is greater than// or lower than the destionation xy position. It will move the player position// to the destination until it's reachedif (player.x < dest.x)     player.x++; else if (player.x > dest.x)     player.x--;// if player y position is lower than the destionation ypos, increase player.yif (player.y < dest.y)    player.y++; else if (player.x > dest.y)     player.y--;

This code should work if you have a player class, else you have to change the code a little. Hope that helped..

##### Share on other sites
I usually use the following:

Uint8 Buttons;Uint8 *Keys;int MousePosX, MousePosY;Buttons=SDL_GetMouseState(&MousePosX, &MousePosY);Keys=SDL_GetKeyState(NULL);

What the above code does is set the position of the cursor to the variables MousePosX and MousePosY. GetKeyState will also record which keys are currently pressed down. GetMouseState also records which buttons are being held down at that moment.

I use Buttons and Keys like this:
if(Buttons & 1)//left click{     //stuff for left clicking}else if(Buttons & 4)//right click, don't ask me why it's 4{     //stuff for right clicking}if(Keys[SDLK_w])//if the 'w' key is pressed...{     //do stuff for the 'w' key}

On my machine 'Buttons & 4' will get you a right click. I've heard it's sometimes different.

You can also use events to get keyboard and mouse input but I'm not a fan of it.

As for tutorials you should check out what Lazy Foo has, his tutorials really helped me when I was first starting out.
His tutorials can be found at:
http://lazyfoo.net/SDL_tutorials/index.php

Also if you're writing a RTS you'll most definitely want to use path finding. Here's a great tutorail complete with source code that helped me learn A*:
http://www.policyalmanac.org/games/aStarTutorial.htm

Though if you're just starting out save that link for later... it can give you a real headache.

As for me, I'm still learning to make a GUI so I'm no real help there. Anyway, I hope that got a couple of your questions answered.

-Artum.

##### Share on other sites
Quote:
 Original post by ArtumI use Buttons and Keys like this:*** Source Snippet Removed ***On my machine 'Buttons & 4' will get you a right click. I've heard it's sometimes different.

It's probably better to use the SDL_BUTTON definitions than arbitrary numbers :)

SDL_BUTTON_LEFTSDL_BUTTON_MIDDLESDL_BUTTON_RIGHTSDL_BUTTON_WHEELUPSDL_BUTTON_WHEELDOWN

##### Share on other sites
Ok awesome help so far guys!

Just a bit more help if you could, I know i'll prob get slapped on the wrists for some basic programming errors but i'd like you experts to have a nosey at my unit.h code to see where i went wrong.

Basically i tried to create two structs within my unit.h class called unitLocation and unitDestination, and set two sets of variables for x and y locations. Ill paste unitLocation up now:

struct unitLocation
{
double x;
double y;
}

and then i wanted to use setUnitXLocation() and getUnitXLocation() (and same for Y) so i could create a selection method that tested the units X and Y location to see if its within the minX, maxX, minY and MaxY values and thus assign it a SELECTED or NOT_SELECTED value using a enum unitState {SELECTED, NOT_SELECTED} variable.

My get and set methods look like this but i think im doing this wrong:

}

double getXUnitLocation()
{
return unitXloc;
}

double getYUnitLocation()
{
return unitYloc;
}

void setXUnitLocation(int x)
{
this->x = x;
unitXloc = x;
}

void setYUnitLocation(int y)
{
this->y = y;
unitYloc = y;
}
// with my unitYloc and unitXloc being protected variables declared in this file.

Im obviously getting errors but can someone show me a better way to get a units location so i can use it in my selection method.

Sorry for long message.

Sykth

##### Share on other sites
Quote:
 Original post by sprite_houndIt's probably better to use the SDL_BUTTON definitions than arbitrary numbers :)

Well, yeah... I mean if you want to write proper code sure, but who does that?! ;)
Pfft... proper code... *Starts rewriting input functions*

And to answer your lastest question, Sykth, I created a struct/class for my positions. I call it a Pair (who'd of thunk it) as it only contains two variables: an x and a y.

class/struct Pair{     int/double/whatever x;     int/double/whatever y;};class Unit{     Pair Position;     Pair Destination;};

The Pair can be a position, a destination, whatever. With this approach you only need one class/struct since you can just use the Pair class for both current positions and destinations. Hmm... now I'm not sure if I even answered your question. Oh well, let me know.

-Artum.

##### Share on other sites
Hey artum,

thanks for input, ure struct idea really helped, ive managed to partially complete my unit class now and my baseBuilding class :)

I'm just using structs to contain the various x or y variables then assigning them to my mouse X and mouse Y variables.

So far its working out great :)

Now all i have to do is figure out how to draw the png image i got onto the SDL_surface i have created :) Although i have a fair idea how to do that anyways :)

I'm getting on the way to having a working prototype so thanks :D

One more question tho, in SDL should i have an setX and SetY AND a setXmapped and setYmapped, you know taking into account the screenWidth and screenHeight variables to make sure it gets copied correctly to my sdl surface ? Is this needed ? i got the idea from a QONK game and im not sure if i need it.

CHeers,

sykth

##### Share on other sites
If I understand you correctly you're asking how to draw stuff to the screen.

So the way I do it is simple (in my head at least). Each unit has a X/Y coordinate. I created a Camera Class that holds one of my Pairs for its position as well. So, I've got a Unit Pair and a Camera Pair.

Normally when you blit a image to the screen you supply it with an X and Y value so it knows where to put the image. That's fine an dandy... but what if your camera has moved? DUN DUN DUN!

So lets say that Unit1's position is (50,50) and the camera is at (0,0). To draw the unit at (50,50) you'd just need to blit for (50,50).

Well, now lets say you've moved the camera to (100, 100). If you try and blit the unit's image at (50,50) you'll really be blitting it to (150,150). And that's no good. I'm a really bad teacher so I appologize if this doesn't make sense.

So what I do is this...
//handy-dandy drawing function/*Image is the image you are drawing to the Screen.x and y are the positions on the screen where you want to draw the image.*/DrawImage(SDL_Surface *Image, SDL_Surface *Screen, int x, int y){    SDL_Rect rect;    rect.x = x;    rect.y = y;    SDL_BlitSurface(Image, NULL, Screen, &rect);}//in use...DrawImage(Unit1Image, Screen, Unit1XPos-CamXPos, Unit1YPos-CamYPos);

You'll have to excuse my crappy code... it's crappy. I have no excuse, I'm just a bad programmer.

Anyway... as you can see when I use my DrawImage function I don't just give it my Unit's position. I actually give it my unit's position minus the camera's position. This offsets the unit so that when you move the camera the unit is adjusted to where it needs to be on the screen. In fact, everything I draw is offset by the camera's position: tiles, units, objects, everything. If you don't do this all of your objects will never move on the screen when you move your camera.

So no, you don't need to have two sets of positions for each unit. You just need to have one position for your unit and one position for your camera. Then offset the unit's position by the camera's position whenever you're drawing to the screen.

Man I hope that makes sense. Like I said, I'm not a good teacher. Nor am I the best programmer so take what I say with the biggest grain of salt you can find.

Let me know if this doesn't make sense so I can try and find you proper help.

-Artum.

##### Share on other sites
Hey artum,

yeah i think i know what you are getting at, i have a player class that sets the frame++ when a direction is pushed, so i just have to pass the x and y coordinates from the "player location" to the draw image. (player loc is really just where the screen is centered at).

In terms of this unit code could you verify my changed code will actually send the current location of my unit, i hope it does but just wanted an expert to double check before i progress:

struct unitLocation
{
double x;
double y;
};

struct unitDestination
{
double unitXdest;
double unitYdest;
};

unitDestination unitDest;
unitLocation unitLoc;

///sets unitDest
void setUnitDestination(int mouseX, int mouseY)
{
unitDest.unitXdest = mouseX;
unitDest.unitYdest = mouseY;
}

double getXUnitLocation()
{
return unitXloc;
}

double getYUnitLocation()
{
return unitYloc;
}

// sets unitXlocation from the variable in the struct to the variable assigned later in the protected bit, is this right ?
void setXUnitLocation(int x)
{
unitLoc.x = unitXloc;
}

void setYUnitLocation(int y)
{
unitLoc.y = unitYloc;
}

IF this isn't right or im missing some code please let me know, i have a feeling its not just this simple, i mean where am i getting the x and y variables of the unit from ? im a tad confused lol :S

Cheers,

Sykth

##### Share on other sites
Here is some changes you might find useful.
I made the accessor functions part of the Point2D struct's interface.
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;};// As long as a Location and a Destination is the same as a Point2D we can define them as suchtypedef Point2D Location, Destination;struct Unit{	Destination dest;	Location loc;};int main(){	Unit unit1;        ...	unit1.dest.setXY(mouseX, mouseY);        ...        // Artum's DrawImage with some changes        DrawImage(Image, Screen, unit1.loc.getX(), unit1.loc.getY());        // or if we make the x and y arguments to the DrawImage function a Point2D        DrawImage(Image, Screen, unit1.loc);}

##### Share on other sites
Thanks man much appreciated!

##### Share on other sites
Hey guys,

sorry to keep dragging this topic up, but i just wanted to ask one more question about this whole struct and unit location stuff.

I'm now using the struct Point2D in a class Selection.h which is included in my building and unit classes basically as a main way to set and get x,y values.

For example, my selection.h:

#ifndef SELECTION_H
#define SELECTION_H

#include "SDL/SDL.h"

class Selection {
private:
mX, mY;
enum SelectionStates { NOT_SELECTING, SELECTING };
SelectionStates state;
public:

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

double mouseX, mouseY;
Selection();
Selection s1, s2; /* im trying to pass 2 instances of selection so i can use s1.getminX(), s1.getMinY(), s2.getMaxX(), and s2.getMaxY() to set selected or not selected for the units and draw the selection area */
bool update();
void render( SDL_Surface *screen );
double getMinX() const;
double getMaxX() const;
double getMinY() const;
double getMaxY() const;
};

Am i doing this wrong ? Im geting errors that Point2D is unreferenced and so is s1 and s2. And invalid use of non static data member "Selection::mX and mY ?

My selection.cpp looks like this:

#include "selection.h"
#include <iostream>
#include <stdlib.h>
#include <SDL/SDL.h>

Selection::Selection()
{
state = NOT_SELECTING;
mX = 0;
mY = 0;
}

bool Selection::update(double mouseX, mouseY) {
//int mouseX, mouseY;
Uint8 mouseState = SDL_GetMouseState( &mouseX, &mouseY );

if( state == SELECTING ) {
if( !( mouseState & SDL_BUTTON(1) ) ) {
state = NOT_SELECTING;
return true;
} else {
s1.setX( mouseX );
s1.setY( mouseY );

}
} else {
if( mouseState & SDL_BUTTON(1) ) {
state = SELECTING;
s1.setX( mouseX );
s1.setY( mouseY );
s2.setX( mouseX );
s2.setY( mouseY );
}
}

return false;

}

void
Selection::render( SDL_Surface *screen ) {
if( state == SELECTING ) {
rectangleColor( screen, s1.getMinX(), s1.getMinY() , s2.getMaxX(), s2.getMaxY(), 0xfee19080 );
}
}

double
Selection::getMinX() const {
return ( s1.getX() < s2.getX() ? s1.getX() : s2.getX() );
}

double
Selection::getMaxX() const {
return ( s1.getX() > s2.getX() ? s1.getX() : s2.getX() );
}

double
Selection::getMinY() const {
return ( s1.getY() < s2.getY() ? s1.getY() : s2.getY() );
}

double
Selection::getMaxY() const {
return ( s1.getY() > s2.getY() ? s1.getY() : s2.getY() );
}

Sorry for long post!

Any ideas ?

Sykth

##### Share on other sites
Update:

could i use

typedef Point2D SelectionOne, SelectionTwo;

struct Selections
{
selectionOne s1;
selectionTwo s2;
}

In my selection class with the orignal Pointer2D ? and then use calls like s1.getX() and so on to set my selection area ?

##### Share on other sites
That would be a bad idea. SelectionOne and SelectioTwo does not deserve to be data types. It sounds to me that they should be two instances of Point2D:
Point2D SelectionOne, SelectionTwo;
Infact, I would rather make a rectangle struct and only have one of them.

Some changes you might find clearifying:

// selection.h:#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;	}; // Dont forget to finish a class/struct with a ;class Selection {private:	enum SelectionStates { NOT_SELECTING, SELECTING };	SelectionStates state;public:                    	Selection();	// I dont think the Point2D class belong inside a Selection class. 	// Rather just create two instances of it	Point2D s1, s2; 	bool update();	void render( SDL_Surface *screen );	double getMinX() const;	double getMaxX() const;	double getMinY() const;	double getMaxY() const;};#endif // dont forget this// Selection.cpp//#include "selection.h"#include <iostream>#include <stdlib.h>#include <SDL/SDL.h>Selection::Selection() {	state = NOT_SELECTING;}bool Selection::update(int mouseX, int mouseY, int mouseState) {	if( state == SELECTING ) 	{		if( !( mouseState & SDL_BUTTON(1) ) ) 		{			state = NOT_SELECTING;			return true;		} 		else 		{			s1.setX( mouseX );			s1.setY( mouseY );		}	} 	else 	{		if( mouseState & SDL_BUTTON(1) ) 		{			state = SELECTING;			s1.setX( mouseX );			s1.setY( mouseY );			s2.setX( mouseX );			s2.setY( mouseY );		}	}		return false;	}void Selection::render( SDL_Surface *screen ) {	if( state == SELECTING ) 	{		rectangleColor( screen, s1.getMinX(), s1.getMinY() , s2.getMaxX(), s2.getMaxY(),  0xfee19080 );	}}double Selection::getMinX() const {   return ( s1.getX() < s2.getX() ? s1.getX() : s2.getX() );}double Selection::getMaxX() const {   return ( s1.getX() < s2.getX() ? s1.getX() : s2.getX() ); }double Selection::getMinY() const {   return ( s1.getY() < s2.getY() ? s1.getY() : s2.getY() ); }double Selection::getMaxY() const {   return ( s1.getY() < s2.getY() ? s1.getY() : s2.getY() ); }int main(){	    int mouseX, mouseY;	Uint8 mouseState;		// ...	while(game_is_still_running)	{		// I placed the SDL_GetMouseState here. No need to call it once for each selection in the game.		// Once per cycle will do		mouseState = SDL_GetMouseState( &mouseX, &mouseY );				unit1.selection.update(mouseX, mouseY, mouseState);		// ...	}	// ...}

I assume that you want to have a selection rectangle so that the player can select units by dragging a selection over them.
Once the player release the mouse button, the units contained in the selection rectangle
should be considered selected. Is this right?

##### Share on other sites
yes thats correct :) thanks for your input :)

##### Share on other sites
No problem =)

You said that you thought it was too simple earlier, but Id say it is the other way around.
You should convince yourself that there must be a simpler/slickier way to do it and find it :D
Its a lot of tools in the language and the libraries to make things like this look easy, and it seems like you still need to read up on some of them.

Anyway, I suggest that you also create a Player struct and place an instance of Selection in there. Not in the Unit struct.
A game often have more units than players, so there would typically be one struct for each of them. The Selection object would go in the Player struct, and all the Unit struct need is a bool selected.
The Unit struct's render function can check this variable, and if it is true, you add some extra graphics to indicate that this unit is selected.
This variable is also needed other places in the game, like in the AI module.

This is ofcourse just one way to do it. There is many, and most of them are more sophisticated.

Feel free to ask, and good luck

##### Share on other sites
Hey,

massive thanks for the input so far. Unfortunately i'm still experiencing problems with this struct stuff. I have taken aboard pulp's advice, and at the moment all of my files were compiling ok until I suffered a minor hick up with s1, s2 being "undeclared" and by getMinX() etc. not being a member of my struct Point2D.

I rectified this by declaring the getMinxX() methods inside the Point2D struct and then declaring their contents in the public section of my selection.h

This was wonderful except when i compile main, even tho i have included selection.h in all relevant files such as my baseBuilding, testBuliding (or soon to be barracks) and unit.h i get error messages that all my mini structs for setting building and unit locations are undeclared. For example:

// above is class testBuilding : sprite and includes (including selection.h)
public:

typedef Point2D tBuildingLocation, tBuildingDrawArea;

struct tBuilding
{
tBuildingLocation tBLoc;
tBuildingDrawArea tBDArea;
}; //added semi-colon still same errors

void setTBuildingXLoc(int mouseX)
{
tBLoc.mX = mouseX;
}

void setTbuildingYLoc(int mouseY)
{
tBLoc.mY = mouseY;
}

double getTbuildingXLoc()
{
return tBLoc.mX;
}

double getTbuildingYloc()
{
return tBLoc.mY;
}

I really cant see anything wrong with it, but im getting these errors tBloc undeclared. My baseBuilding is the same and it produces similar errors except its called bLoc. Any ideas ?

[Edited by - Sykth on March 5, 2007 6:24:54 AM]

*shameless bump*

##### Share on other sites
Quote:
 at the moment all of my files were compiling ok until I suffered a minor hick up with s1, s2 being "undeclared" and by getMinX() etc. not being a member of my struct Point2D.

s1 and s2 should be declared if you place the Point2D struct outside and above the Selection struct.
Note that a struct is very different from an instance of a struct.
The Point2D struct is like a blueprint for the compiler so that it knows how to create a Point2D.
For example, if you want to have a Point2D inside the Selection struct it would look like this.
// First we must create the blueprint of a Point2D:struct Point2D{// same old...};// At this point the compiler knows how to create a Point2D and will give us no errors if we do.struct Selection{    // Here we create an instance of a Point2D. This one happens to be inside the Selection struct    Point2D s1;};// Putting a struct inside another struct, like this:struct Selection{    struct Point2D    {        // ...    };};/*This is like putting a blueprint inside another blueprint. It is possible, but it does not make sense in this situation. This kind of construct is rarely needed.To create a Point2D instance we would now have to type Selection::Point2D myPoint;*/

Quote:
 I rectified this by declaring the getMinxX() methods inside the Point2D struct and then declaring their contents in the public section of my selection.h

getMinX() does not belong in the Point2D struct. Remember that there is only one x in a Point2D so MinX does not make sense here.
It seems to me that what you need inside the Selection struct is a rectangle.
This rectangle will replace s1 and s2.
I have made a rectangle struct that will take care of your needs.
It has a bit more functionality than a traditional Rectangle, so I called it Area:
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;	}};

Now, rather than having Point2D s1, s2; in the Selection struct you get Area selection;
This Area struct will also make sure that left <= right and top <= bottom, as long as you only use the get* and set* functions to change its variables (left, top, right, bottom)
Using this you no longer need the Min/Max functions at all.

Quote:
 This was wonderful except when i compile main, even tho i have included selection.h in all relevant files such as my baseBuilding, testBuliding (or soon to be barracks) and unit.h i get error messages that all my mini structs for setting building and unit locations are undeclared. For example:// above is class testBuilding : sprite and includes (including selection.h)public: typedef Point2D tBuildingLocation, tBuildingDrawArea; struct tBuilding { tBuildingLocation tBLoc; tBuildingDrawArea tBDArea; }; //added semi-colon still same errors void setTBuildingXLoc(int mouseX) { tBLoc.mX = mouseX; } void setTbuildingYLoc(int mouseY) { tBLoc.mY = mouseY; } double getTbuildingXLoc() { return tBLoc.mX; } double getTbuildingYloc() { return tBLoc.mY; } I really cant see anything wrong with it, but im getting these errors tBloc undeclared. My baseBuilding is the same and it produces similar errors except its called bLoc. Any ideas ?

I can use setTBuildingXLoc as an example.
If you change it to
void setTBuildingXLoc(tBuilding& building, int mouseX){    building.tBLoc.setX(mouseX);    // building.tBLoc.mX = mouseX;     // The above line will work if you remove the private: keyword in the     // Point2D struct, but it is not recommended.     // (See the chapter about classes for more info about these things)}

it will work, or at least one step closer to working.

Another option (the one I used earler with the Point2D struct) is to make the functions part of the tBuilding struct.
After all, all these functions do is to operate on the variables inside a tBuilding struct.

This line:
typedef Point2D tBuildingLocation, tBuildingDrawArea;
is rely not needed. We already made a data type called Location for this purpose, or just use a Point2D and skip the typedef all together.
The last one, tBuildingDrawArea sounds like a rectangle (area = rectange right?), so use the Area struct from my earlier code example instead. Something along these lines:
struct tBuilding{    Point2D location;    Area drawArea;    void setXLoc(int mouseX)    {        location.setX(mouseX);    }    int getXLoc() const     {        return location.getX();     }    // ...};

Also note that a struct is like a package. Everything you put inside a struct is "invisible" to the rest of the code. Thats a simple way to put it but what the heck...
So, we can do like this:
struct tBuilding{    Point2D location;    ...};struct tPlayer{    Point2D location;    ...};Point2D location;...

None of these three variables will get a naming conflict.
From the compilers viewpoint they are called
tBuilding::location, tPlayer::location and location
In other words, it is able to differenciate between them so there is no error =)

PS:
You dont want to bump on gamedev m8 =)
If you dont get response on your post, it is usually becouse you did not express yourself clearly or becouse you ask something you should know already.

##### Share on other sites
Thanks for all your help pulpfist, artum and co., and apologies for the bump, im new to forums :)

##### Share on other sites
Hey there i've recently encountered some minor problems with the Area() struct that was created here for me i was wondering if any of you guys could help me out.

Using the previously stated Area() 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;	}};

I am having problems getting the rendered rectangle for my selection to draw at the proper coordinates! When i left click in my game the point at which i clicked at draws a rectangle's bottom right corner and the top left is stuck at (0,0). I can't seem to figure out whats going wrong so i'll slap up my selection.cpp in hope an expert eye can pick out what has been confusing me for some time now:

#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 );              //selection.setAll( mouseX, mouseY, mouseX, mouseY);				}	}		return false;	}void Selection::render( SDL_Surface *screen ) {	if( state == SELECTING ) 	{		rectangleColor( screen, selection.left(), selection.top(), selection.right(), selection.bottom(), 0xfe00008b);	}}

Any ideas will mbe much appreciated!

Sykth