Drawing Rectangles in Video Memory

Programming, for all ages and all languages.
Post Reply
TomB
Posts: 8
Joined: Thu Dec 08, 2005 12:00 am

Drawing Rectangles in Video Memory

Post 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
Last edited by TomB on Fri Dec 07, 2007 10:48 am, edited 1 time in total.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Drawing Rectangles in Video Memory

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
TomB
Posts: 8
Joined: Thu Dec 08, 2005 12:00 am

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
TomB
Posts: 8
Joined: Thu Dec 08, 2005 12:00 am

Post 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.
TomB
Posts: 8
Joined: Thu Dec 08, 2005 12:00 am

Post 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.

Image
Image
Post Reply