If nothing else, it'll make an interesting programming exercise for you debugging it and improving it. (I'm sure by introducing a deliberate little error here or there I'll have provoked people who don't normally reply to questions to "correct me" - Cunningham's Law)
They work with coordinates which cover the whole screen, not individual windows. To get pixels from a window is much more complex as you need to work out where the window starts. However, if you've defined a window in your program, you'll know where it is and just add the coordinates, e.g. WINDOW 256,128,128,64 means that to get the colour of pixel 0,0 in that window you'd add the origin, i.e. PRINT PIXEL4%(128+0,64+0).
The procedure called ScreenDetails seeks to set the screen base address (screenbase) and line length in bytes (screenllen). This allows the routines to work in high resolution screens, not just on 512x256. This works by using VER$ to check if we are running on an SBASIC system which has the SCR_BASE and SCR_LLEN functions. The use of VER$ prevents QDOS systems trying to use non-existent functions. Where this falls over is on uQLx/sQLux systems which have some high resolution modes which work under QDOS, and may have these extensions, but I don't know how to specifically test for those emulators.
There is a separate function for each mode.
PRINT PIXEL4%(x,y) prints the pixel colour at the top left of the screen in 4 colour mode.
PRINT PIXEL8%(x,y) prints the pixel colour at the top left fo the screen in 8 colour mode (note: accepts x values 0 to 511 across a mode 8 QL screen, not 0 to 255 - pixels are double width in mode 8)
PRINT PIXEL16%(x,y) gives the colour byte value at x,y pixels across and down the screen in 256 colour modes on Aurora, QPC2 and SMSQmulator. Note, this is the native colour value, no attempt to read palettes etc.
PRINT PIXEL32%(x,y) does the same for 16-bit colour modes on QPC2, Q40, Q60 etc. Note, this is the native colour value as stored in the screen RAM, no attempt at looking up the colour palettes etc. e.g. in mode 32 16-bit colour, the first 256 colours may be paletted to look like the QL mode 4/mode 8 colours.
If nothing else, these routines show how to handle the screen and an easy way to ensure your programs work in non-QL modes and high resolution screens.
Code: Select all
200 DEFine PROCedure ScreenDetails
210 REMark QDOS ROM defaults
220 screenbase = 131072 : REMark base address of screen
230 screenllen = 128 : REMark screen line width in bytes
240 IF VER$ = 'HBA' THEN
250 REMark use SBASIC functions on hires systems
260 screenbase = SCR_BASE
270 screenllen = SCR_LLEN
280 END IF
290 END DEFine ScreenDetails
300 :
310 DEFine FuNction PIXEL4%(x,y)
320 LOCal screenbase,screenllen,pixel,word,green,red,addr
330 ScreenDetails
340 pixel = 7-(x MOD 8) : REMark pixel number across colour byte
350 word = 2*(x DIV 8)
360 addr = screenbase+(y*screenllen)+word
370 green = 4*((PEEK(addr)&&(2^pixel))<>0)
380 red = 2*((PEEK(addr+1)&&(2^pixel))<>0)
390 RETurn green+red+(green<>0 AND red<>0) : REMark 4+2->7 for white
400 END DEFine PIXEL4%
410 :
420 DEFine FuNction PIXEL8%(x,y)
430 LOCal screenbase,screenllen,pixel,word,green,red,blue,addr
440 ScreenDetails
450 pixel = 7-((x&&254) MOD 8) : REMark pixel number across colour byte
460 word = 2*(x DIV 8)
470 addr = screenbase+(y*screenllen)+word
480 green = 4*((PEEK(addr)&&(2^pixel))<>0)
490 red = 2*((PEEK(addr+1)&&(2^pixel))<>0)
500 blue = (PEEK(addr+1)&&(2^(pixel-1)))<>0
510 RETurn green+red+blue
520 END DEFine PIXEL8%
530 :
540 DEFine FuNction PIXEL16%(x,y)
550 ScreenDetails
560 RETurn PEEK(screenbase+(y*screenllen)+x)
570 END DEFine PIXEL16%
580 :
590 DEFine PROCedure PIXEL32%
600 ScreenDetails
610 RETurn PEEK_W(screenbase+(y*screenllen)+(2*x))
620 END DEFine PIXEL32%