English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 22 October 2023, 15:32   #1
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
Dumb Question - More Efficient Way to Gosub?

Soo, im rewriting my GUI program from scratch using better more efficient code, basically trimming the fat, when i made my GUI program originally i was perfectly fine for the features i had at the time, but now im stuffing more and more into it, it is easy to see my hack/bolt in extras kinda bloat what i have so im starting from scratch with all my current features in mind.

Things are going well so far, 1 little annoyance however is my main loop for waiting/reading for gadtools gadget hits, all my gadgets are assigned individual numbers starting from 50 and i just noticed that once a gadget is hit all i am doing pretty much is calling a gosub according to what was hit, heres a trimmed/cut down version of that as an example.

Code:
; Main Loop
Repeat
  WHATEVENT.l = WaitEvent                       ; Wait for IDCMP event
  KEYHIT$ = Inkey$                              ; Check for keypress.
  If WHATEVENT= $40 AND EventWindow = mywindow  ; Do something if gadget is selected/hit
     Select GadgetHit
       Case #_SEARCHBOX        ; Gadget 50
         Gosub HITSEARCHBOX
       Case #_SEARCHBUTT       ; Gadget 51
         Gosub HITSEARCHBUTT
       Case #_DESTBOX          ; Gadget 52
         Gosub HITDESTBOX
       Case #_DESTBUTT         ; Gadget 53
         Gosub HITDESTBUTT
       Case #_EXTRACTTYPE      ; Gadget 54
         Gosub HITEXTRACTTYPE
     End Select
  EndIf
  If KEYHIT$ = Chr$(27) Then WHATEVENT= $200    ; Exit loop on escape button
Until WHATEVENT= $200
As you can see theres only a handful of example Gosub's thrown in, but once i have a LOT more thrown in it looks kinda messy, i was wondering if there is a better way to call the gosub routines without what i am doing above, as they are all based off gadget numbers, is there perhaps a way to do something like Gosub HITGADGET with the gadget number appended on the end of it via a variables result, so if i click gadget 53 then Gosub HITGADGET53 happens, meaning i could remove all the unneccesary case select/gosub's or do i have to stick with the precise/exact method i am using already?

Last edited by DisasterIncarna; 22 October 2023 at 15:39.
DisasterIncarna is offline  
Old 22 October 2023, 19:54   #2
S0ulA55a551n
Registered User
 
S0ulA55a551n's Avatar
 
Join Date: Nov 2010
Location: South Wales
Age: 47
Posts: 937
Does blitz not have functions ? It might be better to create functions that do what you need. and Pass the data back and for.

So you could have a HITSEARCHBOX function that takes the garnet number as parameter

I think you still need a switch case as I assume this is your main loop that is polling for events.
S0ulA55a551n is offline  
Old 22 October 2023, 20:17   #3
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
yeah i mean i could also just nest my subroutines directly into the case statements thus not needing any gosubs, i only opted for the gosubs because amiblitz3 conveniently has a window for all your subroutine names you can click to jump to that position, if they are all nested into 1 function then its just harder to quickly jump around the code.

Was hoping to delete a load of the case/gosub statements if they could all be replaced with 1 simple Gosub GADGETHIT+<Appended Variable Number> nothing really needed, it would just help chopping down code size while keeping things readable.
DisasterIncarna is offline  
Old 23 October 2023, 11:14   #4
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,167
I'm no basic programmer but looking at your construct, do you especially need to worry about the efficiency of GOSUB here? It's event driven so I don't know that it's going to make any difference to optimise the way the handler is reached once an event has arrived.
Karlos is offline  
Old 23 October 2023, 11:37   #5
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
a have a lot of events and no its not really needed, im just looking to trim some fat and a whole heap of case select/gosubs kinda stands out, but only if there was a convenient way to shrink that all down. Again its not really needed, just a nifty thing to have if possible that might reduce some code size.
DisasterIncarna is offline  
Old 23 October 2023, 11:40   #6
aNdy/AL/COS
Registered User
 
aNdy/AL/COS's Avatar
 
Join Date: Jan 2022
Location: Wales
Posts: 91
I've not used 'Case' in Blitz Basic for 30 odd years, but how you're doing it already seems to be pretty 'standard'. I use PureBasic on Windows/Linux and that's pretty much what I've done for my applications, save having to use 'Gosub' because I can call a subroutine by it's name only.
aNdy/AL/COS is offline  
Old 23 October 2023, 11:41   #7
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,167
I would risk an assumption here that GOSUB is maybe more computationally efficient than a function call as there's no parameter/return management to worry about.
Karlos is offline  
Old 23 October 2023, 13:50   #8
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
yeah i was wondering if we could include the result of a variable, in this case an eventcode/number, in my example 50 = a button gadget, and slap that on the end of a Gosub Call, something like Gosub HITGADGET{EventCode} which could equate to Gosub HITGADGET50.

So in my example code above, those 5 case select/gosubs is what i was hoping to be replaced with 1 Gosub, i would still need to keep something like below which is useful in the code source window as i can still click to go to those subroutines quickly...

Code:
.HITGADGET50
;  Do clever stuff here when button is clicked
Return

.HITGADGET51
; Do clever stuff here when listview is clicked
Return

.HITGADGET52
; Do clever stuff here when CycleGadget used
Return

