|
| 1 | +Fast Two Player Code using RGBASM |
| 2 | +(or Fast OOP using RGBASM) |
| 3 | +Written by GeeBee |
| 4 | +With an addition by Otaku |
| 5 | + |
| 6 | + As an example, let's say you have an assembly language two |
| 7 | +player game that you are working on where the majority of |
| 8 | +the code for player one might be identical for player two. |
| 9 | +There are basically two ways of handling this: |
| 10 | + |
| 11 | + 1) Pass a pointer to these routines that points to the |
| 12 | + "object" structure that you wish to process. |
| 13 | + |
| 14 | + 2) Repeat all of these code routines using slightly |
| 15 | + different function/variable names. |
| 16 | + |
| 17 | + The advantage to using approach number 1 is that you can |
| 18 | +have two or more objects that share the same code routines. |
| 19 | +One disadvantage is the code will be slower than approach |
| 20 | +2 because you have to access the object structure through |
| 21 | +a pointer. Also this pointer can tie up valuable GB registers |
| 22 | +which can cause longer/slower code due to fewer available |
| 23 | +registers. |
| 24 | + |
| 25 | + The advantage to using approach number 2 is that since your |
| 26 | +object data doesn't have to be accessed through a pointer the |
| 27 | +code can be much quicker which sometimes is very important |
| 28 | +on the limited GB/GBC platform. The disadvantages are that |
| 29 | +code must be repeated for each object (which can rapidly eat |
| 30 | +up ROM space for a large number of objects) and having several |
| 31 | +identical routines (with different function & variable names) |
| 32 | +is a big maintenance problem when the same changes need to be |
| 33 | +applied to each object. |
| 34 | + |
| 35 | + |
| 36 | + If you can get around the maintenance problems of approach |
| 37 | +number 2, this can be ideal in some cases where you need |
| 38 | +fast execution speed OR in cases where you have just finished |
| 39 | +coding a 1 player game and as an after-thought you decide to |
| 40 | +add 2 player support but you don't want to rewrite your code |
| 41 | +to use object pointers. |
| 42 | + |
| 43 | + RGBASM is powerful enough that you can eliminate the majority |
| 44 | +of the maintenance problems in this case. Here's example code: |
| 45 | +(The macro operator '@' is incremented with each macro included |
| 46 | +in your source code so this code needs to be compiled stand-alone |
| 47 | +and then linked with other code or you must guarantee that no |
| 48 | +macros are included before this code. The macro FOO below is |
| 49 | +only used to increment '@' from 0 to 1 so that the first object |
| 50 | +ends with _1 instead of _0.) |
| 51 | + |
| 52 | + |
| 53 | +ObjectX_1: DB |
| 54 | +ObjectY_1: DB |
| 55 | +ObjectX_2: DB |
| 56 | +ObjectY_2: DB |
| 57 | + |
| 58 | +FOO: MACRO |
| 59 | + db 0 |
| 60 | + ENDM |
| 61 | + |
| 62 | +; Set Object X,Y coordinates to H,L |
| 63 | + |
| 64 | +OBJCODE: MACRO |
| 65 | + |
| 66 | +SetObjectPos\@: |
| 67 | + ld a,h |
| 68 | + ld [ObjectX\@],a |
| 69 | + ld a,l |
| 70 | + ld [ObjectY\@],a |
| 71 | + ret |
| 72 | + |
| 73 | +GetObjectPos\@: |
| 74 | + ld a,[ObjectX\@] |
| 75 | + ld h,a |
| 76 | + ld a,[ObjectY\@] |
| 77 | + ld l,a |
| 78 | + ret |
| 79 | + |
| 80 | + ENDM |
| 81 | + |
| 82 | +; Include code for object #1 |
| 83 | + OBJCODE |
| 84 | +; Include code for object #2 |
| 85 | + OBJCODE |
| 86 | + |
| 87 | +; Set Object 1 coordinates to 0,0 |
| 88 | + ld hl,0 |
| 89 | + call SetObjectPos_1 |
| 90 | + |
| 91 | +; Set Object 2 coordinates to 0,0 |
| 92 | + ld hl,0 |
| 93 | + call SetObjectPos_2 |
| 94 | + |
| 95 | +; Return Object 1 coordinates in H,L |
| 96 | + call GetObjectPos_1 |
| 97 | + |
| 98 | +; Return Object 2 coordinates in H,L |
| 99 | + call GetObjectPos_2 |
| 100 | + |
| 101 | +-- |
| 102 | +added by Otaku |
| 103 | + |
| 104 | +Using SET to point to the start of your object |
| 105 | +structure, and then calculating offsets into the |
| 106 | +object structure is a neater way of doing this, |
| 107 | +without having to worry about an idle use of MACRO |
| 108 | +incrementing the @ counter. |
| 109 | + |
| 110 | +So: |
| 111 | +Object1: DS 16 |
| 112 | +Object2: DS 16 |
| 113 | + |
| 114 | + RSRESET |
| 115 | +X_POS RB 2 |
| 116 | +Y_POS RB 2 |
| 117 | +HEALTH RB 1 |
| 118 | + |
| 119 | + |
| 120 | +; assume that x & y are passed in BC & DE |
| 121 | +; assume for this example that GB has the ability |
| 122 | +; to move registers directly to memory without using |
| 123 | +; the A register |
| 124 | +SetObjectPos: MACRO |
| 125 | +ObjectBase SET \1 |
| 126 | + LD [ObjectBase+X_POS],C |
| 127 | + LD [ObjectBase+X_POS+1],B |
| 128 | + LD [ObjectBase+Y_POS],E |
| 129 | + LD [ObjectBase+Y_POS+1],D |
| 130 | + PURGE ObjectBase |
| 131 | + ENDM |
| 132 | + |
| 133 | +SetObject1Pos: |
| 134 | + SetObjectPos Object1 |
| 135 | + RET |
| 136 | +SetObject2Pos: |
| 137 | + SetObjectPos Object2 |
| 138 | + RET |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
0 commit comments