Fixed rendering in the iOS touch demo

This commit is contained in:
Sam Lantinga 2021-11-09 13:36:44 -08:00
parent 7681929cb4
commit a7e2e3a329
1 changed files with 23 additions and 13 deletions

View File

@ -81,6 +81,7 @@ main(int argc, char *argv[])
SDL_Event event; SDL_Event event;
SDL_Window *window; /* main window */ SDL_Window *window; /* main window */
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *target;
int done; /* does user want to quit? */ int done; /* does user want to quit? */
int w, h; int w, h;
@ -100,12 +101,15 @@ main(int argc, char *argv[])
initializeTexture(renderer); initializeTexture(renderer);
/* fill canvass initially with all black */ /* fill canvass initially with all black */
target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, w, h);
SDL_SetRenderTarget(renderer, target);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderPresent(renderer); SDL_SetRenderTarget(renderer, NULL);
done = 0; done = 0;
while (!done && SDL_WaitEvent(&event)) { while (!done) {
while (SDL_PollEvent(&event) == 1) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
done = 1; done = 1;
@ -114,15 +118,21 @@ main(int argc, char *argv[])
state = SDL_GetMouseState(&x, &y); /* get its location */ state = SDL_GetMouseState(&x, &y); /* get its location */
SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */ SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */
if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */ if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */
SDL_SetRenderTarget(renderer, target);
drawLine(renderer, x - dx, y - dy, dx, dy); /* draw line segment */ drawLine(renderer, x - dx, y - dy, dx, dy); /* draw line segment */
SDL_RenderPresent(renderer); SDL_SetRenderTarget(renderer, NULL);
} }
break; break;
} }
} }
SDL_RenderCopy(renderer, target, NULL, NULL);
SDL_RenderPresent(renderer);
}
/* cleanup */ /* cleanup */
SDL_DestroyTexture(brush); SDL_DestroyTexture(brush);
SDL_DestroyTexture(target);
SDL_Quit(); SDL_Quit();
return 0; return 0;