-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindow.c
183 lines (158 loc) · 4.48 KB
/
window.c
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
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "types.h"
#include "window.h"
#define className "wat"
HINSTANCE hInstance;
HWND mainwnd;
HDC hdc;
HGLRC hrc = NULL;
u32 mouse[3] = {0}; // {x, y, drag state}
u8 keyboard[256] = {0}; //vkey
u32 winwidth = 100;
u32 winheight = 100;
u32 winscale = 1;
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool initwnd(u32 w, u32 h, u32 scale, bool fullscreen){
RECT size = {0,0,w,h};
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_OWNDC, WinProc, 0, 0, GetModuleHandle(NULL), NULL/*icon*/, NULL/*cursor*/, NULL, NULL, className, NULL};
int scwidth = GetSystemMetrics(SM_CXSCREEN);
int scheight = GetSystemMetrics(SM_CYSCREEN);
//scAspect = (double)scWidth/(double)scHeight;
hInstance = wc.hInstance;
RegisterClassEx(&wc);
AdjustWindowRect(&size, WS_OVERLAPPEDWINDOW, 0);
size.right -= size.left;
size.bottom -= size.top;
size.left = (scwidth - size.right)/2;
size.top = (scheight - size.bottom)/2;
mainwnd = CreateWindowEx(WS_EX_APPWINDOW, className, "Window", WS_OVERLAPPEDWINDOW, size.left, size.top, size.right, size.bottom, NULL, NULL, hInstance, NULL);
PIXELFORMATDESCRIPTOR pfd = {sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0};
int pf;
hdc = GetDC(mainwnd);
if(!(pf = ChoosePixelFormat(hdc, &pfd))){
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return;
}
SetPixelFormat(hdc, pf, &pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
ShowWindow(mainwnd,SW_SHOW);
SetForegroundWindow(mainwnd);
SetFocus(mainwnd);
//float bgColor[4] = {0.3f,0.3f,0.3f, 1.0f};
//glClearColor(bgColor[0],bgColor[1],bgColor[2],bgColor[3]);
//glClearColor(0.39f,0.58f,0.93f, 1.0f);
glClearColor(0.0,0.0,0.0, 1.0f);
glClearDepth(1.0f);
//glDepthFunc(GL_LEQUAL);
//glEnable(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
//glShadeModel(GL_SMOOTH);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDisable(GL_POLYGON_SMOOTH);
//glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//glEnable(GL_CULL_FACE);
glAlphaFunc(GL_GREATER, 0.0f);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
resize(w,h,scale);
}
void dispwnd(){
MSG msg;
u32 tc = 0;
u32 tcnew;
u32 ticks;
while(msg.message != WM_QUIT){
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}else{
tcnew = GetTickCount();
if(tcnew > tc){
ticks = tcnew - tc;
tc = tcnew;
if(update(ticks)) return;
}
SwapBuffers(hdc);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
render();
glFlush();
}
}
}
void resize(u32 w, u32 h, u32 scale){
if(!h){
puts("Why is height zero?");
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// scale render target
w *= scale;
h *= scale;
glOrtho(0, w, h, 0, -1000.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
winwidth = w;
winheight = h;
winscale = scale;
//render
SwapBuffers(hdc);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}
void closewnd(){
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hrc);
ReleaseDC(mainwnd, hdc);
DestroyWindow(mainwnd);
UnregisterClass(className,hInstance);
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_KILLFOCUS:
//active = 0;
break;
case WM_ACTIVATE:
//active = HIWORD(wParam) ? 0 : 1;
return 0;
/*case WM_SYSCOMMAND:
switch(wParam){
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;*/
case WM_MOUSEMOVE:
mouse[0] = LOWORD(lParam);
mouse[1] = HIWORD(lParam);
mouse[2] = wParam;
break;
case WM_KEYDOWN:
keyboard[wParam] |= 1;
return 0;
case WM_KEYUP:
keyboard[wParam] &= ~1;
return 0;
case WM_SIZE:
if(wParam != SIZE_MINIMIZED){
resize(LOWORD(lParam), HIWORD(lParam), winscale);
return 0;
}
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}