Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

varseg, varptr, poke, peek, def seg, inp, int, clng, etc. etc...

Status
Not open for further replies.

Barok

MIS
Dec 13, 2002
134
0
0
CA
anyone got any good websites on these commands? I know about them, but it is how to use them that's got me stumped. like, &h60. this writes to the keyboard buffer (i think) but what about other addresses? anyone got any information about this? i know varseg, varptr def seg, poke and peek write to the memory, but what about inp and int, out, wait, etc. etc. any help is appreciated. btw, here's some code to crunch on. it's a scrolling engine. try to modify it, get a few fps out of it! happy coding!

'Program: Pure QBasic pixel by pixel tile engine
'Author: Jesse Regier aka...
'
' É͸ Ö· Ö ·Ö ɸ
' ÈÍ» ºº º ºº È»
' ÖÄÄĺÄӽĽÄÓ½ÄÔ¼Ä
' ÈÍÍͼ
'
'E-mail: theregiers@sk.sympatico.ca
'ICQ: 37962076
'Website: Darkle - '
'Notes:
' I wrote this as a small experiment to see how fast of a tile
' scrolling engine could be made using only QBasic's intrinsic
' functionality. I was impressed by how fast it actually is once
' been *compiled*. There are a few bugs that will become apparent if
' you play around with this long enough... I didn't do any bounds
' checking, you'll notice it as soon as you go too far left or up.
' There isn't too much for documentation... the code is all simple
' enough that anyone should be able to figure it out, if you can't
' well then you've got a lot of learning to do... Bonne chance!
'
' Bored? Optimize this simple engine... see if you can squeeze
' a few more frames per second out of it.... and send me a copy!
'
DEFINT A-Z

CONST screenw = 320, screenh = 200
CONST mw = 99, mh = 99
CONST MaxTiles = 5
CONST tw = 20, th = 20
CONST tsize = (tw * th + 1) \ 2 + 1

DIM map(mw, mh)
DIM Tiles(tsize, MaxTiles - 1)
DIM scanline(1 TO tw)
DIM soffset AS LONG


'create map
FOR x = 0 TO mw
FOR y = 0 TO mh
map(x, y) = RND * (MaxTiles - 1)
NEXT
NEXT


'create tiles
DEF SEG = VARSEG(Tiles(0, 0))
offset = VARPTR(Tiles(0, 0))

FOR tile = 0 TO MaxTiles - 1
Tiles(0, tile) = tw * 8
Tiles(1, tile) = th
offset = offset + 4
FOR x = 0 TO tw - 1
FOR y = 0 TO th - 1
POKE offset, tile
offset = offset + 1
NEXT
NEXT
NEXT

DEF SEG


SCREEN 13

x = mw * tw \ 2
y = mh * th \ 2

time! = TIMER

DO
'clear the key buffer
DEF SEG = 0
POKE (1050), PEEK(1052)
DEF SEG

'get keypress and handle it
SELECT CASE INP(96)
CASE 1: EXIT DO
CASE 72: y = y - 1
CASE 75: x = x - 1
CASE 77: x = x + 1
CASE 80: y = y + 1
END SELECT

sx.b = -(x MOD tw)
sy = -(y MOD th)
tx.b = x \ tw
ty = y \ th

DO
sx = sx.b
tx = tx.b
DO
t = map(tx, ty)

IF ((sx >= 0) AND (sy >= 0) AND (sx <= (screenw - tw)) AND (sy <= (screenh - th))) THEN
PUT (sx, sy), Tiles(0, t), PSET
ELSEIF ((sx >= 0) AND (sx <= (screenw - tw)) AND (sy > (screenh - th))) THEN
h = Tiles(1, t)
Tiles(1, t) = (screenh - sy)
PUT (sx, sy), Tiles(0, t), PSET
Tiles(1, t) = h
ELSE

tseg = VARSEG(Tiles(0, t))
toffset = VARPTR(Tiles(2, t))

IF sx < 0 THEN
px = 0
toffset = toffset - sx
w = tw + sx
woff = -sx
soff = screenw - (tw - woff)
ELSEIF sx > (screenw - tw) THEN
px = sx
w = screenw - sx
woff = (sx + tw) - screenw
soff = screenw - (tw - woff)
ELSE
w = tw
px = sx
soff = screenw - tw
woff = 0
END IF

