25 August 2021, 18:00 | #1 |
Registered User
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! |
25 August 2021, 18:51 | #2 |
Registered User
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. |
25 August 2021, 18:59 | #3 |
Registered User
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. |
25 August 2021, 19:42 | #4 |
Registered User
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.
|
25 August 2021, 20:21 | #5 |
Registered User
Join Date: Mar 2012
Location: UK
Posts: 1,893
|
Now I know who to blame if it all goes Pete Tong
|
26 August 2021, 12:00 | #6 |
Registered User
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. |
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 |
|
|