【问题标题】:How to trigger animation in Turbo-C with a mouse? [closed]如何用鼠标在 Turbo-C 中触发动画? [关闭]
【发布时间】:2019-03-19 11:06:53
【问题描述】:

我目前正在从事一个C 语言的计算机图形学项​​目。 我们的教授希望我们制作一个由动画组成的项目,但它是通过鼠标而不是键盘点击触发的。我在互联网上搜索解决方案,但对我没有任何帮助。该项目将在 Turbo-C 中完成。

【问题讨论】:

  • 如果您的应用程序是基于 Windows 的,您可以只监视鼠标消息。
  • 感谢您的回复。你能详细说明一下解决方案吗?
  • Turbo C 是一个 MS DOS 编译器。没有对鼠标驱动程序的内置支持,因此您必须手动编写该部分代码,这是一件非常痛苦的事情。对于 30 岁以下的编译器,这会容易得多。为什么要培训为 DOS 编写驱动程序?你的老师认为这是下一件大事吗?使用适合初学者的工具链会更明智。
  • 顺便说一句,您不太可能在互联网上找到解决方案,因为在 Turbo C 发布时互联网还没有发明。
  • 感谢您的回复。实际上,我们在 C 语言课程中将计算机图形学作为一门学科。而且我们仍然被教导使用 Turbo C 进行编码。所以,除了上面所说的,我别无选择。那么,您能否建议如何为鼠标驱动程序手动编码?

标签: c graphics mouse dos turbo-c


【解决方案1】:

您需要安装 DOS 鼠标驱动程序。如果使用 DOSBox,则已经可以使用一个。您需要通过 (Int 0x33) 对鼠标服务1 例程进行中断调用。 DOS 中断相关信息的最佳来源之一是Ralph Brown's Interrupt List。 Turbo-C 支持通过int86 函数调用 BIOS 服务(包括 DOS.H)。

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, &regs_in, &regs_out);
}

void mouse_show()
{
    union REGS regs_in, regs_out;
    regs_in.x.ax = MOUSE_FUNC_SHOW;
    int86(MOUSE_INT, &regs_in, &regs_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, &regs_in, &regs_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, &regs_in, &regs_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_showmouse_hidemouse_get_statemouse_set_pos 函数,以使用Turbo-C 的int86 函数提供基本的鼠标轮询接口。测试代码反复更新屏幕左上角的当前鼠标状态,直到按下某个键。这是在 DOSBox 中出现的:

由于您正在做作业,我不会为您提供检查按钮状态和绘制图像的代码,但这足以让您确定当前的鼠标状态并自己触发动画。

鼠标界面应该可以在大多数标准图形模式下工作。


脚注

- 1一般 PC BIOS 不直接支持 `Int 0x33`。 DOS 鼠标驱动程序实现此接口以增强标准 BIOS 服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多