22 October 2023, 15:32 | #1 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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 Last edited by DisasterIncarna; 22 October 2023 at 15:39. |
22 October 2023, 19:54 | #2 |
Registered User
Join Date: Nov 2010
Location: South Wales
Age: 47
Posts: 947
|
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. |
22 October 2023, 20:17 | #3 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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. |
23 October 2023, 11:14 | #4 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,671
|
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.
|
23 October 2023, 11:37 | #5 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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.
|
23 October 2023, 11:40 | #6 |
Registered User
Join Date: Jan 2022
Location: Wales
Posts: 92
|
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.
|
23 October 2023, 11:41 | #7 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,671
|
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.
|
23 October 2023, 13:50 | #8 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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. |
23 October 2023, 14:02 | #9 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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.
|
23 October 2023, 14:07 | #10 |
HOL/FTP busy bee
Join Date: Sep 2006
Location: Germany
Age: 46
Posts: 32,192
|
That imgur link shows me this:
|
23 October 2023, 14:19 | #11 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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.
|
23 October 2023, 14:21 | #12 |
HOL/FTP busy bee
Join Date: Sep 2006
Location: Germany
Age: 46
Posts: 32,192
|
Maybe the dragon is too hot for imgur?
|
23 October 2023, 14:26 | #13 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
|
23 October 2023, 14:28 | #14 |
HOL/FTP busy bee
Join Date: Sep 2006
Location: Germany
Age: 46
Posts: 32,192
|
Ah, problem fixed now
|
23 October 2023, 16:39 | #15 | |
Registered User
Join Date: May 2023
Location: Norwich
Posts: 469
|
Quote:
|
|
23 October 2023, 22:27 | #16 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 48
Posts: 3,881
|
|
24 October 2023, 08:21 | #17 |
Registered User
Join Date: Dec 2016
Location: Warsaw area
Posts: 153
|
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 |
24 October 2023, 09:32 | #18 |
Registered User
Join Date: Jan 2022
Location: Wales
Posts: 92
|
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. |
24 October 2023, 10:23 | #19 |
Registered User
Join Date: Oct 2021
Location: England
Posts: 1,284
|
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. |
24 October 2023, 14:26 | #20 | |
Registered User
Join Date: Nov 2010
Location: South Wales
Age: 47
Posts: 947
|
Quote:
I think something oft overlooked is that we spend something like 80% of our time maintaining existing code rather than writing new code |
|
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 |
|
|