⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel.c

📁 pyos is a teach operater system build by a chinese student
💻 C
📖 第 1 页 / 共 2 页
字号:
    }
  }// i 键处理结束
  else if( ch == 'k' ){ // k ,向下走
    if( chess[ x + 1 ][ y ] == '|' || chess[ x + 1 ][ y ] == '-' || chess[ x + 1 ][ y ] == 'Q' ){
      // 如果下方是墙,或有球的箱子,则不能通过,直接返回
      return ;
    }
    else{
      // 可以移动
      // 把老地方置空
      chess[ x ][ y ] = ' ' ;
      // 检测人欲移到的位置现在是不是一个箱子
      if( chess[ x + 1 ][ y ] == 'O' ){
        // 是一个箱子,则把箱子移动一下
        // 检测箱子移动到的地方是不是球
        if( chess[ x + 2 ][ y ] == 'o' ){
          // 是球,则把此地作为有球的箱子
          chess[ x + 2 ][ y ] = 'Q' ;
        }
        else{
          // 不是球,则把此地作为箱子
          chess[ x + 2 ][ y ] = 'O' ;
        }
      }
      // 人向下走一格
      ++x ; // 因为,x 是行,
      chess[ x ][ y ] = '*' ;
    }
  }
  else if( ch == 'j' ){ // j ,向左走
    if( chess[ x ][ y - 1 ] == '|' || chess[ x ][ y - 1 ] == '-' || chess[ x ][ y - 1 ] == 'Q' ){
      // 如果左方是墙,或有球的箱子,则不能通过,直接返回
      return ;
    }
    else{
      // 可以移动
      // 把老地方置空
      chess[ x ][ y ] = ' ' ;
      // 检测人欲移到的位置现在是不是一个箱子
      if( chess[ x ][ y - 1 ] == 'O' ){
        // 是一个箱子,则把箱子移动一下
        // 检测箱子移动到的地方是不是球
        if( chess[ x ][ y - 2 ] == 'o' ){
          // 是球,则把此地作为有球的箱子
          chess[ x ][ y - 2 ] = 'Q' ;
        }
        else{
          // 不是球,则把此地作为箱子
          chess[ x ][ y - 2 ] = 'O' ;
        }
      }
      // 人向左走一格
      --y ; // 因为,y 是列,
      chess[ x ][ y ] = '*' ;
    }
  }// j 键处理结束
  else if( ch == 'l' ){ // l ,向右走            
    if( chess[ x ][ y + 1 ] == '|' || chess[ x ][ y + 1 ] == '-' || chess[ x ][ y + 1 ] == 'Q' ){
      // 如果左方是墙,或有球的箱子,则不能通过,直接返回
      return ;
    }
    else{
      // 可以移动
      // 把老地方置空
      chess[ x ][ y ] = ' ' ;
      // 检测人欲移到的位置现在是不是一个箱子
      if( chess[ x ][ y + 1 ] == 'O' ){
        // 是一个箱子,则把箱子移动一下
        // 检测箱子移动到的地方是不是球
        if( chess[ x ][ y + 2 ] == 'o' ){
          // 是球,则把此地作为有球的箱子
          chess[ x ][ y + 2 ] = 'Q' ;
        }
        else{
          // 不是球,则把此地作为箱子
          chess[ x ][ y + 2 ] = 'O' ;
        }
      }
      // 人向左走一格
      ++y ; // 因为,y 是列,
      chess[ x ][ y ] = '*' ;
    }
  }// l 键处理结束
  else if( ch == 'r' ){ // R 键( 此处是 Esc )
    close_game() ;
    return ;
  }
  // 重画棋盘
  kernel_draw_game( game_picture_x , game_picture_y )  ;
}


