Code:
#include <windows.h>
#include <gl/gl.h>
#include <stdlib.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);
int true=1;
struct block
{
float x;
float y;
int exists;
float trecy;
};
struct block *block_array;
void add_to_array(struct block array_pointer[],struct block*obj_pointer)
{
float array_size=sizeof(&array_pointer);
printf("array size is %f",array_size);
float obj_size=sizeof(&obj_pointer);
printf("object size is %f",obj_size);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
HWND hwnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "GLSample";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
if (!RegisterClassEx(&wcex))
return 0;
/* create main window */
hwnd = CreateWindowEx(0,
"GLSample",
"OpenGL Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
/* enable OpenGL for the window */
EnableOpenGL(hwnd, &hDC, &hRC);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glClearColor(0, 0, 0, 0);
glOrtho (0,800,600 , 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.375, 0.375, 0);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
srand(1);
block_array = malloc(1 * sizeof(struct block));
add_to_array(block_array,&block_array[0]);
float x=0;
float y=0;
int block_iter=0;
for (x=40;x<800;x=x+80)
{
for (y=30;y<600;y=y+60)
{
block_array[block_iter].exists=true;
block_array[block_iter].x=x;
block_array[block_iter].y=y;
block_iter++;
}
}
/* 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();
//glRotatef(theta, 0.0f, 0.0f, 1.0f);
float x=0;
float y=0;
int block_iter=0;
for (x=40;x<800;x=x+80)
{
for (y=30;y<600;y=y+60)
{
glLoadIdentity();
glTranslatef(0.375, 0.375, 0);
glTranslatef(block_array[block_iter].x,block_array[block_iter].y,0);
glRotatef(block_array[block_iter].x,0,0,1);
glBegin(GL_QUADS);
glColor3f(.3f, .3f, .3);
glVertex2f(-40,-30);
glColor3f(.60f, .60f, .60f);
glVertex2f(40, -30);
glColor3f(0.9f, 0.9f, 0.9f);
glVertex2f(40,30);
glColor3f(0.60f, 0.60f, 0.60f);
glVertex2f(-40,30);
glEnd();
block_iter++;
}
}
//?//glPopMatrix();
SwapBuffers(hDC);
theta += 1.0f;
Sleep (1);
}
}
/* shutdown OpenGL */
DisableOpenGL(hwnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow(hwnd);
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
case 0x41: //a
block_array[1].y++;//'=block_array[10].y+20.0;
printf("%f\n",block_array[10].y);
break;
case 0x51: //q
break;
}
}
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
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);
}
void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hwnd, hDC);
}
its supposed to crash because the array is only 1 element long, if you comment out the call to 'add_to_array' it will crash as its supposed to.