Here is another include to add the prototyping toolkit, a quick and easy way to add a RTS style selection rectangle to your game, its just a few helpful functions to make the whole process hassle free
Online Example
SelectionRect.agc
// helper types
Type RECT
x1
y1
x2
y2
EndType
Type POINT
x
y
EndType
// draw the selection rect
Function DrawSelectionRect(x1,y1,x2,y2)
if (abs(x1-y1)>2) and (abs(x2-y2)>2)
col=MakeColor(255, 0, 0)
DrawBox(x1, y1, x2, y2,col,col,col,col, 0)
endif
EndFunction
// checks if a point is inside a rectangle
Function PointInRect(pt as POINT, rc as RECT)
result = 0
if pt.x > rc.x1 and pt.x<rc.x2 and pt.y>rc.y1 and pt.y<rc.y2
result = 1
endif
EndFunction result
// macro to quickly get a sprite rect
Function SpriteToRect(sprite_id, rc Ref as RECT)
rc.x1=GetSpriteX(sprite_id)
rc.y1=GetSpriteY(sprite_id)
rc.x2=GetSpriteX(sprite_id)+GetSpriteWidth(sprite_id)
rc.y2=GetSpriteY(sprite_id)+GetSpriteWidth(sprite_id)
EndFunction
// this function checks if the sprite co-ords are within the selection rectangle
// it check from all 4 possible selection directions
Function PointInSelectionRect(sprite_x, sprite_y, press_x, press_y, release_x, release_y)
result = 0
tlbr = (sprite_x>press_x and sprite_x<release_x and sprite_y>press_y and sprite_y<release_y)
bltr = (sprite_x>press_x and sprite_x<release_x and sprite_y<press_y and sprite_y>release_y)
brtl = (sprite_x<press_x and sprite_x>release_x and sprite_y<press_y and sprite_y>release_y)
trbl = (sprite_x<press_x and sprite_x>release_x and sprite_y>press_y and sprite_y<release_y)
if tlbr // top left to bottom right
result = 1
elseif bltr // bottom left to top right
result = 1
elseif brtl // bottom right to top left
result = 1
elseif trbl // top right to bottom left
result = 1
endif
EndFunction result
Example
// global POINT types for mouse co-ords
global m_down as POINT
global m_up as POINT
// our sprite type
Type sprite_box
sprite_id
selected
EndType
// global sprite array
sprBox as sprite_box[]
// create a bunch of sprites
for x=0 to 1024 step 50
for y = 0 to 768 step 50
sprTmp = CreateSprite(0)
SetSpriteSize(sprTmp, 32, 32)
SetSpriteOffset(sprTmp, 16, 16)
SetSpritePosition(sprTmp, x, y)
// add the sprite to the global array
tmpBox as sprite_box
tmpBox.sprite_id=sprTmp
tmpBox.selected=0
sprBox.insert(tmpBox)
next
next
do
// when the mouse is clicked set down co-ords
if GetPointerPressed()
m_down.x=GetPointerX()
m_down.y=GetPointerY()
endif
// when mouse is released
if GetPointerReleased()
// set up co-ords
m_up.x=GetPointerX()
m_up.y=GetPointerY()
// loop sprite array
for i=0 to sprBox.length
// get the sprite coords
sprite_x=GetSpriteXByOffset(sprBox[i].sprite_id)
sprite_y=GetSpriteYByOffset(sprBox[i].sprite_id)
// first we check if the sprite is inside the selection rectangle
if PointInSelectionRect(sprite_x, sprite_y, m_down.x, m_down.y, m_up.x, m_up.y)
// the sprite is selected
sprBox[i].selected=1
SetSpriteColor(sprBox[i].sprite_id, 255,0,0,255)
else
// sprite is not in selection rect or no rect selection made
// so check if the sprite was clicked
// get the sprite dimensions
rc as RECT
SpriteToRect(sprBox[i].sprite_id, rc)
// check if the mouse was released over a sprite
// if it was set it selected
if PointInRect(m_up, rc)
// the sprite is selected
sprBox[i].selected=1
SetSpriteColor(sprBox[i].sprite_id, 255,0,0,255)
else
// the sprite is NOT selected
sprBox[i].selected=0
SetSpriteColor(sprBox[i].sprite_id, 255,255,255,255)
endif
endif
next
endif
// the mouse is down so draw the selection rectangle
if GetPointerState()
DrawSelectionRect(m_down.x, m_down.y, GetPointerX(), GetPointerY())
endif
Sync()
loop