IF sy < 0 THEN
py = 0
toffset = toffset - (sy * tw)
h = th + sy
ELSEIF sy > (screenh - th) THEN
py = sy
h = screenh - sy
ELSE
py = sy
h = th
END IF

soffset = CLNG(py) * screenw + px

FOR py = 1 TO h

'peek the scanline into a temp buffer
DEF SEG = tseg
FOR px = 1 TO w
scanline(px) = PEEK(toffset)
toffset = toffset + 1
NEXT

'poke the temp buffer onto the screen
DEF SEG = &HA000
FOR px = 1 TO w
POKE (soffset), scanline(px)
soffset = soffset + 1
NEXT
toffset = toffset + woff
soffset = soffset + soff
NEXT
END IF
tx = tx + 1
sx = sx + tw
LOOP UNTIL sx > 319
ty = ty + 1
sy = sy + th
LOOP UNTIL sy > 199

f = f + 1
IF (time! + 1) < TIMER THEN
fps$ = STR$(f)
time! = TIMER
f = 0
END IF

LOCATE 22: PRINT fps$

LOOP
 
INP(&h60) aka INP(96) READS from the keyboard pre-buffer as key strokes are processed and added to the buffer... This is What you can use to create multikey input programs because it returns a code for individual key presses AND key releases... when you Press the Up Arrow it Returns 72 ... When you Press left (while holding Up) it returns 75... when you release Up it returns 200 (72+128) ... and releasing it returns 203 (75+128)

for more details see thread: thread314-431683
Where I explained this in detail...

this is actually used in the method shown above...
'get keypress and handle it
SELECT CASE INP(96)
CASE 1: EXIT DO
CASE 72: y = y - 1
CASE 75: x = x - 1
CASE 77: x = x + 1
CASE 80: y = y + 1
END SELECT


But using it in this way somewhat eliminates the point...
This method is more or less a direct replacement for Inkey$ as opposed to a multikey function...

In most cases you want to use an array, such as KB(128) to store the status of each key...

So then you would do something similar to this...

Dim KB(128) as integer
Do
GetKeyRoutine....
IF KB(72) THEN Y = Y - 1
IF KB(75) THEN X = X - 1
IF KB(77) THEN X = X + 1
IF KB(80) THEN Y = Y + 1
Loop until KB(1)
'esc is pressed

---------------------------------------------------

In Screen mode 13 you can use INP and OUT to change the palette faster than with the PALETTE command And retrieve the colors easier... Here are 2 little Functions I wrote to Get and Set colors this way...

SUB PALGET (C, R, G, B)
OUT &H3C7, C
R = INP(&H3C9)
G = INP(&H3C9)
B = INP(&H3C9)
END SUB

SUB PALSET (C, R, G, B)
OUT &H3C8, C
OUT &H3C9, R
OUT &H3C9, G
OUT &H3C9, B
END SUB


Where C is the color (0 to 255)
R is the Red Value (0 to 63)
G is the Green Value (0 to 63)
B is the Blue Value (0 to 63)

------------------------------------------------

As for DEF SEG, VARSEG, VARPTR, PEEK, POKE, etc...

This also has multiple uses...

Such as retrieving the system character masks used to create text...

Here is a function I wrote to center text anywhere (Y axis) on the screen, in a graphics mode...

SUB CENTER (Y, S$, C)
X = 160 - (LEN(S$) * 4)
A = X: B = Y
EXTX = 8: EXTY = 0
DEF SEG = &HFFA6
FOR I = 1 TO LEN(S$)
ADDR = 8 * ASC(MID$(S$, I)) + 14
FOR J = 0 TO 7: MASK = PEEK(ADDR + J) * 128
LINE (X + 7, Y + J)-(X, Y + J), C, , MASK
NEXT
X = X + EXTX: Y = Y + EXTY
NEXT
DEF SEG
X = A: Y = B
END SUB


This can be shortened and Made into an anywhere font...

SUB FONT (X, Y, S$, C)
A = X: B = Y
EXTX = 8: EXTY = 0
DEF SEG = &HFFA6
FOR I = 1 TO LEN(S$)
ADDR = 8 * ASC(MID$(S$, I)) + 14
FOR J = 0 TO 7: MASK = PEEK(ADDR + J) * 128
LINE (X + 7, Y + J)-(X, Y + J), C, , MASK
NEXT
X = X + EXTX: Y = Y + EXTY
NEXT
DEF SEG
X = A: Y = B
END SUB


----------------------------------------