Etc and so forth.
Again its nothing major or important, its just that in 2 projects i have that use a gadtools GUI replacing all my case selects and gosubs with 1 that uses the results of an EventCode number would let me chop nearly a page and a half of Case Select/Gosub's.
DisasterIncarna is offline  
Old 23 October 2023, 14:02   #9
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
heres a snap of my top 2 projects using a load of gadgets and i use a similar main loop to the example above: https://imgur.com/a/kEc69f7 yeah theres not exactly "loads", but like i said i was just wondering/looking of ways to trim some fat/space and this came to mind.
DisasterIncarna is offline  
Old 23 October 2023, 14:07   #10
TCD
HOL/FTP busy bee
 
TCD's Avatar
 
Join Date: Sep 2006
Location: Germany
Age: 46
Posts: 31,608
That imgur link shows me this:
TCD is offline  
Old 23 October 2023, 14:19   #11
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
yeah same here, no idea why, first time ive seen it, you can trust me, pictures of Amiga Gadtools programs arent any kind or pr0n i am aware of would post the pics as [ i m g ] links, but i dont think you can scale images in the links so theyd end up massive.
DisasterIncarna is offline  
Old 23 October 2023, 14:21   #12
TCD
HOL/FTP busy bee
 
TCD's Avatar
 
Join Date: Sep 2006
Location: Germany
Age: 46
Posts: 31,608
Maybe the dragon is too hot for imgur?
TCD is offline  
Old 23 October 2023, 14:26   #13
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
Quote:
Originally Posted by TCD View Post
Maybe the dragon is too hot for imgur?
Classic schoolboy error on my behalf...
DisasterIncarna is offline  
Old 23 October 2023, 14:28   #14
TCD
HOL/FTP busy bee
 
TCD's Avatar
 
Join Date: Sep 2006
Location: Germany
Age: 46
Posts: 31,608
Ah, problem fixed now
TCD is offline  
Old 23 October 2023, 16:39   #15
AestheticDebris
Registered User
 
Join Date: May 2023
Location: Norwich
Posts: 380
Quote:
Originally Posted by DisasterIncarna View Post
yeah i was wondering if we could include the result of a variable, in this case an eventcode/number, in my example 50 = a button gadget, and slap that on the end of a Gosub Call, something like Gosub HITGADGET{EventCode} which could equate to Gosub HITGADGET50.
Generally speaking, that sort of thing is a very bad idea even when it is possible. It creates code that's very brittle and difficult to maintain.
AestheticDebris is offline  
Old 23 October 2023, 22:27   #16
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,764
Quote:
Originally Posted by AestheticDebris View Post
Generally speaking, that sort of thing is a very bad idea even when it is possible. It creates code that's very brittle and difficult to maintain.
Indeed. Functions are much better.
Thorham is offline  
Old 24 October 2023, 08:21   #17
carrion
Registered User
 
carrion's Avatar
 
Join Date: Dec 2016
Location: Warsaw area
Posts: 152
Quote:
Originally Posted by Thorham View Post
Indeed. Functions are much better.
Maybe they are but as far as I can tell (and tested) they are slow in BB2. What I do in my quite complex code (for a game I develop for many years now) is that I use JSR (instead of GOSUB) and RTS assembler commands. I pass the arguments via global variables for example like this:

Code:
  a = 123 
  JSR subroutine
  ...
  ...

subroutine:
  do something with 'a' variable.
  ..
  ..
  RTS
it's faster and compiles strictly to the same code in assembly language.
carrion is offline  
Old 24 October 2023, 09:32   #18
aNdy/AL/COS
Registered User
 
aNdy/AL/COS's Avatar
 
Join Date: Jan 2022
Location: Wales
Posts: 91
You can use JSR and RTS directly in BB2?

If so, that's news to me and I'll be much happier since I'm use to that with 6502 assembler!

What is the speed difference?

Last edited by aNdy/AL/COS; 24 October 2023 at 13:59.
aNdy/AL/COS is offline  
Old 24 October 2023, 10:23   #19
DisasterIncarna
Registered User
 
DisasterIncarna's Avatar
 
Join Date: Oct 2021
Location: England
Posts: 1,180
nice, might have to give that a try, also i do use a few functions, 1 of which is a function to "try" and get a near pixel width accurate length of a text string from whatever font i am using, i have recently tried using a far less complex version which kinda works the same so might delete that particular function.

JSR/RTS?, never used that kind of thing before but ill try giving that a go, looks like a simple find/replace can be used to replace Gosub>JSR and Return>RTS.
DisasterIncarna is offline  
Old 24 October 2023, 14:26   #20
S0ulA55a551n
Registered User
 
S0ulA55a551n's Avatar
 
Join Date: Nov 2010
Location: South Wales
Age: 47
Posts: 937
Quote:
Originally Posted by Karlos View Post
I would risk an assumption here that GOSUB is maybe more computationally efficient than a function call as there's no parameter/return management to worry about.
I assumed efficient in this case, meant more readable/maintainable code

I think something oft overlooked is that we spend something like 80% of our time maintaining existing code rather than writing new code
S0ulA55a551n is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Probably a dumb question about the aca1232 source Hardware mods 4 05 March 2019 18:47
Dumb Question but need help with printers source support.Apps 2 04 June 2015 06:21
Probably a dumb question.. xlar54 support.Other 6 03 January 2010 21:20
Question from a dumb American. illy5603 support.Hardware 43 19 September 2009 00:27
Probably a really dumb question... BumpyCarrot New to Emulation or Amiga scene 3 28 February 2003 02:04

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:01.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.14680 seconds with 13 queries