SDL分层渲染系统

时间:2018-07-01 07:57:33

标签: sdl sdl-2

我希望在SDL2中构建分层的渲染系统。

我有第一层,其中包含巴黎及其道路的地图。 我需要在此地图上的两个点之间画一条线-当此线的前状态没有消失时,就会出现问题。

我需要在地图上画出这条线,并将其保留。

我如何使系统在没有绘制任何重叠线的情况下保存地图状态,这样一帧又一帧地显示重叠了该线的新状态的地图呢?

1 个答案:

答案 0 :(得分:0)

解决了,下面是一个例子!谢谢大家

SDL_Window *window; 
SDL_Renderer *render;
SDL_Texture *map; //map texture (my layer)

window = SDL_CreateWindow("Test window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
map = SDL_CreateTexture(render,  SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480); //Creating a texture

/*Map is a red background stored in map texture*/
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_SetRenderTarget(render, map);
SDL_RenderClear(render);
SDL_SetRenderTarget(render, NULL);

/*Seting the line color*/
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);

/*Coping the map texture to the render and drawing a green line on top of this*/
SDL_RenderCopy(render, map, NULL, NULL);
SDL_RenderDrawLine(render, 0, 0, 640, 480);
SDL_RenderPresent(render);
SDL_Delay(2000);

/*Another line*/
SDL_RenderCopy(render, map, NULL, NULL);
SDL_RenderDrawLine(render, 0, 480, 640, 0);
SDL_RenderPresent(render);
SDL_Delay(2000);

SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();