gpt4 book ai didi

c - 画一条线: Problems with the mouse cursor on C Vga graphics

转载 作者:行者123 更新时间:2023-11-30 15:14:36 26 4
gpt4 key购买 nike

我正在尝试制作一个程序,您可以通过单击鼠标来画一条线,当您按下按钮移动时,该线将改变大小和方向,并且当您松开按钮时,该线将保留在屏幕上(只需就像在油漆或任何其他照片编辑器中一样)。我正在使用 256-VGA 进行 C 工作。我遇到的问题是在鼠标光标所在的空间上画一条线。我有这两个函数来显示和隐藏鼠标:

typedef struct MOUSE{
byte on;
byte button1;
byte button2;
byte button3;
int num_buttons;
sword x;
sword y;
byte under[MOUSE_SIZE];
MOUSEBITMAP *bmp;


} MOUSE;
/**************************************************************************
* show_mouse *
* Displays the mouse. *
**************************************************************************/
void show_mouse(MOUSE *mouse){
int x,y;
int mx = mouse->x - mouse->bmp->hot_x;
int my= mouse->y - mouse->bmp->hot_y;
long screen_offset = (my<<8)+(my<<6);
word bitmap_offset = 0;
byte data;
//memcpy(double_buffer,VGA,SCREEN_SIZE);
/* allocate memory for double buffer and background image */
/*if ((double_buffer = (byte *) malloc(SCREEN_SIZE)) == NULL)
{
printf("Not enough memory for double buffer.\n");
exit(1);
} */
for(y=0;y<MOUSE_HEIGHT;y++){
for(x=0;x<MOUSE_WIDTH;x++,bitmap_offset++){
mouse->under[bitmap_offset] = VGA[(word) (screen_offset+mx+x)];
if(mx+x < SCREEN_WIDTH && mx+x >=0 &&
my+y < SCREEN_HEIGHT && my+y >=0) //Verificando que estemos dentro de pantalla
{
/*Pintando en pantalla*/
data = mouse->bmp->data[bitmap_offset];
if(data)
{
VGA[(word)(screen_offset+mx+x)] = data;
}
}
}
screen_offset+=SCREEN_WIDTH;
}
/*show_buffer(double_buffer);
free(double_buffer);*/
}
/**************************************************************************
* hide_mouse *
* hides the mouse. This code is not optimized. *
**************************************************************************/

void hide_mouse(MOUSE *mouse){

int x,y;
int mx = mouse->x - mouse->bmp->hot_x;
int my = mouse->y - mouse->bmp->hot_y;
long screen_offset = (my<<8)+(my<<6);
word bitmap_offset = 0;
/*memcpy(double_buffer,VGA,SCREEN_SIZE);
/* allocate memory for double buffer and background image */
/*if ((double_buffer = (byte *) malloc(SCREEN_SIZE)) == NULL)
{
printf("Not enough memory for double buffer.\n");
exit(1);
} */
for(y=0;y<MOUSE_HEIGHT;x++,y++){

for(x=0;x<MOUSE_WIDTH;x++,bitmap_offset++){
//Verificando que este dentro de pantalla
if(mx+x < SCREEN_WIDTH &&mx+x >=0 &&
my+y <SCREEN_HEIGHT && my+y >=0){

/*Pintando en pantalla*/

VGA[(word)(screen_offset+mx+x)] = mouse->under[bitmap_offset];
}

}
screen_offset+=SCREEN_WIDTH;

}
/*show_buffer(double_buffer);
free(double_buffer);*/
}

我有 MOUSE 结构的 under 变量,它在绘制之前具有屏幕的背景。为了使按住按钮时线条随鼠标移动,我存储鼠标之前的位置并绘制一条白线(以清理它),然后绘制另一条具有新位置的线。

问题是,然后我缩小了一条线的尺寸并沿着该线移动鼠标指针,鼠标的重画又再次绘制了之前的线。我得到这样的东西:

收缩线条之前: enter image description here

缩小线条后:

enter image description here