Everyone's Favorite use for this, As it seems, is for graphics... (usually Screen 13)

You Can write to the screen MUCH FASTER than with Pset using Peek & Poke... There are several ways to do this...

You can write directly to the screen using...

DEF SEG = &HA000
POKE Address&, Color


Where Address& = X + (Y& * 320)
and Color = 0 to 255

---OR---

You can make a screen buffer:
DIM SCRBUF(32002) AS INTEGER

Initialize the buffer:
SCREEN 13
GET (0, 0)-(319, 199), SCRBUF


Write to the Buffer:
DEF SEG = VARSEG(SCRBUF(0))
DST& = VARPTR(SCRBUF(2))
POKE DST& + X + (Y& * 320), Color


Or Read from the buffer...
DEF SEG = VARSEG(SCRBUF(0))
DST& = VARPTR(SCRBUF(2))
Color = PEEK(DST& + X + (Y& * 320))


-----------------------------------------

Another interesting Command is CALL ABSOLUTE
which basically runs Assembly code in QB...

this can get a bit tricky...

For example to use a mouse in a program, the HexCode string to use the mouse driver looks like this...

55 89 E5 8B 5E 0C 8B 07 50 8B 5E 0A 8B 07 50 8B
5E 08 8B 0F 8B 5E 06 8B 17 5B 58 1E 07 CD 33 53
8B 5E 0C 89 07 58 8B 5E 0A 89 07 8B 5E 08 89 0F
8B 5E 06 89 17 5D CA 08 00


This is actually an assembly code that was compiled w/ DEBUG
A program that comes on almost every OS made by Microsoft...

There are some very good tutorials out there on the net... try a google search with &quot;Debug&quot; and &quot;QBASIC&quot; together...

what it boils down to is that the ASM code looks like this...

HexCode | Asm Command
Code:
55       PUSH	BP
89E5     MOV	BP,SP
8B5E0C   MOV	BX,[BP+0C]
8B07     MOV	AX,[BX]
50       PUSH	AX
8B5E0A   MOV	BX,[BP+0A]
8B07     MOV	AX,[BX]
50       PUSH	AX
8B5E08   MOV	BX,[BP+08]
8B0F     MOV	CX,[BX]
8B5E06   MOV	BX,[BP+06]
8B17     MOV	DX,[BX]
5B       POP	BX
58       POP	AX
1E       PUSH	DS
07       POP	ES
CD33     INT	33
53       PUSH	BX
8B5E0C   MOV	BX,[BP+0C]
8907     MOV	[BX],AX
58       POP	AX
8B5E0A   MOV	BX,[BP+0A]
8907     MOV	[BX],AX
8B5E08   MOV	BX,[BP+08]
890F     MOV	[BX],CX
8B5E06   MOV	BX,[BP+06]
8917     MOV	[BX],DX
5D       POP	BP
CA0800   RETF	0008

Notice in the above Code that the Hexcode on the left go in order with the Hexcode string...

Take another look at the code...
55 89 E5 8B 5E 0C 8B 07 50 8B 5E 0A 8B 07 50 8B
5E 08 8B 0F 8B 5E 06 8B 17 5B 58 1E 07 CD 33 53
8B 5E 0C 89 07 58 8B 5E 0A 89 07 8B 5E 08 89 0F
8B 5E 06 89 17 5D CA 08 00

Then at a few of the assembly lines...
55 PUSH BP
89E5 MOV BP,SP
8B5E0C MOV BX,[BP+0C]
...
5D POP BP
CA0800 RETF 0008

See how these are related...
Now the actual assemlby code looks like this...
PUSH BP
MOV BP,SP
8B5E0C MOV BX,[BP+0C]
...

The hexcodes are the compiled assembly code...

Notice the Pattern in these commands...
8B5E0C MOV BX,[BP+0C]
8B5E08 MOV BX,[BP+08]
8B5E06 MOV BX,[BP+06]

8B5Exx is MOV BX,[BP+xx] where xx is the offset address...

And for the last command...
CA0800 RETF 0008
CA returns the data for the function
08 is how many Bytes (0008)
00 is the end of the program/function

Now, maybe, this won't look comletely greek to you when you see this type of stuff in a program...

Now to actually use it you need to place it in a string...

There are many ways to do this...

Some people like to use DATA commands to store the values...
I like to use other strings...

I will explain this later... I am running short on time for now...

Hope this helps...
Good Luck
-Josh

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top