forked from gdpinchina/DeeperGTAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenCapturer.cpp
84 lines (70 loc) · 2.24 KB
/
ScreenCapturer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "ScreenCapturer.h"
#include "lib/natives.h"
#include "defaults.h"
#include <stdlib.h>
#include <stdio.h>
ScreenCapturer::ScreenCapturer() : pixels(NULL), isActivate(false){
}
ScreenCapturer::~ScreenCapturer() {
DestroyCapturer();
}
void ScreenCapturer::setDatasetParams(const Value & dc, Document & d)
{
if (!dc["frame"].IsNull()) {
DestroyCapturer();
if (!dc["frame"][0].IsNull()) imageWidth = dc["frame"][0].GetInt();
else imageWidth = _WIDTH_;
if (!dc["frame"][1].IsNull()) imageHeight = dc["frame"][1].GetInt();
else imageHeight = _HEIGHT_;
isActivate = true;
// Round up the scan line size to a multiple of 4
length = ((imageWidth * 3 + 3) / 4 * 4) * imageHeight;
//Screen capture buffer
GRAPHICS::_GET_SCREEN_ACTIVE_RESOLUTION(&windowWidth, &windowHeight);
hWnd = ::FindWindow(NULL, "Grand Theft Auto V");
//hWindowDC = GetDC(NULL);
hWindowDC = GetDC(hWnd);
//the capture tool's object with memory
hCaptureDC = CreateCompatibleDC(hWindowDC);
//the bitmap format object
hCaptureBitmap = CreateCompatibleBitmap(hWindowDC, imageWidth, imageHeight);
SelectObject(hCaptureDC, hCaptureBitmap);
SetStretchBltMode(hCaptureDC, COLORONCOLOR);
pixels = (UINT8*)malloc(length);
info.biSize = sizeof(BITMAPINFOHEADER);
info.biPlanes = 1;
info.biBitCount = 24;
info.biWidth = imageWidth;
info.biHeight = -imageHeight;
info.biCompression = BI_RGB;
info.biSizeImage = 0;
}
else DestroyCapturer();
}
void ScreenCapturer::DestroyCapturer()
{
if (!isActivate) return;
if (pixels != NULL) {
free(pixels);
pixels = NULL;
}
isActivate = false;
ReleaseDC(hWnd, hWindowDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}
bool ScreenCapturer::isCapturerActivate()
{
return isActivate;
}
void ScreenCapturer::capture() {
//time cost is about 15~32ms
// copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit
switch (isActivate)
{
case true:
StretchBlt(hCaptureDC, 0, 0, imageWidth, imageHeight, hWindowDC, 0, 0, windowWidth, windowHeight, SRCCOPY);
GetDIBits(hCaptureDC, hCaptureBitmap, 0, imageHeight, pixels, (BITMAPINFO*)&info, DIB_RGB_COLORS);
break;
}
}