2009/6/13 Chris Hagan <captain.mister.hagan at gmail.com>
void showWindow(sdl_data *data, int bufferLength, char *buffer)
{
SDL_WindowID mainWnd;
if(SDL_Init(SDL_INIT_VIDEO) < 0){ fprintf(stderr,“Couldn’t initialize
SDL:%s\n”,SDL_GetError());}
if(SDL_VideoInit(NULL,0) < 0){ fprintf(stderr,“Couldn’t initialize
window:%s\n”,SDL_GetError());}
mainWnd = SDL_CreateWindow(“Title”, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
if(mainWnd <= 0){ fprintf(stderr,“Couldn’t create
window:%s\n”,SDL_GetError());}
SDL_CreateRenderer(mainWnd, -1,
SDL_RENDERER_PRESENTFLIP2|SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED);
SDL_SelectRenderer(mainWnd);
SDL_ShowWindow(mainWnd);
int width,height;
SDL_SetRenderDrawColor(85,85,85,85);
SDL_RenderFill(NULL);
SDL_RenderPresent();
}
A few things here…
You don’t need both SDL_Init(SDL_INIT_VIDEO) and SDL_VideoInit(NULL,0).
You can either use SDL_Init(SDL_INIT_VIDEO) and later use SDL_Quit() or use
SDL_Init(0) followed by SDL_VideoInit(NULL,0) and later use SDL_VideoQuit()
followed by SDL_Quit(). Also, remember you really don’t need to call
SDL_Init or SDL_VideoInit more than once in your program, so you might want
to move it into main() instead.
Also, in each of your checks, you should really use != 0, not < 0, since
anything, positive or negative, other than 0 is an error, and in the case of
mainWnd <= 0, it should be mainWnd == 0, since SDL_WindowID is not signed.
You’re right about SDL_WINDOW_SHOW. If you have that in the
SDL_CreateWindow flags then the window should be visible by default, which
means you don’t need SDL_ShowWindow(mainWnd);
width and height are never used. Neither are any of the parameters you pass
in.
If you plan to do anything with this window outside of this function
(drawing to it, closing it, etc), you will probably want to return mainWnd.
What Bob mentioned isn’t quite right. There’s nothing here that would close
the window, but if all your program does is call this function and then
exit, then the user will not see the window. You still need to usual SDL
event loop. One thing worthy of note is that clicking the window’s close
button will not send an SDL_QUIT event as it would with a window created
using SDL_SetVideoMode. You should watch for SDL_WINDOWEVENT instead with
.window.event == SDL_WINDOWEVENT_CLOSE instead.
The last parameter to SDL_SetRenderDrawColor() is the alpha value, or
opacity, which will be used to blend the color with whatever is already
there. In this case, you would want 255 or 0xFF, not 85.
At present, when an error occurs, you print an error message, but keep on
going. You need to add a return statement after each fprintf.
SDL_CreateRenderer will fail if it can’t find a render driver that matches
the flags you pass in. You might want to use fewer flags for
SDL_CreateRenderer() and check the return value to see if you were
successful or not. For example, you could use:
if (SDL_CreateRenderer(mainWnd, -1, SDL_RENDERER_PRESENTFLIP2) != 0) {
fprintf(stderr,“Couldn’t initialize renderer: %s\n”,SDL_GetError());
return 0;
}
If you want an example of how to use rendering contexts to draw to the
screen, check out test/testdraw2.c in trunk/SDL on svn.