SDL的编程与vc

来源:互联网 发布:易知投资 编辑:程序博客网 时间:2024/06/14 17:33
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://scalpel00.blog.51cto.com/1071749/247399
SDL(simple DirectMedia Layer)是一个可跨平台的开源库,最近由于自己的兴趣,就把它windosXP下的环境搭建了下。
PC:Mircrosoft Windows XP Service Pack3
Platform:Mircrosoft Visual C++ 6.0
SourceCode:SDL-devel-1.2.14-VC6.zip
步骤
    1. 解压SDL-devel-1.2.14-VC6.zip.将解压后的lib文件夹里把SDL.lib SDLmain.lib拷贝到VC6.0安装目录的lib文件夹下面。
    2. 将SDL.dll拷贝到系统盘的WINDOWS/SYSTEM32目录下(如果你要将之后生成的SDL应用程序转移到其他没有配置SDL环境的机器上用的话,请将SDL.dll一起拷贝)。
    3. 在VC6.0安装目录的include文件夹下面新建一个SDL的目录,并将SDL-devel-1.2.14-VC6.zip解压后的include里的文件拷贝到这个SDL目录下面。
    4. 打开VC6,新建一个project->win32 Application.打开project目录下面的那个setting,选中C/C++,Category里选中Code Generation,Use run-time library使用Multithread DLL.
    5. 继续在上面的setting中选中Link,Category里选中input,在Object/library modules中填入SDL.lib SDLmain.lib
    6. 在VC项目中新建一个cpp,并添加到项目中,编译,运行.
相关链接
 
1.SDL http://www.libsdl.org/download-1.2.php
测试代码
#include <stdlib.h>
#include "SDL/SDL.h"

SDL_Surface *screen;

void render()
{        
    // Lock surface if needed
    if (SDL_MUSTLOCK(screen))    
        if (SDL_LockSurface(screen) < 0)    
            return;

    // Ask SDL for the time in milliseconds
    int tick = SDL_GetTicks();

    // Declare a couple of variables
    int i, j, yofs, ofs;

    // Draw to screen
    yofs = 0;
    for (i = 0; i < 480; i++)
    {
        for (j = 0, ofs = yofs; j < 640; j++, ofs++)
        {
            ((unsigned int*)screen->pixels)[ofs] = i * i + j * j + tick;
        }
        yofs += screen->pitch / 4;
    }

    // Unlock if needed
    if (SDL_MUSTLOCK(screen))    
        SDL_UnlockSurface(screen);

    // Tell SDL to update the whole screen
    SDL_UpdateRect(screen, 0, 0, 640, 480);        
}


// Entry point
int main(int argc, char *argv[])
{
    // Initialize SDL's subsystems - in this case, only video.
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )    
    {
        fprintf(stderr, "Unable to init SDL: %s/n", SDL_GetError());
        exit(1);
    }

    // Register SDL_Quit to be called at exit; makes sure things are
    // cleaned up when we quit.
    atexit(SDL_Quit);
        
    // Attempt to create a 640x480 window with 32bit pixels.
    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
    // If we fail, return error.
    if ( screen == NULL )    
    {
        fprintf(stderr, "Unable to set 640x480 video: %s/n", SDL_GetError());
        exit(1);
    }

    // Main loop: loop forever.
    while (1)
    {
        // Render stuff
        render();

        // Poll for events, and handle the ones we care about.
        SDL_Event event;
        while (SDL_PollEvent(&event))    
        {
            switch (event.type)    
            {
            case SDL_KEYDOWN:
                break;
            case SDL_KEYUP:
                // If escape is pressed, return (and thus, quit)
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    return 0;
                break;
            case SDL_QUIT:
                return(0);
            }
        }
    }
    return 0;
}