/**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
HWND hWnd;
/**************************
* Function Declarations
*
**************************/
static int number_of_ball=0;
struct top{
float koor_x,koor_y;
float hiz_x,hiz_y;
float ivme_x,ivme_y;
}toplar[10000];
void kontrol(int x){
if(toplar[x].koor_x>1||toplar[x].koor_x<-1)
toplar[x].hiz_x=- (toplar[x].hiz_x);
if(toplar[x].koor_y>1||toplar[x].koor_y<-0.9)
{if(toplar[x].koor_y<-0.9)
toplar[x].koor_y-= toplar[x].hiz_y;
toplar[x].hiz_y=- (toplar[x].hiz_y*4)/5;
}
if(toplar[x].koor_y<-0.9&&toplar[x].hiz_y<0.001){
toplar[x].hiz_y=0;
toplar[x].hiz_x=0;}
else{
toplar[x].hiz_y= toplar[x].hiz_y+toplar[x].ivme_y;
toplar[x].koor_x+=toplar[x].hiz_x;
toplar[x].koor_y+=toplar[x].hiz_y+toplar[x].ivme_y;
}
}
void yenile(int top_sayisi)
{
for(int i=0;i<top_sayisi;i++)
{
kontrol(i);
glBegin (GL_QUADS);
glColor3f (1.0f, 0.0f, 0.0f);
glVertex2f (toplar[i].koor_x-0.02, toplar[i].koor_y+0.02);
glVertex2f (toplar[i].koor_x+0.02, toplar[i].koor_y+0.02);
glVertex2f (toplar[i].koor_x+0.02, toplar[i].koor_y-0.02);
glVertex2f (toplar[i].koor_x-0.02, toplar[i].koor_y-0.02);
glEnd ();
}
}
void bir_top(){
//ilk topu ekle
toplar[0].koor_x=0;
toplar[0].koor_y=0;
toplar[0].hiz_x=0.017;
toplar[0].hiz_y=0.015;
toplar[0].ivme_y=-0.001;
number_of_ball++;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 500, 500,
NULL, NULL, hInstance, NULL);
bir_top();
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
yenile(number_of_ball);
glBegin (GL_LINES); //yer hizasındaki sarı cizgi
glColor3f (1.0f, 1.0f, 0.0f); glVertex2f (-1.0f, -0.9);
glColor3f (1.0f, 1.0f, 0.0f); glVertex2f (1.0f, -0.9);
glEnd ();
SwapBuffers (hDC);
theta += 1.0f;
Sleep (1);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{float x,y;
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_LBUTTONDOWN: //tıklayinca kare ekle
x=LOWORD(lParam); //farenin koordinatını al
y=HIWORD(lParam); //farenin koordinatını al
x=x/250-1; //cözünürlüge göre farenin yerini ayarla
y=-(y/250-1); //cözünürlüge göre farenin yerini ayarla
toplar[number_of_ball].koor_x=x; //ilk degerleri ver
toplar[number_of_ball].koor_y=y; //ilk degerleri ver
toplar[number_of_ball].hiz_x=0.01; //ilk degerleri ver
toplar[number_of_ball].hiz_y=0.01; //ilk degerleri ver
toplar[number_of_ball].ivme_y=-0.001; //ilk degerleri ver
number_of_ball++;
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
murat çakal
muratsal1985@gmail.com