winlivebg_test1

view 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 source
1 #include <stdio.h>
2 #include <string.h>
3 #include <windows.h>
4 #include <GL/gl.h>
6 #define WCNAME "livebg_test"
8 void draw(void);
9 HWND create_window(int width, int height);
10 int init_gl(void);
11 long CALLBACK handle_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
13 HWND win;
14 HDC dc;
15 HGLRC ctx;
16 int wcreg, quit;
17 unsigned long start_time;
19 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdline, int cmdshow)
20 {
21 MSG msg;
23 if(!(win = create_window(400, 300))) {
24 return 1;
25 }
26 if(init_gl() == -1) {
27 DestroyWindow(win);
28 return 1;
29 }
31 start_time = timeGetTime();
33 for(;;) {
34 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
35 TranslateMessage(&msg);
36 DispatchMessage(&msg);
38 if(quit) goto done;
39 }
41 draw();
42 Sleep(10);
43 }
44 done:
46 if(wcreg) {
47 UnregisterClass(WCNAME, hinst);
48 }
49 return 0;
50 }
52 void draw(void)
53 {
54 float tm = (timeGetTime() - start_time) / 1000.0f;
55 glClearColor(0.1, 0.1, 0.1, 1);
56 glClear(GL_COLOR_BUFFER_BIT);
58 glMatrixMode(GL_MODELVIEW);
59 glLoadIdentity();
60 glRotatef(tm * 10.0f, 0, 0, 1);
62 glBegin(GL_TRIANGLES);
63 glColor3f(1, 0, 0);
64 glVertex2f(-0.5, -0.5);
65 glColor3f(0, 1, 0);
66 glVertex2f(0.5, -0.5);
67 glColor3f(0, 0, 1);
68 glVertex2f(0, 0.68);
69 glEnd();
71 SwapBuffers(dc);
72 }
74 HWND fadewin;
76 int CALLBACK match_win(HWND win, LPARAM lparam)
77 {
78 HWND shwin = FindWindowEx(win, 0, "SHELLDLL_DefView", 0);
79 if(shwin) {
80 fadewin = FindWindowEx(0, shwin, "WorkerW", 0);
81 }
82 return 1;
83 }
85 HWND create_window(int width, int height)
86 {
87 HINSTANCE hinst;
88 HWND win, parent = 0;
89 HWND pgman_win;
90 unsigned int style;
91 RECT rect;
93 hinst = GetModuleHandle(0);
95 if(!wcreg) {
96 WNDCLASSEX wc;
97 memset(&wc, 0, sizeof wc);
98 wc.cbSize = sizeof wc;
99 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
100 wc.lpfnWndProc = handle_events;
101 wc.hInstance = hinst;
102 wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
103 wc.hCursor = LoadCursor(0, IDC_ARROW);
104 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
105 wc.lpszClassName = WCNAME;
106 RegisterClassEx(&wc);
107 wcreg = 1;
108 }
110 if((pgman_win = FindWindow("Progman", 0))) {
111 parent = pgman_win;
112 GetWindowRect(pgman_win, &rect);
113 //width = rect.right - rect.left;
114 //height = rect.bottom - rect.top;
116 /*
117 SendMessage(pgman_win, 0x52c, 0, 0);
119 fadewin = 0;
120 EnumWindows(match_win, 0);
121 if(fadewin) {
122 //ShowWindow(fadewin, 0);
123 }
124 */
125 }
127 style = parent ? WS_CHILD : WS_POPUP;
128 if(!(win = CreateWindow(WCNAME, WCNAME, style | WS_VISIBLE, 0, 0, width, height,
129 parent, 0, hinst, 0))) {
130 fprintf(stderr, "failed to create window\n");
131 return 0;
132 }
133 dc = GetDC(win);
134 return win;
135 }
137 int init_gl(void)
138 {
139 int pixfmt;
140 PIXELFORMATDESCRIPTOR pfd;
142 memset(&pfd, 0, sizeof pfd);
143 pfd.nSize = sizeof pfd;
144 pfd.nVersion = 1;
145 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
146 pfd.iPixelType = PFD_TYPE_RGBA;
147 pfd.cColorBits = 24;
148 pfd.cDepthBits = 32;
149 pfd.iLayerType = PFD_MAIN_PLANE;
151 if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
152 fprintf(stderr, "failed to find a suitable pixel format\n");
153 return -1;
154 }
155 SetPixelFormat(dc, pixfmt, &pfd);
157 if(!(ctx = wglCreateContext(dc))) {
158 fprintf(stderr, "failed to create an OpenGL context\n");
159 return -1;
160 }
161 wglMakeCurrent(dc, ctx);
162 return 0;
163 }
165 long CALLBACK handle_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
166 {
167 switch(msg) {
168 case WM_PAINT:
169 ValidateRect(win, 0);
170 break;
172 case WM_CLOSE:
173 DestroyWindow(win);
174 break;
176 case WM_DESTROY:
177 PostQuitMessage(0);
178 break;
180 case WM_QUIT:
181 quit = 1;
182 break;
184 default:
185 return DefWindowProc(win, msg, wparam, lparam);
186 }
187 return 0;
188 }