void Window_InitializeVideo(void) { int iFlags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN; SDL_Surface *sIcon; // [28/7/2013] Moved check here and corrected, seems more secure ~hogsy if (SDL_VideoInit(NULL) < 0) Sys_Error("Failed to initialize video!\n%s\n", SDL_GetError()); SDL_DisableScreenSaver(); // Get display information. if (SDL_GetCurrentDisplayMode(0, &sDisplayMode) != 0) Sys_Error("Failed to get current display information!\n%s\n", SDL_GetError()); if (!Video.fullscreen) iFlags &= ~SDL_WINDOW_FULLSCREEN; #if 0 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); #endif 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_ACCUM_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8); #if 0 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); #endif SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); sMainWindow = SDL_CreateWindow( Game->Name, // [9/7/2013] Window name is based on the name given by Game ~hogsy SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Video.iWidth, Video.iHeight, iFlags); if (!sMainWindow) Sys_Error("Failed to create window!\n%s\n", SDL_GetError()); // Attempt to grab the window icon from the game directory. sIcon = SDL_LoadBMP(va("%s/icon.bmp", com_gamedir)); if (sIcon) { // [25/3/2014] Set the transparency key... ~hogsy SDL_SetColorKey(sIcon, true, SDL_MapRGB(sIcon->format, 0, 0, 0)); SDL_SetWindowIcon(sMainWindow, sIcon); SDL_FreeSurface(sIcon); } else // Give us a warning, but continue. Con_Warning("Failed to load window icon! (%s)\n", SDL_GetError()); sMainContext = SDL_GL_CreateContext(sMainWindow); if (!sMainContext) Sys_Error("Failed to create context!\n%s\n", SDL_GetError()); SDL_GL_SetSwapInterval(0); #ifdef _WIN32 if (SDL_GetWindowWMInfo(sMainWindow, &sSystemInfo)) g_mainwindow.instance = sSystemInfo.info.win.window; else Con_Warning("Failed to get WM information! (%s)\n", SDL_GetError()); #endif }
static SetModeResult SetMode(int mode, bool fullscreen, bool noborder) { interface::Printf("Initializing display\n"); Uint32 flags = SDL_WINDOW_SHOWN; if (g_cvars.allowResize.getBool()) flags |= SDL_WINDOW_RESIZABLE; SDL_Surface *icon = NULL; #ifdef USE_ICON icon = SDL_CreateRGBSurfaceFrom( (void *)CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); #endif // If a window exists, note its display index int display = 0; if (SDL_window != NULL) display = SDL_GetWindowDisplayIndex(SDL_window); SDL_DisplayMode desktopMode; // Get the display aspect ratio if (SDL_GetDesktopDisplayMode(display, &desktopMode) == 0) { displayAspect = desktopMode.w / (float)desktopMode.h; interface::Printf("Display aspect: %.3f\n", displayAspect); } else { memset(&desktopMode, 0, sizeof(SDL_DisplayMode)); interface::Printf("Cannot determine display aspect, assuming 1.333\n"); } interface::Printf("...setting mode %d:", mode ); if (mode == -2) { // use desktop video resolution if( desktopMode.h > 0 ) { s_window.width = desktopMode.w; s_window.height = desktopMode.h; } else { s_window.width = 640; s_window.height = 480; interface::Printf("Cannot determine display resolution, assuming 640x480\n"); } s_window.aspectRatio = s_window.width / (float)s_window.height; } else if (!GetModeInfo(mode)) { interface::Printf(" invalid mode\n"); return SetModeResult::InvalidMode; } interface::Printf(" %d %d\n", s_window.width, s_window.height); // Center window int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED; if (SDL_window != NULL) { SDL_GetWindowPosition(SDL_window, &x, &y); interface::PrintDeveloperf("Existing window at %dx%d before being destroyed\n", x, y); SDL_DestroyWindow(SDL_window); SDL_window = NULL; } if (fullscreen) { flags |= SDL_WINDOW_FULLSCREEN; s_window.isFullscreen = true; } else { if (noborder) flags |= SDL_WINDOW_BORDERLESS; s_window.isFullscreen = false; } if (g_cvars.centerWindow.getBool() && !s_window.isFullscreen) { x = y = SDL_WINDOWPOS_CENTERED; } if ((SDL_window = SDL_CreateWindow(CLIENT_WINDOW_TITLE, x, y, s_window.width, s_window.height, flags)) == 0) { interface::PrintDeveloperf("SDL_CreateWindow failed: %s\n", SDL_GetError()); goto finished; } if (fullscreen) { SDL_DisplayMode mode; mode.format = SDL_PIXELFORMAT_RGB24; mode.w = s_window.width; mode.h = s_window.height; mode.refresh_rate = s_window.refreshRate = interface::Cvar_GetInteger("r_displayRefresh"); mode.driverdata = NULL; if (SDL_SetWindowDisplayMode(SDL_window, &mode) < 0) { interface::PrintDeveloperf("SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError()); goto finished; } } SDL_SetWindowIcon(SDL_window, icon); finished: SDL_FreeSurface(icon); if (!SDL_window) { interface::Printf("Couldn't get a visual\n"); return SetModeResult::InvalidMode; } return SetModeResult::OK; }
void twindow::set_icon(const surface& icon) { SDL_SetWindowIcon(window_, icon); }
static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder) { int perChannelColorBits; int colorBits, depthBits, stencilBits; int samples; int i = 0; SDL_Surface *icon = NULL; SDL_DisplayMode desktopMode; int display = 0; int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED; Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_GRABBED; #ifndef FEATURE_RENDERER_GLES GLenum glewResult; #endif Ren_Print("Initializing OpenGL display\n"); if (r_allowResize->integer && !fullscreen) { flags |= SDL_WINDOW_RESIZABLE; } icon = SDL_CreateRGBSurfaceFrom( (void *)CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); // If a window exists, note its display index if (main_window != NULL) { display = SDL_GetWindowDisplayIndex(main_window); } if (SDL_GetDesktopDisplayMode(display, &desktopMode) == 0) { displayAspect = (float)desktopMode.w / (float)desktopMode.h; Ren_Print("Estimated display aspect: %.3f\n", displayAspect); } else { Com_Memset(&desktopMode, 0, sizeof(SDL_DisplayMode)); Ren_Print("Cannot estimate display aspect, assuming 1.333\n"); } Ren_Print("...setting mode %d: ", mode); if (mode == -2) { // use desktop video resolution if (desktopMode.h > 0) { glConfig.vidWidth = desktopMode.w; glConfig.vidHeight = desktopMode.h; } else { glConfig.vidWidth = 640; glConfig.vidHeight = 480; Ren_Print("Cannot determine display resolution, assuming 640x480\n"); } glConfig.windowAspect = (float)glConfig.vidWidth / (float)glConfig.vidHeight; } else if (!R_GetModeInfo(&glConfig.vidWidth, &glConfig.vidHeight, &glConfig.windowAspect, mode)) { Ren_Print("invalid mode\n"); return RSERR_INVALID_MODE; } Ren_Print("%dx%d\n", glConfig.vidWidth, glConfig.vidHeight); // Center window if (r_centerWindow->integer && !fullscreen) { x = (desktopMode.w / 2) - (glConfig.vidWidth / 2); y = (desktopMode.h / 2) - (glConfig.vidHeight / 2); } // Destroy existing state if it exists if (SDL_glContext != NULL) { SDL_GL_DeleteContext(SDL_glContext); SDL_glContext = NULL; } if (main_window != NULL) { SDL_GetWindowPosition(main_window, &x, &y); Ren_Developer("Existing window at %dx%d before being destroyed\n", x, y); SDL_DestroyWindow(main_window); main_window = NULL; } if (fullscreen) { if (r_mode->integer == -2) { flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; } else { flags |= SDL_WINDOW_FULLSCREEN; } glConfig.isFullscreen = qtrue; } else { if (noborder) { flags |= SDL_WINDOW_BORDERLESS; } glConfig.isFullscreen = qfalse; } colorBits = r_colorbits->value; if ((!colorBits) || (colorBits >= 32)) { colorBits = 24; } if (!r_depthbits->value) { depthBits = 24; } else { depthBits = r_depthbits->value; } stencilBits = r_stencilbits->value; samples = r_ext_multisample->value; for (i = 0; i < 16; i++) { int testColorBits, testDepthBits, testStencilBits; // 0 - default // 1 - minus colorBits // 2 - minus depthBits // 3 - minus stencil if ((i % 4) == 0 && i) { // one pass, reduce switch (i / 4) { case 2: if (colorBits == 24) { colorBits = 16; } break; case 1: if (depthBits == 32) { depthBits = 24; } else if (depthBits == 24) { depthBits = 16; } else if (depthBits == 16) { depthBits = 8; } case 3: // fall through if (stencilBits == 24) { stencilBits = 16; } else if (stencilBits == 16) { stencilBits = 8; } } } testColorBits = colorBits; testDepthBits = depthBits; testStencilBits = stencilBits; if ((i % 4) == 3) // reduce colorbits { if (testColorBits == 24) { testColorBits = 16; } } if ((i % 4) == 2) // reduce depthbits { if (testDepthBits == 24) { testDepthBits = 16; } else if (testDepthBits == 16) { testDepthBits = 8; } } if ((i % 4) == 1) // reduce stencilbits { if (testStencilBits == 24) { testStencilBits = 16; } else if (testStencilBits == 16) { testStencilBits = 8; } else { testStencilBits = 0; } } if (testColorBits == 24) { perChannelColorBits = 8; } else { perChannelColorBits = 4; } #ifdef __sgi // Fix for SGIs grabbing too many bits of color if (perChannelColorBits == 4) { perChannelColorBits = 0; /* Use minimum size for 16-bit color */ } // Need alpha or else SGIs choose 36+ bit RGB mode SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1); #endif SDL_GL_SetAttribute(SDL_GL_RED_SIZE, perChannelColorBits); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, perChannelColorBits); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, perChannelColorBits); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, testDepthBits); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, testStencilBits); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples); // SDL2 uses opengl by default, if we want opengl es we need to set this attribute //SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1); if (r_stereoEnabled->integer) { glConfig.stereoEnabled = qtrue; SDL_GL_SetAttribute(SDL_GL_STEREO, 1); } else { glConfig.stereoEnabled = qfalse; SDL_GL_SetAttribute(SDL_GL_STEREO, 0); } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // If not allowing software GL, demand accelerated if (!r_allowSoftwareGL->integer) { SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); } main_window = SDL_CreateWindow(CLIENT_WINDOW_TITLE, x, y, glConfig.vidWidth, glConfig.vidHeight, flags | SDL_WINDOW_SHOWN); if (!main_window) { Ren_Developer("SDL_CreateWindow failed: %s\n", SDL_GetError()); continue; } //This is disabled since at least now we have no use for this /* if (!Glimp_Create2DRenderer(main_window)) { continue; } */ if (fullscreen) { SDL_DisplayMode mode; switch (testColorBits) { case 16: mode.format = SDL_PIXELFORMAT_RGB565; break; case 24: mode.format = SDL_PIXELFORMAT_RGB24; break; default: Ren_Developer("testColorBits is %d, can't fullscreen\n", testColorBits); continue; } mode.w = glConfig.vidWidth; mode.h = glConfig.vidHeight; mode.refresh_rate = glConfig.displayFrequency = ri.Cvar_VariableIntegerValue("r_displayRefresh"); mode.driverdata = NULL; if (SDL_SetWindowDisplayMode(main_window, &mode) < 0) { Ren_Developer("SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError()); continue; } } SDL_SetWindowIcon(main_window, icon); #if defined(FEATURE_RENDERER2) glewExperimental = GL_TRUE; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); #endif if ((SDL_glContext = SDL_GL_CreateContext(main_window)) == NULL) { Ren_Developer("SDL_GL_CreateContext failed: %s\n", SDL_GetError()); continue; } SDL_GL_MakeCurrent(main_window, SDL_glContext); SDL_GL_SetSwapInterval(r_swapInterval->integer); glConfig.colorBits = testColorBits; glConfig.depthBits = testDepthBits; glConfig.stencilBits = testStencilBits; ri.Printf(PRINT_ALL, "Using %d color bits, %d depth, %d stencil display.\n", glConfig.colorBits, glConfig.depthBits, glConfig.stencilBits); break; } GLimp_DetectAvailableModes(); #if !defined(FEATURE_RENDERER_GLES) glewResult = glewInit(); if (GLEW_OK != glewResult) { // glewInit failed, something is seriously wrong Ren_Fatal("GLW_StartOpenGL() - could not load OpenGL subsystem: %s", glewGetErrorString(glewResult)); } else { Ren_Print("Using GLEW %s\n", glewGetString(GLEW_VERSION)); } #endif if (!GLimp_InitOpenGLContext()) { return RSERR_OLD_GL; } if (!main_window) //|| !main_renderer) { Ren_Print("Couldn't get a visual\n"); return RSERR_INVALID_MODE; } SDL_FreeSurface(icon); return RSERR_OK; }
void graphics_viewport_set_icon(fpath icon) { SDL_Surface* window_icon = SDL_LoadBMP(asset_hndl_new(icon).path.ptr); SDL_SetWindowIcon(screen, window_icon); SDL_FreeSurface(window_icon); }
/* =============== GLimp_SetMode =============== */ static rserr_t GLimp_SetMode(glconfig_t *glConfig, const windowDesc_t *windowDesc, const char *windowTitle, int mode, qboolean fullscreen, qboolean noborder) { int perChannelColorBits; int colorBits, depthBits, stencilBits; int samples; int i = 0; SDL_Surface *icon = NULL; Uint32 flags = SDL_WINDOW_SHOWN; SDL_DisplayMode desktopMode; int display = 0; int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED; if ( windowDesc->api == GRAPHICS_API_OPENGL ) { flags |= SDL_WINDOW_OPENGL; } Com_Printf( "Initializing display\n"); icon = SDL_CreateRGBSurfaceFrom( (void *)CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); // If a window exists, note its display index if( screen != NULL ) display = SDL_GetWindowDisplayIndex( screen ); if( SDL_GetDesktopDisplayMode( display, &desktopMode ) == 0 ) { displayAspect = (float)desktopMode.w / (float)desktopMode.h; Com_Printf( "Display aspect: %.3f\n", displayAspect ); } else { Com_Memset( &desktopMode, 0, sizeof( SDL_DisplayMode ) ); Com_Printf( "Cannot determine display aspect, assuming 1.333\n" ); } Com_Printf( "...setting mode %d:", mode ); if (mode == -2) { // use desktop video resolution if( desktopMode.h > 0 ) { glConfig->vidWidth = desktopMode.w; glConfig->vidHeight = desktopMode.h; } else { glConfig->vidWidth = 640; glConfig->vidHeight = 480; Com_Printf( "Cannot determine display resolution, assuming 640x480\n" ); } //glConfig.windowAspect = (float)glConfig.vidWidth / (float)glConfig.vidHeight; } else if ( !R_GetModeInfo( &glConfig->vidWidth, &glConfig->vidHeight, /*&glConfig.windowAspect,*/ mode ) ) { Com_Printf( " invalid mode\n" ); SDL_FreeSurface( icon ); return RSERR_INVALID_MODE; } Com_Printf( " %d %d\n", glConfig->vidWidth, glConfig->vidHeight); // Center window if( r_centerWindow->integer && !fullscreen ) { x = ( desktopMode.w / 2 ) - ( glConfig->vidWidth / 2 ); y = ( desktopMode.h / 2 ) - ( glConfig->vidHeight / 2 ); } // Destroy existing state if it exists if( opengl_context != NULL ) { SDL_GL_DeleteContext( opengl_context ); opengl_context = NULL; } if( screen != NULL ) { SDL_GetWindowPosition( screen, &x, &y ); Com_DPrintf( "Existing window at %dx%d before being destroyed\n", x, y ); SDL_DestroyWindow( screen ); screen = NULL; } if( fullscreen ) { flags |= SDL_WINDOW_FULLSCREEN; glConfig->isFullscreen = qtrue; } else { if( noborder ) flags |= SDL_WINDOW_BORDERLESS; glConfig->isFullscreen = qfalse; } colorBits = r_colorbits->integer; if ((!colorBits) || (colorBits >= 32)) colorBits = 24; if (!r_depthbits->integer) depthBits = 24; else depthBits = r_depthbits->integer; stencilBits = r_stencilbits->integer; samples = r_ext_multisample->integer; if ( windowDesc->api == GRAPHICS_API_OPENGL ) { for (i = 0; i < 16; i++) { int testColorBits, testDepthBits, testStencilBits; // 0 - default // 1 - minus colorBits // 2 - minus depthBits // 3 - minus stencil if ((i % 4) == 0 && i) { // one pass, reduce switch (i / 4) { case 2 : if (colorBits == 24) colorBits = 16; break; case 1 : if (depthBits == 24) depthBits = 16; else if (depthBits == 16) depthBits = 8; case 3 : if (stencilBits == 24) stencilBits = 16; else if (stencilBits == 16) stencilBits = 8; } } testColorBits = colorBits; testDepthBits = depthBits; testStencilBits = stencilBits; if ((i % 4) == 3) { // reduce colorBits if (testColorBits == 24) testColorBits = 16; } if ((i % 4) == 2) { // reduce depthBits if (testDepthBits == 24) testDepthBits = 16; else if (testDepthBits == 16) testDepthBits = 8; } if ((i % 4) == 1) { // reduce stencilBits if (testStencilBits == 24) testStencilBits = 16; else if (testStencilBits == 16) testStencilBits = 8; else testStencilBits = 0; } if (testColorBits == 24) perChannelColorBits = 8; else perChannelColorBits = 4; SDL_GL_SetAttribute( SDL_GL_RED_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, testDepthBits ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, testStencilBits ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples ); if ( windowDesc->gl.majorVersion ) { int compactVersion = windowDesc->gl.majorVersion * 100 + windowDesc->gl.minorVersion * 10; SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, windowDesc->gl.majorVersion ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, windowDesc->gl.minorVersion ); if ( windowDesc->gl.profile == GLPROFILE_ES || compactVersion >= 320 ) { int profile; switch ( windowDesc->gl.profile ) { default: case GLPROFILE_COMPATIBILITY: profile = SDL_GL_CONTEXT_PROFILE_COMPATIBILITY; break; case GLPROFILE_CORE: profile = SDL_GL_CONTEXT_PROFILE_CORE; break; case GLPROFILE_ES: profile = SDL_GL_CONTEXT_PROFILE_ES; break; } SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, profile ); } } if ( windowDesc->gl.contextFlags & GLCONTEXT_DEBUG ) { SDL_GL_SetAttribute( SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG ); } if(r_stereo->integer) { glConfig->stereoEnabled = qtrue; SDL_GL_SetAttribute(SDL_GL_STEREO, 1); } else { glConfig->stereoEnabled = qfalse; SDL_GL_SetAttribute(SDL_GL_STEREO, 0); } SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ); if( ( screen = SDL_CreateWindow( windowTitle, x, y, glConfig->vidWidth, glConfig->vidHeight, flags ) ) == NULL ) { Com_DPrintf( "SDL_CreateWindow failed: %s\n", SDL_GetError( ) ); continue; } #ifndef MACOS_X SDL_SetWindowIcon( screen, icon ); #endif if( fullscreen ) { SDL_DisplayMode mode; switch( testColorBits ) { case 16: mode.format = SDL_PIXELFORMAT_RGB565; break; case 24: mode.format = SDL_PIXELFORMAT_RGB24; break; default: Com_DPrintf( "testColorBits is %d, can't fullscreen\n", testColorBits ); continue; } mode.w = glConfig->vidWidth; mode.h = glConfig->vidHeight; mode.refresh_rate = glConfig->displayFrequency = r_displayRefresh->integer; mode.driverdata = NULL; if( SDL_SetWindowDisplayMode( screen, &mode ) < 0 ) { Com_DPrintf( "SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError( ) ); continue; } } if( ( opengl_context = SDL_GL_CreateContext( screen ) ) == NULL ) { Com_Printf( "SDL_GL_CreateContext failed: %s\n", SDL_GetError( ) ); continue; } SDL_GL_SetSwapInterval( r_swapInterval->integer ); glConfig->colorBits = testColorBits; glConfig->depthBits = testDepthBits; glConfig->stencilBits = testStencilBits; Com_Printf( "Using %d color bits, %d depth, %d stencil display.\n", glConfig->colorBits, glConfig->depthBits, glConfig->stencilBits ); break; } } else { // Just create a regular window if( ( screen = SDL_CreateWindow( windowTitle, x, y, glConfig->vidWidth, glConfig->vidHeight, flags ) ) == NULL ) { Com_DPrintf( "SDL_CreateWindow failed: %s\n", SDL_GetError( ) ); } else { #ifndef MACOS_X SDL_SetWindowIcon( screen, icon ); #endif if( fullscreen ) { if( SDL_SetWindowDisplayMode( screen, NULL ) < 0 ) { Com_DPrintf( "SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError( ) ); } } } } SDL_FreeSurface( icon ); if (!GLimp_DetectAvailableModes()) { return RSERR_UNKNOWN; } return RSERR_OK; }
SDL_bool CommonInit(CommonState * state) { int i, j, m, n; SDL_DisplayMode fullscreen_mode; if (state->flags & SDL_INIT_VIDEO) { if (state->verbose & VERBOSE_VIDEO) { n = SDL_GetNumVideoDrivers(); if (n == 0) { fprintf(stderr, "No built-in video drivers\n"); } else { fprintf(stderr, "Built-in video drivers:"); for (i = 0; i < n; ++i) { if (i > 0) { fprintf(stderr, ","); } fprintf(stderr, " %s", SDL_GetVideoDriver(i)); } fprintf(stderr, "\n"); } } if (SDL_VideoInit(state->videodriver) < 0) { fprintf(stderr, "Couldn't initialize video driver: %s\n", SDL_GetError()); return SDL_FALSE; } if (state->verbose & VERBOSE_VIDEO) { fprintf(stderr, "Video driver: %s\n", SDL_GetCurrentVideoDriver()); } /* Upload GL settings */ SDL_GL_SetAttribute(SDL_GL_RED_SIZE, state->gl_red_size); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, state->gl_green_size); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, state->gl_blue_size); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, state->gl_alpha_size); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, state->gl_double_buffer); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, state->gl_buffer_size); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, state->gl_depth_size); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, state->gl_stencil_size); SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, state->gl_accum_red_size); SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, state->gl_accum_green_size); SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, state->gl_accum_blue_size); SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, state->gl_accum_alpha_size); SDL_GL_SetAttribute(SDL_GL_STEREO, state->gl_stereo); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, state->gl_multisamplebuffers); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, state->gl_multisamplesamples); if (state->gl_accelerated >= 0) { SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, state->gl_accelerated); } SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, state->gl_retained_backing); if (state->gl_major_version) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, state->gl_major_version); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, state->gl_minor_version); } if (state->verbose & VERBOSE_MODES) { SDL_DisplayMode mode; int bpp; Uint32 Rmask, Gmask, Bmask, Amask; n = SDL_GetNumVideoDisplays(); fprintf(stderr, "Number of displays: %d\n", n); for (i = 0; i < n; ++i) { fprintf(stderr, "Display %d:\n", i); SDL_GetDesktopDisplayMode(i, &mode); SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask, &Amask); fprintf(stderr, " Current mode: %dx%d@%dHz, %d bits-per-pixel (%s)\n", mode.w, mode.h, mode.refresh_rate, bpp, SDL_GetPixelFormatName(mode.format)); if (Rmask || Gmask || Bmask) { fprintf(stderr, " Red Mask = 0x%.8x\n", Rmask); fprintf(stderr, " Green Mask = 0x%.8x\n", Gmask); fprintf(stderr, " Blue Mask = 0x%.8x\n", Bmask); if (Amask) fprintf(stderr, " Alpha Mask = 0x%.8x\n", Amask); } /* Print available fullscreen video modes */ m = SDL_GetNumDisplayModes(i); if (m == 0) { fprintf(stderr, "No available fullscreen video modes\n"); } else { fprintf(stderr, " Fullscreen video modes:\n"); for (j = 0; j < m; ++j) { SDL_GetDisplayMode(i, j, &mode); SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask, &Amask); fprintf(stderr, " Mode %d: %dx%d@%dHz, %d bits-per-pixel (%s)\n", j, mode.w, mode.h, mode.refresh_rate, bpp, SDL_GetPixelFormatName(mode.format)); if (Rmask || Gmask || Bmask) { fprintf(stderr, " Red Mask = 0x%.8x\n", Rmask); fprintf(stderr, " Green Mask = 0x%.8x\n", Gmask); fprintf(stderr, " Blue Mask = 0x%.8x\n", Bmask); if (Amask) fprintf(stderr, " Alpha Mask = 0x%.8x\n", Amask); } } } } } if (state->verbose & VERBOSE_RENDER) { SDL_RendererInfo info; n = SDL_GetNumRenderDrivers(); if (n == 0) { fprintf(stderr, "No built-in render drivers\n"); } else { fprintf(stderr, "Built-in render drivers:\n"); for (i = 0; i < n; ++i) { SDL_GetRenderDriverInfo(i, &info); PrintRenderer(&info); } } } SDL_zero(fullscreen_mode); switch (state->depth) { case 8: fullscreen_mode.format = SDL_PIXELFORMAT_INDEX8; break; case 15: fullscreen_mode.format = SDL_PIXELFORMAT_RGB555; break; case 16: fullscreen_mode.format = SDL_PIXELFORMAT_RGB565; break; case 24: fullscreen_mode.format = SDL_PIXELFORMAT_RGB24; break; default: fullscreen_mode.format = SDL_PIXELFORMAT_RGB888; break; } fullscreen_mode.refresh_rate = state->refresh_rate; state->windows = (SDL_Window **) SDL_malloc(state->num_windows * sizeof(*state->windows)); state->renderers = (SDL_Renderer **) SDL_malloc(state->num_windows * sizeof(*state->renderers)); if (!state->windows || !state->renderers) { fprintf(stderr, "Out of memory!\n"); return SDL_FALSE; } for (i = 0; i < state->num_windows; ++i) { char title[1024]; if (state->num_windows > 1) { SDL_snprintf(title, SDL_arraysize(title), "%s %d", state->window_title, i + 1); } else { SDL_strlcpy(title, state->window_title, SDL_arraysize(title)); } state->windows[i] = SDL_CreateWindow(title, state->window_x, state->window_y, state->window_w, state->window_h, state->window_flags); if (!state->windows[i]) { fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError()); return SDL_FALSE; } SDL_GetWindowSize(state->windows[i], &state->window_w, &state->window_h); if (SDL_SetWindowDisplayMode(state->windows[i], &fullscreen_mode) < 0) { fprintf(stderr, "Can't set up fullscreen display mode: %s\n", SDL_GetError()); return SDL_FALSE; } if (state->window_icon) { SDL_Surface *icon = LoadIcon(state->window_icon); if (icon) { SDL_SetWindowIcon(state->windows[i], icon); SDL_FreeSurface(icon); } } SDL_ShowWindow(state->windows[i]); state->renderers[i] = NULL; if (!state->skip_renderer && (state->renderdriver || !(state->window_flags & SDL_WINDOW_OPENGL))) { m = -1; if (state->renderdriver) { SDL_RendererInfo info; n = SDL_GetNumRenderDrivers(); for (j = 0; j < n; ++j) { SDL_GetRenderDriverInfo(j, &info); if (SDL_strcasecmp(info.name, state->renderdriver) == 0) { m = j; break; } } if (m == n) { fprintf(stderr, "Couldn't find render driver named %s", state->renderdriver); return SDL_FALSE; } } state->renderers[i] = SDL_CreateRenderer(state->windows[i], m, state->render_flags); if (!state->renderers[i]) { fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError()); return SDL_FALSE; } if (state->verbose & VERBOSE_RENDER) { SDL_RendererInfo info; fprintf(stderr, "Current renderer:\n"); SDL_GetRendererInfo(state->renderers[i], &info); PrintRenderer(&info); } } } } if (state->flags & SDL_INIT_AUDIO) { if (state->verbose & VERBOSE_AUDIO) { n = SDL_GetNumAudioDrivers(); if (n == 0) { fprintf(stderr, "No built-in audio drivers\n"); } else { fprintf(stderr, "Built-in audio drivers:"); for (i = 0; i < n; ++i) { if (i > 0) { fprintf(stderr, ","); } fprintf(stderr, " %s", SDL_GetAudioDriver(i)); } fprintf(stderr, "\n"); } } if (SDL_AudioInit(state->audiodriver) < 0) { fprintf(stderr, "Couldn't initialize audio driver: %s\n", SDL_GetError()); return SDL_FALSE; } if (state->verbose & VERBOSE_VIDEO) { fprintf(stderr, "Audio driver: %s\n", SDL_GetCurrentAudioDriver()); } if (SDL_OpenAudio(&state->audiospec, NULL) < 0) { fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); return SDL_FALSE; } } return SDL_TRUE; }
extern "C" int main(int argcount, char* argvec[]) #endif { #if defined(__native_client__) std::cerr << "Running game_main" << std::endl; chdir("/frogatto"); { char buf[256]; const char* const res = getcwd(buf,sizeof(buf)); std::cerr << "Current working directory: " << res << std::endl; } #endif #if defined(_MSC_VER) std::shared_ptr<FILE> f_stderr = std::shared_ptr<FILE>(freopen ("stderr.txt", "wt", stderr), [](FILE* f){fclose(f); delete f;}); #endif #if defined(__APPLE__) && TARGET_OS_MAC chdir([[[NSBundle mainBundle] resourcePath] fileSystemRepresentation]); #endif #ifdef NO_STDERR std::freopen("/dev/null", "w", stderr); std::cerr.sync_with_stdio(true); #endif std::cerr << "Frogatto engine version " << preferences::version() << "\n"; LOG( "After print engine version" ); #if defined(TARGET_BLACKBERRY) chdir("app/native"); std::cout<< "Changed working directory to: " << getcwd(0, 0) << std::endl; #endif game_logic::init_callable_definitions(); std::string level_cfg = "titlescreen.cfg"; bool unit_tests_only = false, skip_tests = false; bool run_benchmarks = false; std::vector<std::string> benchmarks_list; std::string utility_program; std::vector<std::string> util_args; std::string server = "wesnoth.org"; #if defined(UTILITY_IN_PROC) bool create_utility_in_new_process = false; std::string utility_name; #endif bool is_child_utility = false; const char* profile_output = NULL; std::string profile_output_buf; #if defined(__ANDROID__) //monstartup("libapplication.so"); #endif std::string orig_level_cfg = level_cfg; std::string override_level_cfg = ""; int modules_loaded = 0; std::vector<std::string> argv; for(int n = 1; n < argcount; ++n) { #if defined(UTILITY_IN_PROC) std::string sarg(argvec[n]); if(sarg.compare(0, 15, "--utility-proc=") == 0) { create_utility_in_new_process = true; utility_name = "--utility-child=" + sarg.substr(15); } else { argv.push_back(argvec[n]); } #else argv.push_back(argvec[n]); #endif if(argv.size() >= 2 && argv[argv.size()-2] == "-NSDocumentRevisionsDebugMode" && argv.back() == "YES") { //XCode passes these arguments by default when debugging -- make sure they are ignored. argv.resize(argv.size()-2); } } std::cerr << "Build Options:"; for(auto bo : preferences::get_build_options()) { std::cerr << " " << bo; } std::cerr << std::endl; #if defined(UTILITY_IN_PROC) if(create_utility_in_new_process) { argv.push_back(utility_name); #if defined(_MSC_VER) // app name is ignored for windows, we get windows to tell us. is_child_utility = create_utility_process("", argv); #else is_child_utility = create_utility_process(argvec[0], argv); #endif if(!is_child_utility) { argv.pop_back(); } #if defined(_MSC_VER) atexit(terminate_utility_process); #endif } #endif if(sys::file_exists("./master-config.cfg")) { std::cerr << "LOADING CONFIGURATION FROM master-config.cfg" << std::endl; variant cfg = json::parse_from_file("./master-config.cfg"); if(cfg.is_map()) { if( cfg["id"].is_null() == false) { std::cerr << "SETTING MODULE PATH FROM master-config.cfg: " << cfg["id"].as_string() << std::endl; preferences::set_preferences_path_from_module(cfg["id"].as_string()); //XXX module::set_module_name(cfg["id"].as_string(), cfg["id"].as_string()); } if(cfg["arguments"].is_null() == false) { std::vector<std::string> additional_args = cfg["arguments"].as_list_string(); argv.insert(argv.begin(), additional_args.begin(), additional_args.end()); std::cerr << "ADDING ARGUMENTS FROM master-config.cfg:"; for(size_t n = 0; n < cfg["arguments"].num_elements(); ++n) { std::cerr << " " << cfg["arguments"][n].as_string(); } std::cerr << std::endl; } } } stats::record_program_args(argv); for(size_t n = 0; n < argv.size(); ++n) { const int argc = argv.size(); const std::string arg(argv[n]); std::string arg_name, arg_value; std::string::const_iterator equal = std::find(arg.begin(), arg.end(), '='); if(equal != arg.end()) { arg_name = std::string(arg.begin(), equal); arg_value = std::string(equal+1, arg.end()); } if(arg_name == "--module") { if(load_module(arg_value, &argv) != 0) { std::cerr << "FAILED TO LOAD MODULE: " << arg_value << "\n"; return -1; } ++modules_loaded; } else if(arg == "--tests") { unit_tests_only = true; } } if(modules_loaded == 0 && !unit_tests_only) { if(load_module(DEFAULT_MODULE, &argv) != 0) { std::cerr << "FAILED TO LOAD MODULE: " << DEFAULT_MODULE << "\n"; return -1; } } preferences::load_preferences(); LOG( "After load_preferences()" ); // load difficulty settings after module, before rest of args. difficulty::manager(); for(size_t n = 0; n < argv.size(); ++n) { const size_t argc = argv.size(); const std::string arg(argv[n]); std::string arg_name, arg_value; std::string::const_iterator equal = std::find(arg.begin(), arg.end(), '='); if(equal != arg.end()) { arg_name = std::string(arg.begin(), equal); arg_value = std::string(equal+1, arg.end()); } std::cerr << "ARGS: " << arg << std::endl; if(arg.substr(0,4) == "-psn") { // ignore. } else if(arg_name == "--module") { // ignore already processed. } else if(arg_name == "--profile" || arg == "--profile") { profile_output_buf = arg_value; profile_output = profile_output_buf.c_str(); } else if(arg_name == "--utility" || arg_name == "--utility-child") { if(arg_name == "--utility-child") { is_child_utility = true; } utility_program = arg_value; for(++n; n < argc; ++n) { const std::string arg(argv[n]); util_args.push_back(arg); } break; } else if(arg == "--benchmarks") { run_benchmarks = true; } else if(arg_name == "--benchmarks") { run_benchmarks = true; benchmarks_list = util::split(arg_value); } else if(arg == "--tests") { // ignore as already processed. } else if(arg == "--no-tests") { skip_tests = true; } else if(arg_name == "--width") { std::string w(arg_value); preferences::set_actual_screen_width(boost::lexical_cast<int>(w)); } else if(arg == "--width" && n+1 < argc) { std::string w(argv[++n]); preferences::set_actual_screen_width(boost::lexical_cast<int>(w)); } else if(arg_name == "--height") { std::string h(arg_value); preferences::set_actual_screen_height(boost::lexical_cast<int>(h)); } else if(arg == "--height" && n+1 < argc) { std::string h(argv[++n]); preferences::set_actual_screen_height(boost::lexical_cast<int>(h)); } else if(arg_name == "--level") { override_level_cfg = arg_value; } else if(arg == "--level" && n+1 < argc) { override_level_cfg = argv[++n]; } else if(arg_name == "--host") { server = arg_value; } else if(arg == "--host" && n+1 < argc) { server = argv[++n]; } else if(arg == "--compiled") { preferences::set_load_compiled(true); #ifndef NO_EDITOR } else if(arg == "--edit") { preferences::set_edit_on_start(true); #endif } else if(arg == "--no-compiled") { preferences::set_load_compiled(false); #if defined(TARGET_PANDORA) } else if(arg == "--no-fbo") { preferences::set_fbo(false); } else if(arg == "--no-bequ") { preferences::set_bequ(false); #endif } else if(arg == "--help" || arg == "-h") { print_help(std::string(argvec[0])); return 0; } else { const bool res = preferences::parse_arg(argv[n].c_str()); if(!res) { std::cerr << "unrecognized arg: '" << arg << "'\n"; return -1; } } } checksum::manager checksum_manager; #ifndef NO_EDITOR sys::filesystem_manager fs_manager; #endif // NO_EDITOR preferences::expand_data_paths(); background_task_pool::manager bg_task_pool_manager; LOG( "After expand_data_paths()" ); std::cerr << "Preferences dir: " << preferences::user_data_path() << '\n'; //make sure that the user data path exists. if(!preferences::setup_preferences_dir()) { std::cerr << "cannot create preferences dir!\n"; } std::cerr << "\n"; const tbs::internal_server_manager internal_server_manager_scope(preferences::internal_tbs_server()); if(utility_program.empty() == false && test::utility_needs_video(utility_program) == false) { #if defined(UTILITY_IN_PROC) if(is_child_utility) { ASSERT_LOG(ipc::semaphore::create(shared_sem_name, 1) != false, "Unable to create shared semaphore: " << errno); std::cerr.sync_with_stdio(true); } #endif test::run_utility(utility_program, util_args); return 0; } #if defined(TARGET_PANDORA) EGL_Open(); #endif #if defined(__ANDROID__) std::freopen("stdout.txt","w",stdout); std::freopen("stderr.txt","w",stderr); std::cerr.sync_with_stdio(true); #endif LOG( "Start of main" ); if(!skip_tests && !test::run_tests()) { return -1; } if(unit_tests_only) { return 0; } #if !defined(__native_client__) Uint32 sdl_init_flags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; #if defined(_WINDOWS) || TARGET_OS_IPHONE sdl_init_flags |= SDL_INIT_TIMER; #endif if(SDL_Init(sdl_init_flags) < 0) { std::cerr << "could not init SDL\n"; return -1; } LOG( "After SDL_Init" ); #endif #ifdef TARGET_OS_HARMATTAN g_type_init(); #endif i18n::init (); LOG( "After i18n::init()" ); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); #if TARGET_OS_IPHONE || defined(TARGET_BLACKBERRY) || defined(__ANDROID__) //on the iPhone and PlayBook, try to restore the auto-save if it exists if(sys::file_exists(preferences::auto_save_file_path()) && sys::read_file(std::string(preferences::auto_save_file_path()) + ".stat") == "1") { level_cfg = "autosave.cfg"; sys::write_file(std::string(preferences::auto_save_file_path()) + ".stat", "0"); } #endif if(override_level_cfg.empty() != true) { level_cfg = override_level_cfg; orig_level_cfg = level_cfg; } #if !SDL_VERSION_ATLEAST(2, 0, 0) && defined(USE_SHADERS) wm.create_window(preferences::actual_screen_width(), preferences::actual_screen_height(), 0, (preferences::resizable() ? SDL_RESIZABLE : 0) | (preferences::fullscreen() ? SDL_FULLSCREEN : 0)); #else #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR int width, height; iphone_screen_res(&width, &height); preferences::set_actual_screen_width(width); preferences::set_actual_screen_height(height); int multiplier = 2; if (width > 320) { //preferences::set_use_pretty_scaling(true); multiplier = 1; } preferences::set_virtual_screen_width(height*multiplier); preferences::set_virtual_screen_height(width*multiplier); preferences::set_control_scheme(height % 1024 ? "iphone_2d" : "ipad_2d"); SDL_WindowID windowID = SDL_CreateWindow (NULL, 0, 0, preferences::actual_screen_width(), preferences::actual_screen_height(), SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS); if (windowID == 0) { std::cerr << "Could not create window: " << SDL_GetError() << "\n"; return -1; } // if (SDL_GL_CreateContext(windowID) == 0) { // std::cerr << "Could not create GL context: " << SDL_GetError() << "\n"; // return -1; // } if (SDL_CreateRenderer(windowID, -1, 0) != 0) { std::cerr << "Could not create renderer\n"; return -1; } #else #ifdef TARGET_OS_HARMATTAN SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,SDL_OPENGLES | SDL_FULLSCREEN) == NULL) { std::cerr << "could not set video mode\n"; return -1; } preferences::init_oes(); SDL_ShowCursor(0); #else #if defined(TARGET_PANDORA) if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),16,SDL_FULLSCREEN) == NULL) { std::cerr << "could not set video mode\n"; return -1; } EGL_Init(); preferences::init_oes(); #elif defined(TARGET_TEGRA) //if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,preferences::resizable() ? SDL_RESIZABLE : 0|preferences::fullscreen() ? SDL_FULLSCREEN : 0) == NULL) { if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,SDL_FULLSCREEN) == NULL) { std::cerr << "could not set video mode\n"; return -1; } EGL_Init(); preferences::init_oes(); #elif defined(TARGET_BLACKBERRY) if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,SDL_OPENGL|SDL_FULLSCREEN) == NULL) { std::cerr << "could not set video mode\n"; return -1; } preferences::init_oes(); #elif defined(__ANDROID__) #if SDL_VERSION_ATLEAST(2, 0, 0) int num_video_displays = SDL_GetNumVideoDisplays(); SDL_Rect r; if(num_video_displays < 0) { std::cerr << "no video displays available" << std::endl; return -1; } if(SDL_GetDisplayBounds(0, &r) < 0) { preferences::set_actual_screen_width(r.w); preferences::set_actual_screen_height(r.h); if(r.w < 640) { preferences::set_virtual_screen_width(r.w*2); preferences::set_virtual_screen_height(r.h*2); } else { preferences::set_virtual_screen_width(r.w); preferences::set_virtual_screen_height(r.h); } preferences::set_control_scheme(r.h >= 1024 ? "ipad_2d" : "android_med"); } #else SDL_Rect** r = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL); if( r != (SDL_Rect**)0 && r != (SDL_Rect**)-1 ) { preferences::set_actual_screen_width(r[0]->w); preferences::set_actual_screen_height(r[0]->h); if(r[0]->w < 640) { preferences::set_virtual_screen_width(r[0]->w*2); preferences::set_virtual_screen_height(r[0]->h*2); } else { preferences::set_virtual_screen_width(r[0]->w); preferences::set_virtual_screen_height(r[0]->h); } preferences::set_control_scheme(r[0]->h >= 1024 ? "ipad_2d" : "android_med"); } #endif #if SDL_VERSION_ATLEAST(2, 0, 0) if(!graphics::set_video_mode(preferences::actual_screen_width(), preferences::actual_screen_height())) { #else if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,SDL_FULLSCREEN|SDL_OPENGL) == NULL) { #endif std::cerr << "could not set video mode\n"; return -1; } #elif defined(__native_client__) SDL_Rect** r = SDL_ListModes(NULL, SDL_OPENGL); std::cerr << "Video modes"; if(r == (SDL_Rect**)0) { std::cerr << "No modes available"; return -1; } if(r == (SDL_Rect**)-1) { std::cerr << "All modes available"; } else { for(int i = 0; r[i]; ++i) { std::cerr << r[i]->w << r[i]->h << std::endl; } } if (SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,0) == NULL) { std::cerr << "could not set video mode\n"; return -1; } #else #if SDL_VERSION_ATLEAST(2, 0, 0) if(preferences::auto_size_window()) { const SDL_DisplayMode mode = graphics::set_video_mode_auto_select(); preferences::set_actual_screen_width(mode.w); preferences::set_actual_screen_height(mode.h); preferences::set_virtual_screen_width(mode.w); preferences::set_virtual_screen_height(mode.h); } else if(!graphics::set_video_mode(preferences::actual_screen_width(), preferences::actual_screen_height(), SDL_WINDOW_RESIZABLE|SDL_WINDOW_OPENGL)) { #else if(SDL_SetVideoMode(preferences::actual_screen_width(),preferences::actual_screen_height(),0,SDL_OPENGL|(preferences::resizable() ? SDL_RESIZABLE : 0)|(preferences::fullscreen() ? SDL_FULLSCREEN : 0)) == NULL) { #endif std::cerr << "could not set video mode\n"; return -1; } #ifndef __APPLE__ graphics::surface wm_icon = graphics::surface_cache::get("window-icon.png"); if(!wm_icon.null()) { #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_SetWindowIcon(graphics::get_window(), wm_icon.get()); #else SDL_WM_SetIcon(wm_icon, NULL); #endif } #endif // __APPLE__ #endif #endif #endif #endif #if !SDL_VERSION_ATLEAST(2, 0, 0) && defined(__GLEW_H__) GLenum glew_status = glewInit(); ASSERT_EQ(glew_status, GLEW_OK); #endif #if defined(USE_SHADERS) if(glCreateShader == NULL) { const GLubyte* glstrings; if(glGetString != NULL && (glstrings = glGetString(GL_VERSION)) != NULL) { std::cerr << "OpenGL version: " << reinterpret_cast<const char *>(glstrings) << std::endl; } std::cerr << "glCreateShader is NULL. Check that your current video card drivers support " << "an OpenGL version >= 2. Exiting." << std::endl; return 0; } // Has to happen after the call to glewInit(). gles2::init_default_shader(); #endif // srand(time(NULL)); const stats::manager stats_manager; #ifndef NO_EDITOR const external_text_editor::manager editor_manager; #endif // NO_EDITOR #if defined(USE_BOX2D) box2d::manager b2d_manager; #endif std::cerr << std::endl; const GLubyte* glstrings; if((glstrings = glGetString(GL_VENDOR)) != NULL) { std::cerr << "OpenGL vendor: " << reinterpret_cast<const char *>(glstrings) << std::endl; } else { GLenum err = glGetError(); std::cerr << "Error in vendor string: " << std::hex << err << std::endl; } if((glstrings = glGetString(GL_VERSION)) != NULL) { std::cerr << "OpenGL version: " << reinterpret_cast<const char *>(glstrings) << std::endl; } else { GLenum err = glGetError(); std::cerr << "Error in version string: " << std::hex << err << std::endl; } if((glstrings = glGetString(GL_EXTENSIONS)) != NULL) { std::cerr << "OpenGL extensions: " << reinterpret_cast<const char *>(glstrings) << std::endl; } else { GLenum err = glGetError(); std::cerr << "Error in extensions string: " << std::hex << err << std::endl; } #ifdef GL_SHADING_LANGUAGE_VERSION if((glstrings = glGetString(GL_SHADING_LANGUAGE_VERSION)) != NULL) { std::cerr << "GLSL Version: " << reinterpret_cast<const char *>(glstrings) << std::endl; } else { GLenum err = glGetError(); std::cerr << "Error in GLSL string: " << std::hex << err << std::endl; } #endif std::cerr << std::endl; GLint stencil_bits = 0; glGetIntegerv(GL_STENCIL_BITS, &stencil_bits); std::cerr << "Stencil bits: " << stencil_bits << std::endl; #if defined(USE_SHADERS) #if !defined(GL_ES_VERSION_2_0) GLfloat min_pt_sz; glGetFloatv(GL_POINT_SIZE_MIN, &min_pt_sz); GLfloat max_pt_sz; glGetFloatv(GL_POINT_SIZE_MAX, &max_pt_sz); std::cerr << "Point size range: " << min_pt_sz << " < size < " << max_pt_sz << std::endl; glEnable(GL_POINT_SPRITE); glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); #endif #endif glShadeModel(GL_SMOOTH); glEnable(GL_BLEND); #if !defined(USE_SHADERS) glEnable(GL_TEXTURE_2D); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); #endif glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); std::cerr << "JOYSTICKS: " << SDL_NumJoysticks() << "\n"; const load_level_manager load_manager; { //manager scope const font::manager font_manager; const sound::manager sound_manager; #if !defined(__native_client__) const joystick::manager joystick_manager; #endif graphics::texture::manager texture_manager; #ifndef NO_EDITOR editor::manager editor_manager; #endif variant preloads; loading_screen loader; try { variant gui_node = json::parse_from_file(preferences::load_compiled() ? "data/compiled/gui.cfg" : "data/gui.cfg"); gui_section::init(gui_node); loader.draw_and_increment(_("Initializing GUI")); framed_gui_element::init(gui_node); sound::init_music(json::parse_from_file("data/music.cfg")); graphical_font::init_for_locale(i18n::get_locale()); preloads = json::parse_from_file("data/preload.cfg"); int preload_items = preloads["preload"].num_elements(); loader.set_number_of_items(preload_items+7); // 7 is the number of items that will be loaded below custom_object::init(); loader.draw_and_increment(_("Initializing custom object functions")); init_custom_object_functions(json::parse_from_file("data/functions.cfg")); loader.draw_and_increment(_("Initializing textures")); loader.load(preloads); loader.draw_and_increment(_("Initializing tiles")); tile_map::init(json::parse_from_file("data/tiles.cfg")); game_logic::formula_object::load_all_classes(); } catch(const json::parse_error& e) { std::cerr << "ERROR PARSING: " << e.error_message() << "\n"; return 0; } loader.draw(_("Loading level")); #if defined(__native_client__) while(1) { } #endif #if defined(__APPLE__) && !(TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE) && !defined(USE_SHADERS) GLint swapInterval = 1; CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval); #endif loader.finish_loading(); //look to see if we got any quit events while loading. { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { return 0; } } } formula_profiler::manager profiler(profile_output); #ifdef USE_SHADERS texture_frame_buffer::init(preferences::actual_screen_width(), preferences::actual_screen_height()); #else texture_frame_buffer::init(); #endif if(run_benchmarks) { if(benchmarks_list.empty() == false) { test::run_benchmarks(&benchmarks_list); } else { test::run_benchmarks(); } return 0; } else if(utility_program.empty() == false && test::utility_needs_video(utility_program) == true) { test::run_utility(utility_program, util_args); return 0; } bool quit = false; while(!quit && !show_title_screen(level_cfg)) { boost::intrusive_ptr<level> lvl(load_level(level_cfg)); #if !defined(__native_client__) //see if we're loading a multiplayer level, in which case we //connect to the server. multiplayer::manager mp_manager(lvl->is_multiplayer()); if(lvl->is_multiplayer()) { multiplayer::setup_networked_game(server); } if(lvl->is_multiplayer()) { last_draw_position() = screen_position(); std::string level_cfg = "waiting-room.cfg"; boost::intrusive_ptr<level> wait_lvl(load_level(level_cfg)); wait_lvl->finish_loading(); wait_lvl->set_multiplayer_slot(0); if(wait_lvl->player()) { wait_lvl->player()->set_current_level(level_cfg); } wait_lvl->set_as_current_level(); level_runner runner(wait_lvl, level_cfg, orig_level_cfg); multiplayer::sync_start_time(*lvl, boost::bind(&level_runner::play_cycle, &runner)); lvl->set_multiplayer_slot(multiplayer::slot()); } #endif last_draw_position() = screen_position(); assert(lvl.get()); if(!lvl->music().empty()) { sound::play_music(lvl->music()); } if(lvl->player() && level_cfg != "autosave.cfg") { lvl->player()->set_current_level(level_cfg); lvl->player()->get_entity().save_game(); } set_scene_title(lvl->title()); try { quit = level_runner(lvl, level_cfg, orig_level_cfg).play_level(); level_cfg = orig_level_cfg; } catch(multiplayer_exception&) { } } level::clear_current_level(); } //end manager scope, make managers destruct before calling SDL_Quit // controls::debug_dump_controls(); #if defined(TARGET_PANDORA) || defined(TARGET_TEGRA) EGL_Destroy(); #endif #if SDL_VERSION_ATLEAST(2, 0, 0) // Be nice and destroy the GL context and the window. graphics::set_video_mode(0, 0, CLEANUP_WINDOW_CONTEXT); #elif defined(USE_SHADERS) wm.destroy_window(); #endif SDL_Quit(); preferences::save_preferences(); std::cerr << SDL_GetError() << "\n"; #if !defined(_MSC_VER) && defined(UTILITY_IN_PROC) if(create_utility_in_new_process) { terminate_utility_process(); } #endif #ifdef _MSC_VER ExitProcess(0); #endif std::set<variant*> loading; swap_variants_loading(loading); if(loading.empty() == false) { fprintf(stderr, "Illegal object: %p\n", (void*)(*loading.begin())->as_callable_loading()); ASSERT_LOG(false, "Unresolved unserialized objects: " << loading.size()); } return 0; }
Window::Window() { window = SDL_CreateWindow("game1 test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); SDL_Surface *icon = SDL_LoadBMP("data/imgs/icon.bmp"); icon->format->Amask = 0xFF000000; SDL_SetWindowIcon(window, icon); SDL_GetWindowSize(window, &_width, &_height); zoom = 3; _gl = SDL_GL_CreateContext(window); GLuint GLEWerr = glewInit(); if(GLEWerr != GLEW_OK) printf("GLEW: %s\n", glewGetErrorString(GLEWerr)); if( !GLEW_VERSION_3_2 ) printf( "OpenGL 3.2 not supported!\n" ); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glDisable( GL_LIGHTING ); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GLenum err = glGetError(); if(err != GL_NO_ERROR) { printf("Error: %s\n", gluErrorString(err)); } printf( "VENDOR = %s\n", glGetString( GL_VENDOR ) ) ; printf( "RENDERER = %s\n", glGetString( GL_RENDERER ) ) ; printf( "VERSION = %s\n", glGetString( GL_VERSION ) ) ; /* vShadObj = glCreateShader(GL_VERTEX_SHADER); fShadObj = glCreateShader(GL_FRAGMENT_SHADER); std::fstream vShadFile("default.glvs", std::ios::in); std::string vShadStr; if(vShadFile.is_open()) { std::stringstream buffer; buffer << vShadFile.rdbuf(); vShadStr = buffer.str(); } std::fstream fShadFile("default.glfs", std::ios::in); std::string fShadStr; if(fShadFile.is_open()) { std::stringstream buffer; buffer << fShadFile.rdbuf(); fShadStr = buffer.str(); } const char* vc_str = vShadStr.c_str(); glShaderSource(vShadObj, 1, &vc_str, NULL); const char* fc_str = fShadStr.c_str(); glShaderSource(fShadObj, 1, &fc_str, NULL); glCompileShader(vShadObj); glCompileShader(fShadObj); shadProg = glCreateProgram(); glAttachShader(shadProg, vShadObj); glAttachShader(shadProg, fShadObj); glLinkProgram(shadProg); GLint programSuccess = GL_TRUE; glGetProgramiv( shadProg, GL_LINK_STATUS, &programSuccess ); if( programSuccess != GL_TRUE ) printf( "Error linking program %d!\n", shadProg ); glUseProgram(shadProg); _projPointer = glGetUniformLocation(shadProg, "LProjectionMatrix"); _modViewPointer = glGetUniformLocation(shadProg, "LModelViewMatrix"); */ glEnable(GL_MULTISAMPLE); int samples = 2; /* glGenTextures(1, &fbTex); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, fbTex); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA8, _width, _height, false); glGenFramebuffers(1, &fbObj); glBindFramebuffer(GL_FRAMEBUFFER, fbObj); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, fbTex, 0); err = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT); if(err != GL_FRAMEBUFFER_COMPLETE_EXT) { printf("Error: %s\n", gluErrorString(err)); } err = glGetError(); if(err != GL_NO_ERROR) printf("Window: FrameBuffer: %s\n", gluErrorString(err)); */ glHint(GL_NICEST, GL_FRAGMENT_SHADER_DERIVATIVE_HINT); glHint(GL_NICEST, GL_LINE_SMOOTH_HINT); glHint(GL_NICEST, GL_POLYGON_SMOOTH_HINT); glHint(GL_NICEST, GL_TEXTURE_COMPRESSION_HINT); resize(); err = glGetError(); if(err != GL_NO_ERROR) printf("Window: Shader: %s\n", gluErrorString(err)); /*ilInit(); ilClearColour(255, 255, 255, 000); ILenum ilError = ilGetError(); if(ilError != IL_NO_ERROR) { printf("error loading DevIL: %s\n", iluErrorString(ilError)); }*/ }
int main(int argc, char *argv[]) { /* initialize SDL first */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { Debug() << "Error initializing SDL:" << SDL_GetError(); return 0; } if (!EventThread::allocUserEvents()) { Debug() << "Error allocating SDL user events"; return 0; } #ifndef WORKDIR_CURRENT /* set working directory */ char *dataDir = SDL_GetBasePath(); if (dataDir) { int result = chdir(dataDir); (void)result; SDL_free(dataDir); } #endif /* now we load the config */ Config conf; conf.read(argc, argv); conf.readGameINI(); assert(conf.rgssVersion >= 1 && conf.rgssVersion <= 3); printRgssVersion(conf.rgssVersion); int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG; if (IMG_Init(imgFlags) != imgFlags) { Debug() << "Error initializing SDL_image:" << SDL_GetError(); SDL_Quit(); return 0; } if (TTF_Init() < 0) { Debug() << "Error initializing SDL_ttf:" << SDL_GetError(); IMG_Quit(); SDL_Quit(); return 0; } if (Sound_Init() == 0) { Debug() << "Error initializing SDL_sound:" << Sound_GetError(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } SDL_SetHint("SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS", "0"); SDL_Window *win; Uint32 winFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS; if (conf.winResizable) winFlags |= SDL_WINDOW_RESIZABLE; if (conf.fullscreen) winFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; win = SDL_CreateWindow(conf.game.title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, conf.defScreenW, conf.defScreenH, winFlags); if (!win) { Debug() << "Error creating window:" << SDL_GetError(); return 0; } if (!conf.iconPath.empty()) { SDL_Surface *iconImg = IMG_Load(conf.iconPath.c_str()); if (iconImg) { SDL_SetWindowIcon(win, iconImg); SDL_FreeSurface(iconImg); } } EventThread eventThread; RGSSThreadData rtData(&eventThread, argv[0], win, conf); /* Load and post key bindings */ rtData.bindingUpdateMsg.post(loadBindings(conf)); /* Start RGSS thread */ SDL_Thread *rgssThread = SDL_CreateThread(rgssThreadFun, "rgss", &rtData); /* Start event processing */ eventThread.process(rtData); /* Request RGSS thread to stop */ rtData.rqTerm.set(); /* Wait for RGSS thread response */ for (int i = 0; i < 1000; ++i) { /* We can stop waiting when the request was ack'd */ if (rtData.rqTermAck) { Debug() << "RGSS thread ack'd request after" << i*10 << "ms"; break; } /* Give RGSS thread some time to respond */ SDL_Delay(10); } /* If RGSS thread ack'd request, wait for it to shutdown, * otherwise abandon hope and just end the process as is. */ if (rtData.rqTermAck) SDL_WaitThread(rgssThread, 0); else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.game.title.c_str(), "The RGSS script seems to be stuck and mkxp will now force quit", win); if (!rtData.rgssErrorMsg.empty()) { Debug() << rtData.rgssErrorMsg; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.game.title.c_str(), rtData.rgssErrorMsg.c_str(), win); } /* Clean up any remainin events */ eventThread.cleanup(); /* Store key bindings */ BDescVec keyBinds; rtData.bindingUpdateMsg.get(keyBinds); storeBindings(keyBinds, rtData.config); Debug() << "Shutting down."; SDL_DestroyWindow(win); Sound_Quit(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; }
SDL_Surface * SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) { SDL_DisplayMode desktop_mode; SDL_DisplayMode mode; int window_x = SDL_WINDOWPOS_UNDEFINED; int window_y = SDL_WINDOWPOS_UNDEFINED; Uint32 window_flags; Uint32 desktop_format; Uint32 desired_format; Uint32 surface_flags; if (!SDL_GetVideoDevice()) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { return NULL; } } /* See if we can simply resize the existing window and surface */ if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) { return SDL_PublicSurface; } /* Destroy existing window */ SDL_PublicSurface = NULL; if (SDL_ShadowSurface) { SDL_FreeSurface(SDL_ShadowSurface); SDL_ShadowSurface = NULL; } if (SDL_VideoSurface) { SDL_DelPaletteWatch(SDL_VideoSurface->format->palette, SDL_VideoPaletteChanged, NULL); SDL_FreeSurface(SDL_VideoSurface); SDL_VideoSurface = NULL; } if (SDL_VideoContext) { /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */ SDL_GL_DeleteContext(SDL_VideoContext); SDL_VideoContext = NULL; } if (SDL_VideoWindow) { SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y); SDL_DestroyWindow(SDL_VideoWindow); } /* Set up the event filter */ if (!SDL_GetEventFilter(NULL, NULL)) { SDL_SetEventFilter(SDL_CompatEventFilter, NULL); } /* Create a new window */ window_flags = SDL_WINDOW_SHOWN; if (flags & SDL_FULLSCREEN) { window_flags |= SDL_WINDOW_FULLSCREEN; } if (flags & SDL_OPENGL) { window_flags |= SDL_WINDOW_OPENGL; } if (flags & SDL_RESIZABLE) { window_flags |= SDL_WINDOW_RESIZABLE; } if (flags & SDL_NOFRAME) { window_flags |= SDL_WINDOW_BORDERLESS; } GetEnvironmentWindowPosition(width, height, &window_x, &window_y); SDL_SetFullscreenDisplayMode(NULL); SDL_VideoWindow = SDL_CreateWindow(wm_title, window_x, window_y, width, height, window_flags); if (!SDL_VideoWindow) { return NULL; } SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon); window_flags = SDL_GetWindowFlags(SDL_VideoWindow); surface_flags = 0; if (window_flags & SDL_WINDOW_FULLSCREEN) { surface_flags |= SDL_FULLSCREEN; } if (window_flags & SDL_WINDOW_OPENGL) { surface_flags |= SDL_OPENGL; } if (window_flags & SDL_WINDOW_RESIZABLE) { surface_flags |= SDL_RESIZABLE; } if (window_flags & SDL_WINDOW_BORDERLESS) { surface_flags |= SDL_NOFRAME; } /* Set up the desired display mode */ SDL_GetDesktopDisplayMode(&desktop_mode); desktop_format = desktop_mode.format; if (desktop_format && ((flags & SDL_ANYFORMAT) || (bpp == SDL_BITSPERPIXEL(desktop_format)))) { desired_format = desktop_format; } else { switch (bpp) { case 0: if (desktop_format) { desired_format = desktop_format; } else { desired_format = SDL_PIXELFORMAT_RGB888; } bpp = SDL_BITSPERPIXEL(desired_format); break; case 8: desired_format = SDL_PIXELFORMAT_INDEX8; break; case 15: desired_format = SDL_PIXELFORMAT_RGB555; break; case 16: desired_format = SDL_PIXELFORMAT_RGB565; break; case 24: desired_format = SDL_PIXELFORMAT_RGB24; break; case 32: desired_format = SDL_PIXELFORMAT_RGB888; break; default: SDL_SetError("Unsupported bpp in SDL_SetVideoMode()"); return NULL; } } mode.format = desired_format; mode.w = width; mode.h = height; mode.refresh_rate = 0; /* Set the desired display mode */ if (flags & SDL_FULLSCREEN) { if (SDL_SetFullscreenDisplayMode(&mode) < 0) { return NULL; } } /* If we're in OpenGL mode, just create a stub surface and we're done! */ if (flags & SDL_OPENGL) { SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow); if (!SDL_VideoContext) { return NULL; } if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) { return NULL; } SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0); if (!SDL_VideoSurface) { return NULL; } SDL_VideoSurface->flags |= surface_flags; SDL_PublicSurface = SDL_VideoSurface; return SDL_PublicSurface; } /* Create a renderer for the window */ if (SDL_CreateRenderer (SDL_VideoWindow, -1, SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0) { return NULL; } SDL_GetRendererInfo(&SDL_VideoRendererInfo); /* Create a texture for the screen surface */ SDL_VideoTexture = SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width, height); if (!SDL_VideoTexture) { SDL_VideoTexture = SDL_CreateTexture(desktop_format, SDL_TEXTUREACCESS_STREAMING, width, height); } if (!SDL_VideoTexture) { return NULL; } /* Create the screen surface */ SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture); if (!SDL_VideoSurface) { return NULL; } SDL_VideoSurface->flags |= surface_flags; /* Set a default screen palette */ if (SDL_VideoSurface->format->palette) { SDL_VideoSurface->flags |= SDL_HWPALETTE; SDL_DitherColors(SDL_VideoSurface->format->palette->colors, SDL_VideoSurface->format->BitsPerPixel); SDL_AddPaletteWatch(SDL_VideoSurface->format->palette, SDL_VideoPaletteChanged, SDL_VideoSurface); SDL_SetPaletteColors(SDL_VideoSurface->format->palette, SDL_VideoSurface->format->palette->colors, 0, SDL_VideoSurface->format->palette->ncolors); } /* Create a shadow surface if necessary */ if ((bpp != SDL_VideoSurface->format->BitsPerPixel) && !(flags & SDL_ANYFORMAT)) { SDL_ShadowSurface = SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0); if (!SDL_ShadowSurface) { return NULL; } SDL_ShadowSurface->flags |= surface_flags; /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */ if (SDL_ShadowSurface->format->palette) { SDL_ShadowSurface->flags |= SDL_HWPALETTE; if (SDL_VideoSurface->format->palette) { SDL_SetSurfacePalette(SDL_ShadowSurface, SDL_VideoSurface->format->palette); } else { SDL_DitherColors(SDL_ShadowSurface->format->palette->colors, SDL_ShadowSurface->format->BitsPerPixel); } SDL_AddPaletteWatch(SDL_ShadowSurface->format->palette, SDL_VideoPaletteChanged, SDL_ShadowSurface); } } SDL_PublicSurface = (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface); SDL_VideoFlags = flags; ClearVideoSurface(); SetupScreenSaver(flags); /* We're finally done! */ return SDL_PublicSurface; }
/* =============== GLimp_SetMode =============== */ static int GLimp_SetMode( int mode, qboolean fullscreen, qboolean noborder ) { const char *glstring; int perChannelColorBits; int alphaBits, depthBits, stencilBits; int samples; int i = 0; SDL_Surface *icon = NULL; SDL_DisplayMode desktopMode; Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL; int x, y; GLenum glewResult; ri.Printf( PRINT_ALL, "Initializing OpenGL display\n" ); if ( r_allowResize->integer ) { flags |= SDL_WINDOW_RESIZABLE; } if ( r_centerWindow->integer ) { // center window on specified display x = SDL_WINDOWPOS_CENTERED_DISPLAY( r_displayIndex->integer ); y = SDL_WINDOWPOS_CENTERED_DISPLAY( r_displayIndex->integer ); } else { x = SDL_WINDOWPOS_UNDEFINED_DISPLAY( r_displayIndex->integer ); y = SDL_WINDOWPOS_UNDEFINED_DISPLAY( r_displayIndex->integer ); } icon = SDL_CreateRGBSurfaceFrom( ( void * ) CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); if ( SDL_GetDesktopDisplayMode( r_displayIndex->integer, &desktopMode ) == 0 ) { displayAspect = ( float ) desktopMode.w / ( float ) desktopMode.h; ri.Printf( PRINT_ALL, "Display aspect: %.3f\n", displayAspect ); } else { Com_Memset( &desktopMode, 0, sizeof( SDL_DisplayMode ) ); ri.Printf( PRINT_ALL, "Cannot determine display aspect (%s), assuming 1.333\n", SDL_GetError() ); } ri.Printf( PRINT_ALL, "...setting mode %d:", mode ); if ( mode == -2 ) { // use desktop video resolution if ( desktopMode.h > 0 ) { glConfig.vidWidth = desktopMode.w; glConfig.vidHeight = desktopMode.h; } else { glConfig.vidWidth = 640; glConfig.vidHeight = 480; ri.Printf( PRINT_ALL, "Cannot determine display resolution, assuming 640x480\n" ); } glConfig.windowAspect = ( float ) glConfig.vidWidth / ( float ) glConfig.vidHeight; } else if ( !R_GetModeInfo( &glConfig.vidWidth, &glConfig.vidHeight, &glConfig.windowAspect, mode ) ) { ri.Printf( PRINT_ALL, " invalid mode\n" ); return RSERR_INVALID_MODE; } ri.Printf( PRINT_ALL, " %d %d\n", glConfig.vidWidth, glConfig.vidHeight ); do { if ( glContext != NULL ) { SDL_GL_DeleteContext( glContext ); glContext = NULL; } if ( window != NULL ) { SDL_GetWindowPosition( window, &x, &y ); ri.Printf( PRINT_DEVELOPER, "Existing window at %dx%d before being destroyed\n", x, y ); SDL_DestroyWindow( window ); window = NULL; } // we come back here if we couldn't get a visual and there's // something we can switch off if ( fullscreen ) { flags |= SDL_WINDOW_FULLSCREEN; glConfig.isFullscreen = qtrue; } else { if ( noborder ) { flags |= SDL_WINDOW_BORDERLESS; } glConfig.isFullscreen = qfalse; } colorBits = r_colorbits->integer; if ( ( !colorBits ) || ( colorBits >= 32 ) ) { colorBits = 24; } alphaBits = r_alphabits->integer; if ( alphaBits < 0 ) { alphaBits = 0; } depthBits = r_depthbits->integer; if ( !depthBits ) { depthBits = 24; } stencilBits = r_stencilbits->integer; samples = r_ext_multisample->integer; for ( i = 0; i < 16; i++ ) { int testColorBits, testDepthBits, testStencilBits; // 0 - default // 1 - minus colorbits // 2 - minus depthbits // 3 - minus stencil if ( ( i % 4 ) == 0 && i ) { // one pass, reduce switch ( i / 4 ) { case 2: if ( colorBits == 24 ) { colorBits = 16; } break; case 1: if ( depthBits == 24 ) { depthBits = 16; } else if ( depthBits == 16 ) { depthBits = 8; } case 3: if ( stencilBits == 24 ) { stencilBits = 16; } else if ( stencilBits == 16 ) { stencilBits = 8; } } } testColorBits = colorBits; testDepthBits = depthBits; testStencilBits = stencilBits; if ( ( i % 4 ) == 3 ) { // reduce colorbits if ( testColorBits == 24 ) { testColorBits = 16; } } if ( ( i % 4 ) == 2 ) { // reduce depthbits if ( testDepthBits == 24 ) { testDepthBits = 16; } else if ( testDepthBits == 16 ) { testDepthBits = 8; } } if ( ( i % 4 ) == 1 ) { // reduce stencilbits if ( testStencilBits == 24 ) { testStencilBits = 16; } else if ( testStencilBits == 16 ) { testStencilBits = 8; } else { testStencilBits = 0; } } if ( testColorBits == 24 ) { perChannelColorBits = 8; } else { perChannelColorBits = 4; } SDL_GL_SetAttribute( SDL_GL_RED_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, alphaBits ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, testDepthBits ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, testStencilBits ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); #if !SDL_VERSION_ATLEAST( 2, 0, 0 ) SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, r_swapInterval->integer ); #endif #if SDL_VERSION_ATLEAST( 2, 0, 0 ) if ( !r_glAllowSoftware->integer ) { SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ); } if ( r_glCoreProfile->integer || r_glDebugProfile->integer ) { int major = r_glMajorVersion->integer; int minor = r_glMinorVersion->integer; SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, major ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, minor ); if ( r_glCoreProfile->integer ) { SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); } else { SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY ); } if ( r_glDebugProfile->integer ) { SDL_GL_SetAttribute( SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG ); } } #endif window = SDL_CreateWindow( CLIENT_WINDOW_TITLE, x, y, glConfig.vidWidth, glConfig.vidHeight, flags ); if ( !window ) { ri.Printf( PRINT_DEVELOPER, "SDL_CreateWindow failed: %s\n", SDL_GetError() ); continue; } SDL_SetWindowIcon( window, icon ); glContext = SDL_GL_CreateContext( window ); if ( !glContext ) { ri.Printf( PRINT_DEVELOPER, "SDL_GL_CreateContext failed: %s\n", SDL_GetError() ); continue; } #if SDL_VERSION_ATLEAST( 2, 0, 0 ) SDL_GL_SetSwapInterval( r_swapInterval->integer ); #endif SDL_ShowCursor( 0 ); glConfig.colorBits = testColorBits; glConfig.depthBits = testDepthBits; glConfig.stencilBits = testStencilBits; ri.Printf( PRINT_ALL, "Using %d Color bits, %d depth, %d stencil display.\n", glConfig.colorBits, glConfig.depthBits, glConfig.stencilBits ); break; } if ( samples && ( !glContext || !window ) ) { r_ext_multisample->integer = 0; } } while ( ( !glContext || !window ) && samples ); SDL_FreeSurface( icon ); glewResult = glewInit(); if ( glewResult != GLEW_OK ) { // glewInit failed, something is seriously wrong ri.Error( ERR_FATAL, "GLW_StartOpenGL() - could not load OpenGL subsystem: %s", glewGetErrorString( glewResult ) ); } else { ri.Printf( PRINT_ALL, "Using GLEW %s\n", glewGetString( GLEW_VERSION ) ); } int GLmajor, GLminor; sscanf( ( const char * ) glGetString( GL_VERSION ), "%d.%d", &GLmajor, &GLminor ); if ( GLmajor < 2 || ( GLmajor == 2 && GLminor < 1 ) ) { // missing shader support, switch to 1.x renderer return RSERR_OLD_GL; } if ( GLmajor < 3 || ( GLmajor == 3 && GLminor < 2 ) ) { // shaders are supported, but not all GL3.x features ri.Printf( PRINT_ALL, "Using enhanced (GL3) Renderer in GL 2.x mode...\n" ); } else { ri.Printf( PRINT_ALL, "Using enhanced (GL3) Renderer in GL 3.x mode...\n" ); glConfig.driverType = GLDRV_OPENGL3; } #if defined( SMP ) && !SDL_VERSION_ATLEAST( 2, 0, 0 ) // setup context for SDL_GL_MakeCurrent SDL_GL_GetCurrentContext(); #endif GLimp_DetectAvailableModes(); glstring = ( char * ) glGetString( GL_RENDERER ); ri.Printf( PRINT_ALL, "GL_RENDERER: %s\n", glstring ); return RSERR_OK; }
void ZWindow::SetWindowIcon(SDL_Surface *ico) { SDL_SetWindowIcon(m_win,ico); }
bool Window::initialize() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_EVENTS) != 0) { Log::instance()->logError("Unable to initialize SDL\n%s", SDL_GetError()); return false; } const GameHeader& gameHeader = System::getGameHeader(); bool bFullscreen = gameHeader.bFullScreen; if (true == bFullscreen) { SDL_DisplayMode displayMode; if (SDL_GetCurrentDisplayMode(0, &displayMode) != 0) { Log::instance()->logError("Unable to get current display mode\n%s", SDL_GetError()); return false; } m_iWidth = displayMode.w; m_iHeight = displayMode.h; } else { m_iWidth = gameHeader.iWindowedWidth; m_iHeight = gameHeader.iWindowedHeight; } int iFlags = SDL_WINDOW_OPENGL; if (true == gameHeader.bFullScreen) { iFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; } else { iFlags |= SDL_WINDOW_RESIZABLE; } if (SDL_CreateWindowAndRenderer(m_iWidth, m_iHeight, iFlags, &m_pWindow, &m_pRenderer) != 0) { Log::instance()->logError("Unable to create windows and renderer\n%s", SDL_GetError()); return false; } if (false == checkRenderer()) { return false; } if (true == bFullscreen) { showMouse(false); } SDL_SetWindowTitle(m_pWindow, gameHeader.strAppName.c_str()); // Create app icon Image* pImage = Image::create("AppIcon.png"); if (pImage != NULL) { SDL_Surface* pIconSurface = SDL_CreateRGBSurfaceFrom(pImage->getPixels(), pImage->getWidth(), pImage->getHeight(), 32, pImage->getWidth() * 4, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); if (pIconSurface != NULL) { SDL_SetWindowIcon(m_pWindow, pIconSurface); SDL_FreeSurface(pIconSurface); } delete pImage; } SDL_DisableScreenSaver(); return true; }
/* =============== GLimp_SetMode =============== */ static rserr_t GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder) { const char *glstring; int perChannelColorBits; int colorBits, depthBits, stencilBits; int samples; int i = 0; SDL_Surface *icon = NULL; Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL; SDL_DisplayMode desktopMode; int display = 0; int x = 0, y = 0; Com_Printf( "Initializing OpenGL display\n"); if ( r_allowResize->integer && !fullscreen ) flags |= SDL_WINDOW_RESIZABLE; /*icon = SDL_CreateRGBSurfaceFrom( (void *)CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif );*/ // If a window exists, note its display index if( screen != NULL ) display = SDL_GetWindowDisplayIndex( screen ); if( SDL_GetDesktopDisplayMode( display, &desktopMode ) == 0 ) { displayAspect = (float)desktopMode.w / (float)desktopMode.h; Com_Printf( "Display aspect: %.3f\n", displayAspect ); } else { Com_Memset( &desktopMode, 0, sizeof( SDL_DisplayMode ) ); Com_Printf( "Cannot determine display aspect, assuming 1.333\n" ); } Com_Printf( "...setting mode %d:", mode ); if (mode == -2) { // use desktop video resolution if( desktopMode.h > 0 ) { glConfig.vidWidth = desktopMode.w; glConfig.vidHeight = desktopMode.h; } else { glConfig.vidWidth = 640; glConfig.vidHeight = 480; Com_Printf( "Cannot determine display resolution, assuming 640x480\n" ); } //glConfig.windowAspect = (float)glConfig.vidWidth / (float)glConfig.vidHeight; } else if ( !R_GetModeInfo( &glConfig.vidWidth, &glConfig.vidHeight, /*&glConfig.windowAspect,*/ mode ) ) { Com_Printf( " invalid mode\n" ); return RSERR_INVALID_MODE; } Com_Printf( " %d %d\n", glConfig.vidWidth, glConfig.vidHeight); // Center window if( r_centerWindow->integer && !fullscreen ) { x = ( desktopMode.w / 2 ) - ( glConfig.vidWidth / 2 ); y = ( desktopMode.h / 2 ) - ( glConfig.vidHeight / 2 ); } // Destroy existing state if it exists if( opengl_context != NULL ) { SDL_GL_DeleteContext( opengl_context ); opengl_context = NULL; } if( screen != NULL ) { SDL_GetWindowPosition( screen, &x, &y ); ri.Printf( PRINT_DEVELOPER, "Existing window at %dx%d before being destroyed\n", x, y ); SDL_DestroyWindow( screen ); screen = NULL; } if( fullscreen ) { flags |= SDL_WINDOW_FULLSCREEN; glConfig.isFullscreen = qtrue; } else { if( noborder ) flags |= SDL_WINDOW_BORDERLESS; glConfig.isFullscreen = qfalse; } colorBits = r_colorbits->value; if ((!colorBits) || (colorBits >= 32)) colorBits = 24; if (!r_depthbits->value) depthBits = 24; else depthBits = r_depthbits->value; stencilBits = r_stencilbits->value; //samples = r_ext_multisample->value; for (i = 0; i < 16; i++) { int testColorBits, testDepthBits, testStencilBits; // 0 - default // 1 - minus colorBits // 2 - minus depthBits // 3 - minus stencil if ((i % 4) == 0 && i) { // one pass, reduce switch (i / 4) { case 2 : if (colorBits == 24) colorBits = 16; break; case 1 : if (depthBits == 24) depthBits = 16; else if (depthBits == 16) depthBits = 8; case 3 : if (stencilBits == 24) stencilBits = 16; else if (stencilBits == 16) stencilBits = 8; } } testColorBits = colorBits; testDepthBits = depthBits; testStencilBits = stencilBits; if ((i % 4) == 3) { // reduce colorBits if (testColorBits == 24) testColorBits = 16; } if ((i % 4) == 2) { // reduce depthBits if (testDepthBits == 24) testDepthBits = 16; else if (testDepthBits == 16) testDepthBits = 8; } if ((i % 4) == 1) { // reduce stencilBits if (testStencilBits == 24) testStencilBits = 16; else if (testStencilBits == 16) testStencilBits = 8; else testStencilBits = 0; } if (testColorBits == 24) perChannelColorBits = 8; else perChannelColorBits = 4; #ifdef __sgi /* Fix for SGIs grabbing too many bits of color */ if (perChannelColorBits == 4) perChannelColorBits = 0; /* Use minimum size for 16-bit color */ /* Need alpha or else SGIs choose 36+ bit RGB mode */ SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 1); #endif SDL_GL_SetAttribute( SDL_GL_RED_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, testDepthBits ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, testStencilBits ); /*SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples );*/ if(r_stereo->integer) { glConfig.stereoEnabled = qtrue; SDL_GL_SetAttribute(SDL_GL_STEREO, 1); } else { glConfig.stereoEnabled = qfalse; SDL_GL_SetAttribute(SDL_GL_STEREO, 0); } SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // If not allowing software GL, demand accelerated if( !r_allowSoftwareGL->integer ) SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ); if( ( screen = SDL_CreateWindow( CLIENT_WINDOW_TITLE, x, y, glConfig.vidWidth, glConfig.vidHeight, flags ) ) == 0 ) { ri.Printf( PRINT_DEVELOPER, "SDL_CreateWindow failed: %s\n", SDL_GetError( ) ); continue; } if( fullscreen ) { SDL_DisplayMode mode; switch( testColorBits ) { case 16: mode.format = SDL_PIXELFORMAT_RGB565; break; case 24: mode.format = SDL_PIXELFORMAT_RGB24; break; default: ri.Printf( PRINT_DEVELOPER, "testColorBits is %d, can't fullscreen\n", testColorBits ); continue; } mode.w = glConfig.vidWidth; mode.h = glConfig.vidHeight; mode.refresh_rate = glConfig.displayFrequency = ri.Cvar_VariableIntegerValue( "r_displayRefresh" ); mode.driverdata = NULL; if( SDL_SetWindowDisplayMode( screen, &mode ) < 0 ) { ri.Printf( PRINT_DEVELOPER, "SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError( ) ); continue; } } SDL_SetWindowTitle( screen, CLIENT_WINDOW_TITLE ); SDL_SetWindowIcon( screen, icon ); if( ( opengl_context = (QGLContext)SDL_GL_CreateContext( screen ) ) == NULL ) { Com_Printf( "SDL_GL_CreateContext failed: %s\n", SDL_GetError( ) ); continue; } if( SDL_GL_MakeCurrent( screen, opengl_context ) < 0 ) { Com_Printf( "SDL_GL_MakeCurrent failed: %s\n", SDL_GetError( ) ); continue; } SDL_GL_SetSwapInterval( r_swapInterval->integer ); glConfig.colorBits = testColorBits; glConfig.depthBits = testDepthBits; glConfig.stencilBits = testStencilBits; Com_Printf( "Using %d color bits, %d depth, %d stencil display.\n", glConfig.colorBits, glConfig.depthBits, glConfig.stencilBits ); break; } /*SDL_FreeSurface( icon );*/ GLimp_DetectAvailableModes(); glstring = (char *) qglGetString (GL_RENDERER); Com_Printf( "GL_RENDERER: %s\n", glstring ); return RSERR_OK; #if 0 const char* glstring; int sdlcolorbits; int colorbits, depthbits, stencilbits; int tcolorbits, tdepthbits, tstencilbits; int samples; int i = 0; Uint32 flags = SDL_WINDOW_OPENGL; Com_Printf( "Initializing OpenGL display\n"); if ( r_allowResize->integer ) flags |= SDL_WINDOW_RESIZABLE; if( videoInfo == NULL ) { static SDL_DisplayMode sVideoInfo; static SDL_PixelFormat sPixelFormat; if (SDL_GetCurrentDisplayMode( 0, &sVideoInfo ) < 0) Com_Error(ERR_FATAL, "SDL_GetCurrentDisplayMode failed : %s\n", SDL_GetError()); // Take a copy of the videoInfo sPixelFormat.format = sVideoInfo.format; sPixelFormat.palette = NULL; // Should already be the case //Com_Memcpy( &sVideoInfo, videoInfo, sizeof( SDL_DisplayMode ) ); sVideoInfo.format = sPixelFormat.format; videoInfo = &sVideoInfo; if( videoInfo->h > 0 ) { glConfig.displayWidth = videoInfo->w; glConfig.displayHeight = videoInfo->h; // Guess the display aspect ratio through the desktop resolution // by assuming (relatively safely) that it is set at or close to // the display's native aspect ratio glConfig.displayAspect = (float)videoInfo->w / (float)videoInfo->h; Com_Printf( "Estimated display aspect: %.3f\n", glConfig.displayAspect ); } else { glConfig.displayWidth = 480; glConfig.displayHeight = 640; glConfig.displayAspect = 1.333f; Com_Printf( "Cannot estimate display resolution/aspect, assuming 640x480/1.333\n" ); } } Com_Printf( "...setting mode %d:", mode ); if (mode == -2) { // use desktop video resolution if( videoInfo->h > 0 ) { glConfig.vidWidth = videoInfo->w; glConfig.vidHeight = videoInfo->h; } else { glConfig.vidWidth = 640; glConfig.vidHeight = 480; Com_Printf( "Cannot determine display resolution, assuming 640x480\n" ); } glConfig.displayAspect = (float)glConfig.vidWidth / (float)glConfig.vidHeight; } else if ( !R_GetModeInfo( &glConfig.vidWidth, &glConfig.vidHeight, /*&glConfig.displayAspect,*/ mode ) ) { Com_Printf( " invalid mode\n" ); return RSERR_INVALID_MODE; } Com_Printf( " %d %d\n", glConfig.vidWidth, glConfig.vidHeight); if (fullscreen) { flags |= SDL_WINDOW_FULLSCREEN; glConfig.isFullscreen = qtrue; } else { if (noborder) flags |= SDL_WINDOW_BORDERLESS; glConfig.isFullscreen = qfalse; } colorbits = r_colorbits->value; if ((!colorbits) || (colorbits >= 32)) colorbits = 24; if (!r_depthbits->value) depthbits = 24; else depthbits = r_depthbits->value; stencilbits = r_stencilbits->value; //samples = r_ext_multisample->value; for (i = 0; i < 16; i++) { // 0 - default // 1 - minus colorbits // 2 - minus depthbits // 3 - minus stencil if ((i % 4) == 0 && i) { // one pass, reduce switch (i / 4) { case 2 : if (colorbits == 24) colorbits = 16; break; case 1 : if (depthbits == 24) depthbits = 16; else if (depthbits == 16) depthbits = 8; case 3 : if (stencilbits == 24) stencilbits = 16; else if (stencilbits == 16) stencilbits = 8; } } tcolorbits = colorbits; tdepthbits = depthbits; tstencilbits = stencilbits; if ((i % 4) == 3) { // reduce colorbits if (tcolorbits == 24) tcolorbits = 16; } if ((i % 4) == 2) { // reduce depthbits if (tdepthbits == 24) tdepthbits = 16; else if (tdepthbits == 16) tdepthbits = 8; } if ((i % 4) == 1) { // reduce stencilbits if (tstencilbits == 24) tstencilbits = 16; else if (tstencilbits == 16) tstencilbits = 8; else tstencilbits = 0; } sdlcolorbits = 4; if (tcolorbits == 24) sdlcolorbits = 8; #ifdef __sgi /* Fix for SGIs grabbing too many bits of color */ if (sdlcolorbits == 4) sdlcolorbits = 0; /* Use minimum size for 16-bit color */ /* Need alpha or else SGIs choose 36+ bit RGB mode */ SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 1); #endif SDL_GL_SetAttribute( SDL_GL_RED_SIZE, sdlcolorbits ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, sdlcolorbits ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, sdlcolorbits ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, tdepthbits ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, tstencilbits ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples ); /*if(r_stereoEnabled->integer) { glConfig.stereoEnabled = qtrue; SDL_GL_SetAttribute(SDL_GL_STEREO, 1); } else {*/ glConfig.stereoEnabled = qfalse; SDL_GL_SetAttribute(SDL_GL_STEREO, 0); //} SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); #if 0 // See http://bugzilla.icculus.org/show_bug.cgi?id=3526 // If not allowing software GL, demand accelerated if( !r_allowSoftwareGL->integer ) { if( SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ) < 0 ) { Com_Printf( "Unable to guarantee accelerated " "visual with libSDL < 1.2.10\n" ); } } #endif if( SDL_GL_SetSwapInterval(r_swapInterval->integer ) < 0 ) Com_Printf( "SDL_GL_SetSwapInterval not supported\n" ); #ifdef USE_ICON { SDL_Surface *icon = SDL_CreateRGBSurfaceFrom( (void *)CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); SDL_WM_SetIcon( icon, NULL ); SDL_FreeSurface( icon ); } #endif SDL_ShowCursor(0); if (!(window = SDL_CreateWindow(CLIENT_WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, glConfig.vidWidth, glConfig.vidHeight, flags))) { Com_Printf( "SDL_CreateWindow failed: %s\n", SDL_GetError( ) ); continue; } opengl_context = SDL_GL_CreateContext(window); Com_Printf( "Using %d/%d/%d Color bits, %d depth, %d stencil display.\n", sdlcolorbits, sdlcolorbits, sdlcolorbits, tdepthbits, tstencilbits); glConfig.colorBits = tcolorbits; glConfig.depthBits = tdepthbits; glConfig.stencilBits = tstencilbits; break; } GLimp_DetectAvailableModes(); if (!window) { Com_Printf( "Couldn't get a visual\n" ); return RSERR_INVALID_MODE; } screen = window; // FIXME: Defines needed here glstring = (char *) qglGetString (GL_RENDERER); Com_Printf( "GL_RENDERER: %s\n", glstring ); return RSERR_OK; #endif }
void Window::SetIcon(const Surface& pSurface) { SDL_SetWindowIcon(mWindow, pSurface.GetC()); }
//----------------------------------------------------------------- // Game Engine General Methods //----------------------------------------------------------------- bool GameEngine::Initialize(const char* szTitle, const char* szIcon, int iWidth, int iHeight) { // Set the member variables for the game engine m_pGameEngine = this; m_hWindow = NULL; if (strlen(szTitle) > 0) strcpy(m_szTitle, szTitle); if (strlen(szTitle) > 0) strcpy(m_szIcon, szIcon); m_iWidth = iWidth; m_iHeight = iHeight; m_iFrameDelay = 50; // 20 FPS default m_bSleep = true; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER); // Initialize SDL2 //Initialize SDL2 Mixer if (Mix_OpenAudio(22050, AUDIO_U8, 2, 512) < 0){ std::cout << "Unable to init sound engine: " << SDL_GetError() << std::endl; } //Initialize SDL2 TTF if(TTF_Init()==-1) { std::cout << "TTF_Init: " << TTF_GetError() << std::endl; exit(EXIT_FAILURE); } //create the window m_hWindow = SDL_CreateWindow(m_szTitle, // window title SDL_WINDOWPOS_UNDEFINED, // initial x position SDL_WINDOWPOS_UNDEFINED, // initial y position m_iWidth, // width, in pixels m_iHeight, // height, in pixels SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC // flags ); if (!m_hWindow) return false; //set the window icon from the .ico file // The icon is attached to the window pointer SDL_Surface *icone_surface = IMG_Load(szIcon); SDL_SetWindowIcon(m_hWindow, icone_surface); //the surface containing the icon pixel data is no longer required. SDL_FreeSurface(icone_surface); //Create a Renderer m_renderer = SDL_CreateRenderer(m_hWindow, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); //Clear the screen SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255); SDL_RenderClear(m_renderer); SDL_RenderPresent(m_renderer); return true; }
REALIGN STDCALL SDL_Window *WrapperCreateWindow(WindowProc windowProc) { static const uint32_t palette[8] = {0xFF000000, 0xFF000080, 0xFF0000FF, 0xFFC0C0C0, 0xFF00FFFF, 0xFFFFFFFF, 0x00000000, 0xFF008080}; static const uint8_t compressed_icon[372] = { 0x90, 0x00, 0x12, 0x22, 0x33, 0x83, 0x34, 0x86, 0x44, 0x45, 0x82, 0x55, 0x51, 0x83, 0x00, 0x11, 0x83, 0x22, 0x24, 0x42, 0x01, 0x12, 0x82, 0x22, 0x12, 0x44, 0x10, 0x82, 0x66, 0x60, 0x12, 0x83, 0x22, 0x24, 0x41, 0x01, 0x84, 0x22, 0x44, 0x06, 0x82, 0x66, 0x60, 0x12, 0x83, 0x22, 0x24, 0x31, 0x01, 0x84, 0x22, 0x43, 0x06, 0x82, 0x66, 0x60, 0x84, 0x22, 0x34, 0x20, 0x02, 0x83, 0x22, 0x23, 0x42, 0x06, 0x82, 0x66, 0x60, 0x84, 0x22, 0x34, 0x10, 0x02, 0x83, 0x22, 0x23, 0x41, 0x83, 0x66, 0x01, 0x84, 0x22, 0x44, 0x10, 0x12, 0x83, 0x22, 0x24, 0x40, 0x83, 0x66, 0x01, 0x84, 0x22, 0x43, 0x00, 0x12, 0x83, 0x22, 0x24, 0x20, 0x83, 0x66, 0x84, 0x00, 0x22, 0x42, 0x88, 0x00, 0x66, 0x00, 0x73, 0x82, 0x55, 0x43, 0x70, 0x21, 0x00, 0x74, 0x44, 0x45, 0x83, 0x55, 0x47, 0x60, 0x73, 0x84, 0x44, 0x53, 0x70, 0x00, 0x74, 0x85, 0x44, 0x70, 0x07, 0x82, 0x44, 0x30, 0x00, 0x34, 0x44, 0x47, 0x00, 0x34, 0x44, 0x84, 0x33, 0x06, 0x07, 0x44, 0x43, 0x82, 0x00, 0x07, 0x82, 0x44, 0x07, 0x34, 0x44, 0x70, 0x83, 0x00, 0x66, 0x07, 0x34, 0x44, 0x43, 0x37, 0x83, 0x00, 0x07, 0x82, 0x44, 0x84, 0x00, 0x06, 0x60, 0x73, 0x34, 0x82, 0x44, 0x43, 0x70, 0x00, 0x07, 0x44, 0x45, 0x83, 0x55, 0x44, 0x30, 0x66, 0x07, 0x77, 0x33, 0x82, 0x44, 0x43, 0x00, 0x03, 0x85, 0x44, 0x43, 0x06, 0x84, 0x00, 0x07, 0x34, 0x44, 0x30, 0x73, 0x44, 0x43, 0x84, 0x00, 0x66, 0x34, 0x44, 0x47, 0x82, 0x00, 0x03, 0x44, 0x30, 0x74, 0x44, 0x40, 0x84, 0x00, 0x66, 0x74, 0x82, 0x44, 0x37, 0x77, 0x34, 0x44, 0x70, 0x34, 0x85, 0x44, 0x43, 0x06, 0x07, 0x34, 0x84, 0x44, 0x43, 0x07, 0x34, 0x85, 0x44, 0x30, 0x66, 0x60, 0x77, 0x34, 0x82, 0x44, 0x33, 0x70, 0x07, 0x86, 0x33, 0x70, 0x82, 0x66, 0x85, 0x00, 0x01, 0x87, 0x00, 0x06, 0x82, 0x66, 0x02, 0x84, 0x22, 0x42, 0x00, 0x84, 0x22, 0x24, 0x20, 0x83, 0x66, 0x02, 0x84, 0x22, 0x42, 0x01, 0x84, 0x22, 0x24, 0x10, 0x82, 0x66, 0x60, 0x12, 0x83, 0x22, 0x23, 0x41, 0x01, 0x84, 0x22, 0x34, 0x06, 0x82, 0x66, 0x60, 0x84, 0x22, 0x23, 0x41, 0x02, 0x84, 0x22, 0x34, 0x06, 0x82, 0x66, 0x60, 0x84, 0x22, 0x24, 0x30, 0x02, 0x84, 0x22, 0x43, 0x06, 0x82, 0x66, 0x60, 0x84, 0x22, 0x24, 0x20, 0x02, 0x84, 0x22, 0x42, 0x06, 0x82, 0x66, 0x01, 0x84, 0x22, 0x24, 0x10, 0x01, 0x84, 0x22, 0x41, 0x83, 0x00, 0x14, 0x82, 0x54, 0x85, 0x44, 0x84, 0x43, 0x33, 0x82, 0x22, 0x20, 0x90, 0x00 }; uint32_t *icon, i, j; sdlWin = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winWidth, winHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (startInFullScreen ? fullScreenFlag : 0)); if (!sdlWin) { const char *error = SDL_GetError(); int bufferSize = strlen(error) + 71; char *buffer = (char *)malloc(bufferSize); snprintf(buffer, bufferSize, "Cannot create window: %s\nCheck the OpenGL drivers and the game settings!", error); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, buffer, NULL); free(buffer); exit(0); } icon = (uint32_t *)malloc(32 * 32 * 4); for (i = 0, j = 0; i < sizeof compressed_icon; ++i) { uint32_t c; if (compressed_icon[i] & 0x80) c = compressed_icon[i++] & 0x7F; else c = 1; while (c--) { icon[j++] = palette[compressed_icon[i] >> 4]; icon[j++] = palette[compressed_icon[i] & 0x0F]; } } SDL_Surface *icon_surface = SDL_CreateRGBSurfaceFrom(icon, 32, 32, 32, 128, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); SDL_SetWindowIcon(sdlWin, icon_surface); SDL_FreeSurface(icon_surface); free(icon); wndProc = windowProc; return sdlWin; }
int SetVideoMode(int w, int h, int bpp, bool gl) { int flags = SDL_WINDOW_SHOWN | SDL_WINDOW_INPUT_FOCUS; static bool last_gl = false; static int last_x = SDL_WINDOWPOS_UNDEFINED; static int last_y = SDL_WINDOWPOS_UNDEFINED; if(gl) flags |= SDL_WINDOW_OPENGL; if(savedata.fullscreen) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; if(!(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP)) SDL_GetWindowPosition(window, &last_x, &last_y); if(window && gl != last_gl) { SDL_DestroyWindow(window); window = NULL; } last_gl = gl; if(renderer) SDL_DestroyRenderer(renderer); if(texture) SDL_DestroyTexture(texture); renderer = NULL; texture = NULL; if(window) { if(savedata.fullscreen) { SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); } else { #ifndef WIN // hiding and showing the window is problematic on Windows if(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) SDL_HideWindow(window); #endif SDL_SetWindowFullscreen(window, 0); SDL_SetWindowSize(window, w, h); SDL_SetWindowPosition(window, last_x, last_y); SDL_ShowWindow(window); } } else { window = SDL_CreateWindow(windowTitle, last_x, last_y, w, h, flags); if(!window) { printf("Error: failed to create window: %s\n", SDL_GetError()); return 0; } SDL_Surface* icon = (SDL_Surface*)pngToSurface((void*)openbor_icon_32x32_png.data); SDL_SetWindowIcon(window, icon); SDL_FreeSurface(icon); if(!savedata.fullscreen) SDL_GetWindowPosition(window, &last_x, &last_y); } if(!gl) { renderer = SDL_CreateRenderer(window, -1, 0); if(!renderer) { printf("Error: failed to create renderer: %s\n", SDL_GetError()); return 0; } } return 1; }
SDL_Window *GLFuncs::Initialize(int screenWidth, int screenHeight, GLboolean fullscreen, const char* name) { Uint32 flags; if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { Log::Out << "Unable to initialize SDL: " << SDL_GetError() << endl; return NULL; } atexit(SDL_Quit); flags = SDL_WINDOW_OPENGL; _screenWidth = screenWidth; _screenHeight = screenHeight; _windowWidth = _screenWidth; _windowHeight = _screenHeight; if (fullscreen) { flags |= SDL_WINDOW_FULLSCREEN; SDL_DisplayMode currentDisplay; if(SDL_GetCurrentDisplayMode(0, ¤tDisplay)) { Log::Out << "Couldn't get current display mode: " << SDL_GetError() << endl; } else { Log::Out << "Current display mode: " << currentDisplay.w << "x" << currentDisplay.h << endl; _windowWidth = currentDisplay.w; _windowHeight = currentDisplay.h; } } Log::Out << "Window size: " << _windowWidth << "x" << _windowHeight << endl; int realW, realH; GetRealSize(&realW, &realH); Log::Out << "Real size: " << realW << "x" << realH << endl; this->StaticProjection = glm::ortho(0.0f, (float)_windowWidth, (float)_windowHeight, 0.0f, -1.0f, 1.0f); setGLAttributes(); _window = SDL_CreateWindow(name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _windowWidth, _windowHeight, flags); if (!_window) { Log::Out << "Unable to create window." << endl; checkSDLError(__LINE__); } SDL_Surface *icon = Pack::GetInstance()->GetImg("data/UWOLIcon.png"); SDL_SetWindowIcon(_window, icon); _mainContext = SDL_GL_CreateContext(_window); SDL_GL_MakeCurrent(_window, _mainContext); #ifndef __APPLE__ glewExperimental = GL_TRUE; glewInit(); #endif char* obtainedVersion = (char*)glGetString(GL_VERSION); string glVersion = getVersionFromStr(obtainedVersion); Log::Out << "OpenGL: " << obtainedVersion << endl; int version; stringstream ss(glVersion); ss >> version; #ifdef __APPLE__ this->_useShaders = true; #else this->_useShaders = (glCreateProgram != NULL); //version >= 20; #endif if (_useShaders) { this->_vtxAttribIdx = 0; this->_uvAttribIdx = 1; this->_colAttribIdx = 2; this->_glslVersion = getGLSLVersion(); Log::Out << "GLSL: " << (this->_glslVersion == "" ? "Not available!" : this->_glslVersion) << endl; } else { Log::Out << "Shaders are not available" << endl; } this->_useVBO = (glGenVertexArrays!=NULL); //version >= 20; if (this->_useVBO) { this->_useFramebuffer = this->initFramebuffer(); } glViewport(0, 0, _screenWidth, _screenHeight); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); // glShadeModel(GL_SMOOTH); glDisable(GL_CULL_FACE); //#if !ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 // //glEnable(GL_ALPHA_TEST); // //glAlphaFunc(GL_GREATER, 0.01f); // //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //#endif glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPointSize(2.0f); if (this->_useVBO) { glGenVertexArrays(1, &_vaoVertex); glBindVertexArray(_vaoVertex); glGenBuffers(1, &_vboVertex); glGenBuffers(1, &_vboUV); glGenBuffers(1, &_vboColor); glGenBuffers(1, &_vboLineVertex); } this->ResetMVP(); GLint texture_units; glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &texture_units); _activeTextures.resize(texture_units, 0); return _window; }
/* =============== GLimp_SetMode =============== */ static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder) { const char *glstring; int perChannelColorBits; int colorBits, depthBits, stencilBits; int samples; int i = 0; SDL_Surface *icon = NULL; Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL; SDL_DisplayMode desktopMode; int display = 0; int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED; char windowTitle[128]; textureLevel_t *iconPic; byte *pixelData; int bytesPerPixel, width, height; ri.Printf( PRINT_ALL, "Initializing OpenGL display\n"); ri.Cvar_VariableStringBuffer("com_productName", windowTitle, sizeof (windowTitle)); if ( r_allowResize->integer ) flags |= SDL_WINDOW_RESIZABLE; iconPic = NULL; pixelData = NULL; bytesPerPixel = 4; #ifdef USE_ICON // try to load 32 x 32 icon if ( r_forceWindowIcon32->integer ) { int numLevels; R_LoadImage( "windowicon32", &numLevels, &iconPic ); if ( iconPic && ( iconPic[0].width != 32 || iconPic[0].height != 32 ) ) { ri.Free( iconPic ); iconPic = NULL; ri.Printf( PRINT_WARNING, "Ignoring windowicon32: Image must be 32 x 32!\n"); } } else { int numLevels; // try to load high resolution icon R_LoadImage( "windowicon", &numLevels, &iconPic ); } if ( iconPic ) { pixelData = iconPic[0].data; width = iconPic[0].width; height = iconPic[0].height; } else { // fallback to default icon pixelData = (byte *)CLIENT_WINDOW_ICON.pixel_data; bytesPerPixel = CLIENT_WINDOW_ICON.bytes_per_pixel; width = CLIENT_WINDOW_ICON.width; height = CLIENT_WINDOW_ICON.height; } icon = SDL_CreateRGBSurfaceFrom( (void *)pixelData, width, height, bytesPerPixel * 8, bytesPerPixel * width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); #endif // If a window exists, note its display index if( SDL_window != NULL ) { display = SDL_GetWindowDisplayIndex( SDL_window ); if( display < 0 ) { ri.Printf( PRINT_DEVELOPER, "SDL_GetWindowDisplayIndex() failed: %s\n", SDL_GetError() ); } } if( display >= 0 && SDL_GetDesktopDisplayMode( display, &desktopMode ) == 0 ) { glConfig.displayWidth = desktopMode.w; glConfig.displayHeight = desktopMode.h; glConfig.displayAspect = (float)desktopMode.w / (float)desktopMode.h; ri.Printf( PRINT_ALL, "Display aspect: %.3f\n", glConfig.displayAspect ); } else { Com_Memset( &desktopMode, 0, sizeof( SDL_DisplayMode ) ); glConfig.displayWidth = 640; glConfig.displayHeight = 480; glConfig.displayAspect = 1.333f; ri.Printf( PRINT_ALL, "Cannot determine display aspect, assuming 1.333\n" ); } ri.Printf (PRINT_ALL, "...setting mode %d:", mode ); if (mode == -2) { // use desktop video resolution if( desktopMode.h > 0 ) { glConfig.vidWidth = desktopMode.w; glConfig.vidHeight = desktopMode.h; } else { glConfig.vidWidth = 640; glConfig.vidHeight = 480; ri.Printf( PRINT_ALL, "Cannot determine display resolution, assuming 640x480\n" ); } glConfig.windowAspect = (float)glConfig.vidWidth / (float)glConfig.vidHeight; } else if ( !R_GetModeInfo( &glConfig.vidWidth, &glConfig.vidHeight, &glConfig.windowAspect, mode ) ) { ri.Printf( PRINT_ALL, " invalid mode\n" ); return RSERR_INVALID_MODE; } ri.Printf( PRINT_ALL, " %d %d\n", glConfig.vidWidth, glConfig.vidHeight); // Center window if( r_centerWindow->integer && !fullscreen ) { x = ( desktopMode.w / 2 ) - ( glConfig.vidWidth / 2 ); y = ( desktopMode.h / 2 ) - ( glConfig.vidHeight / 2 ); } // Destroy existing state if it exists if( SDL_glContext != NULL ) { SDL_GL_DeleteContext( SDL_glContext ); SDL_glContext = NULL; } if( SDL_window != NULL ) { SDL_GetWindowPosition( SDL_window, &x, &y ); ri.Printf( PRINT_DEVELOPER, "Existing window at %dx%d before being destroyed\n", x, y ); SDL_DestroyWindow( SDL_window ); SDL_window = NULL; } if( fullscreen ) { flags |= SDL_WINDOW_FULLSCREEN; glConfig.isFullscreen = qtrue; } else { if( noborder ) flags |= SDL_WINDOW_BORDERLESS; glConfig.isFullscreen = qfalse; } colorBits = r_colorbits->value; if ((!colorBits) || (colorBits >= 32)) colorBits = 24; if (!r_depthbits->value) depthBits = 24; else depthBits = r_depthbits->value; stencilBits = r_stencilbits->value; samples = r_ext_multisample->value; for (i = 0; i < 16; i++) { int testColorBits, testDepthBits, testStencilBits; // 0 - default // 1 - minus colorBits // 2 - minus depthBits // 3 - minus stencil if ((i % 4) == 0 && i) { // one pass, reduce switch (i / 4) { case 2 : if (colorBits == 24) colorBits = 16; break; case 1 : if (depthBits == 24) depthBits = 16; else if (depthBits == 16) depthBits = 8; case 3 : if (stencilBits == 24) stencilBits = 16; else if (stencilBits == 16) stencilBits = 8; } } testColorBits = colorBits; testDepthBits = depthBits; testStencilBits = stencilBits; if ((i % 4) == 3) { // reduce colorBits if (testColorBits == 24) testColorBits = 16; } if ((i % 4) == 2) { // reduce depthBits if (testDepthBits == 24) testDepthBits = 16; else if (testDepthBits == 16) testDepthBits = 8; } if ((i % 4) == 1) { // reduce stencilBits if (testStencilBits == 24) testStencilBits = 16; else if (testStencilBits == 16) testStencilBits = 8; else testStencilBits = 0; } if (testColorBits == 24) perChannelColorBits = 8; else perChannelColorBits = 4; #ifdef __sgi /* Fix for SGIs grabbing too many bits of color */ if (perChannelColorBits == 4) perChannelColorBits = 0; /* Use minimum size for 16-bit color */ /* Need alpha or else SGIs choose 36+ bit RGB mode */ SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 1); #endif SDL_GL_SetAttribute( SDL_GL_RED_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, perChannelColorBits ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, testDepthBits ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, testStencilBits ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples ); if(r_stereoEnabled->integer) { glConfig.stereoEnabled = qtrue; SDL_GL_SetAttribute(SDL_GL_STEREO, 1); } else { glConfig.stereoEnabled = qfalse; SDL_GL_SetAttribute(SDL_GL_STEREO, 0); } SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); #if 0 // if multisampling is enabled on X11, this causes create window to fail. // If not allowing software GL, demand accelerated if( !r_allowSoftwareGL->integer ) SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ); #endif if( ( SDL_window = SDL_CreateWindow( windowTitle, x, y, glConfig.vidWidth, glConfig.vidHeight, flags ) ) == NULL ) { ri.Printf( PRINT_DEVELOPER, "SDL_CreateWindow failed: %s\n", SDL_GetError( ) ); continue; } if( fullscreen ) { SDL_DisplayMode mode; switch( testColorBits ) { case 16: mode.format = SDL_PIXELFORMAT_RGB565; break; case 24: mode.format = SDL_PIXELFORMAT_RGB24; break; default: ri.Printf( PRINT_DEVELOPER, "testColorBits is %d, can't fullscreen\n", testColorBits ); continue; } mode.w = glConfig.vidWidth; mode.h = glConfig.vidHeight; mode.refresh_rate = glConfig.displayFrequency = ri.Cvar_VariableIntegerValue( "r_displayRefresh" ); mode.driverdata = NULL; if( SDL_SetWindowDisplayMode( SDL_window, &mode ) < 0 ) { ri.Printf( PRINT_DEVELOPER, "SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError( ) ); continue; } } SDL_SetWindowIcon( SDL_window, icon ); if( ( SDL_glContext = SDL_GL_CreateContext( SDL_window ) ) == NULL ) { ri.Printf( PRINT_DEVELOPER, "SDL_GL_CreateContext failed: %s\n", SDL_GetError( ) ); continue; } qglClearColor( 0, 0, 0, 1 ); qglClear( GL_COLOR_BUFFER_BIT ); SDL_GL_SwapWindow( SDL_window ); if( SDL_GL_SetSwapInterval( r_swapInterval->integer ) == -1 ) { ri.Printf( PRINT_DEVELOPER, "SDL_GL_SetSwapInterval failed: %s\n", SDL_GetError( ) ); } glConfig.colorBits = testColorBits; glConfig.depthBits = testDepthBits; glConfig.stencilBits = testStencilBits; ri.Printf( PRINT_ALL, "Using %d color bits, %d depth, %d stencil display.\n", glConfig.colorBits, glConfig.depthBits, glConfig.stencilBits ); break; } SDL_FreeSurface( icon ); if ( iconPic ) { ri.Free( iconPic ); iconPic = NULL; } if( !SDL_window ) { ri.Printf( PRINT_ALL, "Couldn't get a visual\n" ); return RSERR_INVALID_MODE; } GLimp_DetectAvailableModes(); glstring = (char *) qglGetString (GL_RENDERER); ri.Printf( PRINT_ALL, "GL_RENDERER: %s\n", glstring ); return RSERR_OK; }
SDL_Surface * SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) { SDL_DisplayMode desktop_mode; int display = GetVideoDisplay(); int window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display); int window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display); int window_w; int window_h; Uint32 window_flags; Uint32 surface_flags; if (!SDL_GetVideoDevice()) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { return NULL; } } SDL_GetDesktopDisplayMode(display, &desktop_mode); if (width == 0) { width = desktop_mode.w; } if (height == 0) { height = desktop_mode.h; } if (bpp == 0) { bpp = SDL_BITSPERPIXEL(desktop_mode.format); } /* See if we can simply resize the existing window and surface */ if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) { return SDL_PublicSurface; } /* Destroy existing window */ SDL_PublicSurface = NULL; if (SDL_ShadowSurface) { SDL_ShadowSurface->flags &= ~SDL_DONTFREE; SDL_FreeSurface(SDL_ShadowSurface); SDL_ShadowSurface = NULL; } if (SDL_VideoSurface) { SDL_VideoSurface->flags &= ~SDL_DONTFREE; SDL_FreeSurface(SDL_VideoSurface); SDL_VideoSurface = NULL; } if (SDL_VideoContext) { /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */ SDL_GL_DeleteContext(SDL_VideoContext); SDL_VideoContext = NULL; } if (SDL_VideoWindow) { SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y); SDL_DestroyWindow(SDL_VideoWindow); } /* Set up the event filter */ if (!SDL_GetEventFilter(NULL, NULL)) { SDL_SetEventFilter(SDL_CompatEventFilter, NULL); } /* Create a new window */ window_flags = SDL_WINDOW_SHOWN; if (flags & SDL_FULLSCREEN) { window_flags |= SDL_WINDOW_FULLSCREEN; } if (flags & SDL_OPENGL) { window_flags |= SDL_WINDOW_OPENGL; } if (flags & SDL_RESIZABLE) { window_flags |= SDL_WINDOW_RESIZABLE; } if (flags & SDL_NOFRAME) { window_flags |= SDL_WINDOW_BORDERLESS; } GetEnvironmentWindowPosition(width, height, &window_x, &window_y); SDL_VideoWindow = SDL_CreateWindow(wm_title, window_x, window_y, width, height, window_flags); if (!SDL_VideoWindow) { return NULL; } SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon); SetupScreenSaver(flags); window_flags = SDL_GetWindowFlags(SDL_VideoWindow); surface_flags = 0; if (window_flags & SDL_WINDOW_FULLSCREEN) { surface_flags |= SDL_FULLSCREEN; } if ((window_flags & SDL_WINDOW_OPENGL) && (flags & SDL_OPENGL)) { surface_flags |= SDL_OPENGL; } if (window_flags & SDL_WINDOW_RESIZABLE) { surface_flags |= SDL_RESIZABLE; } if (window_flags & SDL_WINDOW_BORDERLESS) { surface_flags |= SDL_NOFRAME; } SDL_VideoFlags = flags; /* If we're in OpenGL mode, just create a stub surface and we're done! */ if (flags & SDL_OPENGL) { SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow); if (!SDL_VideoContext) { return NULL; } if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) { return NULL; } SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0); if (!SDL_VideoSurface) { return NULL; } SDL_VideoSurface->flags |= surface_flags; SDL_PublicSurface = SDL_VideoSurface; return SDL_PublicSurface; } /* Create the screen surface */ SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow); if (!SDL_WindowSurface) { return NULL; } /* Center the public surface in the window surface */ SDL_GetWindowSize(SDL_VideoWindow, &window_w, &window_h); SDL_VideoViewport.x = (window_w - width)/2; SDL_VideoViewport.y = (window_h - height)/2; SDL_VideoViewport.w = width; SDL_VideoViewport.h = height; SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0); SDL_VideoSurface->flags |= surface_flags; SDL_VideoSurface->flags |= SDL_DONTFREE; SDL_FreeFormat(SDL_VideoSurface->format); SDL_VideoSurface->format = SDL_WindowSurface->format; SDL_VideoSurface->format->refcount++; SDL_VideoSurface->w = width; SDL_VideoSurface->h = height; SDL_VideoSurface->pitch = SDL_WindowSurface->pitch; SDL_VideoSurface->pixels = (void *)((Uint8 *)SDL_WindowSurface->pixels + SDL_VideoViewport.y * SDL_VideoSurface->pitch + SDL_VideoViewport.x * SDL_VideoSurface->format->BytesPerPixel); SDL_SetClipRect(SDL_VideoSurface, NULL); /* Create a shadow surface if necessary */ if ((bpp != SDL_VideoSurface->format->BitsPerPixel) && !(flags & SDL_ANYFORMAT)) { SDL_ShadowSurface = SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0); if (!SDL_ShadowSurface) { return NULL; } SDL_ShadowSurface->flags |= surface_flags; SDL_ShadowSurface->flags |= SDL_DONTFREE; /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */ if (SDL_ShadowSurface->format->palette) { SDL_ShadowSurface->flags |= SDL_HWPALETTE; SDL_DitherColors(SDL_ShadowSurface->format->palette->colors, SDL_ShadowSurface->format->BitsPerPixel); } SDL_FillRect(SDL_ShadowSurface, NULL, SDL_MapRGB(SDL_ShadowSurface->format, 0, 0, 0)); } SDL_PublicSurface = (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface); ClearVideoSurface(); /* We're finally done! */ return SDL_PublicSurface; }
/*virtual*/ void Framework3D::Initialize() { XTRACE_FUNCTION; m_IsInitializing = true; #if BUILD_SDL const int Error = SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_NOPARACHUTE); ASSERT(0 == Error); Unused(Error); if (0 != Error) { PRINTF("SDL_Init: %s\n", SDL_GetError()); } SDL_DisableScreenSaver(); #ifdef HAVE_GLES SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); // SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); #endif #endif STATICHASH(Framework); #if BUILD_WINDOWS #if BUILD_FINAL STATICHASH(ShowConsole); const bool ShowConsole = ConfigManager::GetBool(sShowConsole, false, sFramework); if (ShowConsole) #endif { Console::GetInstance()->SetPos(0, 0); } #endif STATICHASH(UseRandomSeed); const bool UseRandomSeed = ConfigManager::GetBool(sUseRandomSeed, false, sFramework); STATICHASH(RandomSeed); const int RandomSeed = ConfigManager::GetInt(sRandomSeed, 0, sFramework); if (UseRandomSeed) { Math::SeedGenerator(RandomSeed); } else { Math::SeedGenerator(); } STATICHASH(UseFixedFrameTime); m_UseFixedFrameTime = ConfigManager::GetBool(sUseFixedFrameTime, true, sFramework); #ifdef HAVE_GLES0 m_UseFixedFrameTime = false; // GLES hardware are less powerfull than Desktop #endif STATICHASH(FixedFrameTime); m_FixedFrameTime = ConfigManager::GetFloat(sFixedFrameTime, 1.0f / 60.0f, sFramework); #ifdef PANDORA m_FixedFrameTime = 1.0f / 14.0f; // 60fps cannot be sustained on the Pandora #endif STATICHASH(FramesLimit); const int FramesLimit = ConfigManager::GetInt(sFramesLimit, 5, sFramework); m_FrameTimeLimit = m_FixedFrameTime * static_cast<float>(FramesLimit); STATICHASH(DoVideoCapture); m_DoVideoCapture = ConfigManager::GetBool(sDoVideoCapture, false, sFramework); STATICHASH(VideoCaptureFixedFrameTime); m_VideoCaptureFixedFrameTime = ConfigManager::GetFloat( sVideoCaptureFixedFrameTime, 1.0f / 30.0f, sFramework); #ifdef PANDORA sVideoCaptureFixedFrameTime = 1.0f / 8.0f; // 30fps cannot be sustained on the Pandora (even 10fps I'm not sure) #endif uint DisplayWidth = 0; uint DisplayHeight = 0; SimpleString WindowTitle; // Loads display parameters from config, so GetInitialDisplaySize can use // that. m_Display = new Display; // Make sure that we use a supported resolution regardless of what the config // file said. const SDisplayMode BestDisplayMode = m_Display->GetBestDisplayMode(m_Display->m_Width, m_Display->m_Height); m_Display->SetResolution(BestDisplayMode.Width, BestDisplayMode.Height); uint WindowIcon = 0; GetInitialWindowIcon(WindowIcon); GetInitialDisplaySize(DisplayWidth, DisplayHeight); GetInitialWindowTitle(WindowTitle); CreateSplashWindow(WindowIcon, WindowTitle.CStr()); m_Window = new Window; #if BUILD_WINDOWS_NO_SDL DWORD WindowStyle = 0; if (m_Display->m_Fullscreen || (m_Display->m_ScreenWidth == m_Display->m_Width && m_Display->m_ScreenHeight == m_Display->m_Height)) { WindowStyle = WS_POPUP; } else { WindowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; } m_Window->Init(WindowTitle.CStr(), "Class1", WindowStyle, 0, DisplayWidth, DisplayHeight, m_hInstance, WindowProc, WindowIcon, m_Display->m_ScreenWidth, m_Display->m_ScreenHeight); #elif BUILD_SDL uint WindowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN; if (m_Display->m_Fullscreen || (m_Display->m_ScreenWidth == m_Display->m_Width && m_Display->m_ScreenHeight == m_Display->m_Height)) { WindowFlags |= SDL_WINDOW_BORDERLESS; } // TODO SDL: Unify interface? m_Window->Init(WindowTitle.CStr(), WindowFlags, DisplayWidth, DisplayHeight); STATICHASH(IconImage); const char* const pIconImage = ConfigManager::GetString(sIconImage, nullptr, sFramework); if(pIconImage) { ASSERT(pIconImage); const Surface IconSurface = Surface(PackStream(pIconImage), Surface::ESFT_BMP); SDL_SetWindowIcon(m_Window->GetSDLWindow(), IconSurface.GetSDLSurface()); } #endif m_Window->SetFullscreen(m_Display->m_Fullscreen); if (m_Display->m_Fullscreen) { m_Window->SetPosition(0, 0); } m_Clock = new Clock; XTRACE_BEGIN(InitializeDevices); m_Keyboard = new Keyboard; #if BUILD_WINDOWS_NO_SDL m_Mouse = new Mouse(m_hInstance, m_Window->GetHWnd()); #elif BUILD_SDL m_Mouse = new Mouse(m_Window); #endif XTRACE_END; InitializeUIInputMap(); XTRACE_BEGIN(InitializeAudioSystem); InitializeAudioSystem(); XTRACE_END; XTRACE_BEGIN(InitializeRenderer); #if BUILD_WINDOWS_NO_SDL STATICHASH(OpenGL); const bool OpenGL = ConfigManager::GetBool(sOpenGL); if (OpenGL) #endif { PRINTF("Using OpenGL renderer.\n"); m_Renderer = CreateGL2Renderer(m_Window); } #if BUILD_WINDOWS_NO_SDL else { PRINTF("Using Direct3D renderer.\n"); m_Renderer = CreateD3D9Renderer(m_Window->GetHWnd(), m_Display->m_Fullscreen); } #endif IRenderer::SRestoreDeviceCallback Callback; Callback.m_Callback = &Framework3D::RendererRestoreDeviceCallback; Callback.m_Void = this; m_Renderer->SetRestoreDeviceCallback(Callback); InitializeRender(); m_Renderer->Initialize(); m_Renderer->SetDisplay(m_Display); m_Renderer->SetClock(m_Clock); XTRACE_END; if (ShowWindowASAP()) { SafeDelete(m_SplashWindow); #if BUILD_WINDOWS_NO_SDL m_Window->Show(m_CmdShow); #elif BUILD_SDL m_Window->Show(); #endif // Reattach GL context if needed. m_Renderer->Refresh(); } if (UseClassicTargetManager()) { m_TargetManager = new TargetManager(m_Renderer, DisplayWidth, DisplayHeight); } XTRACE_BEGIN(InitializeUI); SimpleString UIManagerDefinitionName; GetUIManagerDefinitionName(UIManagerDefinitionName); m_UIManager = new UIManagerFramework(this); m_UIManager->InitializeFromDefinition(UIManagerDefinitionName); m_UIManager->GetUIStack()->SetFadeOverlay(m_UIManager->GetScreen("Fade")); UIScreen::UpdateMouseButtonsSwapped(); XTRACE_END; m_IsInitializing = false; }
bool Window::Create(){ logger << ( "[LOG] CREATING WINDOW:" ); if( SDL_WasInit( SDL_INIT_VIDEO ) == 0 ){ if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ){ logger << ( "[ERROR] SDL_Init: " + string( SDL_GetError() ) ); this->Init = false; return false; } else{ logger << ( "[LOG] SDL_Init: SUCCESS" ); } } this->Id = SDL_CreateWindow( this->Title.c_str(), this->PositionX, this->PositionY, this->Width, this->Height, this->Flag ); if( this->Id == NULL ){ logger << ( "[ERROR] SDL_CreateWindow: " + string( SDL_GetError() ) ); this->Init = false; return false; } else{ logger << ( "[LOG] SDL_CreateWindow: SUCCESS" ); } SDL_Surface *surface = NULL; Uint16 pixels[16*16] = { 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xffff, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xffff, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xffff, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000 }; surface = SDL_CreateRGBSurfaceFrom( pixels, 16, 16, 16, 16*2, 0x0f00, 0x00f0, 0x000f, 0xf000 ); if( surface == NULL ){ logger << ( "[ERROR] SDL_SetWindowIcon: " + string( SDL_GetError() ) ); this->Init = false; return false; } else{ logger << ( "[LOG] SDL_SetWindowIcon: SUCCESS" ); SDL_SetWindowIcon( this->Id, surface ); SDL_FreeSurface( surface ); } this->GLContext = SDL_GL_CreateContext( this->Id ); if( this->GLContext == NULL ){ logger << ( "[ERROR] SDL_GL_CreateContext: " + string( SDL_GetError() ) ); this->Init = false; return false; } else{ logger << ( "[LOG] SDL_GL_CreateContext: SUCCESS" ); } logger << ( "[LOG] SDL_GetWindowDisplayIndex: " + to_string( SDL_GetWindowDisplayIndex( this->Id ) ) ); logger << ( "[LOG] SDL_GetWindowID: " + to_string( SDL_GetWindowID( this->Id ) ) ); logger << ( "[LOG] SDL_GetWindowBrightness: " + to_string( SDL_GetWindowBrightness( this->Id ) ) ); SDL_SetWindowMinimumSize( this->Id, this->WidthMin, this->HeightMin ); SDL_SetWindowMaximumSize( this->Id, this->WidthMax, this->HeightMax ); Window::SetMaxMinResolution( this->Id, this->WidthMin, this->HeightMin, this->WidthMax, this->HeightMax ); logger << ( "[LOG] CREATED WINDOW" ); this->Init = true; return this->Init; }
bool createSurface() { LOG(LogInfo) << "Creating surface..."; if(SDL_Init(SDL_INIT_VIDEO) != 0) { LOG(LogError) << "Error initializing SDL!\n " << SDL_GetError(); return false; } 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_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // multisample anti-aliasing //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2); #ifdef USE_OPENGL_ES SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); #endif //SDL_GL_SetSwapInterval(1); //0 for immediate updates, 1 for updates synchronized with the vertical retrace, -1 for late swap tearing SDL_DisplayMode dispMode; SDL_GetDesktopDisplayMode(0, &dispMode); if(display_width == 0) display_width = dispMode.w; if(display_height == 0) display_height = dispMode.h; sdlWindow = SDL_CreateWindow("EmulationStation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, display_width, display_height, SDL_WINDOW_OPENGL | (Settings::getInstance()->getBool("Windowed") ? 0 : SDL_WINDOW_FULLSCREEN)); if(sdlWindow == NULL) { LOG(LogError) << "Error creating SDL window!\n\t" << SDL_GetError(); return false; } LOG(LogInfo) << "Created window successfully."; //set an icon for the window size_t width = 0; size_t height = 0; std::vector<unsigned char> rawData = ImageIO::loadFromMemoryRGBA32(window_icon_256_png_data, window_icon_256_png_size, width, height); if (!rawData.empty()) { ImageIO::flipPixelsVert(rawData.data(), width, height); //SDL interprets each pixel as a 32-bit number, so our masks must depend on the endianness (byte order) of the machine #if SDL_BYTEORDER == SDL_BIG_ENDIAN Uint32 rmask = 0xff000000; Uint32 gmask = 0x00ff0000; Uint32 bmask = 0x0000ff00; Uint32 amask = 0x000000ff; #else Uint32 rmask = 0x000000ff; Uint32 gmask = 0x0000ff00; Uint32 bmask = 0x00ff0000; Uint32 amask = 0xff000000; #endif //try creating SDL surface from logo data SDL_Surface * logoSurface = SDL_CreateRGBSurfaceFrom((void *)rawData.data(), width, height, 32, width * 4, rmask, gmask, bmask, amask); if (logoSurface != NULL) { SDL_SetWindowIcon(sdlWindow, logoSurface); SDL_FreeSurface(logoSurface); } } sdlContext = SDL_GL_CreateContext(sdlWindow); //hide mouse cursor initialCursorState = SDL_ShowCursor(0) == 1; return true; }
void RenderManagerSDL::init(int xResolution, int yResolution, bool fullscreen) { SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); // Set modesetting Uint32 screenFlags = 0; if (fullscreen) { screenFlags |= SDL_WINDOW_FULLSCREEN; } else { screenFlags |= SDL_WINDOW_RESIZABLE; } // Create window mWindow = SDL_CreateWindow(AppTitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, xResolution, yResolution, screenFlags); // Set icon #if !defined(MAC_OS_X) SDL_Surface* icon = loadSurface("Icon.bmp"); SDL_SetColorKey(icon, SDL_TRUE, SDL_MapRGB(icon->format, 0, 0, 0)); SDL_SetWindowIcon(mWindow, icon); SDL_FreeSurface(icon); #endif // Create renderer to draw in window mRenderer = SDL_CreateRenderer(mWindow, -1, 0); // Hide mousecursor SDL_ShowCursor(0); // Create rendertarget to make window resizeable mRenderTarget = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, xResolution, yResolution); // Load all textures and surfaces to render the game SDL_Surface* tmpSurface; // Create a 1x1 black surface which will be scaled to draw an overlay tmpSurface = SDL_CreateRGBSurface(0, 1, 1, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); // Because of SDL bug we can't check at the moment if color mod is available... no risk no fun ;) SDL_FillRect(tmpSurface, NULL, SDL_MapRGB(tmpSurface->format, 255, 255, 255)); mOverlayTexture = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); SDL_FreeSurface(tmpSurface); // Create marker texture for mouse and ball tmpSurface = SDL_CreateRGBSurface(0, 5, 5, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); SDL_FillRect(tmpSurface, NULL, SDL_MapRGB(tmpSurface->format, 255, 255, 255)); mMarker[0] = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); SDL_FillRect(tmpSurface, NULL, SDL_MapRGB(tmpSurface->format, 0, 0, 0)); mMarker[1] = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); SDL_FreeSurface(tmpSurface); // Load background tmpSurface = loadSurface("backgrounds/strand2.bmp"); mBackground = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); BufferedImage* bgImage = new BufferedImage; bgImage->w = tmpSurface->w; bgImage->h = tmpSurface->h; bgImage->sdlImage = mBackground; SDL_FreeSurface(tmpSurface); mImageMap["background"] = bgImage; // Load ball for (int i = 1; i <= 16; ++i) { char filename[64]; sprintf(filename, "gfx/ball%02d.bmp", i); tmpSurface = loadSurface(filename); SDL_SetColorKey(tmpSurface, SDL_TRUE, SDL_MapRGB(tmpSurface->format, 0, 0, 0)); SDL_Texture *ballTexture = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); SDL_FreeSurface(tmpSurface); mBall.push_back(ballTexture); } // Load ball shadow tmpSurface = loadSurface("gfx/schball.bmp"); SDL_SetColorKey(tmpSurface, SDL_TRUE, SDL_MapRGB(tmpSurface->format, 0, 0, 0)); SDL_SetSurfaceAlphaMod(tmpSurface, 127); mBallShadow = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); SDL_FreeSurface(tmpSurface); // Load blobby and shadows surface // Load streamed textures for coloring for (int i = 1; i <= 5; ++i) { // Load blobby surface char filename[64]; sprintf(filename, "gfx/blobbym%d.bmp", i); SDL_Surface* blobImage = loadSurface(filename); SDL_Surface* formatedBlobImage = SDL_ConvertSurfaceFormat(blobImage, SDL_PIXELFORMAT_ABGR8888, 0); SDL_FreeSurface(blobImage); SDL_SetColorKey(formatedBlobImage, SDL_TRUE, SDL_MapRGB(formatedBlobImage->format, 0, 0, 0)); for(int j = 0; j < formatedBlobImage->w * formatedBlobImage->h; j++) { SDL_Color* pixel = &(((SDL_Color*)formatedBlobImage->pixels)[j]); if (!(pixel->r | pixel->g | pixel->b)) { pixel->a = 0; } } mStandardBlob.push_back(formatedBlobImage); // Load blobby shadow surface sprintf(filename, "gfx/sch1%d.bmp", i); SDL_Surface* blobShadow = loadSurface(filename); SDL_Surface* formatedBlobShadowImage = SDL_ConvertSurfaceFormat(blobShadow, SDL_PIXELFORMAT_ABGR8888, 0); SDL_FreeSurface(blobShadow); SDL_SetSurfaceAlphaMod(formatedBlobShadowImage, 127); SDL_SetColorKey(formatedBlobShadowImage, SDL_TRUE, SDL_MapRGB(formatedBlobShadowImage->format, 0, 0, 0)); for(int j = 0; j < formatedBlobShadowImage->w * formatedBlobShadowImage->h; j++) { SDL_Color* pixel = &(((SDL_Color*)formatedBlobShadowImage->pixels)[j]); if (!(pixel->r | pixel->g | pixel->b)) { pixel->a = 0; } else { pixel->a = 127; } } mStandardBlobShadow.push_back(formatedBlobShadowImage); // Prepare blobby textures SDL_Texture* leftBlobTex = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, formatedBlobImage->w, formatedBlobImage->h); SDL_SetTextureBlendMode(leftBlobTex, SDL_BLENDMODE_BLEND); SDL_UpdateTexture(leftBlobTex, NULL, formatedBlobImage->pixels, formatedBlobImage->pitch); mLeftBlob.push_back(DynamicColoredTexture( leftBlobTex, Color(255, 255, 255))); SDL_Texture* rightBlobTex = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, formatedBlobImage->w, formatedBlobImage->h); SDL_SetTextureBlendMode(rightBlobTex, SDL_BLENDMODE_BLEND); SDL_UpdateTexture(rightBlobTex, NULL, formatedBlobImage->pixels, formatedBlobImage->pitch); mRightBlob.push_back(DynamicColoredTexture( rightBlobTex, Color(255, 255, 255))); // Prepare blobby shadow textures SDL_Texture* leftBlobShadowTex = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, formatedBlobShadowImage->w, formatedBlobShadowImage->h); SDL_SetTextureBlendMode(leftBlobShadowTex, SDL_BLENDMODE_BLEND); mLeftBlobShadow.push_back(DynamicColoredTexture( leftBlobShadowTex, Color(255, 255, 255))); SDL_UpdateTexture(leftBlobShadowTex, NULL, formatedBlobShadowImage->pixels, formatedBlobShadowImage->pitch); SDL_Texture* rightBlobShadowTex = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, formatedBlobShadowImage->w, formatedBlobShadowImage->h); SDL_SetTextureBlendMode(rightBlobShadowTex, SDL_BLENDMODE_BLEND); mRightBlobShadow.push_back(DynamicColoredTexture( rightBlobShadowTex, Color(255, 255, 255))); SDL_UpdateTexture(rightBlobShadowTex, NULL, formatedBlobShadowImage->pixels, formatedBlobShadowImage->pitch); // Load iOS specific icon (because we have no backbutton) #ifdef __APPLE__ #if !MAC_OS_X tmpSurface = loadSurface("gfx/flag.bmp"); SDL_SetColorKey(tmpSurface, SDL_TRUE, SDL_MapRGB(tmpSurface->format, 0, 0, 0)); mBackFlag = SDL_CreateTextureFromSurface(mRenderer, tmpSurface); SDL_FreeSurface(tmpSurface); #endif #endif } // Load font for (int i = 0; i <= 54; ++i) { char filename[64]; sprintf(filename, "gfx/font%02d.bmp", i); SDL_Surface* tempFont = loadSurface(filename); SDL_SetColorKey(tempFont, SDL_TRUE, SDL_MapRGB(tempFont->format, 0, 0, 0)); mFont.push_back(SDL_CreateTextureFromSurface(mRenderer, tempFont)); SDL_Surface* tempFont2 = highlightSurface(tempFont, 60); mHighlightFont.push_back(SDL_CreateTextureFromSurface(mRenderer, tempFont2)); SDL_FreeSurface(tempFont); SDL_FreeSurface(tempFont2); } // Load blood surface SDL_Surface* blobStandardBlood = loadSurface("gfx/blood.bmp"); SDL_Surface* formatedBlobStandardBlood = SDL_ConvertSurfaceFormat(blobStandardBlood, SDL_PIXELFORMAT_ABGR8888, 0); SDL_FreeSurface(blobStandardBlood); SDL_SetColorKey(formatedBlobStandardBlood, SDL_TRUE, SDL_MapRGB(formatedBlobStandardBlood->format, 0, 0, 0)); for(int j = 0; j < formatedBlobStandardBlood->w * formatedBlobStandardBlood->h; j++) { SDL_Color* pixel = &(((SDL_Color*)formatedBlobStandardBlood->pixels)[j]); if (!(pixel->r | pixel->g | pixel->b)) { pixel->a = 0; } else { pixel->a = 255; } } mStandardBlobBlood = formatedBlobStandardBlood; // Create streamed textures for blood SDL_Texture* leftBlobBlood = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, formatedBlobStandardBlood->w, formatedBlobStandardBlood->h); SDL_SetTextureBlendMode(leftBlobBlood, SDL_BLENDMODE_BLEND); mLeftBlobBlood = DynamicColoredTexture( leftBlobBlood, Color(255, 0, 0)); SDL_UpdateTexture(leftBlobBlood, NULL, formatedBlobStandardBlood->pixels, formatedBlobStandardBlood->pitch); SDL_Texture* rightBlobBlood = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, formatedBlobStandardBlood->w, formatedBlobStandardBlood->h); SDL_SetTextureBlendMode(rightBlobBlood, SDL_BLENDMODE_BLEND); mRightBlobBlood = DynamicColoredTexture( rightBlobBlood, Color(255, 0, 0)); SDL_UpdateTexture(rightBlobBlood, NULL, formatedBlobStandardBlood->pixels, formatedBlobStandardBlood->pitch); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); }
int main(int argc, char *argv[]) { SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); /* initialize SDL first */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { showInitError(std::string("Error initializing SDL: ") + SDL_GetError()); return 0; } if (!EventThread::allocUserEvents()) { showInitError("Error allocating SDL user events"); return 0; } #ifndef WORKDIR_CURRENT /* set working directory */ char *dataDir = SDL_GetBasePath(); if (dataDir) { int result = chdir(dataDir); (void)result; SDL_free(dataDir); } #endif /* now we load the config */ Config conf; conf.read(argc, argv); if (!conf.gameFolder.empty()) if (chdir(conf.gameFolder.c_str()) != 0) { showInitError(std::string("Unable to switch into gameFolder ") + conf.gameFolder); return 0; } conf.readGameINI(); assert(conf.rgssVersion >= 1 && conf.rgssVersion <= 3); printRgssVersion(conf.rgssVersion); int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG; if (IMG_Init(imgFlags) != imgFlags) { showInitError(std::string("Error initializing SDL_image: ") + SDL_GetError()); SDL_Quit(); return 0; } if (TTF_Init() < 0) { showInitError(std::string("Error initializing SDL_ttf: ") + SDL_GetError()); IMG_Quit(); SDL_Quit(); return 0; } if (Sound_Init() == 0) { showInitError(std::string("Error initializing SDL_sound: ") + Sound_GetError()); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } /* Setup application icon */ SDL_RWops *iconSrc; if (conf.iconPath.empty()) iconSrc = SDL_RWFromConstMem(assets_icon_png, assets_icon_png_len); else iconSrc = SDL_RWFromFile(conf.iconPath.c_str(), "rb"); SDL_Surface *iconImg = IMG_Load_RW(iconSrc, SDL_TRUE); SDL_Window *win; Uint32 winFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS; if (conf.winResizable) winFlags |= SDL_WINDOW_RESIZABLE; if (conf.fullscreen) winFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; win = SDL_CreateWindow(conf.game.title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, conf.defScreenW, conf.defScreenH, winFlags); if (!win) { showInitError(std::string("Error creating window: ") + SDL_GetError()); return 0; } if (iconImg) { SDL_SetWindowIcon(win, iconImg); SDL_FreeSurface(iconImg); } ALCdevice *alcDev = alcOpenDevice(0); if (!alcDev) { showInitError("Error opening OpenAL device"); SDL_DestroyWindow(win); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } SDL_DisplayMode mode; SDL_GetDisplayMode(0, 0, &mode); /* Can't sync to display refresh rate if its value is unknown */ if (!mode.refresh_rate) conf.syncToRefreshrate = false; EventThread eventThread; RGSSThreadData rtData(&eventThread, argv[0], win, alcDev, mode.refresh_rate, conf); int winW, winH; SDL_GetWindowSize(win, &winW, &winH); rtData.windowSizeMsg.post(Vec2i(winW, winH)); /* Load and post key bindings */ rtData.bindingUpdateMsg.post(loadBindings(conf)); /* Start RGSS thread */ SDL_Thread *rgssThread = SDL_CreateThread(rgssThreadFun, "rgss", &rtData); /* Start event processing */ eventThread.process(rtData); /* Request RGSS thread to stop */ rtData.rqTerm.set(); /* Wait for RGSS thread response */ for (int i = 0; i < 1000; ++i) { /* We can stop waiting when the request was ack'd */ if (rtData.rqTermAck) { Debug() << "RGSS thread ack'd request after" << i*10 << "ms"; break; } /* Give RGSS thread some time to respond */ SDL_Delay(10); } /* If RGSS thread ack'd request, wait for it to shutdown, * otherwise abandon hope and just end the process as is. */ if (rtData.rqTermAck) SDL_WaitThread(rgssThread, 0); else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.game.title.c_str(), "The RGSS script seems to be stuck and mkxp will now force quit", win); if (!rtData.rgssErrorMsg.empty()) { Debug() << rtData.rgssErrorMsg; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.game.title.c_str(), rtData.rgssErrorMsg.c_str(), win); } /* Clean up any remainin events */ eventThread.cleanup(); Debug() << "Shutting down."; alcCloseDevice(alcDev); SDL_DestroyWindow(win); Sound_Quit(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; }
static int GLimp_SetMode( void ) { const char* glstring; //int sdlcolorbits; int /*colorbits, depthbits, stencilbits, */stereo; //int tcolorbits, tdepthbits, tstencilbits; int samples; int i = 0; SDL_DisplayMode displayMode; //SDL_Surface *vidscreen = NULL; Uint32 flags = SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE; static int desktop_w, desktop_h; // desktop resolution /*if(window) { return RSERR_INVALID_MODE; }*/ ri.Printf( PRINT_DEVELOPER, "Initializing OpenGL display\n"); //if ( r_allowResize->integer ) // flags |= SDL_WINDOW_RESIZABLE; // Guess the display aspect ratio through the desktop resolution // by assuming (relatively safely) that it is set at or close to // the display's native aspect ratio //videoInfo = SDL_GetVideoInfo(); //if( videoInfo->current_h == 0 ) if( SDL_GetDesktopDisplayMode( 0, &displayMode ) == 0 )//FIXME: Use SDL_GetWindowDisplayIndex instead of 0? { if( !desktop_w ) { // first time through, resolve desktop resolution desktop_w = displayMode.w; desktop_h = displayMode.h; } } else { ri.Printf( PRINT_ALL, "Cannot get display info, assuming 800x600\n" ); if( !desktop_w ) { // first time through, resolve desktop resolution desktop_w = 800; desktop_h = 600; } } glConfig.displayAspect = (float)desktop_w / (float)desktop_h; ri.Printf( PRINT_DEVELOPER, "Estimated display aspect: %.3f\n", glConfig.displayAspect ); //if( !failSafe ) //{ if ( r_width->modified || r_height->modified || r_pixelAspect->modified ) { for ( i = 0; i < 12; i++ ) { if ( r_width->integer == vidModes[ i ].width && r_height->integer == vidModes[ i ].height && r_pixelAspect->integer == vidModes[ i ].pixelAspect ) { Cvar_SetValue( "r_mode", i ); break; } } if ( i == 12 ) Cvar_Set( "r_mode", "-1" ); } else if ( r_mode->modified && r_mode->integer >= 0 ) { Cvar_SetValue( "r_width", vidModes[ r_mode->integer ].width ); Cvar_SetValue( "r_height", vidModes[ r_mode->integer ].height ); Cvar_SetValue( "r_pixelAspect", vidModes[ r_mode->integer ].pixelAspect ); } r_width->modified = qfalse; r_height->modified = qfalse; r_pixelAspect->modified = qfalse; r_mode->modified = qfalse; glConfig.vidWidth = ( r_width->integer ? r_width->integer : desktop_w ); glConfig.vidHeight = ( r_height->integer ? r_height->integer : desktop_h ); if(glConfig.vidWidth > desktop_w) { glConfig.vidWidth = desktop_w; } if(glConfig.vidHeight > desktop_h) { glConfig.vidHeight = desktop_h; } glConfig.windowAspect = glConfig.vidWidth / ( (float)glConfig.vidHeight * r_pixelAspect->value ); /*} else if( glConfig.vidWidth != R_FAILSAFE_WIDTH && glConfig.vidHeight != R_FAILSAFE_HEIGHT ) { ri.Printf( PRINT_ALL, "Setting mode %dx%d failed, falling back on mode %dx%d\n", glConfig.vidWidth, glConfig.vidHeight, R_FAILSAFE_WIDTH, R_FAILSAFE_HEIGHT ); glConfig.vidWidth = R_FAILSAFE_WIDTH; glConfig.vidHeight = R_FAILSAFE_HEIGHT; glConfig.windowAspect = 1.0f; } else return RSERR_INVALID_MODE;*/ ri.Printf (PRINT_DEVELOPER, "...setting mode %dx%d\n", glConfig.vidWidth, glConfig.vidHeight); if (r_fullscreen->integer) { glConfig.vidWidth = desktop_w; glConfig.vidHeight = desktop_h; flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; } /*colorbits = r_colorbits->value; if (!colorbits || colorbits > 32) colorbits = 32; if (!r_depthbits->value) depthbits = 24; else depthbits = r_depthbits->value; stencilbits = r_stencilbits->value; samples = r_ext_multisample->value;*/ samples = r_ext_multisample->value; /*for (i = 0; i < 16; i++) { // 0 - default // 1 - minus colorbits // 2 - minus depthbits // 3 - minus stencil if ((i % 4) == 0 && i) { // one pass, reduce switch (i / 4) { case 2 : if (colorbits == 32) colorbits = 24; else if (colorbits == 24) colorbits = 16; break; case 1 : if (depthbits == 24) depthbits = 16; else if (depthbits == 16) depthbits = 8; case 3 : if (stencilbits == 24) stencilbits = 16; else if (stencilbits == 16) stencilbits = 8; } } tcolorbits = colorbits; tdepthbits = depthbits; tstencilbits = stencilbits; if ((i % 4) == 3) { // reduce colorbits if (tcolorbits == 32) tcolorbits = 24; if (tcolorbits == 24) tcolorbits = 16; } if ((i % 4) == 2) { // reduce depthbits if (tdepthbits == 24) tdepthbits = 16; else if (tdepthbits == 16) tdepthbits = 8; } if ((i % 4) == 1) { // reduce stencilbits if (tstencilbits == 24) tstencilbits = 16; else if (tstencilbits == 16) tstencilbits = 8; else tstencilbits = 0; } if (tcolorbits >= 24) sdlcolorbits = 8; else sdlcolorbits = 4;*/ //#ifdef __sgi /* Fix for SGIs grabbing too many bits of color */ // if (sdlcolorbits == 4) // sdlcolorbits = 0; /* Use minimum size for 16-bit color */ /* Need alpha or else SGIs choose 36+ bit RGB mode */ // SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 1); //#endif /*SDL_GL_SetAttribute( SDL_GL_RED_SIZE, colorbits ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, colorbits ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, colorbits ); SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, colorbits ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, depthbits ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, stencilbits ); */ if(r_stereoEnabled->integer) { //glConfig.stereoEnabled = qtrue; SDL_GL_SetAttribute(SDL_GL_STEREO, 1); } else { //glConfig.stereoEnabled = qfalse; SDL_GL_SetAttribute(SDL_GL_STEREO, 0); } SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples ); if ( !(window = SDL_CreateWindow( CLIENT_WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, glConfig.vidWidth, glConfig.vidHeight, flags )) ) //if (!(vidscreen = SDL_SetVideoMode(glConfig.vidWidth, glConfig.vidHeight, colorbits, flags))) { ri.Printf( PRINT_ALL, "SDL_CreateWindow failed: %s\n", SDL_GetError( ) ); return RSERR_INVALID_MODE; } if ( !(context = SDL_GL_CreateContext( window )) ) { ri.Printf( PRINT_ALL, "SDL_GL_CreateContext failed: %s\n", SDL_GetError( ) ); SDL_DestroyWindow( window ); window = NULL; return RSERR_INVALID_MODE; } //if( SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, r_swapInterval->integer ) < 0 ) if( SDL_GL_SetSwapInterval( r_swapInterval->integer ) != 0 ) ri.Printf( PRINT_ALL, "r_swapInterval failed (%s)\n", SDL_GetError( ) ); #ifdef USE_ICON { SDL_Surface *icon = SDL_CreateRGBSurfaceFrom( (void *)CLIENT_WINDOW_ICON.pixel_data, CLIENT_WINDOW_ICON.width, CLIENT_WINDOW_ICON.height, CLIENT_WINDOW_ICON.bytes_per_pixel * 8, CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width, #ifdef Q3_LITTLE_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); //SDL_WM_SetIcon( icon, NULL ); SDL_SetWindowIcon( window, icon ); SDL_FreeSurface( icon ); } #endif //SDL_WM_SetCaption(CLIENT_WINDOW_TITLE, CLIENT_WINDOW_MIN_TITLE); //SDL_ShowCursor(0); //SDL_ShowCursor(0); GLimp_GetCurrentContext(); //ri.Printf( PRINT_DEVELOPER, "Using %d/%d/%d Color bits, %d depth, %d stencil display.\n", // sdlcolorbits, sdlcolorbits, sdlcolorbits, tdepthbits, tstencilbits); SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &glConfig.colorBits ); SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &glConfig.depthBits ); SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &glConfig.stencilBits ); SDL_GL_GetAttribute( SDL_GL_STEREO, &stereo ); if(stereo) { glConfig.stereoEnabled = qtrue; } else { glConfig.stereoEnabled = qfalse; } if(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) { glConfig.isFullscreen = qtrue; } else { if(r_fullscreen->integer) { Cvar_Set( "r_fullscreen", "0" ); } glConfig.isFullscreen = qfalse; } /*glConfig.colorBits = tcolorbits; glConfig.depthBits = tdepthbits; glConfig.stencilBits = tstencilbits; break; } */ GLimp_DetectAvailableModes(); if (!window || !context) { ri.Printf( PRINT_ALL, "Couldn't get a visual\n" ); return RSERR_INVALID_MODE; } //screen = vidscreen; glstring = (char *) qglGetString (GL_RENDERER); ri.Printf( PRINT_ALL, "GL_RENDERER: %s\n", glstring ); return RSERR_OK; }