Skip to content

Commit

Permalink
optimize virtual screen to avoid drawing offscreen on multiple screen…
Browse files Browse the repository at this point in the history
…s with different resolutions
  • Loading branch information
peterparker2k committed Aug 29, 2020
1 parent 2046308 commit 85b032e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 52 deletions.
4 changes: 2 additions & 2 deletions BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ bool BaseWindow::registerWindow()

// TODO: TitleString into Basewindow::strTitle

bool BaseWindow::createWindow()
bool BaseWindow::createWindow(int screen_index)
{

this->screen_index = screen_index;
BaseWindow* w=this;
// Install the CBT hook
// Note: hook the thread immediately before, and unhook it immediately after CreateWindow call.
Expand Down
5 changes: 3 additions & 2 deletions BaseWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <windows.h>
#include "resource.h"

class BaseWindow
class BaseWindow
{
private:
static bool isRegistered;
Expand All @@ -25,6 +25,7 @@ class BaseWindow
DWORD dwExStyle;
DWORD dwStyle;
RECT rctPosition;
int screen_index;
public:
HWND getWindowHandle();
void setParentHandle(HWND h);
Expand All @@ -34,7 +35,7 @@ class BaseWindow
DWORD getStyle(bool getExStyle=false);
void setStyles(DWORD exStyle=-1, DWORD style=-1);

bool createWindow();
bool createWindow(int screen_index);
int MessageLoop();

BaseWindow();
Expand Down
6 changes: 3 additions & 3 deletions Matrix/MatrixWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MatrixWindow::MatrixWindow()
{
ilter=0;
Expand Down Expand Up @@ -62,7 +62,7 @@ void MatrixWindow::UpdateFunction(int dTime)
{
Cursor.NextFrame();
Screen->TheMatrixShow(dTime);
Screen->ShowScreen(&Text, &Cursor); // Zeigt den Textbildschirm an....
Screen->ShowScreen(&Text, &Cursor, screen_index); // Zeigt den Textbildschirm an....
}

void MatrixWindow::loadGFX(HWND GMainHwnd, int wid, int hei)
Expand All @@ -74,7 +74,7 @@ void MatrixWindow::loadGFX(HWND GMainHwnd, int wid, int hei)
ReleaseDC(GMainHwnd, dc);

Screen=new Show(wid/CHARWIDTH,hei/CHARHEIGHT);

Screen->transformMultiScreen();
Team.destination=GMainHwnd;
Team.sourceDC=src;
Team.AddAnimationRect(368,289,639,479);
Expand Down
129 changes: 92 additions & 37 deletions Matrix/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//#include "screensave.h"
#include "Bob.h"
#include "Screen.h"
#include "../ScreensaverApp.h"


//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -45,15 +46,60 @@ void CScreen::Clear()
*laufpointer++=Translate((int)(' '));
}

/* Transform the different Monitors into CScreen Space
This avoids heavy draw outside any screen
if screensizes are not equal or not aligned
*/
void CScreen::transformMultiScreen(){
RECT maxDimensions;
maxDimensions.left = maxDimensions.top = INT_MAX;
maxDimensions.right = maxDimensions.bottom = 0;
for(auto minfo:ScreensaverApp::getDisplays()){
RECT tmp = RECT(minfo->rcMonitor);
tmp.bottom+=tmp.top; // convert from x/y, w,h to x1/y1, x2/y2
tmp.right+=tmp.left;
if (tmp.left<maxDimensions.left)
maxDimensions.left=tmp.left;
if (tmp.top<maxDimensions.top)
maxDimensions.top=tmp.top;
if (tmp.right>maxDimensions.right)
maxDimensions.right=tmp.right;
if (tmp.bottom>maxDimensions.bottom)
maxDimensions.bottom=tmp.bottom;
}
for(auto minfo:ScreensaverApp::getDisplays()){
RECT* charArea = new RECT();
RECT tmp = RECT(minfo->rcMonitor);
if (CRegistry::MatrixShow[REGISTRY_VIRTUAL]){
charArea->left = (tmp.left - maxDimensions.left)/CHARWIDTH;
charArea->top = (tmp.top - maxDimensions.top)/CHARHEIGHT;
}else{
charArea->left = 0;
charArea->top = 0;
}
charArea->right = tmp.right/CHARWIDTH + charArea->left;
charArea->bottom =tmp.bottom/CHARHEIGHT + charArea->top;
monitor_dimensions.push_back(charArea);
}
}
RECT CScreen::clipRectangle(RECT src, RECT clip){
RECT retval = src;
retval.left = std::max(src.left, clip.left);
retval.top = std::max(src.top, clip.top);
retval.right = std::min(src.right, clip.right);
retval.bottom = std::min(src.bottom, clip.bottom);
return retval;
}

void inline CScreen::SetChar(int x, int y, int wert, int intensity)
{
wert=Translate(wert);
if (intensity<4)
wert+=intensity;
else
if (intensity<4)
wert+=intensity;
else
wert+=rand()%4;

if (IsVisible(x,y))
if (IsVisible(x,y))
m_ptrMemory[x + y*Width] = wert;
}

Expand All @@ -68,40 +114,49 @@ void CScreen::Scroll(int Line)
}
}

