r_sdl.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 1997-2001 Id Software, Inc.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "r_local.h"
00027 #include "r_main.h"
00028 #include "r_sdl.h"
00029 #include "../../ports/system.h"
00030 
00031 r_sdl_config_t r_sdl_config;
00032 
00033 #ifndef _WIN32
00034 static void R_SetSDLIcon (void)
00035 {
00036 #include "../../ports/linux/ufoicon.xbm"
00037     SDL_Surface *icon;
00038     SDL_Color color;
00039     Uint8 *ptr;
00040     unsigned int i, mask;
00041 
00042     icon = SDL_CreateRGBSurface(SDL_SWSURFACE, ufoicon_width, ufoicon_height, 8, 0, 0, 0, 0);
00043     if (icon == NULL)
00044         return;
00045     SDL_SetColorKey(icon, SDL_SRCCOLORKEY, 0);
00046 
00047     color.r = color.g = color.b = 255;
00048     SDL_SetColors(icon, &color, 0, 1); /* just in case */
00049     color.r = color.b = 0;
00050     color.g = 16;
00051     SDL_SetColors(icon, &color, 1, 1);
00052 
00053     ptr = (Uint8 *)icon->pixels;
00054     for (i = 0; i < sizeof(ufoicon_bits); i++) {
00055         for (mask = 1; mask != 0x100; mask <<= 1) {
00056             *ptr = (ufoicon_bits[i] & mask) ? 1 : 0;
00057             ptr++;
00058         }
00059     }
00060 
00061     SDL_WM_SetIcon(icon, NULL);
00062     SDL_FreeSurface(icon);
00063 }
00064 #endif
00065 
00066 qboolean Rimp_Init (void)
00067 {
00068     SDL_version version;
00069     int attrValue;
00070     const SDL_VideoInfo* info;
00071     char videoDriverName[MAX_VAR] = "";
00072 
00073     Com_Printf("\n------- video initialization -------\n");
00074 
00075     if (r_driver->string[0] != '\0') {
00076         Com_Printf("using driver: %s\n", r_driver->string);
00077         SDL_GL_LoadLibrary(r_driver->string);
00078     }
00079 
00080     Sys_Setenv("SDL_VIDEO_CENTERED", "1");
00081     Sys_Setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "0");
00082 
00083     if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
00084         if (SDL_Init(SDL_INIT_VIDEO) < 0)
00085             Com_Error(ERR_FATAL, "Video SDL_Init failed: %s", SDL_GetError());
00086     }
00087 
00088     SDL_VERSION(&version)
00089     Com_Printf("SDL version: %i.%i.%i\n", version.major, version.minor, version.patch);
00090 
00091     info = SDL_GetVideoInfo();
00092     if (info) {
00093         Com_Printf("I: desktop depth: %ibpp\n", info->vfmt->BitsPerPixel);
00094         r_config.videoMemory = info->video_mem;
00095         Com_Printf("I: video memory: %i\n", r_config.videoMemory);
00096         memcpy(&r_sdl_config.pixelFormat, info->vfmt, sizeof(r_sdl_config.pixelFormat));
00097         memcpy(&r_sdl_config.videoInfo, info, sizeof(r_sdl_config.videoInfo));
00098         r_sdl_config.videoInfo.vfmt = &r_sdl_config.pixelFormat;
00099         r_sdl_config.modes = SDL_ListModes(r_sdl_config.videoInfo.vfmt, SDL_OPENGL | SDL_FULLSCREEN);
00100         if (r_sdl_config.modes) {
00101             char buf[4096] = "";
00102             Q_strcat(buf, "I: Available resolutions:", sizeof(buf));
00103             for (r_sdl_config.numModes = 0; r_sdl_config.modes[r_sdl_config.numModes]; r_sdl_config.numModes++) {
00104                 const char *modeStr = va(" %ix%i",
00105                         r_sdl_config.modes[r_sdl_config.numModes]->w,
00106                         r_sdl_config.modes[r_sdl_config.numModes]->h);
00107                 Q_strcat(buf, modeStr, sizeof(buf));
00108             }
00109             Com_Printf("%s (%i)\n", buf, r_sdl_config.numModes);
00110         } else {
00111             Com_Printf("I: Could not get list of available resolutions\n");
00112         }
00113     } else {
00114         r_config.videoMemory = 0;
00115     }
00116     SDL_VideoDriverName(videoDriverName, sizeof(videoDriverName));
00117     Com_Printf("I: video driver: %s\n", videoDriverName);
00118 
00119     if (!R_SetMode())
00120         Com_Error(ERR_FATAL, "Video subsystem failed to initialize");
00121 
00122     SDL_WM_SetCaption(GAME_TITLE, GAME_TITLE_LONG);
00123 
00124 #ifndef _WIN32
00125     R_SetSDLIcon(); /* currently uses ufoicon.xbm data */
00126 #endif
00127 
00128     if (!SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &attrValue))
00129         Com_Printf("I: got %d bits of stencil\n", attrValue);
00130     if (!SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &attrValue))
00131         Com_Printf("I: got %d bits of depth buffer\n", attrValue);
00132     if (!SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &attrValue))
00133         Com_Printf("I: got double buffer\n");
00134     if (!SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &attrValue))
00135         Com_Printf("I: got %d bits for red\n", attrValue);
00136     if (!SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &attrValue))
00137         Com_Printf("I: got %d bits for green\n", attrValue);
00138     if (!SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &attrValue))
00139         Com_Printf("I: got %d bits for blue\n", attrValue);
00140     if (!SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &attrValue))
00141         Com_Printf("I: got %d bits for alpha\n", attrValue);
00142     if (!SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &attrValue))
00143         Com_Printf("I: got %d multisample buffers\n", attrValue);
00144 
00145     /* we need this in the renderer because if we issue an vid_restart we have
00146      * to set these values again, too */
00147     SDL_EnableUNICODE(SDL_ENABLE);
00148     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
00149 
00150     return qtrue;
00151 }
00152 
00156 qboolean R_InitGraphics (qboolean fullscreen, int width, int height)
00157 {
00158     uint32_t flags;
00159     int i;
00160     SDL_Surface* screen = NULL;
00161 
00162     vid_strech->modified = qfalse;
00163     vid_fullscreen->modified = qfalse;
00164     vid_mode->modified = qfalse;
00165     r_ext_texture_compression->modified = qfalse;
00166 
00167     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
00168     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00169 
00170     /* valid values are between 0 and 4 */
00171     i = min(4, max(0, r_multisample->integer));
00172     if (i > 0) {
00173         Com_Printf("I: set multisample buffers to %i\n", i);
00174         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
00175         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, i);
00176     }
00177 
00178     /* valid values are between 0 and 2 */
00179     i = min(2, max(0, r_swapinterval->integer));
00180     Com_Printf("I: set swap control to %i\n", i);
00181     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, i);
00182 
00183     flags = SDL_OPENGL;
00184     if (fullscreen)
00185         flags |= SDL_FULLSCREEN;
00186     /*flags |= SDL_NOFRAME;*/
00187 
00188     screen = SDL_SetVideoMode(width, height, 0, flags);
00189     if (!screen) {
00190         const char *error = SDL_GetError();
00191         Com_Printf("SDL SetVideoMode failed: %s\n", error);
00192         return qfalse;
00193     }
00194 
00195     SDL_ShowCursor(SDL_DISABLE);
00196 
00197     return qtrue;
00198 }
00199 
00200 void Rimp_Shutdown (void)
00201 {
00202     SDL_ShowCursor(SDL_ENABLE);
00203 
00204     if (SDL_WasInit(SDL_INIT_EVERYTHING) == SDL_INIT_VIDEO)
00205         SDL_Quit();
00206     else
00207         SDL_QuitSubSystem(SDL_INIT_VIDEO);
00208 }

Generated by  doxygen 1.6.2