我目前正在使用C语言编写计算机图形学项目。我们的教授希望我们制作一个由动画组成的项目,但是通过鼠标而不是键盘来触发。我在互联网上搜索了解决方案,但没有任何对我有用。该项目将在Turbo-C中完成。
您将需要安装DOS鼠标驱动程序。如果使用DOSBox,则已经可以使用。您需要通过(Int 0x33)对鼠标services1例程进行中断调用。 DOS中断相关信息的最佳来源之一是Ralph Brown's Interrupt List。 Turbo-C支持通过int86
函数(包括DOS.H)调用BIOS服务。
Int 0x33提供鼠标服务,但为了让您入门,立即使用的鼠标功能是:
使用RBIL上的鼠标中断信息指定输入和输出参数,您可以为Turbo-C代码创建一些基本的鼠标包装器。基本包装器看起来像:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#define MOUSE_INT 0x33
#define MOUSE_FUNC_SHOW 1
#define MOUSE_FUNC_HIDE 2
#define MOUSE_FUNC_GETSTATE 3
#define MOUSE_FUNC_SETPOS 4
#define MOUSE_LEFT_BUTTON 0x01
#define MOUSE_RIGHT_BUTTON 0x02
typedef struct {
unsigned int cur_x;
unsigned int cur_y;
unsigned int buttons;
} mouse_state_t;
void mouse_hide()
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_HIDE;
int86(MOUSE_INT, ®s_in, ®s_out);
}
void mouse_show()
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_SHOW;
int86(MOUSE_INT, ®s_in, ®s_out);
}
void mouse_get_state(mouse_state_t *mouse_state)
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_GETSTATE;
int86(MOUSE_INT, ®s_in, ®s_out);
mouse_state->cur_x = regs_out.x.cx;
mouse_state->cur_y = regs_out.x.dx;
mouse_state->buttons = regs_out.x.bx;
/* buttons field is a bit mask. Bit 0 (lowest bit) is set when left
mouse button is pressed. Bit 1 is set when right mouse button is pressed */
}
void mouse_set_pos(int pos_x, int pos_y)
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_SETPOS;
regs_in.x.cx = pos_x;
regs_in.x.dx = pos_y;
int86(MOUSE_INT, ®s_in, ®s_out);
}
int main()
{
mouse_state_t mouse_state;
/* Show mouse and set initial position to middle of text screen */
clrscr();
mouse_show();
mouse_set_pos(80*8/2, 25*8/2);
/* Repeat until any key is pressed */
do
{
/* Get current mouse state and display it */
mouse_get_state(&mouse_state);
gotoxy(1, 1);
printf("Mouse X: %3d\n", mouse_state.cur_x);
printf("Mouse Y: %3d\n", mouse_state.cur_y);
printf("Buttons: %3d\n", mouse_state.buttons);
/* Display the button state as LEFT?RIGHT when pressed */
if (mouse_state.buttons & MOUSE_LEFT_BUTTON)
printf("LEFT ");
else
printf(" ");
if (mouse_state.buttons & MOUSE_RIGHT_BUTTON)
printf("RIGHT");
else
printf(" ");
} while (!kbhit());
mouse_hide();
return 0;
}
此代码提供了mouse_show
,mouse_hide
,mouse_get_state
和mouse_set_pos
函数,以使用Turbo-C的int86
函数提供基本的鼠标轮询界面。测试代码重复更新屏幕左上角的当前鼠标状态,直到按下某个键。这是在DOSBox中出现的:
当您正在完成作业时,我不会为您提供检查按钮状态和绘制图像的代码,但这足以让您确定当前的鼠标状态并自行触发动画。
鼠标界面应该在大多数标准图形模式下工作。
Int 0x33
。 DOS鼠标驱动程序实现此接口以增强标准BIOS服务。