-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle.cpp
More file actions
91 lines (73 loc) · 2.65 KB
/
Copy pathcircle.cpp
File metadata and controls
91 lines (73 loc) · 2.65 KB
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
// This program creates a simple OpenGL window and draws a blue circle in the center of the window.
// It uses the Windows API for window management and OpenGL for rendering.
// Compile with: g++ -o circle.exe circle.cpp -lopengl32 -lgdi32
// Note: Make sure you have the OpenGL and GDI32 libraries available in your compiler's library path.
#include <windows.h>
#include <GL/gl.h>
#include <math.h>
#define WIDTH 800
#define HEIGHT 600
#define SEGMENTS 100
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_CLOSE) PostQuitMessage(0);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
void drawCircle(float cx, float cy, float r, int segments) {
glColor3f(0.0f, 0.0f, 1.0f); // blue
glBegin(GL_TRIANGLE_FAN);
glVertex2f(cx, cy); // center
for (int i = 0; i <= segments; ++i) {
float angle = 2.0f * 3.1415926f * i / segments;
float x = cx + cosf(angle) * r;
float y = cy + sinf(angle) * r;
glVertex2f(x, y);
}
glEnd();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "BareOpenGLWindowClass";
RegisterClass(&wc);
HWND hWnd = CreateWindow(wc.lpszClassName, "Blue Circle", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT,
NULL, NULL, hInstance, NULL);
HDC hDC = GetDC(hWnd);
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,
24, 8, 0,
PFD_MAIN_PLANE, 0, 0, 0, 0
};
int pf = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pf, &pfd);
HGLRC hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
ShowWindow(hWnd, nCmdShow);
// OpenGL setup
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
MSG msg = { 0 };
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white background
glClear(GL_COLOR_BUFFER_BIT);
drawCircle(0.0f, 0.0f, 0.5f, SEGMENTS);
SwapBuffers(hDC);
}
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
DestroyWindow(hWnd);
return 0;
}