Page 1 of 1
Drawing Rectangles in Video Memory
Posted: Thu Dec 06, 2007 8:09 pm
by TomB
I have to create a function that draws a rectangle using the parameters passed to the function, in inline assembler, by writing to video memory instead of using interrupts.
function prototype:
void DrawRectangle(left, top, bottom, right, character, attributes);
I have an attempt at writing this but it always writes 1 column too short.
DrawRectangle(0, 0, 79, 24, 219, BLACK); would effectively draw a black rectangle filling the console, but it is 1 column too short. The source is currently on my laptop, although I think it might be easier for myself to rewrite the function from scratch.
Thanks for any help.
Edit: Source
http://slexy.org/view/s21lgdLzi5
Posted: Fri Dec 07, 2007 2:49 am
by AJ
Hi,
You may have tried this fairly simple answer, but presumably seeing as the parameters are 'right' and 'bottom' rather than 'width' and 'height', you are using some kind of loop with "while <=" rather than just "while <" ?
Cheers,
Adam
Re: Drawing Rectangles in Video Memory
Posted: Sat Dec 08, 2007 12:00 am
by Brendan
Hi,
I was bored. I looked at your code. I wrote this:
Code: Select all
mov ax, VIDEO_MEMORY
mov es, ax
mov di, [bLeft]
mov si, SCREEN_WIDTH * 2
mov ax, [bTop]
add di, di
mul si
mov dx, [bRight]
add di, ax
mov bx, [bBottom]
sub dx, [bLeft]
mov ah, [bAttribute]
inc dx
sub bx, [bTop]
sub si, dx
mov al, [bCharacter]
sub si, dx
cld
.nextLine:
mov cx, dx
rep stosw
add di, si
sub bx, 1
jbe .nextLine
Normally I'd complain if someone posted code like this without comments, but I'm guessing you're a student and you'd learn more by adding the comments than you would by doing "cut & paste"...
Cheers,
Brendan
Posted: Sat Dec 08, 2007 2:31 pm
by TomB
Thanks for writing an example, unfortunately when I try to use it, it doesn't draw the rectangle properly.
Edit: It's drawing the top row, and then just a 'C' on the next row.
Posted: Sat Dec 08, 2007 5:45 pm
by Brendan
Hi,
TomB wrote:Thanks for writing an example, unfortunately when I try to use it, it doesn't draw the rectangle properly.
Edit: It's drawing the top row, and then just a 'C' on the next row.
I didn't test it before I posted it and made a silly mistake - the last line should be "jae .nextLine".
I have tested it now though and (after fixing the last line) it works fine, as long as you give it correct input (for e.g. you can expect problems if "bRight" is 10 and "bLeft" is 20). I have no idea how you would've got a 'C' on the next row...
Cheers,
Brendan
Posted: Sat Dec 08, 2007 7:51 pm
by TomB
Ok thanks a lot. I really appreciate this. I think the 'C' was from some weird thing I did seeing if I could fix it. Thanks again.
Posted: Sun Dec 09, 2007 5:02 pm
by TomB
So after much work, I have finally finish the simple copy of the snake game I had to do for an assignment. Here are a few screenshots of it before I tidied the UI up a bit. A lot of thanks to Brendan who helped with the DrawWindow function.