Correct/Fastest way of drawing a selection box?

Just starting out? Need help? Post your questions and find answers here.
User avatar
KilljoyHeathen
User
User
Posts: 13
Joined: Fri Feb 14, 2014 10:56 pm
Location: Ab, Canada

Correct/Fastest way of drawing a selection box?

Post by KilljoyHeathen »

Promise to be my last post for awhile, I just really like this language and since there is such a wealth of info from people much more experienced than I.........Yea yea shut up and get to the point right..

I am drawing a selection box for a simple start of a strategy game now I have read through quite a few posts and documentation, and this is the solution I came up with:

Code: Select all

;IncludeFile "Init.pb"
IncludeFile "Screen3DRequester.pb"
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

ClearColor = RGB(0,0,0)     ;Our ClearScreen Color
bX = 0
bY = 0
bX0 = 0
bX1 = 0
bX2 = 0
bY0 = 0
bY1 = 0
bY2 = 0
SelFlag = 0

Screen3DRequester()
UsePNGImageDecoder()
;UseJPEGImageDecoder()

LoadSprite(0,"swordcursor2.png",#PB_Sprite_AlphaBlending)
CreateSprite(1,1024,768)
;LoadSprite(1,"media/back1.jpg")

ExitCondition.b = 0

While ExitCondition = 0
  
  ClearScreen(ClearColor)
  FPS = Engine3DFrameRate(#PB_Engine3D_Current)     
  ExamineMouse()
  
  mX = MouseX()
  mY = MouseY()
  mC = MouseButton(#PB_MouseButton_Left)
  
  CreateCamera(0, 0, 0, 100, 100)
  CameraLookAt(0, 0, 0, 0)
  RenderWorld()
  DisplayTransparentSprite(0,mX,mY)
  
  If mC = 0
    SelFlag = 0
  EndIf
  
  If mC And SelFlag = 0
    SelFlag = 1
    bX = mX
    bY = mY
  EndIf
  
  If mC And SelFlag = 1
    bX2 = mX
    bY2 = mY
    If bX < bX2
      bX0 = bX
      bX1 = bX2
    Else
      bX0 = bX2
      bX1 = bX
    EndIf
    
    If bY < bY2
      bY0 = bY
      bY1 = bY2
    Else
      bY0 = bY2
      bY1 = bY
    EndIf      
    
    ;CreateSprite(1,1024,768)              ;Drawing box quicker
    StartDrawing(SpriteOutput(1))                     ;|
    Box(0,0,1024,768,ClearColor)                       ;|
    DrawText(0,0,"FPS: "+Str(FPS))                   ;|
    LineXY(bX0,bY0,bX1,bY0,RGB(255,0,0))         ;|
    LineXY(bX0,bY1,bX1,bY1,RGB(255,0,0))         ;|
    LineXY(bX0,bY0,bX0,bY1,RGB(255,0,0))         ;|    
    LineXY(bX1,bY0,bX1,bY1,RGB(255,0,0))         ;|
    StopDrawing()                                          ;|
    DisplaySprite(1,0,0)                                   ;|
    DisplayTransparentSprite(0,mX,mY)               ;| 
                                                                  ;|
  ;ElseIf IsSprite(1)                                         |
    ;FreeSprite(1)                               ;<-------|
  EndIf  
  
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Escape)
    ExitCondition = 1
  EndIf
  
  FlipBuffers()
  
Wend

End
I initially tried drawing to the directly to screen....wow painfully slow (Then read in the manual on Linux it would be), tried drawing to sprite outside of a 3d enviroment and almost as slow. If there is a way to do it without the 3d world, I would prefer that, I am not quite ready to get into figuring out 2d coord's from 3d etc...

If not, all is well, will keep reading through the forums and PB code archive.

Thanks.
“ It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Correct/Fastest way of drawing a selection box?

Post by blueb »

KilljoyHeathen wrote:P
... I am drawing a selection box for a simple start of a strategy game
You might do a forum search for: Programming 2D Scrolling Games Book

It's a free book by Krylar in PDF form that will give you lots of insight info.

-blueb
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
KilljoyHeathen
User
User
Posts: 13
Joined: Fri Feb 14, 2014 10:56 pm
Location: Ab, Canada

Re: Correct/Fastest way of drawing a selection box?

Post by KilljoyHeathen »

I didn't realize there was a free version. I seen the paperback version but wasn't ready to put out money for it yet until I was sure or comfortable with PureBASIC.

Will look for the book thank you for the suggestion. :)
“ It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas
Post Reply