Code:
int SetPixel(int x, int y, int color);
int main(void) {
typedef unsigned char byte;
byte *VGA = (byte *)0xA0000; //This gives you a pointer to the
//graphics video memory
int x;
int y;
for(y=0;y<201;y++) {
for(x=0;x<321;x++) {
SetPixel(x,y,x); //Plot a pixel
}
int SetPixel(int x, int y, int color) {
VGA[320*y+x]=color; //This plots a pixel at x,y
};
Looks better as:
Code:
int SetPixel(int x, int y, int color);
typedef unsigned char BYTE;
int main(void)
{
//Don't typedef here - only valid in main
//This gives you a pointer to the graphics video memory
BYTE far *VGA = (BYTE far *)0xA0000; //This gives you a pointer
// ** Alternate far pointer creation **
//MK_FP is in dos.h, if you have it
//BYTE far *VGA=(BYTE far *)MK_FP(0,0xA000);
int x;
int y;
for(y=0;y<201;y++)
{
for(x=0;x<321;x++)
{
SetPixel(x,y,x); //Plot a pixel
}
}
}
int SetPixel(int x, int y, int color)
{
VGA[320*y+x]=color; //This plots a pixel at x,y
}
But your code is going to be slow if you use it in a game. Each time you call SetPixel a stack frame is setup and then the stack is cleaned up prior to returning to the caller. A better way to do this is:
Code:
BYTE *Surface;
BYTE *Screen;
BYTE *Buffer;
#define PIXEL(x,y,c) (Surface[(y<<6)+(y<<8)+x]=c)
In this snippet every time you do a PIXEL(x,y,c) the compiler will insert your #define code as is - hence it is not a function call thus saving the cycles needed to setup a stack frame and clean the stack up.
Surface is a pointer to the current surface. Reason for this is that you may want a double buffer but you want one function that can plot to both. If you simply change the surface pointer to the buffer pointer, you write to the buffer - if you change the surface pointer to point at the screen - you write to the screen. No additional overhead involved - except for the flipping of pointers which is next to nil.
Also if a section of code is going to be enclosed in braces I always place the braces on separate lines and I indent each section of code 2 spaces. As you can see it would have been easy to spot your error since if your closing brace does not lie on the left margin then you need another brace to close a section of code.
Using my system of braces, I rarely, if ever, have any brace related problems.