English Amiga Board


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

 
 
Thread Tools
Old 25 August 2021, 18:00   #1
Havie
Registered User
 
Havie's Avatar
 
Join Date: Mar 2012
Location: UK
Posts: 1,893
Structured Code

So, starting my next game and I know that my code always becomes a mess of spaghetti so thought I'd pick everyone's brain as to how to structure it neatly!

I am guessing the statements and functions are the way to go but it seems to me that you are adding an overhead to your program by doing this? Jump to procedure and share required variables, on top of just running the code?

Always used Gosubs (but I was weaned on ZX basic) which I know is frowned upon and seems to clog up the stack.

All suggestions welcome before I get too deep into the game!
Havie is offline  
Old 25 August 2021, 18:51   #2
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
If you need absolute speed, then there's no real substitute for Gosubs. Adding layers of structure adds overheads. However, the overheads might not be as much as you think, so it's worth investigating whether they work for your use case.

If you're using AmiBlitz, there's also the option of making functions and statements "fast" by using the FAST keyword. These have certain limitations (local variables aren't initialised and possibly only primitive types are allowed), but they have a lower overhead than standard functions.

I tend to use functions and statements wherever possible, and go one step further by putting them in a separate file to be included at compile time. But Gosubs do have their place if you need an extra few cycles here and there, and I would use them for critical parts of the main game loop, in particular if they're called many times per frame. Calling a function or statement just once or twice a frame is unlikely to have a significant impact though...

Also worth looking into are macros - these can take arguments and are expanded to in-line code by the compiler, but can make for neater code with no significant overheads at runtime.
Daedalus is offline  
Old 25 August 2021, 18:59   #3
Havie
Registered User
 
Havie's Avatar
 
Join Date: Mar 2012
Location: UK
Posts: 1,893
Just using plain old Blitz 2.1 here.

Sounds like a combination of functions/statements and gosubs is there way to go. Just trying to keep my code tidy and neat (not very good at that).

Thanks.
Havie is offline  
Old 25 August 2021, 19:42   #4
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Cool, well I'd suggest doing everything as a function/statement, and only converting to inline code or Gosubs if you need the extra cycles.
Daedalus is offline  
Old 25 August 2021, 20:21   #5
Havie
Registered User
 
Havie's Avatar
 
Join Date: Mar 2012
Location: UK
Posts: 1,893
Now I know who to blame if it all goes Pete Tong
Havie is offline  
Old 26 August 2021, 12:00   #6
carrion
Registered User
 
carrion's Avatar
 
Join Date: Dec 2016
Location: Warsaw area
Posts: 152
In one of my big Blitz Basic projects I still work on I changed all functions/procedures to...
JSR + RTS combination.
I also use only global variables so before calling a subroutine by JSR I setup parameters like:
a=123; b=456; x=0; JSR subroutine

to be honest I can't measure how much faster it is than GOSUB but it gives me feeling that I do control the situation.

I also try to use assembly code where possible.
carrion 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
Why does this code crash? kamelito Coders. General 8 23 December 2019 18:38
Any C/C++ example code? vim Coders. C/C++ 6 10 February 2015 05:34
What's this code doing? Jherek Carnelia Coders. General 13 15 August 2011 17:55
Motorola 68000 Structured Assembler Manual. Thorham Coders. General 3 16 May 2009 15:16
3D code and/or internet code for Blitz Basic 2.1 EdzUp Retrogaming General Discussion 0 10 February 2002 11:40

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 22:31.

Top

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