如何在Turbo-C中通过鼠标触发动画?计算机图形学项目求助
Hey there! I remember wrestling with this exact issue back when I was working on Turbo C graphics projects—getting mouse clicks to kick off animations instead of keyboard inputs is totally achievable, you just need to work with DOS interrupts since Turbo C doesn’t have built-in mouse support. Let’s break this down step by step.
Key Concepts to Know
Turbo C relies on the DOS mouse interrupt (int 0x33) for all mouse operations. This interrupt has different functions to initialize the mouse, check button status, get coordinates, and more. Here’s what you’ll need:
- Initialize the Mouse: First, you have to tell the system you want to use the mouse.
- Detect Mouse Clicks: Continuously check if a mouse button (like left-click) is pressed.
- Trigger Animation: Once a click is detected, run your animation logic, then wait for the button to release to avoid repeated triggers.
Full Working Code Example
This code sets up a graphics window, waits for a left-click, then runs a simple moving circle animation at the click position:
#include <graphics.h> #include <dos.h> #include <conio.h> // Initialize mouse and return success status (1 = success, 0 = failure) int initMouse() { union REGS regs; regs.x.ax = 0; // Function 0: Initialize mouse int86(0x33, ®s, ®s); return regs.x.ax; } // Get current mouse button status and coordinates void getMouseState(int *buttonStatus, int *xPos, int *yPos) { union REGS regs; regs.x.ax = 3; // Function 3: Get mouse state & position int86(0x33, ®s, ®s); *buttonStatus = regs.x.bx; // Bit 0 = left click, Bit 1 = right click *xPos = regs.x.cx; *yPos = regs.x.dx; } // Sample animation: Moving circle from click position void playAnimation(int startX, int startY) { for (int offset = 0; offset < 100; offset++) { cleardevice(); // Clear screen for next frame circle(startX + offset, startY, 25); // Draw circle at new position delay(30); // Control animation speed (ms) } } int main() { int graphDriver = DETECT, graphMode; initgraph(&graphDriver, &graphMode, ""); // Initialize graphics mode // Check if mouse initialization worked if (!initMouse()) { outtextxy(50, 50, "Mouse initialization failed!"); outtextxy(50, 70, "Make sure mouse driver is loaded (mouse.com/mouse.sys)"); delay(3000); closegraph(); return 1; } int buttonState, mouseX, mouseY; outtextxy(50, 50, "Left-click anywhere to start animation!"); outtextxy(50, 70, "Press ESC to exit"); while (1) { getMouseState(&buttonState, &mouseX, &mouseY); // Check for left-click (bit 0 of buttonState is set) if (buttonState & 1) { cleardevice(); playAnimation(mouseX, mouseY); outtextxy(50, 50, "Click again for another animation!"); // Wait for mouse button release to avoid repeated triggers while (buttonState & 1) { getMouseState(&buttonState, &mouseX, &mouseY); } } // Exit on ESC key press if (kbhit() && getch() == 27) { break; } } closegraph(); return 0; }
Important Notes
- Mouse Driver Requirement: In DOS (or DOSBox, which you’re probably using for Turbo C), you need to load the mouse driver first. Run
mouse.comin the DOS prompt before launching Turbo C, or add it to yourautoexec.batfile. - Coordinate Alignment: The mouse coordinates returned by
int 0x33match Turbo C’s graphics mode coordinates (y=0 at the top of the screen), so no conversion is needed. - Avoiding Repeat Triggers: The
whileloop waiting for button release ensures the animation only runs once per click, even if you hold the button down. - Animation Speed: Adjust the
delay()value to make the animation faster or slower—lower values mean faster movement.
内容的提问来源于stack exchange,提问作者Sheldor2304