// 内核主函数
void kernel_main()
{
  // 操作系统初始化
  system_init() ;  //0x903ae

  old_picture = ( unsigned short * )0x100000 ; // 越过前面的只读内存区

  // 画登陆界面
  kernel_draw_login_form() ; // 0x903b3

  // 清空键盘缓冲区
  io_read_from_io_port( 0x60 ) ;
  
  // 初始化消息队列
  message_init_message_queue( &kernel_message_queue ) ;

  // 开中断
  interrupt_open_interrupt() ;

  struct message_message_struct message ;

  kernel_kernel_state = KERNEL_WAIT_USER_LOGIN ;

  // 进入消息循环
  for( ;; ){
    if( !message_get_message( &kernel_message_queue , &message ) ){
      continue ;
    }
    if( message.message_type == MESSAGE_SHUTDOWN_COMPUTER ){
      break ;
    }    
    switch( message.message_type ){
      case MESSAGE_KEYBOARD_MESSAGE : // 键盘消息
        // 检测是不是正在游戏,否则不处理
        if( kernel_kernel_state == KERNEL_GAME ){
          // 调用游戏处理函数进行处理
          game_handle( message.key ) ;
        }
        break ;

      case MESSAGE_MOUSE_MESSAGE : // mouse 消息
        if( kernel_kernel_state == KERNEL_WAIT_USER_LOGIN ){
          // 检测是否需要登陆
          if( message.dose_left_button_down && kernel_mouse_type == MOUSE_OVER_MOUSE ){
            kernel_login() ;
          }
          // 检测是否需要移动 mouse
          else if( kernel_mouse_x_position != message.x_position || kernel_mouse_y_position != message.y_position ){
            // 检测是否需要改变 mouse 形状
            if( kernel_does_point_in_rect( message.x_position , message.y_position , 150 , 271 , 431 , 375 ) ){
              kernel_mouse_type = MOUSE_OVER_MOUSE ;
            }
            else{
              kernel_mouse_type = MOUSE_NORMAL_MOUSE ;
            }
            mouse_move_mouse( kernel_mouse_x_position , kernel_mouse_y_position , message.x_position , message.y_position , kernel_mouse_type ) ;
            kernel_mouse_x_position = message.x_position ;
            kernel_mouse_y_position = message.y_position ;          
          }
        }
        else if( kernel_kernel_state == KERNEL_USER_LOGED_IN || kernel_kernel_state == KERNEL_GAME ){
          // 检测是否要启动游戏,如果游戏已启动,则不再启动,但如果在游戏状态下,则应用程序图标失效
          int ibool2 ;
          if( kernel_kernel_state == KERNEL_GAME ){
            // 在游戏状态下,应用程序图标失效
            ibool2 = 0 ; 
          }
          else{
            int x_temp = message.x_position ;
            if( kernel_mouse_type == MOUSE_OVER_MOUSE ){
              x_temp += 8 ;
            }
            ibool2 = kernel_does_point_in_rect( x_temp , message.y_position , application_picture_x , application_picture_y , application_picture_x + 128 , application_picture_y + 128 ) ;
          }

          if( ibool2 && message.dose_left_button_down && kernel_mouse_type == MOUSE_OVER_MOUSE ){
            kernel_start_game() ;
          }

          // 检测是否要关闭游戏,如果不是游戏状态,失效
          int ibool3 ;
          if( kernel_kernel_state == KERNEL_GAME ){   
            int x_temp = message.x_position ;
            if( kernel_mouse_type == MOUSE_OVER_MOUSE ){
              x_temp += 8 ;
            }
            ibool3 = kernel_does_point_in_rect( x_temp , message.y_position , game_picture_x + 338 , game_picture_y + 2 , game_picture_x + 354 , game_picture_y + 16 ) ;
          }
          else{
            ibool3 = 0 ;
          }
          if( ibool3 && message.dose_left_button_down && kernel_mouse_type == MOUSE_OVER_MOUSE ){
            // 关闭游戏
            // 重画桌面
            close_game() ;
            break ;
          }

          // 检测是否需要移动
          // 获得 x , y  的移动量
          int imove_x = kernel_mouse_x_position - message.x_position ;
          int imove_y = kernel_mouse_y_position - message.y_position ;

          // 检测是否需要移动应用程序图标
          if( ibool2 && message.dose_right_button_down && kernel_mouse_type == MOUSE_DOWN_MOUSE && ( imove_x || imove_y ) ){
            // 表示要移动图标
            // 先恢复原图片
            vesa_copy_picture_to_screen( application_picture_x , application_picture_y , old_picture , 128 + 32 , 128 + 32 ) ;
            // 再保存目标地址图片
            vesa_copy_picture_from_screen( application_picture_x - imove_x , application_picture_y - imove_y, old_picture , 128 + 32 , 128 + 32 ) ;
            // 再在新位置画出应用程序图片
            application_picture_x -= imove_x ;
            application_picture_y -= imove_y ;
            unsigned short color = vesa_compond_rgb( 0 , 255 , 0 ) ;
            vesa_show_bmp_picture( application_picture_x , application_picture_y , ( void * )0x70000 , color , 1 ) ;
            // mouse 由下面画,这里先保存 mouse 原位置的图
            mouse_save_picture( kernel_mouse_x_position , kernel_mouse_y_position ) ;
          }
          
          // 检测是否需要移动游戏,如果不是在游戏状态下,则失效,如果 ibool3 有效,则也失效,因为它同 ibool3 互不相容
          int ibool4 ;
          if( kernel_kernel_state != KERNEL_GAME || ibool3 ){
            ibool4 = 0 ;
          }
          else{
            int x_temp = message.x_position ;
            if( kernel_mouse_type == MOUSE_OVER_MOUSE ){
              x_temp += 8 ;
            }
            ibool4 = kernel_does_point_in_rect( x_temp , message.y_position , game_picture_x , game_picture_y , game_picture_x + 356  , game_picture_y + 18 ) ;
          }
          if( ibool4 && kernel_mouse_type == MOUSE_DOWN_MOUSE && ( imove_x || imove_y ) ){
            // 先恢复原图片
            vesa_copy_picture_to_screen( game_picture_x , game_picture_y , old_picture , 356 + 32 , 372 + 32 ) ;
            // 检测一下 mouse 是否在恢复区中,如果在,就让它保存一下最新恢复的东东
            if( kernel_does_point_in_rect( kernel_mouse_x_position , kernel_mouse_y_position , game_picture_x , game_picture_y , game_picture_x + 356 + 32 , game_picture_y + 372 + 32 ) ){
              mouse_save_picture( kernel_mouse_x_position , kernel_mouse_y_position ) ;
            }
            // 再保存目标地址程序图片
            vesa_copy_picture_from_screen( game_picture_x - imove_x , game_picture_y - imove_y , old_picture , 356 + 32 , 372 + 32 ) ;
            
            // 在新位置画出程序图片
            game_picture_x -= imove_x ;
            game_picture_y -= imove_y ;
            kernel_draw_game( game_picture_x , game_picture_y ) ;
          }

          // 检测是否需要退出
          // 由于这个是任务栏上的,因此,当其余有效时,其不可能有效
          int ibool1 ;
          if( ibool2 || ibool3 || ibool4 ){
            ibool1 = 0 ;
          }
          else{
            ibool1 = kernel_does_point_in_rect( message.x_position , message.y_position , 4 , 570 , 38 , 593 ) ;
          }

          if( ibool1 && message.dose_left_button_down && kernel_mouse_type == MOUSE_OVER_MOUSE ){
            kernel_logout() ;
            break ;
          }

          // 检测是否需要改变 mouse 形状
          // 有上可知 ibool1 , ibool2 , ibool3 , ibool4 在同一时刻,只可能有一个有效
          if( ibool1 ){
            kernel_mouse_type = MOUSE_OVER_MOUSE ;
          }
          else if( ibool2 ){
            if( message.dose_right_button_down ){
              kernel_mouse_type = MOUSE_DOWN_MOUSE ;
            }
            else{
              kernel_mouse_type = MOUSE_OVER_MOUSE ;
            }
          }
          else if( ibool3 ){
            kernel_mouse_type = MOUSE_OVER_MOUSE ;
          }
          else if( ibool4 ){
            if( message.dose_right_button_down ){
              kernel_mouse_type = MOUSE_DOWN_MOUSE ;
            }
            else{
              kernel_mouse_type = MOUSE_OVER_MOUSE ;
            }
          }
          else{
            kernel_mouse_type = MOUSE_NORMAL_MOUSE ;
          }
          
          // 重画 mouse
          mouse_move_mouse( kernel_mouse_x_position , kernel_mouse_y_position , message.x_position , message.y_position , kernel_mouse_type ) ;
          kernel_mouse_x_position = message.x_position ;
          kernel_mouse_y_position = message.y_position ;                    
        }
        break ;
    }
  }
  // 停机
  for( ;; ){
    __asm__( "hlt" ) ;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -