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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#include "config.h"
#if _run_app == _sub_menu
#include <asura-utils/io/file_system.h>
#include <asura-utils/io/data_buffer.h>
#include <asura-core/graphics/image.h>
#include <asura-core/graphics/shader.h>
#include <asura-core/image/image_data.h>
#include <SDL2/SDL.h>
#include <string>
using namespace std;
using namespace AEIO;
using namespace AEGraphics;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void AddMenus(HWND);
#define IDM_FILE_NEW 1
#define IDM_FILE_IMPORT 2
#define IDM_IMPORT_MAIL 11
#define IDM_ASSET 20
HWND wnd;
AEMath::Recti viewport;
/*
shader в
* MVP matrix
* position
* UV
* UV_0
* UV_1
* UV_2
* UV_n
* framework涨壬DZ༭ҪΪ༭ȾͼƬȾ
* Asura Shader Semantics.
*/
string shader_frame = R"(
//
in vec2 asura_position;
in vec4 asura_color;
// asura_texcoord(n)
in vec2 asura_texcoord0;
in vec2 asura_texcoord1;
in vec2 asura_texcoord2;
in vec2 asura_texcoord3;
in vec2 asura_texcoord4;
// uniform
uniform mat4 asura_projection_matrix;
uniform mat4 asura_model_matrix;
uniform mat4 asura_view_matrix;
uniform mat4 asura_draw_color;
uniform int asura_layer; // 0~10000Ȳ
uniform vec2 asura_time; // 뵱ǰʱǰ֡ļ
)";
string vert = R"(
in vec2 vertex;
in vec2 vertUVs;
uniform mat4 projMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
out vec2 texCoord;
void main()
{
texCoord = vertUVs;
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(vertex, 1, 1);
}
)";
string frag = R"(
in vec2 texCoord;
uniform sampler2D img;
uniform sampler2D img2;
void main()
{
gl_FragColor = texture2D(img, texCoord);
}
)";
Shader* shader;
GPUBuffer* vb;
struct Vert
{
float x, y; // coord
float s, t; // uv
};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow)
{
AEIO::Filesystem::Get()->Init("D:\Asura\bin\win64");
MSG msg;
WNDCLASSW wc = { 0 };
wc.lpszClassName = L"Submenu";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClassW(&wc);
wnd = CreateWindowW(wc.lpszClassName, L"Asura v0.1",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
200, 200, 550, 450, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
::Sleep(1);
}
return (int)msg.wParam;
}
HDC hdc;
HGLRC glc;
HGLRC glc2;
static PAINTSTRUCT ps;
HBRUSH hBrush;
HBRUSH hOldBrush;
HPEN hPen;
HPEN hOldPen;
PIXELFORMATDESCRIPTOR pfd;
int pf;
File* file;
File* file2;
DataBuffer db(102400);
AEIO::Filesystem* fs;
ImageData* imgdata = new ImageData();
Image* img;
GLint tex;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_SIZE:
{
if (gl.Inited())
{
RECT rect;
GetWindowRect(hwnd, &rect);
viewport.Set(0, 0, rect.right - rect.left, rect.bottom - rect.top);
gl.SetViewport(viewport);
}
}
case WM_PAINT:
if (!gl.Inited())
break;
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
//hdc = GetDC(hwnd);
//glc = wglCreateContext(hdc);
glClearColor(0.16, 0.16, 0.16, 1);
glColor4f(0.219, 0.219, 0.219, 1);
//glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glColor4f(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
tex = img->GetGLTexture();
glBindTexture(GL_TEXTURE_2D, tex); //
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //Sͼ
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //Tͼ
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //Ŵ˷ʽ
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //С˷ʽ
{
int uvAttribLoc = shader->GetAttributeLocation("vertUVs");
int vertAttribLoc = shader->GetAttributeLocation("vertex");
int imgLoc = shader->GetUniformLocation("img");
int img2Loc = shader->GetUniformLocation("img2");
int projmat = shader->GetUniformLocation("projMatrix");
int mmat = shader->GetUniformLocation("modelMatrix");
int vmat = shader->GetUniformLocation("viewMatrix");
int code = gl.GetError();
gl.UseShader(shader);
shader->SetUniformTexture(imgLoc, *img);
shader->SetUniformTexture(img2Loc, *img);
gl.SetMatrixMode(MATRIX_MODE_PROJECTION);
gl.PushMatrix();
gl.LoadIdentity();
gl.Ortho(0, viewport.w, viewport.h, 0, -1, 1);
shader->SetUniformMatrix44(projmat, gl.GetMatrix(MATRIX_MODE_PROJECTION));
gl.PopMatrix();
gl.SetMatrixMode(MATRIX_MODE_MODEL);
gl.LoadIdentity();
gl.Translate(100, 10);
shader->SetUniformMatrix44(mmat, gl.GetMatrix(MATRIX_MODE_MODEL));
shader->SetUniformMatrix44(vmat, gl.GetMatrix(MATRIX_MODE_VIEW));
vb->Bind();
glVertexAttribPointer(vertAttribLoc, 2, GL_FLOAT, GL_FALSE, sizeof(Vert), 0);
glVertexAttribPointer(uvAttribLoc, 2, GL_FLOAT, GL_FALSE, sizeof(Vert), (const void*)(2 * sizeof(float)));
vb->UnBind();
glEnableVertexAttribArray(vertAttribLoc);
glEnableVertexAttribArray(uvAttribLoc);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(vertAttribLoc);
glDisableVertexAttribArray(uvAttribLoc);
gl.UnuseShader();
}
glFlush();
BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps);
UpdateWindow(hwnd);
break;
case WM_CREATE:
AddMenus(hwnd);
hdc = GetDC(hwnd);
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pf = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pf, &pfd);
DescribePixelFormat(hdc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
//ReleaseDC(hwnd, hdc);
//glc = wglCreateContext(hdc);
glc = wglCreateContext(hdc);
// ͼƬ
fs = AEIO::Filesystem::Get();
fs->Mount(".", "root");
file = new File("root/img.jpg");
file->Open(File::FILE_MODE_READ);
file->ReadAll(&db);
wglMakeCurrent(hdc, glc);
RECT rect;
GetWindowRect(hwnd, &rect);
viewport.Set(0, 0, rect.right - rect.left, rect.bottom - rect.top);
if (!gl.Init(viewport))
return 0;
imgdata->Decode(db);
img = new Image();
img->Load(imgdata);
wglMakeCurrent(hdc, glc);
file2 = new File("root/img.png");
file2->Open(File::FILE_MODE_READ);
file2->ReadAll(&db);
imgdata->Decode(db);
img->Load(imgdata, {50, 100});
imgdata->Release();
// shader
shader = new Shader();
shader->Load(vert, frag);
{
int w = img->GetWidth();
int h = img->GetHeight();
Vert v[] = {
{ 0, 0, 0, 0 },
{ 0, h, 0, 1 },
{ w, 0, 1, 0 },
{ w, h, 1, 1 },
};
vb = new GPUBuffer(BUFFER_TYPE_VERTEX, BUFFER_USAGE_STATIC, sizeof(v));
vb->Fill(v, sizeof(v));
};
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_FILE_NEW:
MessageBoxW(wnd, L"New file selected",
L"Information", MB_OK);
break;
case IDM_IMPORT_MAIL:
MessageBoxW(NULL, L"Import mail selected",
L"Information", MB_OK);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
void AddMenus(HWND hwnd) {
HMENU hMenubar = CreateMenu();
HMENU hMenu = CreateMenu();
HMENU hSubMenu = CreatePopupMenu();
HMENU asserMenu = CreateMenu();
AppendMenuW(hMenu, MF_STRING, IDM_FILE_NEW, L"&New");
AppendMenuW(asserMenu, MF_STRING, IDM_ASSET, L"Import &mail &assets");
AppendMenuW(hSubMenu, MF_STRING | MF_POPUP, (UINT_PTR)asserMenu, L"Import &mail");
AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hSubMenu, L"&Import");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu, L"&File");
SetMenu(hwnd, hMenubar);
}
#endif // _run_app == _sub_menu
|