void CScreen::ShowScreen(CBob* Letter, CBob* Cursor)
void CScreen::ShowScreen(CBob* Letter, CBob* Cursor, int screen_index)
{
long iTimeStart=timeGetTime();

int* DisplayMem = m_ptrMemory ;
int character;
int xpos, ypos;
DisplayMem+=UpdateArea->top*Width;

HDC dc=GetDC(Letter->destination);
std::vector<RECT*> toShow;

for (int i=UpdateArea->top; i<UpdateArea->bottom;i++)
{
for (int a=UpdateArea->left; a<UpdateArea->right;a++)
{
character=*DisplayMem++;
xpos=((character>>2)%40)*CHARWIDTH;
ypos=( (character & 0x03)*3 + (character/160) )*CHARHEIGHT;

Letter->SetSrcRect(xpos,ypos,xpos+CHARWIDTH, ypos+CHARHEIGHT);
if (m_iCursor_X==a && m_iCursor_Y==i)
Cursor->Show(dc, CHARWIDTH*a,CHARHEIGHT*i);
else
Letter->Show(dc, CHARWIDTH*a,CHARHEIGHT*i);
}
DisplayMem+=(Width - UpdateArea->right + UpdateArea->left);
}
if (screen_index<0){
toShow = monitor_dimensions;
}else {
toShow.push_back(monitor_dimensions[screen_index]);
}
for(auto mon_rect:toShow){
RECT clippedUpdate = clipRectangle(*UpdateArea, *mon_rect);

HDC dc=GetDC(Letter->destination);
int* DisplayMem = m_ptrMemory ;
int character;
int xpos, ypos;
DisplayMem+=clippedUpdate.top*Width + clippedUpdate.left;
for (int i=clippedUpdate.top; i<clippedUpdate.bottom;i+=1)
{
for (int a=clippedUpdate.left; a<clippedUpdate.right;a+=1)
{
character=*DisplayMem++;
xpos=((character>>2)%40)*CHARWIDTH;
ypos=( (character & 0x03)*3 + (character/160) )*CHARHEIGHT;

Letter->SetSrcRect(xpos,ypos,xpos+CHARWIDTH, ypos+CHARHEIGHT);
if (m_iCursor_X==a && m_iCursor_Y==i)
Cursor->Show(dc, CHARWIDTH*a,CHARHEIGHT*i);
else
Letter->Show(dc, CHARWIDTH*a,CHARHEIGHT*i);
}
DisplayMem+=(Width - clippedUpdate.right + clippedUpdate.left);
}

// FPS Counter..
long iTimeEnd=timeGetTime()-iTimeStart;
char message[50];
wsprintf(message,"elapsed Time: %d", iTimeEnd);
//TextOut(dc,100,100,message, strlen(message));
ReleaseDC(Letter->destination, dc);
// FPS Counter..
long iTimeEnd=timeGetTime()-iTimeStart;
char message[50];
wsprintf(message,"elapsed Time: %d", iTimeEnd);
//TextOut(dc,100+clippedUpdate.left*CHARWIDTH,100+clippedUpdate.top*CHARHEIGHT,message, strlen(message));
ReleaseDC(Letter->destination, dc);
}
}

