c – SDL2:快速像素操作

前端之家收集整理的这篇文章主要介绍了c – SDL2:快速像素操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在显示器上绘制像素,这些像素在某些参数后经常变化.例如.如果红色和绿色像素发生碰撞,它们都会消失,等等.

在每一帧中,我必须操纵大约100-1000像素.我这里有一个多线程的方法,它不会给我30FPS(我想要的).目前,我在RAM中存储了一个像素阵列,其中包含所有像素并具有SDL_Surface.当数组中的像素发生变化时,它也会在Surface中发生变化,然后在完成所有操作后,会对屏幕进行blitting.我目前的做法太慢了,我对如何提高速度做了一些思考.

我目前的想法是:

>使用OpenGL直接在GPU上进行像素操作,一些论坛告诉我这比我目前的方法要慢“这不是GPU的工作方式”
>不要存储像素阵列,直接在RAM中存储BMP,操作它然后将其移动到SDL_Surface或SDL_Texture

关于如何快速操作像素,还有其他方法吗?

解决方法

SDL_CreateTexture()w / SDL_TEXTUREACCESS_STREAMING SDL_UpdateTexture()似乎与正确的像素格式一起工作得很好.

在我的系统上使用默认渲染器:

Renderer name: direct3d
Texture formats:
SDL_PIXELFORMAT_ARGB8888
SDL_PIXELFORMAT_YV12
SDL_PIXELFORMAT_IYUV

(虽然opengl信息是相同的:)

Renderer name: opengl
Texture formats:
SDL_PIXELFORMAT_ARGB8888
SDL_PIXELFORMAT_YV12
SDL_PIXELFORMAT_IYUV

SDL_PIXELFORMAT_ARGB8888给我~1ms /帧:

#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#include <iostream>
#include <vector>

using namespace std;

int main( int argc,char** argv )
{
    SDL_Init( SDL_INIT_EVERYTHING );
    atexit( SDL_Quit );

    SDL_Window* window = SDL_CreateWindow
        (
        "SDL2",SDL_WINDOWPOS_UNDEFINED,600,SDL_WINDOW_SHOWN
        );

    SDL_Renderer* renderer = SDL_CreateRenderer
        (
        window,-1,SDL_RENDERER_ACCELERATED
        );

    SDL_RendererInfo info;
    SDL_GetRendererInfo( renderer,&info );
    cout << "Renderer name: " << info.name << endl;
    cout << "Texture formats: " << endl;
    for( Uint32 i = 0; i < info.num_texture_formats; i++ )
    {
        cout << SDL_GetPixelFormatName( info.texture_formats[i] ) << endl;
    }

    const unsigned int texWidth = 1024;
    const unsigned int texHeight = 1024;
    SDL_Texture* texture = SDL_CreateTexture
        (
        renderer,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,texWidth,texHeight
        );

    vector< unsigned char > pixels( texWidth * texHeight * 4,0 );

    SDL_Event event;
    bool running = true;
    while( running )
    {
        const Uint64 start = SDL_GetPerformanceCounter();

        SDL_SetRenderDrawColor( renderer,SDL_ALPHA_OPAQUE );
        SDL_RenderClear( renderer );

        while( SDL_PollEvent( &event ) )
        {
            if( ( SDL_QUIT == event.type ) ||
                ( SDL_KEYDOWN == event.type && SDL_SCANCODE_ESCAPE == event.key.keysym.scancode ) )
            {
                running = false;
                break;
            }
        }

        // splat down some random pixels
        for( unsigned int i = 0; i < 1000; i++ )
        {
            const unsigned int x = rand() % texWidth;
            const unsigned int y = rand() % texHeight;

            const unsigned int offset = ( texWidth * 4 * y ) + x * 4;
            pixels[ offset + 0 ] = rand() % 256;        // b
            pixels[ offset + 1 ] = rand() % 256;        // g
            pixels[ offset + 2 ] = rand() % 256;        // r
            pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE;    // a
        }

        //unsigned char* lockedPixels;
        //int pitch;
        //SDL_LockTexture
        //    (
        //    texture,//    NULL,//    reinterpret_cast< void** >( &lockedPixels ),//    &pitch
        //    );
        //std::copy( pixels.begin(),pixels.end(),lockedPixels );
        //SDL_UnlockTexture( texture );

        SDL_UpdateTexture
            (
            texture,NULL,&pixels[0],texWidth * 4
            );

        SDL_RenderCopy( renderer,texture,NULL );
        SDL_RenderPresent( renderer );

        const Uint64 end = SDL_GetPerformanceCounter();
        const static Uint64 freq = SDL_GetPerformanceFrequency();
        const double seconds = ( end - start ) / static_cast< double >( freq );
        cout << "Frame time: " << seconds * 1000.0 << "ms" << endl;
    }

    SDL_DestroyRenderer( renderer );
    SDL_DestroyWindow( window );
    SDL_Quit();
}

确保您没有启用vsync(在驱动程序中强制执行,运行合成器等),否则所有帧时间将为~16ms(或者您的显示刷新设置为).

原文链接:https://www.f2er.com/c/118328.html

猜你在找的C&C++相关文章