sdl2从Linux与Windows上的不同线程绘制

时间:2016-09-22 14:21:48

标签: c sdl-2

我有一些非常简单的SDL2绘图,它在Windows下完美运行,但现在我将它移植到Linux上,并不是从我生成的不同线程中提取的。

我正在使用互斥锁控制对渲染器的访问。

函数按预期处理和输出日志记录,唯一看起来不起作用的是渲染器永远不会更新Linux下的显示。

如果我注释掉线程,并从main运行函数,我得到预期的结果。 Linux和Windows版本之间的代码没有变化。

代码在-wall和-pedantic下编译时没有警告。自从我开始使用Linux进行编译以来,我已经添加了-pthread标志,以防需要(没有任何区别)。

如果有人知道任何陷阱,或者可能会暗示为什么这不起作用,那么你会给我一个巨大的帮助。

        static int thread_processing(void* data) 
    {
        srand(time(NULL));      //randomize 

        ThreadData *td = data;  
        Bucket *bucket; 
        int b_Id = -1;

        Color threadColor;
        //unique color for each respective thread
        color_randomize(&threadColor);
        threadColor.a = 255;

        fprintf(stderr, "%d Buckets to process..\n", td->bArray->size);
        while (1) {
            //check there are buckets left to process
            if (td->bArray->rendered >= td->bArray->size)
                break;

            //acquie/lock access to bucket array
            SDL_LockMutex(td->b_mutex);

            //retrieve buucket id to process
            b_Id = td->bArray->rendered;
            td->bArray->rendered++;

            fprintf(stderr, "Rendering bucket: %d\n", b_Id);
            //release access to bucket array
            SDL_UnlockMutex(td->b_mutex);

            //retrieve addr of bucket to process
            bucket = &(td->bArray->buckets[b_Id]);

            //lock access to renderer
            SDL_LockMutex(td->r_mutex);

            //draw rect on screen where bucket will be processed
            draw_bucketOutline(td->renderer, bucket, &threadColor);
            SDL_RenderPresent(td->renderer);

            //release access to renderer object
            SDL_UnlockMutex(td->r_mutex);

            //process the bucket
            process_bucket(td->scene, bucket);

            //acquire/lock acess to renderer object
            SDL_LockMutex(td->r_mutex);

            //draw the processed data ot the screen
            draw_bucket(td->renderer, bucket);
            SDL_RenderPresent(td->renderer);

            //release access to renderer object
            SDL_UnlockMutex(td->r_mutex);
        }

        return 0;
    }

    void draw_bucketOutline(SDL_Renderer *renderer, Bucket *b, Color *color)
{
    //set the colour of the outline
    SDL_SetRenderDrawColor(renderer, 125, 125, 125, 255);

    //set the outline position
    SDL_Rect rect;
    rect.w = b->resolution.width;
    rect.h = b->resolution.height;
    rect.x = b->start.x;
    rect.y = b->start.y;

    //draw the outline
    SDL_RenderDrawRect(renderer, &rect);

    //crop rectangle inwards for filling inside of outline
    rect.w -= 2;
    rect.h -= 2;
    rect.x += 1;
    rect.y += 1;

    //set colour for fill area
    SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);

    //draw fill area
    SDL_RenderFillRect(renderer, &rect);
}

主.....

//iterate over threads, do the processing
int t;
for (t = 0; t < THREAD_COUNT; t++) {
    threads[t] = SDL_CreateThread(thread_processing, NULL, &td);
}

//iterate over threads, clean them up
for (t = 0; t < THREAD_COUNT; t++) {
    int status;
    SDL_WaitThread(threads[t], &status);
}

使用

进行编译
gcc -Wall -pedantic -lm -I/usr/include/SDL2 -D_REENTRANT -lX11 -pthread raytracer.c -lSDL2 -o raytracer

1 个答案:

答案 0 :(得分:0)

Linux上的SDL2使用OpenGL加速渲染(默认为Windows上的d3d加速),OpenGL具有线程局部上下文。您不能在单独的线程中使用相同的GL上下文。你必须在单线程中渲染,例如将渲染命令放入队列中,然后在渲染线程中重放它们。而你首先ActiveRecord::populateRelation()