15-3.c

来自「提供了用linux下的C语言实现的作图程序。」· C语言 代码 · 共 35 行

C
35
字号
#include <SDL.h>

void DrawPixel ( SDL_Surface *screen, Uint32 color)
{
    Uint16 *bufp;
    if ( SDL_MUSTLOCK ( screen) ) {
        if ( SDL_LockSurface ( screen) < 0 ) {
            return;
        }
    }
    bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
    *bufp = color;
    if ( SDL_MUSTLOCK(screen) ) {
        SDL_UnlockSurface(screen);
    }
    SDL_UpdateRect(screen, x, y, 1, 1); /* 更新屏幕 */
}

int main()
{
    SDL_Surface *screen;
    Uint32 color;
    if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "无法初始化SDL: %s\n", SDL_GetError());
        exit(1);
    }
    screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);  /* 640×480×16位色 */
    if ( screen == NULL ) {
        fprintf(stderr, "无法设置640x480x16位色的视频模式:%s\n", SDL_GetError());
        exit(1);
    }
    atexit(SDL_Quit);
    DrawPixel(screen,320,240,color);
    SDL_Delay(5000);
}

⌨️ 快捷键说明

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