Drawing Lines
Posted: Fri Jun 13, 2008 10:39 pm
Hi. How do you draw lines in Pmode? I have a menu and would like to draw a box around it in C. Thanks.
void plot_pixel(int x,int y,unsigned char color)
{
char *vga = (char *) 0xb8000;
unsigned int i = 160*y + 2*x;
i = 160*y + 2*x;
vga[i++] = color;
}
Now the fun is writing my own drawing routine. =)Example Usage wrote: plot_pixel(1, 1, 0xDA);
That code will not work at all, 0xb8000 is color text memory, graphic memory is located at 0xa0000 if i'm not mistaken.void plot_pixel(int x,int y,unsigned char color)
{
char *vga = (char *) 0xb8000;
unsigned int i = 160*y + 2*x;
i = 160*y + 2*x;
vga[i++] = color;
}
You're not mistaken - I was just about to write that same reply.Pyrofan1 wrote:That code will not work at all, 0xb8000 is color text memory, graphic memory is located at 0xa0000 if i'm not mistaken.void plot_pixel(int x,int y,unsigned char color)
{
char *vga = (char *) 0xb8000;
unsigned int i = 160*y + 2*x;
i = 160*y + 2*x;
vga[i++] = color;
}
Code: Select all
void draw_box(void)
{
int y=0;
plot_pixel((13/2)-1,7,0xDA); //left corner
for(y=0;y<37;y++) { plot_pixel((13/2)+y,7,0xC4); } //top line
plot_pixel((13/2)+37,7,0xBF); //right corner
//*******************
int u=0;
for(u=8;u<14;u++) { plot_pixel((13/2)-1,u,0xB3); } //left side
//*******************
int s=0;
for(s=8;s<14;s++) { plot_pixel((13/2)+37,s,0xB3); } //right side
//*******************
int r=0;
plot_pixel((13/2)-1,14,0xC0); //left corner
for(r=0;r<37;r++) { plot_pixel((13/2)+r,14,0xC4); } //bottom line
plot_pixel((13/2)+37,14,0xD9); //right corner
}
Not trying to be fancy, but you could at least have used function and parameter names that actually describe what the function is intended to do.vst_0201 wrote:It works for me. I just wrote a routine that draws the box around my menu using that function.
Not trying to be fancy... =)Code: Select all
void draw_box(void) { int y=0; plot_pixel((13/2)-1,7,0xDA); //left corner for(y=0;y<37;y++) { plot_pixel((13/2)+y,7,0xC4); } //top line plot_pixel((13/2)+37,7,0xBF); //right corner //******************* int u=0; for(u=8;u<14;u++) { plot_pixel((13/2)-1,u,0xB3); } //left side //******************* int s=0; for(s=8;s<14;s++) { plot_pixel((13/2)+37,s,0xB3); } //right side //******************* int r=0; plot_pixel((13/2)-1,14,0xC0); //left corner for(r=0;r<37;r++) { plot_pixel((13/2)+r,14,0xC4); } //bottom line plot_pixel((13/2)+37,14,0xD9); //right corner }
Code: Select all
void plot_ascii(int x,int y,unsigned char ascii)
{
char *vga = (char *) 0xb8000;
unsigned int i = 160*y + 2*x;
i = 160*y + 2*x;
vga[i++] = ascii;
}
Code: Select all
void draw_box(void)
{
int y=0;
plot_ascii((13/2)-1,7,0xDA); //left corner
for(y=0;y<37;y++) { plot_ascii((13/2)+y,7,0xC4); } //top line
plot_ascii((13/2)+37,7,0xBF); //right corner
//*******************
int u=0;
for(u=8;u<14;u++) { plot_ascii((13/2)-1,u,0xB3); } //left side
//*******************
int s=0;
for(s=8;s<14;s++) { plot_ascii((13/2)+37,s,0xB3); } //right side
//*******************
int r=0;
plot_ascii((13/2)-1,14,0xC0); //left corner
for(r=0;r<37;r++) { plot_ascii((13/2)+r,14,0xC4); } //bottom line
plot_ascii((13/2)+37,14,0xD9); //right corner
}
Code: Select all
typedef unsiged long u32;
typedef unsigned short u16;
u16 *ScreenPtr16 = (u16*)0xB8000;
struct Rect_S
{
u32 x1,y1,x2,y2;
};
//Used to display windows/menus
u16 Pieces[11] =
{
218, //Top Left
192, //Bottom Left
191, //Top Right
217, //Bottom Right
196, //Straight Horizontal
179, //Straight Vertical
194, //Split Vertical Down
193, //Split Vertical Up
195, //Split Horizontal Right
180, //Split Horizontal Left
197 //Cross
};
void DrawBox(struct Rect_S *rect, u32 Color) //Draws a box :)
{
u32 Ctr, Off1, Off2;
Off1 = rect->y1*80+rect->x1;
Off2 = rect->y2*80+rect->x1;
ScreenPtr16[Off1] = Pieces[0]+Color;
ScreenPtr16[Off2] = Pieces[1]+Color;
++Off1;
++Off2;
for (Ctr=0;Ctr!=rect->x2-rect->x1-1;++Ctr)
{
ScreenPtr16[Off1] = Pieces[4]+Color;
ScreenPtr16[Off2] = Pieces[4]+Color;
++Off1;
++Off2;
}
ScreenPtr16[Off1] = Pieces[2]+Color;
ScreenPtr16[Off2] = Pieces[3]+Color;
Off1 = rect->y1*80+rect->x1;
Off2 = rect->y1*80+rect->x2;
for (Ctr=0;Ctr!=rect->y2-rect->y1-1;++Ctr)
{
Off1+=80;
Off2+=80;
ScreenPtr16[Off1] = Pieces[5]+Color;
ScreenPtr16[Off2] = Pieces[5]+Color;
}
}