diff options
Diffstat (limited to 'sdl/platform.c')
-rw-r--r-- | sdl/platform.c | 104 |
1 files changed, 78 insertions, 26 deletions
diff --git a/sdl/platform.c b/sdl/platform.c index 69235bd..270a938 100644 --- a/sdl/platform.c +++ b/sdl/platform.c @@ -22,9 +22,10 @@ GLfloat sourceLight[] = { 0.75f, 0.75f, 0.75f, 1.0f }; GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f }; /* variables that should be already defined and declared for us by main program */ -extern const unsigned int xres; -extern const unsigned int yres; -extern const unsigned int bpp; +extern const unsigned int xres_w; +extern const unsigned int yres_w; +extern const unsigned int bpp_w; +extern const unsigned int sdl_video_flags; extern const char *window_caption; extern const char *window_icon_path; @@ -94,40 +95,105 @@ void setup_opengl(void) glClearColor(0.5f, 0.5f, 0.5f, 1.0f); } +SDL_Surface *setup_sdl_video(int w, int h, int bpp, unsigned int flags) +{ + SDL_Surface *srfc; + + if (!bpp) + { + const SDL_VideoInfo* info = NULL; + + /* get some video information. */ + info = SDL_GetVideoInfo(); + if (!info) + { + fprintf( stderr, "SDL: video query failed: %s\n", SDL_GetError()); + exit(-1); + } + + bpp = info->vfmt->BitsPerPixel; + printf("SDL: bpp was not specified, chose %d bits\n", bpp); + } + + if (!flags) + flags = SDL_OPENGL | SDL_RESIZABLE; + + if ((srfc = SDL_SetVideoMode(w, h, bpp, flags)) == NULL) + { + fprintf(stderr, "SDL: unable to set video mode: %s\n", SDL_GetError()); + exit(-1); + } + + return srfc; +} + void setup_sdl(void) { SDL_Surface *screen; + const SDL_VideoInfo* info = NULL; - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1) { - fprintf(stderr, "unable to init SDL: %s\n", SDL_GetError()); + fprintf(stderr, "SDL: unable to init, %s\n", SDL_GetError()); exit(-1); } atexit(SDL_Quit); - SDL_WM_SetCaption(window_caption, NULL); - SDL_WM_SetIcon(IMG_Load(window_icon_path), NULL); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + + SDL_WM_SetCaption(window_caption, NULL); + SDL_WM_SetIcon(IMG_Load(window_icon_path), NULL); + + /* set video mode */ + screen = setup_sdl_video(xres_w, yres_w, bpp_w, sdl_video_flags); - if ((screen = SDL_SetVideoMode(xres, yres, bpp, SDL_OPENGL | SDL_RESIZABLE)) == NULL) + /* get some video information. */ + info = SDL_GetVideoInfo(); + if (!info) { - fprintf(stderr, "unable to set video mode: %s\n", SDL_GetError()); + fprintf( stderr, "SDL: video query failed: %s\n", SDL_GetError()); exit(-1); } + int bpp = info->vfmt->BitsPerPixel; + printf("SDL: applied %d bits per pixel\n", bpp); + + /* query opengl attributes after SetVideoMode call */ + int fb_red_comp; + int fb_green_comp; + int fb_blue_comp; + int fb_alpha_comp; + int double_buff; + int depth_sz; + + SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &fb_red_comp); + SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &fb_green_comp); + SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &fb_blue_comp); + SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &fb_alpha_comp); + SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &double_buff); + SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth_sz); + printf("SDL: framebuffer; size of components, red: %d bits, green: %d bits, " + "blue: %d bits, alpha: %d bits\n" + "SDL: double-buffering: %s, depth buffer size: %d bits\n", + fb_red_comp, fb_green_comp, fb_blue_comp, fb_alpha_comp, + double_buff ? "enabled" : "disabled", depth_sz); + SDL_EnableUNICODE(1); + /* SDL doesn't trigger off a ResizeEvent at startup, but as we need this * for OpenGL, we do this ourselves */ SDL_Event resizeEvent; resizeEvent.type = SDL_VIDEORESIZE; - resizeEvent.resize.w = xres; - resizeEvent.resize.h = yres; + resizeEvent.resize.w = xres_w; + resizeEvent.resize.h = yres_w; SDL_PushEvent(&resizeEvent); + + /* save the surf pointer in platform */ } void setup_glew(void) @@ -150,17 +216,3 @@ void setup_glew(void) fprintf(stdout, "GLEW: detected OpenGL %d.%d\n", major, minor); } -inline void fps_control(const unsigned int startclock) -{ - unsigned int deltaclock = SDL_GetTicks() - startclock; - if (deltaclock < 1000 / FRAMES_PER_SECOND) - SDL_Delay((1000 / FRAMES_PER_SECOND) - deltaclock); - -#ifdef STAT_FPS - char buffer[30] = { 0 }; - sprintf(buffer, "%s: %4d fps", window_caption, - 1000 / (SDL_GetTicks() - startclock)); - SDL_WM_SetCaption(buffer, NULL); -#endif -} - |