Printing To Screen: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
→‎Printing Strings: Added a more advanced C function
Solar (talk | contribs)
m Reverted edits by Bellezzasolo (talk) to last revision by Solar
Line 27: Line 27:
If you have a pointer to video memory and want to write a string, here is how you might do it;
If you have a pointer to video memory and want to write a string, here is how you might do it;


<pre>
<source lang="c">
// note this example will always write to the top
// note this example will always write to the top
// line of the screen
// line of the screen
Line 39: Line 39:
}
}
}
}
</source>
</pre>


This simply cycles through each character in the string, and copies it to the appropriate place in video memory.
This simply cycles through each character in the string, and copies it to the appropriate place in video memory.

Here is a more advanced set of functions (C code compatible with C++):
<source lang="c">
//User:bellezzasolo: excerpt from my OS. In my OS this is C++
//This will work in both C and C++
//MODE 3 printing text
#include "stdio.h" //Put definitons of these functions here
#define UCHAR unsigned char
int x = 0;
int y = 0;
unsigned short* vidmem = (unsigned short*)0xb8000;
UCHAR color = (UCHAR)0x3f; //Avoid compiler error. This produces white on light blue. You can change this if you want
void setColor(UCHAR foreground, UCHAR background)
{
color = (background<<4 | foreground);
}
void putc(char c) //Prints a character
{
unsigned short temp = (unsigned short)((color << 8)|c);
unsigned short* vid = (unsigned short*)(vidmem + y*80 +x);
*vid = c;
x++;
if(x>80)
{
y++;
x=0;
if(y>25)
{
scroll();
}
}
}
void Puts(char* string)
{
if(!string)
return;
char* stream = string;
while(*stream != 0)
{
putc(*stream);
stream++;
}
}
</source>


==Printing Integers==
==Printing Integers==