0
我需要創建一個窗口,並用一種顏色填充其所有像素。這不應該太難,但一個小時左右後,我仍然無法正常工作。這是我的代碼,因爲它看起來現在:SDL2像素操作
#include <SDL2/SDL.h>
int main(int argc, char** argv)
{
enum {
WIDTH = 640,
HEIGHT = 480
};
SDL_Window* window = SDL_CreateWindow(
"SDL test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH,
HEIGHT,
SDL_WINDOW_SHOWN
);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture* texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
WIDTH,
HEIGHT
);
SDL_Surface* surface = SDL_CreateRGBSurface(
0,
WIDTH,
HEIGHT,
32,
0x00000000,
0x00000000,
0x00000000,
0xff000000
);
Uint32 gray = SDL_MapRGBA(surface->format, 0x1E, 0x1E, 0x1E, 0xFF);
Uint32 red = SDL_MapRGBA(surface->format, 0xFA, 0x32, 0x32, 0xFF);
int i, j;
Uint32* pixels32 = (Uint32*) surface->pixels;
for (i = 0; i < HEIGHT; ++i)
{
for (j = 0; j < WIDTH; ++j)
{
pixels32[i * WIDTH + j] = j%10 ? gray : red;
}
}
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
SDL_RenderPresent(renderer);
int quit = 0;
SDL_Event event;
while (!quit)
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
quit = 1;
}
}
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
但它顯示一個白色的窗口。我究竟做錯了什麼?
當您處於程序的關閉階段時,爲什麼要「SDL_RenderPresent」? – 2015-10-15 20:42:50
@Rhymoid哦,這是一個錯誤,當我想移動'SDL_FreeSurface'函數時,我也移動了它。現在我看到一個黑色的窗戶,太棒了。 – klenium
至少它是不同的顏色。現在,您是否嘗試過使用['SDL_RenderCopy'](https://wiki.libsdl.org/SDL_RenderCopy)(或其-Ex變體)實際上將紋理圖案貼到表面上? – 2015-10-15 22:52:10