Had this sitting around and thought I'd share it since it's unlikely I'll ever touch this code again.
Just a little skeleton of a VGA unit for DOS. Not sure where to put it as it can be used in DOS game programming (if anyone does that anymore) so it's not exactly only for the DOS board.
Just a little something to help for the new people getting started with VGA and mode 13h.
Here is how to use it:
void SetBufferMode(int mode=FALSE)
- Sets double buffer mode to true or false
- When true surface is buffer, else surface is screen
- All pixel and line functions write to surface so this effects them
void Pixel(WORD x,WORD y,BYTE color)
- Writes a colored pixel to surface (screen or buffer) at x,y
void PixelEx(WORD offset,BYTE color)
- Write a colored pixel to offset in surface at x,y
- Faster than Pixel since offset not computed
void InitVGA(void)
- Sets up screen, buffer, and surface pointers
- Inits BufferMode to FALSE
BYTE far *GetSurface()
- Returns a pointer to the current surface
BYTE far *GetScreen()
- Returns a pointer to the current screen (always A000:0000)
BYTE far *GetBuffer()
- Returns a pointer to the current double buffer
void Flip(void)
- Flips the buffer to the screen
- Does not use surface
- Always flips from buffer to screen
- Does not check for vertical sync
- Uses movsw so it is fairly fast
- Most of the function is in assembly
void CLS(BYTE color=0)
- Clears the screen (A000:0000) with a color
- Again, assembly language
void CLSB(BYTE color=0)
- Clears the double buffer with a color
- Written in inline assembler
void Line(int x,int y,int x2,int y2,BYTE color)
- Draws a line from x,y to x2,y2 on current surface and in chosen color (BYTE color)
- Uses Brehsenams (or however you spell it) line drawing algorithm
- Pure C - could be sped up in assembly
Setup
- Call InitVGA() to setup pointers
- Call CLSB to clear buffer if double buffering
- Call CLS to clear screen if not double buffering
- The rest is up to you
This is for 16-bit real mode DOS. DO NOT execute this code inside of Turbo C++ 1.0-3.0 as it will crash the IDE due to lack of memory. There is not enough memory for the double buffer and the memory that the IDE needs.
This should get some of you going, at least those who are just starting out in DOS - yes there are still some of you out there.
I would not recommend using Line and Pixel too much since they generate function calls. My advice is to write directly to video memory, the current surface, or the double buffer. You can retrieve these pointers with the aforementioned functions.
Questions? Post them here