bool gl_initialize(int screen_wid,int screen_high,int fsaa_mode,char *err_str) { int sdl_flags; GLint ntxtsize; #if defined(D3_OS_LINUX) || defined(D3_OS_WINDOWS) GLenum glew_error; #endif #ifdef D3_OS_IPHONE const GLenum discards[]={GL_DEPTH_ATTACHMENT,GL_STENCIL_ATTACHMENT}; #endif // reset sizes to the desktop // if they are at default if ((screen_wid==-1) || (screen_high==-1)) { screen_wid=render_info.desktop.wid; screen_high=render_info.desktop.high; } // setup rendering sizes #ifndef D3_ROTATE_VIEW view.screen.x_sz=screen_wid; view.screen.y_sz=screen_high; #else view.screen.x_sz=screen_high; view.screen.y_sz=screen_wid; #endif view.screen.wide=gl_is_size_widescreen(view.screen.x_sz,view.screen.y_sz); // normal attributes 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_DEPTH_SIZE,24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); #ifdef D3_OPENGL_ES SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,0); #endif #ifdef D3_OS_IPHONE SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING,1); #endif // full screen anti-aliasing attributes switch (fsaa_mode) { case fsaa_mode_2x: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,2); break; case fsaa_mode_4x: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4); break; case fsaa_mode_8x: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,8); break; } // start window or full screen sdl_flags=SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN; if (!gl_in_window_mode()) sdl_flags|=(SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS); sdl_wind=SDL_CreateWindow("dim3",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,screen_wid,screen_high,sdl_flags); if (sdl_wind==NULL) { sprintf(err_str,"SDL: Could not create window (Error: %s)",SDL_GetError()); return(FALSE); } sdl_gl_ctx=SDL_GL_CreateContext(sdl_wind); // use glew on linux and windows #if defined(D3_OS_LINUX) || defined(D3_OS_WINDOWS) glew_error=glewInit(); if (glew_error!=GL_NO_ERROR) { strcpy(err_str,glewGetErrorString(glew_error)); return(FALSE); } #endif // grab openGL attributes strncpy(render_info.name,(char*)glGetString(GL_RENDERER),64); render_info.name[63]=0x0; strncpy(render_info.ext_string,(char*)glGetString(GL_EXTENSIONS),8192); render_info.ext_string[8191]=0x0; glGetIntegerv(GL_MAX_TEXTURE_SIZE,&ntxtsize); render_info.texture_max_size=(int)ntxtsize; if (!gl_check_initialize(err_str)) return(FALSE); // stick refresh rate to 60 render_info.monitor_refresh_rate=60; #ifndef D3_ROTATE_VIEW gl_set_viewport(0,0,view.screen.x_sz,view.screen.y_sz); #else gl_set_viewport(0,0,view.screen.y_sz,view.screen.x_sz); #endif gl_setup_context(); #ifndef D3_OPENGL_ES if (fsaa_mode!=fsaa_mode_none) glEnable(GL_MULTISAMPLE); #endif // clear the entire window so it doesn't flash glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT); #ifdef D3_OS_IPHONE glDiscardFramebufferEXT(GL_FRAMEBUFFER,2,discards); #endif SDL_GL_SwapWindow(sdl_wind); // texture utility initialize gl_texture_initialize(); return(TRUE); }
bool gl_initialize(int screen_wid,int screen_high,bool lock_fps_refresh,int fsaa_mode,bool reset,char *err_str) { GLint ntxtunit,ntxtsize; #ifdef D3_OS_MAC long swapint,rect[4]; CGLContextObj current_ctx; CFDictionaryRef mode_info; CFNumberRef cf_rate; #else GLenum glew_error; #endif // setup rendering sizes setup.screen.x_sz=screen_wid; setup.screen.y_sz=screen_high; // normal attributes 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_DEPTH_SIZE,16); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); // full screen anti-aliasing attributes switch (fsaa_mode) { case fsaa_mode_low: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,2); break; case fsaa_mode_medium: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4); break; case fsaa_mode_high: SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,6); break; } // start window or full screen if (gl_in_window_mode()) { surface=SDL_SetVideoMode(setup.screen.x_sz,setup.screen.y_sz,32,SDL_OPENGL|SDL_HWSURFACE); SDL_WM_SetCaption("dim3",NULL); } else { surface=SDL_SetVideoMode(setup.screen.x_sz,setup.screen.y_sz,32,SDL_OPENGL|SDL_FULLSCREEN); } if (surface==NULL) { sprintf(err_str,"SDL: Could not set video mode (Error: %s)",SDL_GetError()); return(FALSE); } // work around the dock losing minimize buttons because // of SDL luncancy #ifdef D3_OS_MAC if (!gl_in_window_mode()) SetSystemUIMode(kUIModeContentSuppressed,0); #endif // use glew on linux and windows #ifndef D3_OS_MAC glew_error=glewInit(); if (glew_error!=GL_NO_ERROR) { strcpy(err_str,glewGetErrorString(glew_error)); return(FALSE); } #endif // grab openGL attributes strncpy(render_info.name,(char*)glGetString(GL_RENDERER),64); render_info.name[63]=0x0; strncpy(render_info.ext_string,(char*)glGetString(GL_EXTENSIONS),8192); render_info.ext_string[8191]=0x0; glGetIntegerv(GL_MAX_TEXTURE_UNITS,&ntxtunit); render_info.texture_unit_count=(int)ntxtunit; glGetIntegerv(GL_MAX_TEXTURE_SIZE,&ntxtsize); render_info.texture_max_size=(int)ntxtsize; // in case screen is bigger than desired drawing surface render_info.monitor_x_sz=surface->w; render_info.monitor_y_sz=surface->h; render_info.view_x=(render_info.monitor_x_sz-setup.screen.x_sz)>>1; render_info.view_y=(render_info.monitor_y_sz-setup.screen.y_sz)>>1; // determine the referesh rate render_info.monitor_refresh_rate=60; // windows XP has a stuck refresh rate of 60 #ifdef D3_OS_MAC mode_info=CGDisplayCurrentMode(CGMainDisplayID()); if (mode_info!=NULL) { cf_rate=(CFNumberRef)CFDictionaryGetValue(mode_info,kCGDisplayRefreshRate); if (cf_rate) { CFNumberGetValue(cf_rate,kCFNumberIntType,&render_info.monitor_refresh_rate); if (render_info.monitor_refresh_rate==0) render_info.monitor_refresh_rate=60; } } #endif // clear the entire window so it doesn't flash glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); SDL_GL_SwapBuffers(); // setup renderer #ifdef D3_OS_MAC current_ctx=CGLGetCurrentContext(); rect[0]=render_info.view_x; rect[1]=render_info.view_y; rect[2]=setup.screen.x_sz; rect[3]=setup.screen.y_sz; CGLSetParameter(current_ctx,kCGLCPSwapRectangle,rect); CGLEnable(current_ctx,kCGLCESwapRectangle); if (lock_fps_refresh) { swapint=1; CGLSetParameter(current_ctx,kCGLCPSwapInterval,&swapint); } #endif glViewport(render_info.view_x,render_info.view_y,setup.screen.x_sz,setup.screen.y_sz); // perspective correction glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); // smoothing and anti-aliasing glDisable(GL_DITHER); glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT,GL_NICEST); if (fsaa_mode!=fsaa_mode_none) glEnable(GL_MULTISAMPLE); // texture compression glHint(GL_TEXTURE_COMPRESSION_HINT,GL_NICEST); glHint(GL_GENERATE_MIPMAP_HINT,GL_NICEST); // all alphas by 8 bit component glDisable(GL_ALPHA_TEST); // texture utility initialize gl_texture_initialize(); // do an initial draw if (!reset) { glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); SDL_GL_SwapBuffers(); } return(TRUE); }