Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:56 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

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: Germany
Posts: 671
Posted: 10:33pm 21 Apr 2022
Copy link to clipboard 
Print this post

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: Germany
Posts: 65
Posted: 06:32am 22 Apr 2022
Copy link to clipboard 
Print this post

Where is background?
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 671
Posted: 08:15am 22 Apr 2022
Copy link to clipboard 
Print this post

On my SD-card (background.bmp)
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 03:37pm 22 Apr 2022
Copy link to clipboard 
Print this post

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: Germany
Posts: 671
Posted: 06:29pm 22 Apr 2022
Copy link to clipboard 
Print this post

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: Australia
Posts: 2640
Posted: 12:17am 23 Apr 2022
Copy link to clipboard 
Print this post

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: Germany
Posts: 671
Posted: 08:41am 23 Apr 2022
Copy link to clipboard 
Print this post

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: Canada
Posts: 1132
Posted: 04:57pm 23 Apr 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4044
Posted: 04:11pm 25 Apr 2022
Copy link to clipboard 
Print this post

The RP2040 in the Pico is very short of RAM. Compromises have to be made...

John
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 04:55pm 25 Apr 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4311
Posted: 06:28pm 25 Apr 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1646
Posted: 06:39pm 25 Apr 2022
Copy link to clipboard 
Print this post

  thwill said  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


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: Germany
Posts: 671
Posted: 08:57pm 25 Apr 2022
Copy link to clipboard 
Print this post

Thanks both of you for the great tips and hints! :)

Greetings
Daniel
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025