Discussion:
[SDL] Could someone with a retina Mac please try this code?
oviano
2017-01-04 12:33:22 UTC
Permalink
It should draw a small black rectangle towards the top left of the screen, on a background that alternates between red, green and blue.

The problem is it (and things like drawing textures) only works for me if SDL_WINDOW_ALLOW_HIGHDPI is left commented out during window creation.

I'm trying to establish if it's a peculiarity with my machine or a bug in SDL.

I've tried debugging it a little but none of the lower level GL_XXX functions are failing, no errors are produced, yet the SDL_RenderFillRect doesn't draw anything?


Code:

#include <SDL2/SDL.h>

static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;

static void clean_up(void);

int main(int argc, char** argv)
{
start:
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Error initialising SDL library (%s)\n", SDL_GetError());
clean_up();
return 1;
}

SDL_DisplayMode desktop_dm;
if (SDL_GetDesktopDisplayMode(0, &desktop_dm) < 0)
{
printf("Error getting desktop display mode (%s)\n", SDL_GetError());
clean_up();
return 1;
}

if (!(window = SDL_CreateWindow("Test", 0, 0, desktop_dm.w, desktop_dm.h,
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL)))
{
printf("Error creating window (%s)\n", SDL_GetError());
clean_up();
return 1;
}

if (!(renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC)))
{
printf("Error creating renderer (%s)\n", SDL_GetError());
clean_up();
return 1;
}

SDL_RendererInfo info;
if (SDL_GetRendererInfo(renderer, &info) < 0)
{
printf("Error getting renderer info (%s)\n", SDL_GetError());
clean_up();
return 1;
}

int32_t render_w, render_h;
if (SDL_GetRendererOutputSize(renderer, &render_w, &render_h) < 0)
{
printf("Error getting rendering size (%s)\n", SDL_GetError());
clean_up();
return 1;
}

printf("Renderer is %s, size %d x %d\n", info.name, render_w, render_h);

int32_t render_time = 1000 / desktop_dm.refresh_rate;

uint32_t update_time = 0;
uint32_t update_index = 0;

bool stop = false;

while (!stop)
{
uint32_t start_time = SDL_GetTicks();

SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
stop = true;
break;
}
}

if (SDL_GetTicks() - update_time > 1000)
{
SDL_SetRenderTarget(renderer, NULL);
SDL_SetRenderDrawColor(renderer,
update_index % 3 == 0 ? 255 : 0,
update_index % 3 == 1 ? 255 : 0,
update_index % 3 == 2 ? 255 : 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_Rect rect;
rect.x = 100;
rect.y = 100;
rect.w = 100;
rect.h = 100;
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);

update_time = SDL_GetTicks();
update_index ++;
}

uint32_t elapsed_time = SDL_GetTicks() - start_time;;
if (elapsed_time < render_time)
{
SDL_Delay(render_time - elapsed_time);
}
}

clean_up();

return 0;
}

void clean_up(void)
{
if (renderer)
{
SDL_DestroyRenderer(renderer);
renderer = NULL;
}

if (window)
{
SDL_DestroyWindow(window);
window = NULL;
}

SDL_Quit();
}

Loading...