SDL2.0中的像素处理

时间:2018-01-31 06:48:54

标签: c sdl-2

我无法在SDL 2.0中显示像素。

#include<SDL2/SDL.h>
#include<stdbool.h>

void end( SDL_Window *Window, SDL_Surface *Surface);
void drawPixel( SDL_Surface *Surface, int x, int y);
void main(){
    Uint32 start;
    const int FPS = 60;
    bool running = true;
    SDL_Event event;
    SDL_Window *main_window = NULL;
    SDL_Surface *main_screen = NULL;
    SDL_Init( SDL_INIT_EVERYTHING );
    main_window =  SDL_CreateWindow("Pixel", SDL_WINDOWPOS_UNDEFINED,
                        SDL_WINDOWPOS_UNDEFINED,800,600,
                        SDL_WINDOW_RESIZABLE);

    main_screen = SDL_GetWindowSurface ( main_window );
    SDL_UpdateWindowSurface( main_window );

    //printf("%d\n",main_screen->pixels);
    SDL_FillRect( main_screen, NULL, SDL_MapRGB(main_screen->format, 255, 255, 255));
    SDL_UpdateWindowSurface( main_window );
    for(int i = 0; i <= 3000; i++ ){
        drawPixel( main_screen, i, 0);
        //break;
    }
   // SDL_UpdateWindowSurface( main_window );

    while( running ) {
      //  start = SDL_GetTicks();
        while ( SDL_PollEvent( &event ) ){
            switch( event.type ){
                case SDL_QUIT:
                    running = false;
                    break;
            }
        }
    }
    end( main_window, main_screen);

}

void end(SDL_Window *Window, SDL_Surface *Surface){
    SDL_FreeSurface( Surface );
    SDL_DestroyWindow( Window );
}

void drawPixel(SDL_Surface *Surface, int x, int y){
    Uint8 *pixel_array = (Uint8 *) Surface->pixels;
    Uint8 *pixel = pixel_array + Surface->pitch * y + x;
    *pixel = SDL_MapRGB(Surface->format, 0, 0, 0 );
}`

我试图找到像素位置并尝试将像素设置为黑色而背景颜色为白色,但它不会打印任何内容。 虽然我试图创建一个行,以防我无法看到像素,但事实证明它不起作用。我只看到白屏。

0 个答案:

没有答案