diff options
Diffstat (limited to 'Source/tests/win32/01-window/03_sub_menu.cpp')
-rw-r--r-- | Source/tests/win32/01-window/03_sub_menu.cpp | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/Source/tests/win32/01-window/03_sub_menu.cpp b/Source/tests/win32/01-window/03_sub_menu.cpp new file mode 100644 index 0000000..fed841d --- /dev/null +++ b/Source/tests/win32/01-window/03_sub_menu.cpp @@ -0,0 +1,355 @@ +#include "config.h" +#if _run_app == _sub_menu + +#include <asura-base/FileSystem/FileManager.h> +#include <asura-base/FileSystem/DataBuffer.h> +#include <asura-core/graphics/image.h> +#include <asura-core/graphics/shader.h> +#include <asura-core/image/ImageData.h> +#include <asura-core/graphics/VertexBuffer.h> +#include <SDL2/SDL.h> +#include <string> + +using namespace std; +using namespace AEFileSystem; +using namespace AEGraphics; +using namespace AEImage; + +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; +HWND wnd2; + +AEMath::Recti viewport; + +string vert = R"( + +in vec2 position; +in vec2 uv; + +uniform mat4 asura_projection_matrix; +uniform mat4 asura_model_matrix; +uniform mat4 asura_view_matrix; + +out vec2 texCoord; + +void main() +{ + gl_Position = asura_projection_matrix * asura_view_matrix * asura_model_matrix * vec4(position, 0, 1); + texCoord = uv; +} +)"; + +string frag = R"( +in vec2 texCoord; +uniform sampler2D img; +uniform vec4 color ; +void main() +{ + //gl_FragColor = color * texture2D(img, texCoord); + gl_FragColor = color; +} +)"; + +Shader* shader; +VertexBuffer* vb; + +struct +{ + int pos; + int tex; + int m; + int v; + int p; + int color; +} locs; + +struct Vert +{ + float x, y; // position + float s, t; // uv +}; + +int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) +{ + + AEFileSystem::FileManager::Get()->Init("D:\Asura\bin\win64"); + MSG msg; + + WNDCLASSEXW wcex; + memset(&wcex, 0, sizeof(wcex)); + wcex.cbSize = sizeof(wcex); + wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; + wcex.lpfnWndProc = WndProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = hInstance; + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + wcex.hbrBackground = NULL; + wcex.lpszMenuName = NULL; + wcex.lpszClassName = L"Submenu"; + + RegisterClassExW(&wcex); + + //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); + + DWORD windowStyle = 0; + DWORD extendedStyle = 0; + windowStyle = WS_POPUP | WS_CLIPCHILDREN | WS_THICKFRAME | WS_VISIBLE; + extendedStyle = WS_EX_TOOLWINDOW; + + windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_VISIBLE; + extendedStyle = 0; + + //windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;WS_VISIBLE + + RECT rect = { 100, 100, 500, 500 }; + AdjustWindowRectEx(&rect, windowStyle, true, extendedStyle); + + wnd = CreateWindowExW(extendedStyle, wcex.lpszClassName, L"", windowStyle, + rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, + NULL, NULL, hInstance, NULL); + + // child + windowStyle = WS_POPUP | WS_CLIPCHILDREN | WS_THICKFRAME | WS_VISIBLE; + extendedStyle = WS_EX_TOOLWINDOW; + rect = { 100, 100, 500, 500 }; + AdjustWindowRectEx(&rect, windowStyle, false, extendedStyle); +/* + wnd2 = CreateWindowExW(extendedStyle, wcex.lpszClassName, L"", windowStyle, + rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, + NULL, NULL, hInstance, NULL); +*/ + //wnd = CreateWindowW(wcex.lpszClassName, L"Asura", + // 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); +AEFileSystem::FileManager* fs; +ImageData* imgdata = new ImageData(); +AEGraphics::Image* img; +GLint tex; +LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, + WPARAM wParam, LPARAM lParam) { + + switch (msg) { + + case WM_SIZE: + { + if (g_Device.Inited()) + { + RECT rect; + GetClientRect(hwnd, &rect); + viewport.Set(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); + g_Device.SetViewport(viewport); + } + } + case WM_PAINT: + if (!g_Device.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_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + // ˲ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + { + int imgLoc = shader->GetUniformLocation("img"); + int code = g_Device.GetError(); + g_Device.SetActiveShader(shader); + shader->SetUniformTexture(imgLoc, *img); + g_Device.SetMatrixMode(MATRIX_MODE_PROJECTION); + g_Device.LoadIdentity(); + g_Device.Ortho(0, viewport.w, viewport.h, 0, -1, 1); + g_Device.SetMatrixMode(MATRIX_MODE_MODEL); + g_Device.LoadIdentity(); + g_Device.Translate(100, 100); + shader->SetUniformMatrix44(locs.m, g_Device.GetMatrix(MATRIX_MODE_MODEL)); + shader->SetUniformMatrix44(locs.v, g_Device.GetMatrix(MATRIX_MODE_VIEW)); + shader->SetUniformMatrix44(locs.p, g_Device.GetMatrix(MATRIX_MODE_PROJECTION)); + shader->SetAttribute(locs.pos, vb, 0, 4); + shader->SetAttribute(locs.tex, vb, 2, 4); + g_Device.SetDrawColor(1, 1, 0, 1); + shader->SetUniformColor(locs.color, g_Device.GetDrawColor()); + //glLineWidth(1); + g_Device.DrawArrays(GL_LINE_STRIP, 0, 5); + //g_Device.DrawArrays(GL_TRIANGLE_STRIP, 0, 4); + shader->DisableAttribute(locs.pos); + shader->DisableAttribute(locs.tex); + g_Device.SetActiveShader(NULL); + } + 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 = AEFileSystem::FileManager::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 (!g_Device.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.5f, 0.5f, 0, 0 }, + //{ 0.5f, h + 0.5f, 0, 1 }, + //{ w + 0.5f, 0.5f, 1, 0 }, + //{ w + 0.5f, h + 0.5f, 1, 1 }, + //}; + Vert v[] = { + { 0.5f, 0.5f, 0, 0 }, + { 0.5f, h + 55.8f, 0, 1 }, + { w + 0.5f, h + 0.5f, 1, 1 }, + { w + 0.5f, 0.5f, 1, 0 }, + { 0.5f, 0.5f, 0, 0 }, + }; + vb = new VertexBuffer(BUFFER_USAGE_STATIC, BUFFER_DATA_TYPE_FLOAT, sizeof(v)); + vb->Fill(v, sizeof(v)); + }; + locs.m = shader->GetUniformLocation("asura_model_matrix"); + locs.v = shader->GetUniformLocation("asura_view_matrix"); + locs.p = shader->GetUniformLocation("asura_projection_matrix"); + locs.color = shader->GetUniformLocation("color"); + locs.pos = shader->GetAttributeLocation("position"); + locs.tex = shader->GetAttributeLocation("uv"); + + 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
\ No newline at end of file |