int CScreen::Translate(int Zeichen)
Expand All @@ -115,8 +170,8 @@ int CScreen::Translate(int Zeichen)
if (TranslationMap[translation]==TransByte) break;
translation++;
}
if (m_bolCryptic)
while (translation>0)
if (m_bolCryptic)
while (translation>0)
translation-=25; //Texte werden verschluesselt!
Returnwert=(translation+25)<<2;
return (Returnwert);
Expand Down Expand Up @@ -156,7 +211,7 @@ void CScreen::PrintLen(int x, int y, std::string str,int Len, int intensity)
int index=0;
while ((count<Len) && (x<Width) && (str[index]!=0))
{
if (intensity==-1)
if (intensity==-1)
{
SetChar(x,y,(int)(str[index++]), GetIntensity(x,y));
x++;
Expand Down Expand Up @@ -217,7 +272,7 @@ int CScreen::GetChar(int x, int y)
bool CScreen::IsVisible(int x, int y)
{
bool Vis=false;
if ((x>=0) && (x<Width) && (y>=0) && (y<Height))
if ((x>=0) && (x<Width) && (y>=0) && (y<Height))
Vis=true;
return Vis;
}
Expand Down
7 changes: 5 additions & 2 deletions Matrix/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define CHARWIDTH 16
#define CHARHEIGHT 24

class CScreen
class CScreen
{
public:
int GetIntensity(int x, int y);
Expand All @@ -34,9 +34,11 @@ class CScreen
void Print(int x, int y, std::string str,int intensity=0);
void PrintLen(int x, int y, std::string str, int len,int intensity=-1);
void VPrint(int x, int y, std::string str,int intensity=0);
void ShowScreen(CBob *Letter, CBob* Cursor);
void ShowScreen(CBob *Letter, CBob* Cursor, int screen_index);
void Scroll(int Line);
void Clear();
virtual void transformMultiScreen();
RECT clipRectangle(RECT src, RECT clip);
void SetChar(int x, int y, int wert, int intensity=0);
CScreen(int x, int y);
virtual ~CScreen();
Expand All @@ -45,6 +47,7 @@ class CScreen
int* m_ptrMemory;
protected:
RECT* UpdateArea;
std::vector<RECT*> monitor_dimensions;
bool IsVisible(int x, int y);
bool m_bolCryptic;
int Translate(int Zeichen);
Expand Down
1 change: 1 addition & 0 deletions Matrix/Show.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ bool Show::ShowTheMatrix(bool timeout)
iTimer=-1;
return false;
}
SetCrypto(false);
}

bool Show::ShowPhone()
Expand Down
6 changes: 3 additions & 3 deletions ScreensaverApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int ScreensaverApp::run()
vecDisplays[0]->Window.setPosition(rcPos);
vecDisplays[0]->Window.setScreensaverMode(iScreensaverMode);

if (vecDisplays[0]->Window.createWindow())
if (vecDisplays[0]->Window.createWindow(0))
retVal=vecDisplays[0]->Window.MessageLoop();
}
break;
Expand Down Expand Up @@ -88,7 +88,7 @@ int ScreensaverApp::run()
for (unsigned int x=0;x<vecDisplays.size();x++)
{
vecDisplays[x]->Window.setScreensaverMode(iScreensaverMode);
vecDisplays[x]->Window.createWindow();
vecDisplays[x]->Window.createWindow(x);
}
} else {
RECT rcAll;
Expand All @@ -111,7 +111,7 @@ int ScreensaverApp::run()
rcAll.right-=rcAll.left;
vecDisplays[0]->Window.setPosition(rcAll);
vecDisplays[0]->Window.setScreensaverMode(iScreensaverMode);
vecDisplays[0]->Window.createWindow();
vecDisplays[0]->Window.createWindow(-1);
}
}
retVal=vecDisplays[0]->Window.MessageLoop();
Expand Down
6 changes: 3 additions & 3 deletions ScreensaverApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MonitorInfo
};


class ScreensaverApp
class ScreensaverApp
{
private:
bool isSingleInstance();
Expand All @@ -42,13 +42,13 @@ class ScreensaverApp
static bool CALLBACK MonitorEnum(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
static bool isInitialized;
int InterpreteCommandLine();
HWND parentHwnd;
HWND parentHwnd;
protected:
int iScreensaverMode;
public:
ScreensaverApp();
virtual ~ScreensaverApp();

static std::vector<MonitorInfo*> getDisplays(){return vecDisplays;}
virtual int init();
virtual int run();
virtual int exit();
Expand Down

0 comments on commit 85b032e

Please sign in to comment.