Cursor code

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
FireDemon

Cursor code

Post by FireDemon »

//Devices.h
//System
__inline__ unsigned inportb(unsigned port)
{
   unsigned ret_val;

   __asm__ __volatile__("inb %w1,%b0"
      : "=a"(ret_val)
      : "d"(port));

   return ret_val;
}

__inline__ void outportb(unsigned port, unsigned val)
{
   __asm__ __volatile__("outb %b0,%w1"
      :
      : "a"(val), "d"(port));
}

//Cursor.h
class Cursor
{
public:
Cursor()
{
y=0;
x=0;
}

~Cursor()
{
}

void resetcursor()
{
positioncursor(0,0);
}

void positioncursor(int pcx,int pcy)
{
// cursor LOW port to vga INDEX register
outportb( 0x3D4, 0x0F );
outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

// cursor HIGH port to vga INDEX register
outportb( 0x3D4, 0x0E );
    outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

    x = x;
y = y;
}

unsigned int getx()
{
return x;
}

unsigned int gety()
{
return y;
}

private:
unsigned int y;
unsigned int x;
}; //don't forget the semicolon!

Does not work for some reason
jrfritz

Re:Cursor code

Post by jrfritz »

Here is my poscursor...it's function is called gotoxy():
// data:
int cur_cursorX, cur_cursorY;            // this is for keeping track of the text mode cursor
static unsigned short *vgaadr;            // the VGA address
static unsigned textattrib, scrwidth, scrheight;   // the text color, the screen width, & height
// use this function to set up all the variables needed. call this once and it is needed before you use gotoxy.
void start_textmode()
{
   cur_cursorX = 0;
   cur_cursorY = 0;

   gotoxy( 0, 0 );

   vgaadr = ( unsigned short * ) 0xb8000;

   textattrib = WHITE_BLACK;      // white on black
   scrwidth = 80;
   scrheight = 25;
}
void gotoxy( int x, int y )
{
USHORT   position = ( y * scrwidth + x );

// cursor LOW port to vga INDEX register
outportb( 0x3D4, 0x0F );
outportb( 0x3D5, ( UCHAR ) ( position & 0xFF ) );

// cursor HIGH port to vga INDEX register
outportb( 0x3D4, 0x0E );
   outportb( 0x3D5, ( UCHAR ) ( ( position >> 8 ) & 0xFF ) );

   cur_cursorX = x;
   cur_cursorY = y;
}
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Cursor code

Post by df »

FireDemon wrote: void positioncursor(int pcx,int pcy)
{
// cursor LOW port to vga INDEX register
outportb( 0x3D4, 0x0F );
outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

// cursor HIGH port to vga INDEX register
outportb( 0x3D4, 0x0E );
outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

x = x;
y = y;
}
dont forget to add the video memory address to the value!
so its + 0xB8000

this is how I do it;

Code: Select all

void UpdateCursor(text_screen *vc)
{
   int   xx=(vc->intCurrentRow * vc->intMaximumColumn)+vc->intCurrentColumn;
   xx+=(int)(vc->intVideoMemoryBuffer)>>1;

   out_byte(TEXT_INDEX, TEXT_CURSOR_LO);   /* access lo cursor data reg */
   out_byte(TEXT_DATA, xx&0xFF);
   out_byte(TEXT_INDEX, TEXT_CURSOR_HI);   /* access hi cursor data reg */
   out_byte(TEXT_DATA, (xx>>8)&0xFF);
}
you can see I do a + video memory then divide by 2, to get the proper address
-- Stu --
FireDemon

Re:Cursor code

Post by FireDemon »

Thanks, I forgot to shift the code. I used the FritzOS code.
Using the above code,how could I change it to get the mouse position.
jrfritz

Re:Cursor code

Post by jrfritz »

Don't you mean the text cursor pos? If so...I have code...
FireDemon

Re:Cursor code

Post by FireDemon »

I got the text position already, I need to know how to get data from a mouse
nullify

Re:Cursor code

Post by nullify »

You need to write mouse driver code then, e.g., to track the mouse position.
FireDemon

Re:Cursor code

Post by FireDemon »

Can someone show me a mouse function to get the x and y pos
Tim

Re:Cursor code

Post by Tim »

You're welcome to study my PS/2 mouse driver.
FireDemon

Re:Cursor code

Post by FireDemon »

Man, can it get anymore complex then this.
Maybe I will do the mouse last.
Tim

Re:Cursor code

Post by Tim »

Yes, I think that's a wise idea.
DarylD

Re:Cursor code

Post by DarylD »

As I always say, leave things like the keyboard and mouse until later on when you have a scheduler/memory manager etc in place.

What is the use of reading in keyboard presses or mouse movements when you have no OS kernel to have actions acted upon?!!

To be honest, I have a working keyboard driver but it will be no use until I have an elf loader which needs an IO system/cache manager which in my case means I will have a good chunk of the kernel done before I even process my first keystroke.

Daryl.
FireDemon

Re:Cursor code

Post by FireDemon »

I am really not making an OS.
It is a game that will work on all OS systems.
jrfritz

Re:Cursor code

Post by jrfritz »

That would not always work...some OSes don't allow ports to be read like that...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Cursor code

Post by Pype.Clicker »

Solar Ray wrote: That would not always work...some OSes don't allow ports to be read like that...
The only OS i know that allow such kind of direct hardware access is MS-DOS and clones.
Post Reply