DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/output/output_tools.h
00001 #ifndef DOSBOX_OUTPUT_TOOLS_H
00002 #define DOSBOX_OUTPUT_TOOLS_H
00003 
00004 #include <sys/types.h>
00005 #include <assert.h>
00006 #include <math.h>
00007 
00008 #include "dosbox.h"
00009 #include "sdlmain.h"
00010 
00011 // common headers and static routines reused in different outputs go there
00012 
00013 static inline int int_log2(int val)
00014 {
00015     int log = 0;
00016     while ((val >>= 1) != 0) log++;
00017     return log;
00018 }
00019 
00020 template <class WH>
00021 inline void aspectCorrectExtend(volatile WH &width, volatile WH &height)
00022 {
00023     if (width * sdl.srcAspect.y != height * sdl.srcAspect.x)
00024     {
00025         // abnormal aspect ratio detected, apply correction
00026         if (width * sdl.srcAspect.y > height * sdl.srcAspect.x)
00027         {
00028             // wide pixel ratio, height should be extended to fit
00029             height = (WH)floor((double)width * sdl.srcAspect.yToX + 0.5);
00030         }
00031         else
00032         {
00033             // long pixel ratio, width should be extended
00034             width = (WH)floor((double)height * sdl.srcAspect.xToY + 0.5);
00035         }
00036     }
00037 }
00038 
00039 template <class WH, class XY, class TWH>
00040 inline void aspectCorrectFitClip(volatile WH &clipW, volatile WH &clipH, volatile XY &clipX, volatile XY &clipY, const TWH fullW, const TWH fullH)
00041 {
00042     XY ax, ay;
00043     WH sh = fullH;
00044     WH sw = (WH)floor((double)fullH * sdl.srcAspect.xToY);
00045 
00046     if (sw > fullW) {
00047         sh = (WH)floor(((double)sh * fullW) / sw);
00048         sw = fullW;
00049     }
00050 
00051     ax = (XY)floor((fullW - sw) / 2);
00052     ay = (XY)floor((fullH - sh) / 2);
00053     if (ax < 0) ax = 0;
00054     if (ay < 0) ay = 0;
00055 
00056     clipX = ax; clipY = ay; clipW = sw; clipH = sh;
00057 
00058 //    assert((sdl.clip.x + sdl.clip.w) <= sdl.desktop.full.width);
00059 //    assert((sdl.clip.y + sdl.clip.h) <= sdl.desktop.full.height);
00060 }
00061 
00062 #endif