SDL Guide 中文译版(二)[3]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 nightmare 的 blog

例2-7 初始化sdl加opengl

    /* information about the current video settings. */
    const sdl_videoinfo* info = null;
    /* dimensions of our window. */
    int width = 0;
    int height = 0;
    /* color depth in bits of our window. */
    int bpp = 0;
    /* flags we will pass into sdl_setvideomode. */
    int flags = 0;

    /* first, initialize sdl's video subsystem. */
    if( sdl_init( sdl_init_video ) < 0 ) {
        /* failed, exit. */
        fprintf( stderr, "video initialization failed: %s\n",
             sdl_geterror( ) );
        quit_tutorial( 1 );
    }

    /* let's get some video information. */
    info = sdl_getvideoinfo( );

    if( !info ) {
        /* this should probably never happen. */
        fprintf( stderr, "video query failed: %s\n",
             sdl_geterror( ) );
        quit_tutorial( 1 );
    }

    /*
     * set our width/height to 640/480 (you would
     * of course let the user decide this in a normal
     * app). we get the bpp we will request from
     * the display. on x11, vidmode can't change
     * resolution, so this is probably being overly
     * safe. under win32, changedisplaysettings
     * can change the bpp.
     */
    width = 640;
    height = 480;
    bpp = info->vfmt->bitsperpixel;

    /*
     * now, we want to setup our requested
     * window attributes for our opengl window.
     * we want *at least* 5 bits of red, green
     * and blue. we also want at least a 16-bit
     * depth buffer.
     *
     * the last thing we do is request a double
     * buffered window. '1' turns on double
     * buffering, '0' turns it off.
     *
     * note that we do not use sdl_doublebuf in
     * the flags to sdl_setvideomode. that does
     * not affect the gl attribute state, only
     * the standard 2d blitting setup.
     */
    sdl_gl_setattribute( sdl_gl_red_size, 5 );
    sdl_gl_setattribute( sdl_gl_green_size, 5 );
    sdl_gl_setattribute( sdl_gl_blue_size, 5 );
    sdl_gl_setattribute( sdl_gl_depth_size, 16 );
    sdl_gl_setattribute( sdl_gl_doublebuffer, 1 );

    /*
     * we want to request that sdl provide us
     * with an opengl window, in a fullscreen
     * video mode.
     *
     * exercise:
     * make starting windowed an option, and
     * handle the resize events properly with
     * glviewport.
     */
    flags = sdl_opengl | sdl_fullscreen;

    /*
     * set the video mode
     */
    if( sdl_setvideomode( width, height, bpp, flags ) == 0 ) {
        /* 
         * this could happen for a variety of reasons,
         * including display not being set, the specified
         * resolution not being available, etc.
         */
        fprintf( stderr, "video mode set failed: %s\n",
             sdl_geterror( ) );
        quit_tutorial( 1 );
    }

opengl绘图

除了初始化,在sdl程序中使用opengl和其他情况基本相同,是同样函数和数据类型。但是如果您使用双缓冲,则必须用sdl_gl_swapbuffers()来交换前后缓冲,而不是glxswapbuffers()(glx)或swapbuffers()(windows)。

本文关键:sdl,game,multimedia,cross-platform
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top