这是与控制鼠标按钮的循环相关的其余代码void realizar_accion(){

void realizar_accion(){
/*Caso1: Pintar pixel simple (lapiz)*/
if(accion==1){

plot_pixel(mouse_g.x,mouse_g.y,2);
plot_pixel(mouse_g.x-1,mouse_g.y,2);
plot_pixel(mouse_g.x,mouse_g.y-1,2);
plot_pixel(mouse_g.x-1,mouse_g.y-1,2);

}
/*Caso2: Pintar Linea*/
else if(accion==2){
init_x = mouse_g.x;
prevx = mouse_g.x;
init_y = mouse_g.y;
prevy = mouse_g.y;
//line(mouse_g.x,mouse_g.y,mouse_g.x-20,mouse_g.y-20,2);
}
}
void realizar_accion_mantenido(){
/*Caso1: Pintar pixel simple (lapiz)*/
if(accion==1){

plot_pixel(mouse_g.x,mouse_g.y,2);
plot_pixel(mouse_g.x-1,mouse_g.y,2);
plot_pixel(mouse_g.x,mouse_g.y-1,2);
plot_pixel(mouse_g.x-1,mouse_g.y-1,2);


}
/*Caso2: Pintar Linea*/
else if(accion==2){
if(on_draw==-1){

line(init_x,init_y,mouse_g.x,mouse_g.y,2);
prevx=mouse_g.x;
prevy=mouse_g.y;
on_draw=0;

}
else{
if(prevx!=mouse_g.x&&prevy!=mouse_g.y){
line(init_x,init_y,prevx,prevy,0xFFFF); //borrando la linea anterior
line(init_x,init_y,mouse_g.x,mouse_g.y,2);
prevx=mouse_g.x;
prevy=mouse_g.y;
}

}



}


}

int sobre_barra(){
if(new_x1>0 && new_x1<33 &&new_y1>0 &&new_y1<181){


return 1;

}
else{

return -1;
}


}
void boton_soltado(){

/*Caso1: Pintar pixel simple (lapiz)*/
if(accion==1){



}
/*Caso2: Pintar Linea*/
else if(accion==2){
if(on_draw==0){
line(init_x,init_y,prevx,prevy,0xFFFF); //borrando la linea anterior
wait_for_retrace();
hide_mouse(&mouse_g);
if (mouse_new!=NULL) mouse_g.bmp=mouse_new;
mouse_g.x = new_x1;
mouse_g.y=new_y1;
show_mouse(&mouse_g);
line(init_x,init_y,mouse_g.x,mouse_g.y,2);
prevx=mouse_g.x;
prevy=mouse_g.y;
on_draw=-1;

}

}



}
void boton_mantenido(){

/*Verificar que este dento del buffer de dibujo....*/
if(sobre_barra()!=1){
realizar_accion_mantenido();

}
}
void boton_presionado(){

if(sobre_barra()==1){
cambiar_herramienta();

}
else{

realizar_accion();

}

}
/**************************************************************************
* paint_screen *
* show main screen paint *
**************************************************************************/
void paint_screen(BITMAP *fondo){
int mantenido;
BITMAP barra,barra_color,normal_ptr_image;
int anterior_presionado;
word last_time;
word redraw,press,release;
sword dx,dy=0;
MOUSEBITMAP *mouse_new=NULL;
int i,done = 0;
on_draw=-1;
accion =2;
current_color=2;
/*Pintando fondo blanco*/
clear_screen();
/*Pintando barra de herramientas*/
load_bmp("normal.bmp",&normal_ptr_image);
load_bmp("mainbar.bmp",&barra);
load_bmp("colores.bmp",&barra_color);
set_pallete(fondo->pallete);
draw_bitmap(&barra,0,0);
draw_bitmap(&barra_color,0,180);
load_mouse(&mouse_g);
show_mouse(&mouse_g);
wait(50);
while(!done){
if(redraw){
wait_for_retrace();
hide_mouse(&mouse_g);
if (mouse_new!=NULL) mouse_g.bmp=mouse_new;
mouse_g.x = new_x1;
mouse_g.y=new_y1;
show_mouse(&mouse_g);
redraw=0;
mouse_new=NULL;
}

do { // check mouse status
anterior_presionado = press;
get_mouse_motion(&dx,&dy);
press = get_mouse_press(LEFT_BUTTON);
release = get_mouse_release(LEFT_BUTTON);
//Si el estado estaba presionado y no se ha soltado.. el boton esta mantenido
if(anterior_presionado==1 &&release==0){
mantenido =1;

}
} while (dx==0 && dy==0 && press==0 && release==0&&*my_clock==last_time);
if (release){
mouse_g.button1=0;
mantenido=0;
boton_soltado();


}

if (press){

mouse_g.button1=1;
boton_presionado();


}
//El boton se mantiene presionado
else if(mantenido){
boton_mantenido();
}
else{
release=1;
}


if (dx || dy) // calculate movement
{
new_x1 = mouse_g.x+dx;
new_y1 = mouse_g.y+dy; //Actualizamos posicion mouse
if (new_x1<0) new_x1=0;
if (new_y1<0) new_y1=0;
if (new_x1>319) new_x1=319;
if (new_y1>199) new_y1=199;
redraw=1;
}

if(new_x1>=287 && new_x1 <320
&& new_y1>=180 && new_y1 < 200 &&press){

set_mode(TEXT_MODE);
printf("Adios!!");
wait(25);
done=0;
return;


}
}
}

谁能帮我解决这个问题吗?

最佳答案

我不建议在旧线上画白线。相反,您应该使用帧缓冲区,它是屏幕外的内存区域,您每次都在其中重新组装它。画出背景,然后画出线条。一旦你放置了这条线,就把它画到背景上..但在那之前它是在之后绘制的。然后,在每个循环中,整个内容都会翻转/复制到屏幕上。有道理吗?

关于c - 画一条线: Problems with the mouse cursor on C Vga graphics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com