src/render/rSysdep.cpp File Reference

#include "defs.h"
#include "rGL.h"
#include "rSDL.h"
#include "rSysdep.h"
#include "tInitExit.h"
#include "tDirectories.h"
#include "tSysTime.h"
#include "rConsole.h"
#include "aa_config.h"
#include <iostream>
#include "rScreen.h"
#include "rTexture.h"
#include "tCommandLine.h"
#include "tConfiguration.h"
#include "tRecorder.h"
#include "rTextureRenderTarget.h"
#include <memory>
#include "SDL_thread.h"
#include "SDL_mutex.h"
#include <png.h>
#include <unistd.h>
#include <SDL_syswm.h>

Include dependency graph for rSysdep.cpp:

Go to the source code of this file.

Classes

class  PerformanceCounter
class  rFastForwardCommandLineAnalyzer

Defines

#define SCREENSHOT_PNG_BITDEPTH   8
#define SCREENSHOT_BYTES_PER_PIXEL   3

Functions

static void SDL_SavePNG (SDL_Surface *image, tString filename)
static void make_screenshot ()
static void breakpoint ()
int sr_NetSyncThread (void *lockVoid)
int NextPowerOfTwo (int in)
bool sr_MotionBlurCore (REAL alpha, rTextureRenderTarget &blurTarget)
bool sr_MotionBlur (double time, std::auto_ptr< rTextureRenderTarget > &blurTarget)
static void stuff_init ()
void sr_LockSDL ()
void sr_UnlockSDL ()

Variables

bool sr_screenshotIsPlanned = false
static bool s_videoout = false
static int s_videooutDest = fileno(stdout)
static bool png_screenshot = true
static tConfItem< bool > pns ("PNG_SCREENSHOT", png_screenshot)
static double s_nextFastForwardFrameRecorded = 0
static double s_nextFastForwardFrameReal = 0
static REAL sr_FF_Maxstep = 1
static tSettingItem< REALc_ff ("FAST_FORWARD_MAXSTEP", sr_FF_Maxstep)
static REAL sr_FF_MaxstepReal = .05
static tSettingItem< REALc_ffre ("FAST_FORWARD_MAXSTEP_REAL", sr_FF_MaxstepReal)
static REAL sr_FF_MaxstepRel = 1
static tSettingItem< REALc_ffr ("FAST_FORWARD_MAXSTEP_REL", sr_FF_MaxstepRel)
static double s_fastForwardTo = 0
static bool s_fastForward = false
static bool s_benchmark = false
static
rFastForwardCommandLineAnalyzer 
analyzer
static bool sr_netSyncThreadGoOn = true
static rSysDep::rNetIdlersr_netIdler = NULL
static SDL_Thread * sr_netSyncThread = NULL
static SDL_mutex * sr_netLock = NULL
static REAL sr_motionBlurTime = .0075
static tSettingItem< REALc_mb ("MOTION_BLUR_TIME", sr_motionBlurTime)
static SDL_mutex * mut
static tInitExit stuff_ie & stuff_init


Define Documentation

#define SCREENSHOT_BYTES_PER_PIXEL   3

Definition at line 59 of file rSysdep.cpp.

Referenced by SDL_SavePNG().

#define SCREENSHOT_PNG_BITDEPTH   8

Definition at line 58 of file rSysdep.cpp.

Referenced by SDL_SavePNG().


Function Documentation

static void breakpoint (  )  [static]

Definition at line 518 of file rSysdep.cpp.

Referenced by rSysDep::SwapGL().

00519 {}

Here is the caller graph for this function:

static void make_screenshot (  )  [static]

Definition at line 313 of file rSysdep.cpp.

References png_screenshot, s_videoout, s_videooutDest, tDirectories::Screenshot(), SDL_SavePNG(), sr_screenHeight, sr_screenshotIsPlanned, and sr_screenWidth.

Referenced by rSysDep::SwapGL().

