So I've been playing around with SDL2 to see if it would suit my needs. I was following introductory examples (from lazyfoo mostly) and made a simple test.
#include <stdio.h> #include <SDL.h> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int main(int argc, char const *argv[]) { /* The window handle */ SDL_Window* window = NULL; /* The surface contained in the window */ SDL_Surface* screen_surface = NULL; /* Init SDL */ if(SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); return 1; } else { /* Create window */ window = SDL_CreateWindow( "SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS ); if(window == NULL) { printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); return 1; } else { // /* Get window surface */ screen_surface = SDL_GetWindowSurface(window); // /* Fill the surface white */ SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x50, 0x50, 0x50)); // /* Update the surface */ SDL_UpdateWindowSurface(window); printf("%s\n", SDL_GetError()); /* Wait n secs */ SDL_Delay(2000); } } /* Destroy the window */ SDL_DestroyWindow(window); /* Quit SDL subsystem */ SDL_Quit(); return 0; }
Compiled on OSX 10.7.5 (MBA 2011) with:
gcc `sdl2-config --cflags --libs` -o sdl_test sdl_test.c
Example is simple enough. It should init SDL, create a window, attach a surface to the window, set the color to gray and render it out - after two seconds it should exit.
However, window stays black. No gray surface shown. That's why I introduced that printf for SDL errors before delay. It shows Invalid renderer
which I have tracked down to be triggered by screen_surface = SDL_GetWindowSurface(window);
I have also tried to do it the other way with
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_RenderClear(renderer); SDL_RenderPresent(renderer);
instead of SDL_Surface way.. but same thing happens Invalid renderer
thrown out on SDL_CreateRenderer. As you can see, I tried it with _SOFTWARE and _ACCELERATED flags - same thing.
Now, here's the 'funny' part. In first code, with SDL_Surface way I have called SDL_UpdateWindowSurface(window); twice - one after the other. Gray color shows! However, Invalid renderer
is still thrown out, but code works. Why would calling SDL_UpdateWindowSurface(window);
twice, one after the other, work and once it wouldn't? Why would it work at all if Invalid renderer
is thrown out? I'm deeply puzzled.
- OSX: 10.7.5
- compiler: i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
- SDL2: 2.0.3 (installed via homebrew)
I also tried invoking gcc without sdl2-config by manually typing include path and libs.