当我已经渲染纹理时,我不知道如何填充矩形。当我使用SDL_RenderClear(renderer);
时
矩形消失,但当我使用SDL_UpdateWindowSurface(window);
纹理消失时。
我尝试使用SDL_CreateRGBSurface
方法将矩形渲染为纹理,但这也不起作用。输出只是一个黑色全屏窗口,其中呈现图像。按箭头键可以移动图像。
这是我的代码:
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <stdio.h>
SDL_Rect sr;
SDL_Rect dr;
SDL_Rect dr2;
SDL_Renderer * renderer;
SDL_Texture * texture;
int main(int argc, char* argv[])
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_JPG);
TTF_Init();
sr.x = sr.y = sr.h = sr.w = 40;
dr.x = 20;
dr.y = 20;
dr.h = 100;
dr.w = 100;
dr2.x = 80;
dr2.y = 80;
dr2.h = 100;
dr2.w = 100;
SDL_Window *window = SDL_CreateWindow("Game window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 1000, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_Surface *screen = SDL_GetWindowSurface(window);
SDL_UpdateWindowSurface(window);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
Uint32 color2 = SDL_MapRGB(screen->format, 255, 0, 0);
SDL_UpdateWindowSurface(window);
SDL_Surface * image= IMG_Load("p.jpg");
if (image== NULL){ printf(SDL_GetError()); }
texture = SDL_CreateTextureFromSurface(renderer,
message);
SDL_Surface *cr = SDL_CreateRGBSurface(NULL, 1000, 1000, 8, 255, 0, 0, 255);
SDL_Texture *texture2 = SDL_CreateTextureFromSurface(renderer, cr);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture2, NULL, &dr2);
SDL_RenderCopy(renderer, texture, NULL, &dr);
SDL_RenderPresent(renderer);
while (!quit)
{
while (SDL_PollEvent(&event)!=0){
if (event.type == SDL_QUIT){ quit = true; }
else if (event.type==SDL_KEYDOWN)
{
switch (event.key.keysym.sym){
if (event.key.keysym.sym == SDLK_LEFT && event.key.keysym.sym == SDLK_UP){
dr.x -= 30;
dr.y -= 30;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dr2);
SDL_RenderCopy(renderer, texture2, NULL, &dr);
SDL_RenderPresent(renderer); }
case SDLK_LEFT:
if (dr.x <= (-20)){ dr.x = ((screen->w) + 20); }
dr.x -=30;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dr2);
SDL_RenderCopy(renderer, texture2, NULL, &dr);
SDL_RenderPresent(renderer);
break;
case SDLK_UP:
if (dr.y <= 0){ dr.y = screen->h; }
dr.y -= 30;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dr2);
SDL_RenderCopy(renderer, texture2, NULL, &dr2);
SDL_RenderPresent(renderer);
break;
case SDLK_RIGHT:
if (dr.x >= (screen->w)){ dr.x = -1 * 20; }
dr.x += 30;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dr2);
SDL_RenderCopy(renderer, texture2, NULL, &dr);
SDL_RenderPresent(renderer);
break;
case SDLK_DOWN:
if (dr.y >= (screen->h)){ dr.y = -20; }
dr.y += 30;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dr2);
SDL_RenderCopy(renderer, texture2, NULL, &dr);
SDL_RenderPresent(renderer);
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
}
}
SDL_DestroyTexture(texture);
SDL_DestroyTexture(texture2);
SDL_FreeSurface(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
答案 0 :(得分:1)
请勿将屏幕表面仿真与新的SDL_Render API混合使用。也就是说,根本不要使用SDL_GetWindowSurface
。
另外,每次画画时都不要清除渲染器。在绘制所有东西之前,每帧只清除一次。将SDL_RenderClear
,SDL_RenderCopy
和SDL_RenderPresent
移出您的重复块,移出事件循环。
答案 1 :(得分:0)
在屏幕上绘制一个简单的矩形:
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Rect box;
//define box attributes
box.w = 10;
box.h = 10;
box.x = 0;
box.y = 0;
window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 250, 250, NULL);
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderClear(renderer);
//outline rect
SDL_SetRenderDrawColor(renderer, r , g , b, a);
SDL_RenderDrawRect(renderer, &box);
SDL_SetRenderDrawColor(renderer, r , g , b, a);
//fill up rectangle with color
SDL_RenderFillRect(renderer, &box);