00314                              {
00315 #ifndef DEDICATED
00316     // screenshot count
00317     static int number=0;
00318     number++;
00319 
00320     SDL_Surface *image;
00321     SDL_Surface *temp;
00322     int idx;
00323     image = SDL_CreateRGBSurface(SDL_SWSURFACE, sr_screenWidth, sr_screenHeight,
00324                                  24, 0x0000FF, 0x00FF00, 0xFF0000 ,0);
00325     temp = SDL_CreateRGBSurface(SDL_SWSURFACE, sr_screenWidth, sr_screenHeight,
00326                                 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
00327 
00328     // make upside down screenshot (make sure it comes from the screen)
00329 
00330     glReadPixels(0,0,sr_screenWidth, sr_screenHeight, GL_RGB,
00331                  GL_UNSIGNED_BYTE, image->pixels);
00332 
00333     // turn image around
00334     for (idx = 0; idx < sr_screenHeight; idx++)
00335     {
00336         memcpy(reinterpret_cast<char *>(temp->pixels) + 3 * sr_screenWidth * idx,
00337                reinterpret_cast<char *>(image->pixels)+ 3
00338                * sr_screenWidth*(sr_screenHeight - idx-1),
00339                3*sr_screenWidth);
00340     }
00341 
00342     if (s_videoout)
00343         write(s_videooutDest, temp->pixels, sr_screenWidth * sr_screenHeight * 3);
00344 
00345     if (sr_screenshotIsPlanned) {
00346         // save screenshot in unused slot
00347         bool done = false;
00348         while ( !done )
00349         {
00350             // generate filename
00351             tString fileName("screenshot_");
00352             fileName << number;
00353             if (png_screenshot)
00354                 fileName << ".png";
00355             else
00356                 fileName << ".bmp";
00357 
00358             // test if file exists
00359             std::ifstream s;
00360             if ( tDirectories::Screenshot().Open( s, fileName ) )
00361             {
00362                 // yes! try next number
00363                 number++;
00364                 continue;
00365             }
00366 
00367             // save image
00368             if (png_screenshot)
00369                 SDL_SavePNG(image, tDirectories::Screenshot().GetWritePath( fileName ));
00370             else
00371                 SDL_SaveBMP(temp, tDirectories::Screenshot().GetWritePath( fileName ) );
00372             done = true;
00373         }
00374     }
00375 
00376     // cleanup
00377     SDL_FreeSurface(image);
00378     SDL_FreeSurface(temp);
00379 #endif

Here is the call graph for this function:

Here is the caller graph for this function:

int NextPowerOfTwo ( int  in  ) 

Definition at line 602 of file rSysdep.cpp.

References x.

Referenced by sr_MotionBlur().

00604 {
00605     int x = 1;
00606     while ( x * 32 <= in )
00607         x <<= 5;
00608     while ( x < in )
00609         x <<= 1;
00610 
00611     return x;

Here is the caller graph for this function:

static void SDL_SavePNG ( SDL_Surface *  image,
tString  filename 
) [static]

Definition at line 264 of file rSysdep.cpp.

References free, malloc, NULL, SCREENSHOT_BYTES_PER_PIXEL, SCREENSHOT_PNG_BITDEPTH, sr_screenHeight, and sr_screenWidth.

Referenced by make_screenshot().

00265                                                              {
00266     png_structp png_ptr;
00267     png_infop info_ptr;
00268     png_byte **row_ptrs;
00269     int i;
00270     static FILE *fp;
00271 
00272     if (!(fp = fopen(filename, "wb"))) {
00273         fprintf(stderr, "can't open file for writing\n");
00274         return;
00275     }
00276 
00277     if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
00278         return;
00279     }
00280 
00281     if (!(info_ptr = png_create_info_struct(png_ptr))) {
00282         png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
00283         return;
00284     }
00285 
00286     png_init_io(png_ptr, fp);
00287 
00288     png_set_IHDR(png_ptr, info_ptr, sr_screenWidth, sr_screenHeight,
00289                  SCREENSHOT_PNG_BITDEPTH, PNG_COLOR_TYPE_RGB,
00290                  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
00291                  PNG_FILTER_TYPE_DEFAULT);
00292     png_write_info(png_ptr, info_ptr);
00293 
00294     // get pointers
00295     if (!(row_ptrs = (png_byte**) malloc(sr_screenHeight * sizeof(png_byte*)))) {
00296         png_destroy_write_struct(&png_ptr, &info_ptr);
00297         return;
00298     }
00299 
00300     for (i = 0; i < sr_screenHeight; i++) {
00301         row_ptrs[i] = (png_byte *)image->pixels + (sr_screenHeight - i - 1)
00302                       * SCREENSHOT_BYTES_PER_PIXEL * sr_screenWidth;
00303     }
00304 
00305     png_write_image(png_ptr, row_ptrs);
00306     png_write_end(png_ptr, info_ptr);
00307     png_destroy_write_struct(&png_ptr, &info_ptr);
00308 
00309     free(row_ptrs);
00310     fclose(fp);

Here is the caller graph for this function:

void sr_LockSDL (  ) 

Definition at line 1002 of file rSysdep.cpp.

Referenced by rSurface::Create(), sr_ExitDisplay(), sr_InitDisplay(), su_GetSDLInput(), rSysDep::SwapGL(), rISurfaceTexture::Upload(), and welcome().

01003                  {
01004     //std::cerr << "locking...";
01005 #ifndef DEDICATED
01006 #ifndef WIN32
01007     //SDL_mutexP(mut);
01008 #endif
01009 #endif
01010     //std::cerr << " locked!\n";

Here is the caller graph for this function:

bool sr_MotionBlur ( double  time,
std::auto_ptr< rTextureRenderTarget > &  blurTarget 
)

Definition at line 705 of file rSysdep.cpp.

References ArmageTron_VSync_MotionBlur, ArmageTron_VSync_Off, con, currentScreensetting, lastTime, NextPowerOfTwo(), REAL, sr_MotionBlurCore(), sr_motionBlurTime, sr_screenHeight, sr_screenWidth, and rScreenSettings::vSync.

Referenced by rSysDep::SwapGL().

00707 {
00708     static bool lastActive = false;
00709     bool active = false;
00710 
00711     // measure frame rendering time
00712     static double lastTime = time;
00713     REAL frameTime = time - lastTime;
00714 
00715     if ( currentScreensetting.vSync == ArmageTron_VSync_MotionBlur )
00716     {
00717         // use hysteresis to autodisable motion blurring if rendering gets
00718         // far too slow and reenable it if rendering gets fast enough again
00719         static int hyster = 0;
00720         static int thresh = 100;
00721         active = lastActive;
00722 
00723         if ( frameTime * 2 < sr_motionBlurTime )
00724         {
00725             if ( ++hyster > thresh )
00726             {
00727                 active = true;
00728                 hyster = thresh;
00729             }
00730         }
00731         else if ( frameTime > sr_motionBlurTime * 2 )
00732         {
00733             if ( --hyster < -thresh )
00734             {
00735                 active = false;
00736                 hyster = -thresh;
00737             }
00738         }
00739         else
00740         {
00741             if ( hyster < 0 )
00742                 ++hyster;
00743             else
00744                 --hyster;
00745         }
00746 
00747         lastTime = time;
00748     }
00749 
00750     // really blur.
00751     if ( lastActive )
00752     {
00753         // determine blur texture size
00754         int blurWidth = NextPowerOfTwo( sr_screenWidth );
00755         int blurHeight = NextPowerOfTwo( sr_screenHeight );
00756 
00757         // destroy existing blur texture if it is too small
00758         if ( blurTarget.get() && ( blurTarget->GetWidth() < blurWidth || blurTarget->GetHeight() < blurHeight ) )
00759         {
00760             blurTarget = std::auto_ptr< rTextureRenderTarget >();
00761         }
00762 
00763         // create blur texture
00764         if ( !blurTarget.get() )
00765         {
00766             try
00767             {
00768                 blurTarget = std::auto_ptr< rTextureRenderTarget >( new rTextureRenderTarget( blurWidth, blurHeight  ) );
00769             }
00770             catch( rExceptionGLEW const & e )
00771             {
00772                 // unsupported. Disable motion blur.
00773                 currentScreensetting.vSync = ArmageTron_VSync_Off;
00774                 lastActive = false;
00775 
00776                 con << tOutput("$screen_vsync_motionblur_unsupported");
00777 
00778                 return true;
00779             }
00780         }
00781 
00782         // really blur
00783         bool ret = sr_MotionBlurCore( 1 - frameTime / sr_motionBlurTime, *blurTarget );
00784 
00785         // store active value for the next frame
00786         lastActive = active;
00787 
00788         // if the next frame won't be blurred, deactivate rendering to the texture
00789         if ( !active )
00790         {
00791             blurTarget->Pop();
00792         }
00793 
00794         return ret;
00795     }
00796 
00797     lastActive = active;
00798 
00799     // no motion blur happened when we got here
00800     if ( blurTarget.get() && blurTarget->IsTarget() )
00801     {
00802         blurTarget->Pop();
00803     }
00804 
00805     return true;

Here is the call graph for this function:

Here is the caller graph for this function:

bool sr_MotionBlurCore ( REAL  alpha,
rTextureRenderTarget blurTarget 
)

Definition at line 613 of file rSysdep.cpp.

References con, rTextureRenderTarget::GetHeight(), rTextureRenderTarget::GetWidth(), glBegin, glEnd, glMatrixMode, rTextureRenderTarget::IsTarget(), rTextureRenderTarget::Pop(), rTextureRenderTarget::Push(), REAL, rITexture::Select(), sr_CheckGLError(), sr_screenHeight, and sr_screenWidth.

Referenced by sr_MotionBlur().

00615 {
00616     sr_CheckGLError();
00617 
00618     if ( alpha < 0 )
00619         alpha = 0;
00620 
00621     {
00622         if ( blurTarget.IsTarget() )
00623         {
00624             blurTarget.Pop();
00625 
00626             blurTarget.Select();
00627 
00628             glDrawBuffer( GL_FRONT );
00629 
00630             sr_CheckGLError();
00631 
00632             // determine the texture coordinates of the lower right corner
00633             REAL maxu = REAL(sr_screenWidth)/blurTarget.GetWidth();
00634             REAL maxv = REAL(sr_screenHeight)/blurTarget.GetHeight();
00635 
00636             glEnable(GL_TEXTURE_2D);
00637 
00638             // blend the last frame and the current frame with the specified alpha value
00639             glDisable( GL_DEPTH_TEST );
00640             glDepthMask(0);
00641 
00642             glMatrixMode( GL_PROJECTION );
00643             glLoadIdentity();
00644             glMatrixMode( GL_MODELVIEW );
00645             glLoadIdentity();
00646             glViewport(0,0,sr_screenWidth, sr_screenHeight);
00647 
00648             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,
00649                             GL_NEAREST);
00650             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
00651                             GL_NEAREST);
00652 
00653             glDisable(GL_ALPHA_TEST);
00654             // glDisable(GL_BLEND);
00655             glEnable(GL_BLEND);
00656             glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
00657 
00658             glDisable(GL_LIGHTING);
00659 
00660             glBegin( GL_QUADS );
00661             glColor4f( 1,1,1,alpha );
00662 
00663             glTexCoord2f( 0, 0 );
00664             glVertex2f( -1, -1 );
00665 
00666             glTexCoord2f( maxu, 0 );
00667             glVertex2f( 1, -1 );
00668 
00669             glTexCoord2f( maxu, maxv );
00670             glVertex2f( 1, 1 );
00671 
00672             glTexCoord2f( 0, maxv );
00673             glVertex2f( -1, 1 );
00674             glEnd();
00675 
00676             sr_CheckGLError();
00677 
00678             // clean up
00679             glDepthMask(1);
00680 
00681             glDrawBuffer( GL_BACK );
00682         }
00683 
00684         blurTarget.Push();
00685 
00686         sr_CheckGLError();
00687 
00688         return false;
00689     }
00690 
00691     return true;
00692 
00693 #if 0
00694     GLenum error = glGetError();
00695     if ( error != GL_NO_ERROR )
00696         con << "GL error " << error << "\n";
00697 #endif

Here is the call graph for this function:

Here is the caller graph for this function:

int sr_NetSyncThread ( void *  lockVoid  ) 

Definition at line 522 of file rSysdep.cpp.

References rSysDep::rNetIdler::Do(), sr_glOut, sr_netSyncThreadGoOn, and rSysDep::rNetIdler::Wait().

Referenced by rSysDep::StartNetSyncThread().

00524 {
00525     SDL_mutex *lock = (SDL_mutex *)lockVoid;
00526 
00527     SDL_mutexP(lock);
00528 
00529     while ( sr_netSyncThreadGoOn )
00530     {
00531         SDL_mutexV(lock);
00532         // wait for network data
00533         bool toDo = sr_netIdler->Wait();
00534         SDL_mutexP(lock);
00535 
00536         if ( toDo )
00537         {
00538             // disable rendering (during auto-scrolling of console, for example)
00539             bool glout = sr_glOut;
00540             sr_glOut = false;
00541 
00542             // new network data arrived, handle it
00543             sr_netIdler->Do();
00544 
00545             // enable rendering again
00546             sr_glOut = glout;
00547         }
00548     }
00549 
00550     SDL_mutexV(lock);
00551 
00552     return 0;

Here is the call graph for this function:

Here is the caller graph for this function:

void sr_UnlockSDL (  ) 

Definition at line 1012 of file rSysdep.cpp.

Referenced by rSurface::Create(), sr_ExitDisplay(), sr_InitDisplay(), su_GetSDLInput(), rSysDep::SwapGL(), rISurfaceTexture::Upload(), and welcome().

01013                    {
01014     //std::cerr << "unlocking...";
01015 #ifndef DEDICATED
01016 #ifndef WIN32
01017     //SDL_mutexV(mut);
01018 #endif
01019 #endif
01020     //std::cerr << " unlocked!\n";

Here is the caller graph for this function:

static void stuff_init (  )  [static]

Definition at line 995 of file rSysdep.cpp.

References mut.

00996                         {
00997     mut=SDL_CreateMutex();


Variable Documentation

rFastForwardCommandLineAnalyzer analyzer [static]

Definition at line 484 of file rSysdep.cpp.

tSettingItem<REAL> c_ff("FAST_FORWARD_MAXSTEP", sr_FF_Maxstep) [static]

tSettingItem<REAL> c_ffr("FAST_FORWARD_MAXSTEP_REL", sr_FF_MaxstepRel) [static]

tSettingItem<REAL> c_ffre("FAST_FORWARD_MAXSTEP_REAL", sr_FF_MaxstepReal) [static]

tSettingItem<REAL> c_mb("MOTION_BLUR_TIME", sr_motionBlurTime) [static]

SDL_mutex* mut [static]

Definition at line 993 of file rSysdep.cpp.

Referenced by stuff_init().

bool png_screenshot = true [static]

Definition at line 260 of file rSysdep.cpp.

Referenced by make_screenshot().

tConfItem<bool> pns("PNG_SCREENSHOT", png_screenshot) [static]

bool s_benchmark = false [static]

Definition at line 425 of file rSysdep.cpp.

Referenced by rFastForwardCommandLineAnalyzer::DoAnalyze(), and rSysDep::SwapGL().

bool s_fastForward = false [static]

Definition at line 424 of file rSysdep.cpp.

Referenced by rFastForwardCommandLineAnalyzer::DoAnalyze(), and rSysDep::SwapGL().

double s_fastForwardTo = 0 [static]

Definition at line 423 of file rSysdep.cpp.

Referenced by rFastForwardCommandLineAnalyzer::DoAnalyze(), and rSysDep::SwapGL().

double s_nextFastForwardFrameReal = 0 [static]

Definition at line 406 of file rSysdep.cpp.

Referenced by rSysDep::SwapGL().

double s_nextFastForwardFrameRecorded = 0 [static]

Definition at line 405 of file rSysdep.cpp.

Referenced by rSysDep::SwapGL().

bool s_videoout = false [static]

Definition at line 257 of file rSysdep.cpp.

Referenced by rFastForwardCommandLineAnalyzer::DoAnalyze(), make_screenshot(), and rSysDep::SwapGL().

int s_videooutDest = fileno(stdout) [static]

Definition at line 258 of file rSysdep.cpp.

Referenced by rFastForwardCommandLineAnalyzer::DoAnalyze(), and make_screenshot().

REAL sr_FF_Maxstep = 1 [static]

Definition at line 410 of file rSysdep.cpp.

Referenced by rSysDep::SwapGL().

REAL sr_FF_MaxstepReal = .05 [static]

Definition at line 414 of file rSysdep.cpp.

Referenced by rSysDep::SwapGL().

REAL sr_FF_MaxstepRel = 1 [static]

Definition at line 418 of file rSysdep.cpp.

Referenced by rSysDep::SwapGL().

REAL sr_motionBlurTime = .0075 [static]

Definition at line 700 of file rSysdep.cpp.

Referenced by sr_MotionBlur().

rSysDep::rNetIdler* sr_netIdler = NULL [static]

Definition at line 521 of file rSysdep.cpp.

SDL_mutex* sr_netLock = NULL [static]

Definition at line 555 of file rSysdep.cpp.

Referenced by rSysDep::StartNetSyncThread(), rSysDep::StopNetSyncThread(), and rSysDep::SwapGL().

SDL_Thread* sr_netSyncThread = NULL [static]

Definition at line 554 of file rSysdep.cpp.

Referenced by rSysDep::StartNetSyncThread(), and rSysDep::StopNetSyncThread().

bool sr_netSyncThreadGoOn = true [static]

Definition at line 520 of file rSysdep.cpp.

Referenced by sr_NetSyncThread(), and rSysDep::StopNetSyncThread().

bool sr_screenshotIsPlanned = false

Definition at line 256 of file rSysdep.cpp.

Referenced by make_screenshot(), screenshot_func(), and rSysDep::SwapGL().

tInitExit stuff_ie& stuff_init [static]

Definition at line 999 of file rSysdep.cpp.


Generated on Sat Mar 15 23:04:12 2008 for Armagetron Advanced by  doxygen 1.5.4