Home arrow C++ arrow Page 3 - Video Memory Programming in Text Mode
C++

Video Memory Programming in Text Mode


Nowadays we take so many things for granted. This isn't necessarily a bad thing, as knowing that performing a certain task will usually yield the same result can be a blessing. Nevertheless, some basic knowledge of how a thing gets done lets you better understand the world in which we live. Therefore, today I invite anyone who likes to code in any of the popular languages to see how a word gets printed out on the console screen.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
March 29, 2010
TABLE OF CONTENTS:
  1. · Video Memory Programming in Text Mode
  2. · Where to copy
  3. · The code
  4. · What do we have in the end?

print this article
SEARCH DEVARTICLES

Video Memory Programming in Text Mode - The code
(Page 3 of 4 )

The current operating systems do not allow this kind of low level access to the video card. In order to allow this, we need to use an environment that grants us that access. The Turbo C or the Borland C 3.1 development IDE allow this. First you will need to acquire such a thing. A few Google searches should definitely reveal a couple of sources. Furthermore, these are 16-bit programs.

Because the Windows operating system's backward compatibility is only one step long, you cannot run these programs on a 64-bit operating system. If you are in this situation, to try out this code you will need to install a 32-bit operating system. In order to make the task of installing a new operating system a little easier, you may use a virtualization method like the one presented by the VMware Player or the Windows virtualization system.

Once you have a development environment like the Borland Turbo C++ 3.0 up and running, you can create a new file and use the pokeb function to write data to the video memory. Defined in the dos.h header, this function will store a byte value at the memory location and the offset specified. With the following formula: (80*y+x)*2 we can calculate the offset of a character in row y and column x. Now we can start up and just use the following code: 

#include <dos.h>

#include <time.h>

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

typedef unsigned int uint;

typedef unsigned char uchar;

void clear () // clear the screen -> black bg and fg

{

uint x, y;

uint offset;

for (x=0; x<80; ++x)

for (y=0; y<25; ++y)

{

offset = (80*y + x)*2;

pokeb (0xB800, offset, 0);

pokeb (0xB800, offset+1, 0);

}

}

// Put a character in the x-th row y-th column

// The attributes are inside a and the character is c

void putXY (uint x, uint y, uchar c, uchar a)

{

uint offset;

if (x<80 && y<25)

{

offset = (80*y + x)*2;

pokeb (0xB800, offset, c);

pokeb (0xB800, offset+1, a);

}

}

// Put the text(s) in l-th row with attributes a

void putLine (uint l, uchar *s, uchar a)

{

uint i;

for (i=0; s[i]; ++i)

putXY (i, l, s[i], a);

}

//Print a file text. Attributes are random. Stop per screen.

void putFileText (char *fname)

{

FILE *f;

char s [80];

uint l;

uchar a=15;

randomize (); // to get truly random attributes

f = fopen (fname, "r"); // open file

if (f == NULL)

return;

for (l=0; !feof (f); ++l) // until we have from file

{

do {// avoid same foreground and background colors

a = random (256);

} while ((a & 0XF0)>>4 == (a & 0X0F));

fgets(s, 80, f); //get item and print

putLine (l, (uchar*) s, a);

if (l==24) // when the screen is full, clear and wait

{

getch ();

clear ();

l=-1; //reset to start

}

}

fclose (f); //close the file

}

//Fill the background with the attribute, use space chars

void fillBackground (uchar a)

{

uint x, y;

uint offset;

for (x=0; x<80; ++x)

for (y=0; y<25; ++y)

{

offset = (80*y + x)*2;

pokeb (0xB800, offset, ' ');

pokeb (0xB800, offset+1, a);

}

}

// Fill the screen with zeros

void fillForeground (uchar a)

{

uint x, y;

uint offset;

for (x=0; x<80; ++x)

for (y=0; y<25; ++y)

{

offset = (80*y + x)*2;

pokeb (0xB800, offset, '0');

pokeb (0xB800, offset+1, a);

}

}

// construct an attribute byte based on the input

uchar setAttributes (uchar Bi, uchar Br, uchar Bg, uchar Bb,

uchar i, uchar r, uchar g, uchar b)

{

uchar a=0;

a |= (Bi&1) <<7;

a |= (Br&1) <<6;

a |= (Bg&1) <<5;

a |= (Bb&1) <<4;

a |= (i&1) <<3;

a |= (r&1) <<2;

a |= (g&1) <<1;

a |= (b&1) <<0;

return a;

}

// merge predetermined background and foreground colors

uchar setBackgroundForegound (uchar B, uchar F)

{

uchar a=0;

a |= (B&0xF) <<4;

a |= (F&0xF) <<0;

return a;

}

//The main entry point of the program

int main ()

{ //set SVGA text mode

uchar a;

textmode (3);

clear ();

// Using a yellow char color and dark red background

// print letter a at 0 0

a = setAttributes (1, 1, 0, 0, 1, 0, 1, 0);

putXY (0, 0,'a', a);

//at 39 12 put a white background

a = setBackgroundForegound (0, 4);

putXY (39, 12, 13, a);

getch (); //wait for user input

//clear the screen and wait for user input

clear ();

getch ();

//Black background, red letters print the command code

fillBackground (setBackgroundForegound (7, 10));

putFileText (

"C:Archit~1Archit~1Archit~1Video ext.cpp");

getch (); //wait for user input

textmode (LASTMODE); //reset the previously used text mode

return 0;

}


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials