|
Forum Index : Microcontroller and PC projects : Drawing better lines
| Author | Message | ||||
| karjo238 Regular Member Joined: 12/10/2018 Location: New ZealandPosts: 60 |
Hello there again, for the umpteenth time, it seems. As you know, ?I think I've mentioned it?, I'm writing Missile Command for the Colour Maximite, and having a great time flexing my rusty code writing skills in the process. One of the most important parts of Missile Command is the lines that go hither and thither blowing stuff up. Because these lines grow over time rather than being drawn all at once, I had to find some code that would let me do this. I found this C# code: public void line(int x,int y,int x2, int y2, int color) { int w = x2 - x ; int h = y2 - y ; int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ; if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ; if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ; if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ; int longest = Math.Abs(w) ; int shortest = Math.Abs(h) ; if (!(longest>shortest)) { longest = Math.Abs(h) ; shortest = Math.Abs(w) ; if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ; dx2 = 0 ; } int numerator = longest >> 1 ; for (int i=0;i<=longest;i++) { putpixel(x,y,color) ; numerator += shortest ; if (!(numerator<longest)) { numerator -= longest ; x += dx1 ; y += dy1 ; } else { x += dx2 ; y += dy2 ; } } } and converted it into MMBasic. The two major changes I made were for it to be drawn one pixel at a time, and split it into two functions, one that is only run once, and one that it run every cycle: Sub lineCode(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a12,a13) If a12 + a13 = Timer Then a12 = Timer Pixel(a1,a2) = 7 Rem a9 = a9 + a8 inc a9,a8 If Not(a9 < a7) Then a9 = a9 - a7 a1 = a1 + a3 a2 = a2 + a4 Else a1 = a1 + a5 a2 = a2 + a6 EndIf Rem a10 = a10 + 1 inc a10 EndIf End Sub Sub initMiss(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,x2,y2) w = x2 - a1 h = y2 - a2 If w<0 Then a3 = -1 Else a3 = 1 If h<0 Then a4 = -1 Else a4 = 1 If w<0 Then a5 = -1 Else a5 = 1 a7 = Abs(w) a8 = Abs(h) If Not(a7 > a8) Then a7 = Abs(h) a8 = Abs(w) If h<0 Then a6 = -1 Else a6 = 1 a5 = 0 EndIf a9 = RShift(a7,1) a10 = 0 End Sub The problem I have right now is that the code requires 9 data points to be able to compute the next point for the line, and for the purposes of Missile Command, I need to add a further 6 data points to make it work in game. Basically, it works - it looks really nice - but it's not efficent. I read somewhere (I can't find the reference now) that you can write C code, and link that to your code, and have that C code run much faster. What I'd like to do is modify that C code to something like: - function a1,a2 = linePoint(x1,y1,x2,y2,p) - given the start coordinates x1,y1 and end coordinates x2,y2, return the coordinates of point p along the line generated by Bresenhams algorithm. The function would have to iterate maybe 60 or 70 times, but it would be faster in C as opposed to MMBasic. Is this at all remotely possible? Is it really that much more efficient? I fully admit I've dug a hole for myself here, and what I'd like to do to get out of the hole is waaaaaaaaaay above my pay grade, so if anybody here has any suggestions, they'd be greatly appreciated. Many thanks, Joseph |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4133 |
In case things like inc and RShift are calling functions/subs then that is an easily avoided overhead. I don't know if Cfuncs are available on the CM but if they are then matherp did a number of tutorials which are on TBS. John |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
Unfortunately CFunctions are not available on the Maximite (only on the Micromite). Currently the only way to add C code to the Maximite is to download the source, add your code and recompile. Geoff Geoff Graham - http://geoffg.net |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1671 |
CFunction are not available for the Maximites. AFAIK a there are no plans (by Geoff) to change that. Michael causality ≠ correlation ≠ coincidence |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1671 |
Geoff was faster! causality ≠ correlation ≠ coincidence |
||||
| karjo238 Regular Member Joined: 12/10/2018 Location: New ZealandPosts: 60 |
Ah drat Alrighty then, time to reconsider what I want to do at this point. I'll request a copy of the source code anyhow, because it occurred to me I *might* be able to take Geoffs existing line code and modify it for my needs, but I may also be in over my head.Many thanks, Joseph. |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |