static int nxsnake_handle_event(GR_EVENT *event) { switch(event->type) { case GR_EVENT_TYPE_KEY_DOWN: /* Allow m to quit no matter where we are*/ if (event->keystroke.ch == 'm') { //GrClose(); fin = 1; pz_close_window(offscreen); pz_close_window(swindow); GrDestroyTimer(nxsnake_timer); //exit(0); } switch(game_state) { case SNAKE_START: if (event->keystroke.ch == 'd') { game_state = SNAKE_START; draw_string((char *) instructions, 1); show_buffer(); break; } case SNAKE_INSTRUCTIONS: case SNAKE_DONE: switch(event->keystroke.ch) { default: start_game(); break; } break; case SNAKE_PAUSED: if ((event->keystroke.ch == '\r') || (event->keystroke.ch == 'd')) { draw_score(); do_frame(1); game_state = SNAKE_PLAYING; } break; case SNAKE_NEXTLEVEL: if (current_level >= LEVELCOUNT) current_level = 0; start_level(current_level); draw_score(); do_frame(1); /* and show the first frame */ break; case SNAKE_PLAYING: if (event->keystroke.ch == 'd') { game_state = SNAKE_PAUSED; draw_string((char *) instructions, 1); show_buffer(); break; } if (event->keystroke.ch == '\r') { game_state = SNAKE_PAUSED; draw_string((char *) gamepaused, 2); show_buffer(); break; } if ((event->keystroke.ch == 'w') || (event->keystroke.ch == 'f')) { if (redirect_snake(event->keystroke)) do_snake_advance(); } if ((event->keystroke.ch == 'l') || (event->keystroke.ch == 'r')) { if(last_keystroke == event->keystroke.ch) { if (count_wheel > 7) { if (redirect_snake(event->keystroke)) do_snake_advance(); count_wheel = 0; } else count_wheel ++; } else count_wheel = 0; } last_keystroke = event->keystroke.ch; break; } break; case GR_EVENT_TYPE_EXPOSURE: show_buffer(); break; } return(1); }
int main(int argc, char *argv[]) { int opt; bzero(&g, sizeof(g)); while((opt = getopt(argc, argv, "eiartucg")) != -1) { switch(opt) { case 'e': g.print_flag_frame = 1; break; case 'i': g.print_flag_ip = 1; break; case 'a': g.print_flag_arp = 1; break; case 'r': g.print_flag_rarp = 1; break; case 't': g.print_flag_tcp = 1; break; case 'u': g.print_flag_udp = 1; break; case 'c': g.print_flag_icmp = 1; break; case 'g': g.print_flag_igmp = 1; break; default: exit(-1); } } if(optind == 0) { g.print_flag_frame = 1; g.print_flag_ip = 1; g.print_flag_arp = 1; g.print_flag_rarp = 1; g.print_flag_tcp = 1; g.print_flag_udp = 1; g.print_flag_icmp = 1; g.print_flag_igmp = 1; } int sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if(sd < 0) { perror("socket()"); exit(errno); } signal(SIGINT, print_global); while(1) do_frame(sd); return 0; }
int main( int argc, char ** argv ) { /* 用于存储套接口文件描述符 */ int sockfd; /* 初始化全局变量 */ init_global( &global ); if( argc == 1 ) { /* 表示打印所有包头信息 */ global.print_flag_frame = 1; global.print_flag_arp = 1; global.print_flag_ip = 1; global.print_flag_rarp = 1; global.print_flag_tcp = 1; global.print_flag_udp = 1; global.print_flag_icmp = 1; global.print_flag_igmp = 1; } else { /* 帮助 或者 通过指定协议名称只打印某层些协议 */ if( !strcasecmp( argv[1], "-h" ) ){ help(); exit( 0 ); } else { int i; for( i=1; i < argc; i++ ){ if( !strcasecmp( argv[i], "frame" ) ) global.print_flag_frame = 1; else if( !strcasecmp( argv[i], "arp" ) ) global.print_flag_arp = 1; else if( !strcasecmp( argv[i], "rarp" ) ) global.print_flag_rarp = 1; else if( !strcasecmp( argv[i], "ip" ) ) global.print_flag_ip = 1; else if( !strcasecmp( argv[i], "tcp" ) ) global.print_flag_tcp = 1; else if( !strcasecmp( argv[i], "udp" ) ) global.print_flag_udp = 1; else if( !strcasecmp( argv[i], "icmp" ) ) global.print_flag_icmp = 1; else if( !strcasecmp( argv[i], "igmp" ) ) global.print_flag_igmp = 1; } } } /* 通过协议族AF_PACKET类信SOCK_RAW, 类型SOCK_RAW创建一个用于可以接受网卡帧数据的套接口,同时返回套就口文件描述符 */ if( (sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) ) == -1 ) error_and_exit( "socket", 1 ); /* 如果发生错误,返回错误值, 并退出 */ /* 设定网卡eth0成混杂模式 */ set_card_promisc( "eth0", sockfd ); /* 设定信号处理函数, 下面是设置当我们按下ctrl-c时所调用的处理函数 */ signal( SIGINT, sig_int ); /* 无限循环接收以太网卡数据帧, 并进行数据分用,直到你按下ctrl-c */ while( 1 ){ do_frame( sockfd ); } return 0; }
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd, int nCmdShow) { WSADATA wsadata; WNDCLASS wc; RECT window; int w, h; u_long mode; MSG msg; int pxformat; HDC hdc; HGLRC hrc, hrc_tmp; PIXELFORMATDESCRIPTOR pfd; LARGE_INTEGER freq, now, then; struct addrinfo *root, *info; const char *version; config_t cfg; int len; uint8_t buf[65536]; if (__argc != 3) { printf("usage: %s [ADDR] [PORT]\n", __argv[0]); return 0; } if (!QueryPerformanceFrequency(&freq)) return 0; if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR) return 0; loadconfig(&cfg); sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) goto EXIT_WSACLEANUP; mode = 1; if (ioctlsocket(sock, FIONBIO, &mode)) goto EXIT_CLOSE_SOCK; if (getaddrinfo(__argv[1], __argv[2], 0, &root)) { printf("getaddrinfo() failed\n"); goto EXIT_CLOSE_SOCK; } info = root; do { if (info->ai_socktype && info->ai_socktype != SOCK_DGRAM) continue; if (connect(sock, info->ai_addr, info->ai_addrlen)) break; } while ((info = info->ai_next)); freeaddrinfo(root); wc.style = CS_OWNDC; wc.lpfnWndProc = wndproc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = instance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = 0; wc.lpszMenuName = NULL; wc.lpszClassName = NAME; if (!RegisterClass(&wc)) goto EXIT_CLOSE_SOCK; memset(&window, 0, sizeof(window)); if (!AdjustWindowRect(&window, WS_OVERLAPPEDWINDOW, 0)) goto EXIT_UNREGISTER; w = cfg.width + window.right - window.left; h = cfg.height + window.bottom - window.top; //minSize.x = MIN_WIDTH + window.right - window.left; //minSize.y = MIN_HEIGHT + window.bottom - window.top; hwnd = CreateWindow(NAME, NAME, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, w, h, 0, 0, instance, 0); if (!hwnd) goto EXIT_UNREGISTER; tme.hwndTrack = hwnd; /*if (WSAAsyncSelect(sock, hwnd, WM_SOCKET, FD_READ)) goto EXIT_DESTROY_WINDOW; */ hdc = GetDC(hwnd); memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DEPTH_DONTCARE; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pxformat = ChoosePixelFormat(hdc, &pfd); if (!SetPixelFormat(hdc, pxformat, &pfd)) goto EXIT_DESTROY_WINDOW; hrc_tmp = wglCreateContext(hdc); if (!hrc_tmp) goto EXIT_DESTROY_WINDOW; if (!wglMakeCurrent(hdc, hrc_tmp)) { wglDeleteContext(hrc_tmp); goto EXIT_DESTROY_WINDOW; } if (!linkgl()) { wglMakeCurrent(hdc, 0); wglDeleteContext(hrc_tmp); goto EXIT_DESTROY_WINDOW; } PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = (void*) wglGetProcAddress("wglCreateContextAttribsARB"); if (!wglCreateContextAttribs) printf("....\n"); hrc = wglCreateContextAttribs(hdc, 0, ctx_attribs); wglMakeCurrent(hdc, 0); wglDeleteContext(hrc_tmp); if (!hrc) goto EXIT_DESTROY_WINDOW; if (!wglMakeCurrent(hdc, hrc)) goto EXIT_DELETE_CONTEXT; version = (const char*)glGetString(GL_VERSION); printf("GL version: %s\n", version); wglSwapIntervalEXT(1); if (!do_init(&cfg)) goto EXIT_DELETE_CONTEXT; init_done = 1; thread(audio_thread, 0); QueryPerformanceCounter(&then); while (!done) { while ((len = recv(sock, (char*)buf, sizeof(buf), 0)) >= 0) do_recv(buf, len); QueryPerformanceCounter(&now); do_frame((double)(now.QuadPart - then.QuadPart) / freq.QuadPart); then = now; SwapBuffers(hdc); while (PeekMessage(&msg, 0, 0, 0, 1)) { if (msg.message == WM_QUIT) { done = 1; break; } //TranslateMessage(&msg); DispatchMessage(&msg); } } EXIT_DELETE_CONTEXT: wglMakeCurrent(hdc, 0); wglDeleteContext(hrc); EXIT_DESTROY_WINDOW: DestroyWindow(hwnd); EXIT_UNREGISTER: UnregisterClass(NAME, instance); EXIT_CLOSE_SOCK: closesocket(sock); EXIT_WSACLEANUP: WSACleanup(); return 0; }
/* * M A I N */ int main(int argc, char **argv) { struct rt_i *rtip = NULL; char *title_file = NULL, *title_obj = NULL; /* name of file and first object */ char idbuf[RT_BUFSIZE] = {0}; /* First ID record info */ void application_init(); struct bu_vls times; int i; #if defined(_WIN32) && !defined(__CYGWIN__) setmode(fileno(stdin), O_BINARY); setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #else bu_setlinebuf( stdout ); bu_setlinebuf( stderr ); #endif #ifdef HAVE_SBRK beginptr = (char *) sbrk(0); #endif azimuth = 35.0; /* GIFT defaults */ elevation = 25.0; AmbientIntensity=0.4; background[0] = background[1] = 0.0; background[2] = 1.0/255.0; /* slightly non-black */ /* Before option processing, get default number of processors */ npsw = bu_avail_cpus(); /* Use all that are present */ if ( npsw > MAX_PSW ) npsw = MAX_PSW; /* Before option processing, do application-specific initialization */ RT_APPLICATION_INIT( &ap ); application_init(); /* Process command line options */ if ( !get_args( argc, argv ) ) { (void)fputs(usage, stderr); return 1; } /* Identify the versions of the libraries we are using. */ if (rt_verbosity & VERBOSE_LIBVERSIONS) { (void)fprintf(stderr, "%s%s%s%s\n", brlcad_ident(title), rt_version(), bn_version(), bu_version() ); } #if defined(DEBUG) (void)fprintf(stderr, "Compile-time debug symbols are available\n"); #endif #if defined(NO_BOMBING_MACROS) || defined(NO_MAGIC_CHECKING) || defined(NO_BADRAY_CECHKING) || defined(NO_DEBUG_CHECKING) (void)fprintf(stderr, "WARNING: Run-time debugging is disabled and may enhance performance\n"); #endif /* Identify what host we're running on */ if (rt_verbosity & VERBOSE_LIBVERSIONS) { char hostname[512] = {0}; #ifndef _WIN32 if ( gethostname( hostname, sizeof(hostname) ) >= 0 && hostname[0] != '\0' ) (void)fprintf(stderr, "Running on %s\n", hostname); #else sprintf(hostname, "Microsoft Windows"); (void)fprintf(stderr, "Running on %s\n", hostname); #endif } if ( bu_optind >= argc ) { fprintf(stderr, "%s: MGED database not specified\n", argv[0]); (void)fputs(usage, stderr); return 1; } if (rpt_overlap) ap.a_logoverlap = ((void (*)())0); else ap.a_logoverlap = rt_silent_logoverlap; /* If user gave no sizing info at all, use 512 as default */ if ( width <= 0 && cell_width <= 0 ) width = 512; if ( height <= 0 && cell_height <= 0 ) height = 512; /* If user didn't provide an aspect ratio, use the image * dimensions ratio as a default. */ if (aspect <= 0.0) { aspect = (fastf_t)width / (fastf_t)height; } if ( sub_grid_mode ) { /* check that we have a legal subgrid */ if ( sub_xmax >= width || sub_ymax >= height ) { fprintf( stderr, "rt: illegal values for subgrid %d,%d,%d,%d\n", sub_xmin, sub_ymin, sub_xmax, sub_ymax ); fprintf( stderr, "\tFor a %d X %d image, the subgrid must be within 0, 0,%d,%d\n", width, height, width-1, height-1 ); return 1; } } if ( incr_mode ) { int x = height; if ( x < width ) x = width; incr_nlevel = 1; while ( (1<<incr_nlevel) < x ) incr_nlevel++; height = width = 1<<incr_nlevel; if (rt_verbosity & VERBOSE_INCREMENTAL) fprintf(stderr, "incremental resolution, nlevels = %d, width=%d\n", incr_nlevel, width); } /* * Handle parallel initialization, if applicable. */ #ifndef PARALLEL npsw = 1; /* force serial */ #endif if ( npsw < 0 ) { /* Negative number means "all but" npsw */ npsw = bu_avail_cpus() + npsw; } /* allow debug builds to go higher than the max */ if (!(bu_debug & BU_DEBUG_PARALLEL)) { if ( npsw > MAX_PSW ) { npsw = MAX_PSW; } } if (npsw > 1) { rt_g.rtg_parallel = 1; if (rt_verbosity & VERBOSE_MULTICPU) fprintf(stderr, "Planning to run with %d processors\n", npsw ); } else { rt_g.rtg_parallel = 0; } /* Initialize parallel processor support */ bu_semaphore_init( RT_SEM_LAST ); /* * Do not use bu_log() or bu_malloc() before this point! */ if ( bu_debug ) { bu_printb( "libbu bu_debug", bu_debug, BU_DEBUG_FORMAT ); bu_log("\n"); } if ( RT_G_DEBUG ) { bu_printb( "librt rt_g.debug", rt_g.debug, DEBUG_FORMAT ); bu_log("\n"); } if ( rdebug ) { bu_printb( "rt rdebug", rdebug, RDEBUG_FORMAT ); bu_log("\n"); } /* We need this to run rt_dirbuild */ rt_init_resource( &rt_uniresource, MAX_PSW, NULL ); bn_rand_init( rt_uniresource.re_randptr, 0 ); title_file = argv[bu_optind]; title_obj = argv[bu_optind+1]; nobjs = argc - bu_optind - 1; objtab = &(argv[bu_optind+1]); if ( nobjs <= 0 ) { bu_log("%s: no objects specified -- raytrace aborted\n", argv[0]); return 1; } /* Echo back the command line arugments as given, in 3 Tcl commands */ if (rt_verbosity & VERBOSE_MODELTITLE) { struct bu_vls str; bu_vls_init(&str); bu_vls_from_argv( &str, bu_optind, (const char **)argv ); bu_vls_strcat( &str, "\nopendb " ); bu_vls_strcat( &str, title_file ); bu_vls_strcat( &str, ";\ntree " ); bu_vls_from_argv( &str, nobjs <= 16 ? nobjs : 16, (const char **)argv+bu_optind+1 ); if ( nobjs > 16 ) bu_vls_strcat( &str, " ..."); else bu_vls_putc( &str, ';' ); bu_log("%s\n", bu_vls_addr(&str) ); bu_vls_free(&str); } /* Build directory of GED database */ bu_vls_init( × ); rt_prep_timer(); if ( (rtip=rt_dirbuild(title_file, idbuf, sizeof(idbuf))) == RTI_NULL ) { bu_log("rt: rt_dirbuild(%s) failure\n", title_file); return 2; } ap.a_rt_i = rtip; (void)rt_get_timer( ×, NULL ); if (rt_verbosity & VERBOSE_MODELTITLE) bu_log("db title: %s\n", idbuf); if (rt_verbosity & VERBOSE_STATS) bu_log("DIRBUILD: %s\n", bu_vls_addr(×) ); bu_vls_free( × ); memory_summary(); /* Copy values from command line options into rtip */ rtip->rti_space_partition = space_partition; rtip->rti_nugrid_dimlimit = nugrid_dimlimit; rtip->rti_nu_gfactor = nu_gfactor; rtip->useair = use_air; rtip->rti_save_overlaps = save_overlaps; if ( rt_dist_tol > 0 ) { rtip->rti_tol.dist = rt_dist_tol; rtip->rti_tol.dist_sq = rt_dist_tol * rt_dist_tol; } if ( rt_perp_tol > 0 ) { rtip->rti_tol.perp = rt_perp_tol; rtip->rti_tol.para = 1 - rt_perp_tol; } if (rt_verbosity & VERBOSE_TOLERANCE) rt_pr_tol( &rtip->rti_tol ); /* before view_init */ if ( outputfile && strcmp( outputfile, "-") == 0 ) outputfile = (char *)0; /* * Initialize application. * Note that width & height may not have been set yet, * since they may change from frame to frame. */ if ( view_init( &ap, title_file, title_obj, outputfile!=(char *)0, framebuffer!=(char *)0 ) != 0 ) { /* Framebuffer is desired */ register int xx, yy; int zoom; /* Ask for a fb big enough to hold the image, at least 512. */ /* This is so MGED-invoked "postage stamps" get zoomed up big enough to see */ xx = yy = 512; if ( width > xx || height > yy ) { xx = width; yy = height; } bu_semaphore_acquire( BU_SEM_SYSCALL ); fbp = fb_open( framebuffer, xx, yy ); bu_semaphore_release( BU_SEM_SYSCALL ); if ( fbp == FBIO_NULL ) { fprintf(stderr, "rt: can't open frame buffer\n"); return 12; } bu_semaphore_acquire( BU_SEM_SYSCALL ); /* If fb came out smaller than requested, do less work */ if ( fb_getwidth(fbp) < width ) width = fb_getwidth(fbp); if ( fb_getheight(fbp) < height ) height = fb_getheight(fbp); /* If the fb is lots bigger (>= 2X), zoom up & center */ if ( width > 0 && height > 0 ) { zoom = fb_getwidth(fbp)/width; if ( fb_getheight(fbp)/height < zoom ) zoom = fb_getheight(fbp)/height; } else { zoom = 1; } (void)fb_view( fbp, width/2, height/2, zoom, zoom ); bu_semaphore_release( BU_SEM_SYSCALL ); } if ( (outputfile == (char *)0) && (fbp == FBIO_NULL) ) { /* If not going to framebuffer, or to a file, then use stdout */ if ( outfp == NULL ) outfp = stdout; /* output_is_binary is changed by view_init, as appropriate */ if ( output_is_binary && isatty(fileno(outfp)) ) { fprintf(stderr, "rt: attempting to send binary output to terminal, aborting\n"); return 14; } } /* * Initialize all the per-CPU memory resources. * The number of processors can change at runtime, init them all. */ for ( i=0; i < MAX_PSW; i++ ) { rt_init_resource( &resource[i], i, rtip ); bn_rand_init( resource[i].re_randptr, i ); } memory_summary(); #ifdef SIGUSR1 (void)signal( SIGUSR1, siginfo_handler ); #endif #ifdef SIGINFO (void)signal( SIGINFO, siginfo_handler ); #endif if ( !matflag ) { int frame_retval; def_tree( rtip ); /* Load the default trees */ do_ae( azimuth, elevation ); frame_retval = do_frame( curframe ); if (frame_retval != 0) { /* Release the framebuffer, if any */ if ( fbp != FBIO_NULL ) { fb_close(fbp); } return 1; } } else if ( !isatty(fileno(stdin)) && old_way( stdin ) ) { ; /* All is done */ } else { register char *buf; register int ret; /* * New way - command driven. * Process sequence of input commands. * All the work happens in the functions * called by rt_do_cmd(). */ while ( (buf = rt_read_cmd( stdin )) != (char *)0 ) { if ( R_DEBUG&RDEBUG_PARSE ) fprintf(stderr, "cmd: %s\n", buf ); ret = rt_do_cmd( rtip, buf, rt_cmdtab ); bu_free( buf, "rt_read_cmd command buffer" ); if ( ret < 0 ) break; } if ( curframe < desiredframe ) { fprintf(stderr, "rt: Desired frame %d not reached, last was %d\n", desiredframe, curframe); } } /* Release the framebuffer, if any */ if (fbp != FBIO_NULL) { fb_close(fbp); } return(0); }
bool FeSwf::tick() { return do_frame( true ); }