Hey Folks…
Trying to get started with SDL on iOS.
First test, modified rectangles.c to draw “concentric rectangles” on tap / click.
Is there some “double buffer” setting I don’t see?
Here’s what I get, drawing green, blue, cyan, magenta, yellow rectangles (I left out red, as will be obvious shortly):
It looks like I end up with a black-cleared buffer and a red-cleared buffer, alternating with each call to SDL_RenderPresent(renderer);
?
I also have an old copy of SDL — looks like the last commit was Oct 10, 2018 – which, with the same code, gives me what I expected:
Same result on Simulator and Device.
Here’s the source code (based on the rectangles.c demo):
/*
*
* firstTest.c
*
* based on rectangles.c
* written by Holmes Futrell
* use however you want
*/
#include "SDL.h"
void
fatalError(const char *string)
{
printf("%s: %s\n", string, SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, string, SDL_GetError(), NULL);
exit(1);
}
void
myDrawRect(SDL_Renderer *renderer, int x, int y, int *c)
{
int renderW;
int renderH;
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
// centered rect
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = renderW - x * 2;
rect.h = renderH - y * 2;
/* Fill the rectangle in color */
SDL_SetRenderDrawColor(renderer, c[0], c[1], c[2], 255);
SDL_RenderFillRect(renderer, &rect);
/* update screen */
SDL_RenderPresent(renderer);
}
int
main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event event;
int windowW;
int windowH;
int done;
// 5 colors
int colors[5][3] = {
{ 0, 255, 0}, // green
{ 0, 0, 255}, // blue
{ 0, 255, 255}, // cyan
{255, 0, 255}, // magenta
{255, 255, 0} // yellow
};
int c = 0;
int x = 20, y = 20;
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fatalError("Could not initialize SDL");
}
/* create window and renderer */
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
if (window == 0) {
fatalError("Could not initialize Window");
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
fatalError("Could not create renderer");
}
SDL_GetWindowSize(window, &windowW, &windowH);
SDL_RenderSetLogicalSize(renderer, windowW, windowH);
/* Fill screen with black */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// draw first rect
myDrawRect(renderer, x, y, colors[c % 5]);
x += 20;
y += 20;
c += 1;
done = 0;
while (!done) {
while (SDL_PollEvent(&event) == 1) {
switch (event.type) {
case SDL_QUIT:
done = 1;
break;
case SDL_MOUSEBUTTONDOWN:
myDrawRect(renderer, x, y, colors[c % 5]);
x += 20;
y += 20;
c += 1;
break;
}
}
}
/* shutdown SDL */
SDL_Quit();
return 0;
}