winlivebg_test1
diff src/main.c @ 0:d6d791557330
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Oct 2019 02:54:07 +0300 |
parents | |
children | aa6a9521b088 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.c Thu Oct 17 02:54:07 2019 +0300 1.3 @@ -0,0 +1,188 @@ 1.4 +#include <stdio.h> 1.5 +#include <string.h> 1.6 +#include <windows.h> 1.7 +#include <GL/gl.h> 1.8 + 1.9 +#define WCNAME "livebg_test" 1.10 + 1.11 +void draw(void); 1.12 +HWND create_window(int width, int height); 1.13 +int init_gl(void); 1.14 +long CALLBACK handle_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam); 1.15 + 1.16 +HWND win; 1.17 +HDC dc; 1.18 +HGLRC ctx; 1.19 +int wcreg, quit; 1.20 +unsigned long start_time; 1.21 + 1.22 +int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdline, int cmdshow) 1.23 +{ 1.24 + MSG msg; 1.25 + 1.26 + if(!(win = create_window(400, 300))) { 1.27 + return 1; 1.28 + } 1.29 + if(init_gl() == -1) { 1.30 + DestroyWindow(win); 1.31 + return 1; 1.32 + } 1.33 + 1.34 + start_time = timeGetTime(); 1.35 + 1.36 + for(;;) { 1.37 + while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { 1.38 + TranslateMessage(&msg); 1.39 + DispatchMessage(&msg); 1.40 + 1.41 + if(quit) goto done; 1.42 + } 1.43 + 1.44 + draw(); 1.45 + Sleep(10); 1.46 + } 1.47 +done: 1.48 + 1.49 + if(wcreg) { 1.50 + UnregisterClass(WCNAME, hinst); 1.51 + } 1.52 + return 0; 1.53 +} 1.54 + 1.55 +void draw(void) 1.56 +{ 1.57 + float tm = (timeGetTime() - start_time) / 1000.0f; 1.58 + glClearColor(0.1, 0.1, 0.1, 1); 1.59 + glClear(GL_COLOR_BUFFER_BIT); 1.60 + 1.61 + glMatrixMode(GL_MODELVIEW); 1.62 + glLoadIdentity(); 1.63 + glRotatef(tm * 10.0f, 0, 0, 1); 1.64 + 1.65 + glBegin(GL_TRIANGLES); 1.66 + glColor3f(1, 0, 0); 1.67 + glVertex2f(-0.5, -0.5); 1.68 + glColor3f(0, 1, 0); 1.69 + glVertex2f(0.5, -0.5); 1.70 + glColor3f(0, 0, 1); 1.71 + glVertex2f(0, 0.68); 1.72 + glEnd(); 1.73 + 1.74 + SwapBuffers(dc); 1.75 +} 1.76 + 1.77 +HWND fadewin; 1.78 + 1.79 +int CALLBACK match_win(HWND win, LPARAM lparam) 1.80 +{ 1.81 + HWND shwin = FindWindowEx(win, 0, "SHELLDLL_DefView", 0); 1.82 + if(shwin) { 1.83 + fadewin = FindWindowEx(0, shwin, "WorkerW", 0); 1.84 + } 1.85 + return 1; 1.86 +} 1.87 + 1.88 +HWND create_window(int width, int height) 1.89 +{ 1.90 + HINSTANCE hinst; 1.91 + HWND win, parent = 0; 1.92 + HWND pgman_win; 1.93 + unsigned int style; 1.94 + RECT rect; 1.95 + 1.96 + hinst = GetModuleHandle(0); 1.97 + 1.98 + if(!wcreg) { 1.99 + WNDCLASSEX wc; 1.100 + memset(&wc, 0, sizeof wc); 1.101 + wc.cbSize = sizeof wc; 1.102 + wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 1.103 + wc.lpfnWndProc = handle_events; 1.104 + wc.hInstance = hinst; 1.105 + wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION); 1.106 + wc.hCursor = LoadCursor(0, IDC_ARROW); 1.107 + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 1.108 + wc.lpszClassName = WCNAME; 1.109 + RegisterClassEx(&wc); 1.110 + wcreg = 1; 1.111 + } 1.112 + 1.113 + if((pgman_win = FindWindow("Progman", 0))) { 1.114 + parent = pgman_win; 1.115 + GetWindowRect(pgman_win, &rect); 1.116 + //width = rect.right - rect.left; 1.117 + //height = rect.bottom - rect.top; 1.118 + 1.119 + /* 1.120 + SendMessage(pgman_win, 0x52c, 0, 0); 1.121 + 1.122 + fadewin = 0; 1.123 + EnumWindows(match_win, 0); 1.124 + if(fadewin) { 1.125 + //ShowWindow(fadewin, 0); 1.126 + } 1.127 + */ 1.128 + } 1.129 + 1.130 + style = parent ? WS_CHILD : WS_POPUP; 1.131 + if(!(win = CreateWindow(WCNAME, WCNAME, style | WS_VISIBLE, 0, 0, width, height, 1.132 + parent, 0, hinst, 0))) { 1.133 + fprintf(stderr, "failed to create window\n"); 1.134 + return 0; 1.135 + } 1.136 + dc = GetDC(win); 1.137 + return win; 1.138 +} 1.139 + 1.140 +int init_gl(void) 1.141 +{ 1.142 + int pixfmt; 1.143 + PIXELFORMATDESCRIPTOR pfd; 1.144 + 1.145 + memset(&pfd, 0, sizeof pfd); 1.146 + pfd.nSize = sizeof pfd; 1.147 + pfd.nVersion = 1; 1.148 + pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 1.149 + pfd.iPixelType = PFD_TYPE_RGBA; 1.150 + pfd.cColorBits = 24; 1.151 + pfd.cDepthBits = 32; 1.152 + pfd.iLayerType = PFD_MAIN_PLANE; 1.153 + 1.154 + if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) { 1.155 + fprintf(stderr, "failed to find a suitable pixel format\n"); 1.156 + return -1; 1.157 + } 1.158 + SetPixelFormat(dc, pixfmt, &pfd); 1.159 + 1.160 + if(!(ctx = wglCreateContext(dc))) { 1.161 + fprintf(stderr, "failed to create an OpenGL context\n"); 1.162 + return -1; 1.163 + } 1.164 + wglMakeCurrent(dc, ctx); 1.165 + return 0; 1.166 +} 1.167 + 1.168 +long CALLBACK handle_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam) 1.169 +{ 1.170 + switch(msg) { 1.171 + case WM_PAINT: 1.172 + ValidateRect(win, 0); 1.173 + break; 1.174 + 1.175 + case WM_CLOSE: 1.176 + DestroyWindow(win); 1.177 + break; 1.178 + 1.179 + case WM_DESTROY: 1.180 + PostQuitMessage(0); 1.181 + break; 1.182 + 1.183 + case WM_QUIT: 1.184 + quit = 1; 1.185 + break; 1.186 + 1.187 + default: 1.188 + return DefWindowProc(win, msg, wparam, lparam); 1.189 + } 1.190 + return 0; 1.191 +}