Atrinik Client 2.5
client/video.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (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.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00035 int video_get_bpp()
00036 {
00037     return SDL_GetVideoInfo()->vfmt->BitsPerPixel;
00038 }
00039 
00043 int video_set_size()
00044 {
00045     SDL_Surface *new;
00046 
00047     /* Try to set the video mode. */
00048     new = SDL_SetVideoMode(setting_get_int(OPT_CAT_CLIENT, OPT_RESOLUTION_X), setting_get_int(OPT_CAT_CLIENT, OPT_RESOLUTION_Y), video_get_bpp(), get_video_flags());
00049 
00050     if (new)
00051     {
00052         ScreenSurface = new;
00053         return 1;
00054     }
00055 
00056     return 0;
00057 }
00058 
00063 uint32 get_video_flags()
00064 {
00065     if (setting_get_int(OPT_CAT_CLIENT, OPT_FULLSCREEN))
00066     {
00067         return SDL_FULLSCREEN | SDL_SWSURFACE | SDL_HWACCEL | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_ANYFORMAT;
00068     }
00069     else
00070     {
00071         return SDL_SWSURFACE | SDL_SWSURFACE | SDL_HWACCEL | SDL_HWPALETTE | SDL_ANYFORMAT | SDL_RESIZABLE;
00072     }
00073 }
00074 
00089 int video_fullscreen_toggle(SDL_Surface **surface, uint32 *flags)
00090 {
00091     long framesize = 0;
00092     void *pixels = NULL;
00093     SDL_Rect clip;
00094     uint32 tmpflags = 0;
00095     int w = 0;
00096     int h = 0;
00097     int bpp = 0;
00098     int grabmouse, showmouse;
00099 
00100     grabmouse = SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON;
00101     showmouse = SDL_ShowCursor(-1);
00102 
00103     if ((!surface) || (!(*surface)))
00104     {
00105         return 0;
00106     }
00107 
00108     if (SDL_WM_ToggleFullScreen(*surface))
00109     {
00110         if (flags)
00111         {
00112             *flags ^= SDL_FULLSCREEN;
00113         }
00114 
00115         return 1;
00116     }
00117 
00118     if (!(SDL_GetVideoInfo()->wm_available))
00119     {
00120         return 0;
00121     }
00122 
00123     tmpflags = (*surface)->flags;
00124     w = (*surface)->w;
00125     h = (*surface)->h;
00126     bpp = (*surface)->format->BitsPerPixel;
00127 
00128     if (flags == NULL)
00129     {
00130         flags = &tmpflags;
00131     }
00132 
00133     if ((*surface = SDL_SetVideoMode(w, h, bpp, *flags)) == NULL)
00134     {
00135         *surface = SDL_SetVideoMode(w, h, bpp, tmpflags);
00136     }
00137     else
00138     {
00139         return 1;
00140     }
00141 
00142     if (flags == NULL)
00143     {
00144         flags = &tmpflags;
00145     }
00146 
00147     SDL_GetClipRect(*surface, &clip);
00148 
00149     /* Save the contents of the screen. */
00150     if ((!(tmpflags & SDL_OPENGL)) && (!(tmpflags & SDL_OPENGLBLIT)))
00151     {
00152         framesize = (w * h) * ((*surface)->format->BytesPerPixel);
00153         pixels = malloc(framesize);
00154 
00155         if (pixels == NULL)
00156         {
00157             return 0;
00158         }
00159 
00160         memcpy(pixels, (*surface)->pixels, framesize);
00161     }
00162 
00163     if (grabmouse)
00164     {
00165         SDL_WM_GrabInput(SDL_GRAB_OFF);
00166     }
00167 
00168     SDL_ShowCursor(1);
00169 
00170     *surface = SDL_SetVideoMode(w, h, bpp, (*flags) ^ SDL_FULLSCREEN);
00171 
00172     if (*surface != NULL)
00173     {
00174         *flags ^= SDL_FULLSCREEN;
00175     }
00176     else
00177     {
00178         *surface = SDL_SetVideoMode(w, h, bpp, tmpflags);
00179 
00180         if (*surface == NULL)
00181         {
00182             if (pixels)
00183             {
00184                 free(pixels);
00185             }
00186 
00187             return 0;
00188         }
00189     }
00190 
00191     /* Unfortunately, you lose your OpenGL image until the next frame... */
00192     if (pixels)
00193     {
00194         memcpy((*surface)->pixels, pixels, framesize);
00195         free(pixels);
00196     }
00197 
00198     SDL_SetClipRect(*surface, &clip);
00199 
00200     if (grabmouse)
00201     {
00202         SDL_WM_GrabInput(SDL_GRAB_ON);
00203     }
00204 
00205     SDL_ShowCursor(showmouse);
00206 
00207     return 1;
00208 }