![]() |
Forum Index : Microcontroller and PC projects : picoMite VGA my fake SPRITES approach via BLIT need advice
Author | Message | ||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 671 |
Hello everyone, as I am progressing in learning BASIC, I am aware of the fact that we can not use SPRITES (sadly), so I tested a different approach with BLIT. I was sitting the whole day for this little program, it works but I am asking if there is a simpler method with less code. What this program does: It draws a cursor in the foreground which is a framed BOX, while in the background is an image loaded with lots of different patterns. So normally you would do this with SPRITES. You can navigate the cursor with the arrow keys like a computer mouse cursor. Later on I want to build some kind of GUI application. Maybe one can use it as a base for a point and click game? Here is my code: 'Testprogram which displays a cursor that can be navigated via 'arrow keys while on the background is a complex image CLS Option Explicit MODE 2 Dim integer cursorX=100, cursorY=50, cursorXold=100, cursorYold=50 Dim string keyPressed Dim integer lastCursorKey Load image "background" 'MAIN LOOP ####################################################### Do readKeystroke Loop '------------------------------ Sub readKeystroke keyPressed=Inkey$ 'Get keystroke If keyPressed=Chr$(130) Then 'Left Arrow Key is pressed!------------------ If lastCursorKey = 131 Then 'LastCursorKey: Right cursorX=cursorX-20 EndIf If lastCursorKey = 128 Then 'LastCursorKey: Up cursorY=cursorY+10 cursorX=cursorX-10 Blit Write #1,cursorXold,cursorY,10,10 Blit Close #1 EndIf If lastCursorKey = 129 Then 'LastCursorKey: Down cursorY=cursorY-10 cursorX=cursorX-10 Blit Write #1,cursorXold,cursorY,10,10 Blit Close #1 EndIf If cursorX<>cursorXold And lastCursorKey<>128 And lastCursorKey<>129 Then Blit Write #1,cursorXold,cursorY,10,10 Blit Close #1 EndIf Blit Read #1,cursorX,cursorY,10,10 Box cursorX,cursorY,10,10,1,RGB(black) cursorXold = cursorX cursorX=cursorX-10 lastCursorKey=130 EndIf If keyPressed=Chr$(131) Then 'Right Arrow Key is pressed!------------------ If lastCursorKey = 130 Then cursorX=cursorX+20 EndIf If lastCursorKey = 128 Then cursorY=cursorY+10 cursorX=cursorX+10 Blit Write #1,cursorXold,cursorY,10,10 Blit Close #1 EndIf If lastCursorKey = 129 Then cursorY=cursorY-10 cursorX=cursorX+10 Blit Write #1,cursorXold,cursorY,10,10 Blit Close #1 EndIf If cursorX<>cursorXold And lastCursorKey<>128 And lastCursorKey<>129 Then Blit Write #1,cursorXold,cursorY,10,10 Blit Close #1 EndIf Blit Read #1,cursorX,cursorY,10,10 Box cursorX,cursorY,10,10,1,RGB(black) cursorXold = cursorX cursorX=cursorX+10 lastCursorKey=131 EndIf If keyPressed=Chr$(128) Then 'UP Arrow Key is pressed!------------------ If lastCursorKey = 129 Then cursorY=cursorY-20 EndIf If lastCursorKey = 130 Then cursorY=cursorY-10 cursorX=cursorX+10 Blit Write #1,cursorXold,cursorY+10,10,10 Blit Close #1 EndIf If lastCursorKey = 131 Then cursorY=cursorY-10 cursorX=cursorX-10 Blit Write #1,cursorXold,cursorY+10,10,10 Blit Close #1 EndIf If cursorY<>cursorYold And lastCursorKey<>130 And lastCursorKey<>131 Then Blit Write #1,cursorX,cursorYold,10,10 Blit Close #1 EndIf Blit Read #1,cursorX,cursorY,10,10 Box cursorX,cursorY,10,10,1,RGB(black) cursorYold = cursorY cursorY=cursorY-10 lastCursorKey=128 EndIf If keyPressed=Chr$(129) Then 'DOWN Arrow Key is pressed!------------------ If lastCursorKey = 128 Then cursorY=cursorY+20 EndIf If lastCursorKey = 130 Then cursorY=cursorY+10 cursorX=cursorX+10 Blit Write #1,cursorXold,cursorY-10,10,10 Blit Close #1 EndIf If lastCursorKey = 131 Then cursorY=cursorY+10 cursorX=cursorX-10 Blit Write #1,cursorXold,cursorY-10,10,10 Blit Close #1 EndIf If cursorY<>cursorYold And lastCursorKey<>130 And lastCursorKey<>131 Then Blit Write #1,cursorX,cursorYold,10,10 Blit Close #1 EndIf Blit Read #1,cursorX,cursorY,10,10 Box cursorX,cursorY,10,10,1,RGB(black) cursorYold = cursorY cursorY=cursorY+10 lastCursorKey=129 EndIf If keyPressed=Chr$(13) Then ' ENTER Key is pressed!------------------ If lastCursorKey = 128 Then 'UP Box cursorX+2,cursorY+12,6,6,2,RGB(black) EndIf If lastCursorKey = 129 Then 'DOWN Box cursorX+2,cursorY-8,6,6,2,RGB(black) EndIf If lastCursorKey = 130 Then 'LEFT Box cursorX+12,cursorY+2,6,6,2,RGB(black) EndIf If lastCursorKey = 131 Then 'RIGHT Box cursorX-8,cursorY+2,6,6,2,RGB(black) EndIf EndIf End Sub Greetings Daniel Edited 2022-04-22 08:46 by Amnesie |
||||
Kabron![]() Regular Member ![]() Joined: 30/11/2017 Location: GermanyPosts: 65 |
Where is background? |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 671 |
On my SD-card (background.bmp) |
||||
vegipete![]() Guru ![]() Joined: 29/01/2013 Location: CanadaPosts: 1132 |
If you are not changing the size of the BLIT READ, do you need the BLIT CLOSE? Visit Vegipete's *Mite Library for cool programs. |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 671 |
Thank you for this question but I have no clue about how the BLIT actually works. The manual is a bit complicated to understand about this (for me at least). I think I started to close every BLIT buffer, because I had some trouble with ERROR messages. The problem is at this point I don't really understand how it works. Without the closes I only get errors... Greetings Daniel Edited 2022-04-23 04:31 by Amnesie |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2640 |
BLIT has two modes. The first reads back the pixels from one area of the screen memory and writes them to another. The second reads back the pixels from one area of the screen memory and saves them to RAM. A number of screen areas can be saved this way, but uses a lot of RAM. The stored areas can then be copied to any area of the screen. BLIT CLOSE recovers the RAM for normal use. Not closing means more RAM is used up each time you save another and you soon run out. |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 671 |
Ahh okay, so I've done it right (to close it every time)? Sprites would be more easy, is there any reason to not integrate SPRITES to the picoMite VGA @ Peter? At least one or two sprites would be helpful! Greetings Daniel Edited 2022-04-23 18:41 by Amnesie |
||||
vegipete![]() Guru ![]() Joined: 29/01/2013 Location: CanadaPosts: 1132 |
Yes, using BLIT CLOSE is probably correct. I was thinking of SPRITE READ on the CMM2, which will replace a previous image if the size is the same. Visit Vegipete's *Mite Library for cool programs. |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4044 |
The RP2040 in the Pico is very short of RAM. Compromises have to be made... John |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Just a small, better-efficiency tip: If lastCursorKey = 128 Then 'UP Box cursorX+2,cursorY+12,6,6,2,RGB(black) EndIf If lastCursorKey = 129 Then 'DOWN Box cursorX+2,cursorY-8,6,6,2,RGB(black) EndIf If lastCursorKey = 130 Then 'LEFT Box cursorX+12,cursorY+2,6,6,2,RGB(black) EndIf If lastCursorKey = 131 Then 'RIGHT Box cursorX-8,cursorY+2,6,6,2,RGB(black) EndIf If the value = 128, there's no point in testing for 129,130,131 select case lastCursorKey case 128 Box cursorX+2,cursorY+12,6,6,2,RGB(black) case 129 Box cursorX+2,cursorY-8,6,6,2,RGB(black) case 130 Box cursorX+12,cursorY+2,6,6,2,RGB(black) case 131 Box cursorX-8,cursorY+2,6,6,2,RGB(black) end select Craig |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
Also replace: cursorY=cursorY+10 with: Inc cursorY, 10 Using INC saves a variable lookup which is relatively expensive. There is no explicit DEC command, just suppy INC with a -ve argument. Best wishes, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Ah, nice one...thank you ![]() I remember reading about this, many moons ago but I recently looked for it in the manual and didn't find it. Must've been an out-of-date manual. Craig |
||||
Amnesie Guru ![]() Joined: 30/06/2020 Location: GermanyPosts: 671 |
Thanks both of you for the great tips and hints! :) Greetings Daniel |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |