-
Notifications
You must be signed in to change notification settings - Fork 4
/
videoc-gdi.cpp
180 lines (143 loc) · 4.33 KB
/
videoc-gdi.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "io.h"
#ifdef VIDEO_GDI
#define video cvideo
#undef bind
#include <windows.h>
#define bind bind_func
//force some year-old C code to compile properly as C++ - I decided to switch long ago but still haven't finished.
#define this This
//this file is based on ruby by byuu
struct video_gdi {
struct video i;
unsigned int screenwidth;
unsigned int screenheight;
HDC maindc;
HDC bitmapdc;
unsigned int bmpwidth;
unsigned int bmpheight;
void* bmppixels;
unsigned int bmppitch;
HBITMAP bitmap;
videoformat depth;
};
static void reinit(struct video * this_, unsigned int screen_width, unsigned int screen_height, videoformat depth, double fps)
{
struct video_gdi * this=(struct video_gdi*)this_;
this->screenwidth=screen_width;
this->screenheight=screen_height;
this->depth=depth;
if (this->bitmap) DeleteObject(this->bitmap);
this->bitmap=NULL;
this->bmpwidth=0;
this->bmpheight=0;
free(this->bmppixels);
this->bmppixels=NULL;
}
static void draw(struct video * this_, unsigned int width, unsigned int height, const void * data, unsigned int pitch)
{
struct video_gdi * this=(struct video_gdi*)this_;
if (!data) return;
if (width>this->bmpwidth || height>this->bmpheight)
{
if (width>this->bmpwidth) this->bmpwidth=width;
if (height>this->bmpheight) this->bmpheight=height;
if (this->bitmap) DeleteObject(this->bitmap);
this->bitmap=CreateCompatibleBitmap(this->maindc, this->bmpwidth, this->bmpheight);
SelectObject(this->bitmapdc, this->bitmap);
if (this->depth==15)
{
this->bmppitch=(sizeof(uint16_t)*this->bmpwidth + 2) & ~3;
}
else
{
this->bmppitch=sizeof(uint32_t)*this->bmpwidth;
}
if (this->bmppixels) free(this->bmppixels);
this->bmppixels=malloc(this->bmppitch*this->bmpheight);
}
struct image src;
src.width=width;
src.height=height;
src.pixels=(void*)data;
src.pitch=pitch;
struct image dst;
dst.width=width;
dst.height=height;
dst.pixels=this->bmppixels;
dst.pitch=this->bmppitch;
if (this->depth==fmt_xrgb1555)
{
src.format=fmt_xrgb1555;
dst.format=fmt_xrgb1555;
}
if (this->depth==fmt_rgb565)
{
src.format=fmt_rgb565;
dst.format=fmt_xrgb8888;
}
if (this->depth==fmt_xrgb8888)
{
src.format=fmt_xrgb8888;
dst.format=fmt_xrgb8888;
}
//memset(this->pixels,255,this->pitch*this->screenheight);
image_convert(&src, &dst);
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = (this->depth==15)?16:32; //biBitCount of 15 is invalid, biBitCount of 16 is really RGB555
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
SetDIBits(this->bitmapdc, this->bitmap, 0, height, (void*)this->bmppixels, &bmi, DIB_RGB_COLORS);
StretchBlt(this->maindc, 0,0, this->screenwidth,this->screenheight, this->bitmapdc, 0,0, width,height, SRCCOPY);
}
static bool set_sync(struct video * this_, bool sync)
{
//can't do this
return false;
}
static bool has_sync(struct video * this_)
{
return false;
}
static void free_(struct video * this_)
{
struct video_gdi * this=(struct video_gdi*)this_;
if (this->bitmap) DeleteObject(this->bitmap);
if (this->bitmapdc) DeleteDC(this->bitmapdc);
ReleaseDC(WindowFromDC(this->maindc), this->maindc);
free(this->bmppixels);
free(this);
}
static struct video * cvideo_create_gdi(uintptr_t windowhandle, unsigned int screen_width, unsigned int screen_height,
videoformat depth, double fps)
{
struct video_gdi * this=malloc(sizeof(struct video_gdi));
this->i.reinit=reinit;
this->i.draw=draw;
this->i.set_sync=set_sync;
this->i.has_sync=has_sync;
this->i.free=free_;
this->maindc=NULL;
this->bitmapdc=NULL;
this->bitmap=NULL;
this->bmppixels=NULL;
this->maindc=GetDC((HWND)windowhandle);
this->bitmapdc=CreateCompatibleDC(this->maindc);
if (!this->bitmapdc) goto cancel;
reinit((struct video*)this, screen_width, screen_height, depth, fps);
return (struct video*)this;
cancel:
free_((struct video*)this);
return NULL;
}
#undef video
static video* video_create_gdi(uintptr_t windowhandle)
{
return video_create_compat(cvideo_create_gdi(windowhandle, 256, 256, fmt_xrgb1555, 60));
}
const video::driver video::driver_gdi = {"GDI", video_create_gdi, NULL, 0};
#endif