OSDN Git Service

0e4f9ad22215aaaa0cff7bf6873fdb057b0654fb
[gvonavish/GVONavish.git] / GVONavish / GVONavish / GVOGameProcess.cpp
1 #include "stdafx.h"
2 #include "GVONavish.h"
3 #include "GVOGameProcess.h"
4 #include "GVOWorldMap.h"
5 #include "GVOSurveyCoordExtractor.h"
6
7
8 // \89æ\91\9c\89ð\90Í\83f\83o\83b\83O\97p\81BGVOGameProcess
9 //#define GVO_ANALYZE_DEBUG
10
11 namespace {
12 #ifdef GVO_ANALYZE_DEBUG
13         LPCWSTR const k_debugImageFileName = L"debug5.bmp";
14 #endif
15
16         LPWSTR const k_gvoWindowClassName = L"Greate Voyages Online Game MainFrame";
17         LPWSTR const k_gvoWindowCaption = L"\91å\8dq\8aC\8e\9e\91ã Online";
18
19         const POINT k_surveyCoordOffsetFromRightBottom = { 70, 273 };
20         const SIZE k_surveyCoordSize = { 60, 11 };
21 };
22
23
24
25 void GVOGameProcess::clear()
26 {
27         if ( m_process ) {
28                 ::CloseHandle( m_process );
29                 m_process = NULL;
30         }
31         m_window = NULL;
32 }
33
34
35 void GVOGameProcess::setConfig( const GVOConfig& config )
36 {
37         m_surveyCoord = config.m_initialSurveyCoord;
38 }
39
40 bool GVOGameProcess::updateState()
41 {
42 #ifdef GVO_ANALYZE_DEBUG
43         {
44                 static bool done = false;
45                 if ( !done ) {
46                         done = true;
47                 }
48                 else {
49                         return false;
50                 }
51
52                 static GVOImage debugImage;
53                 if ( !debugImage.bitmapHandle() ) {
54                         if ( !debugImage.loadFromFile( ::g_makeFullPath( k_debugImageFileName ) ) ) {
55                                 ::MessageBox( NULL, L"\83f\83o\83b\83O\83C\83\81\81[\83W\82ª\82È\82¢\82æ", L"\82¦\82ç\81[", MB_ICONERROR );
56                                 exit( 0 );
57                         }
58                 }
59                 HDC hdc = ::GetWindowDC( ::GetDesktopWindow() );
60                 HDC hdcMem = ::CreateCompatibleDC( hdc );
61                 ::SaveDC( hdcMem );
62                 ::SelectObject( hdcMem, debugImage.bitmapHandle() );
63                 grabImage( hdcMem, POINT(), debugImage.size() );
64
65                 ::RestoreDC( hdcMem, -1 );
66                 ::DeleteDC( hdcMem );
67                 ::ReleaseDC( NULL, hdc );
68
69                 updateSurveyCoord();
70                 return true;
71         }
72 #endif
73         if ( !m_window ) {
74                 m_window = ::FindWindow( k_gvoWindowClassName, k_gvoWindowCaption );
75         }
76         if ( m_window ) {
77                 if ( !m_process ) {
78                         DWORD pid = 0;
79                         ::GetWindowThreadProcessId( m_window, &pid );
80                         m_process = ::OpenProcess( SYNCHRONIZE, FALSE, pid );
81                 }
82
83                 RECT rc;
84                 POINT clientOrg = { 0 };
85                 ::ClientToScreen( m_window, &clientOrg );
86                 ::GetClientRect( m_window, &rc );
87                 SIZE size;
88                 size.cx = rc.right;
89                 size.cy = rc.bottom;
90
91                 HDC hdc = ::GetDC( ::GetDesktopWindow() );
92                 if ( !hdc ) {
93                         return false;
94                 }
95                 grabImage( hdc, clientOrg, size );
96                 ::ReleaseDC( ::GetDesktopWindow(), hdc );
97                 m_timeStamp = ::timeGetTime();
98
99                 if ( !updateSurveyCoord() ) {
100                         return false;
101                 }
102                 return true;
103         }
104         return false;
105 }
106
107 void GVOGameProcess::grabImage( HDC hdc, const POINT& offset, const SIZE& size )
108 {
109         if ( !m_surveyCoordImage.bitmapHandle() ) {
110                 m_surveyCoordImage.createDIBImage( k_surveyCoordSize );
111         }
112
113         HDC hdcMem = ::CreateCompatibleDC( hdc );
114         ::SaveDC( hdcMem );
115
116         const int leftEdge = offset.x;
117         const int rightEdge = leftEdge + size.cx;
118         const int topEdge = offset.y;
119         const int bottomEdge = offset.y + size.cy;
120
121         // \91ª\97Ê\8dÀ\95W\83L\83\83\83v\83`\83\83
122         const int xSurvey = rightEdge - k_surveyCoordOffsetFromRightBottom.x;
123         const int ySurvey = bottomEdge - k_surveyCoordOffsetFromRightBottom.y;
124         ::SelectObject( hdcMem, m_surveyCoordImage.bitmapHandle() );
125         ::BitBlt( hdcMem, 0, 0, k_surveyCoordSize.cx, k_surveyCoordSize.cy, hdc, xSurvey, ySurvey, SRCCOPY );
126         ::GdiFlush();
127
128         ::RestoreDC( hdcMem, -1 );
129         ::DeleteDC( hdcMem );
130 }
131
132 bool GVOGameProcess::updateSurveyCoord()
133 {
134         bool succeeded = false;
135         GVOSurveyCoordExtractor extractor( m_surveyCoordImage );
136
137         const std::vector<int>& values = extractor.extractNumbers();
138         if ( values.size() == 2 ) {
139                 m_surveyCoord.x = values[0];
140                 m_surveyCoord.y = values[1];
141                 succeeded = true;
142         }
143         return succeeded;
144 }