Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AspireID

Simple Window

This topic is 5343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In this code chunk, I just create a window and trying to draw a simple 2d rectangle... Everything is OK, except the fact there is no rectangle on the screen.. Any ideas? I have the following simple code
#include <windows.h>
#include <gl\gl.h>

#pragma comment(lib, "opengl32.lib")

GLfloat x,y;
GLfloat xspeed,yspeed;
GLfloat size;
GLfloat windowWidth=500.0f, windowHeight=500.0f;

void ChangeSize(GLsizei width, GLsizei height)
{
	if(height==0) height=1;

	glViewport(0,width,0,height);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	windowWidth=500.0f;
	windowHeight=500.0f;
	if(width>=height)
		windowWidth*=(float)width/height;
	else
		windowHeight*=(float)height/width;

	//glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f);
	glOrtho(0.0f,500.0f,0.0f,500.0f,1.0f,-1.0f);
}

void RenderScene()
{
	glClearColor(1.0f,1.0f,1.0f,0.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	glLoadIdentity();

	glColor3f(0.0f,0.0f,0.0f);
	glRectf(0.0f,0.0f,250.0f,250.0f);

	//glRectf(x,y,x+size,y+size);
	
	//glFlush();
}

void Init()
{
	x=100.0f;
	y=50.0f;
	xspeed=rand()%10;
	yspeed=rand()%20;
	size=rand()%30;
}

void IdleFunction()
{
    if(x<0 || x+size>windowWidth) xspeed=-xspeed;
	if(y<0 || y+size>windowHeight) yspeed=-yspeed;
	x+=xspeed;
	y+=yspeed;
}

void SetDCPixelFormat(HDC hDC)
{
	int pixelFormat;
	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA,
			24,
			0,0,0,0,0,0,
			0,0,
			0,0,0,0,0,0,
			32,
			0,
			PFD_MAIN_PLANE,
			0,
			0,0,0};

		pixelFormat=ChoosePixelFormat(hDC,&pfd);

		SetPixelFormat(hDC,pixelFormat,&pfd);
}

LRESULT CALLBACK WndProc(HWND hWnd,
						 UINT message,
						 WPARAM wParam,
						 LPARAM lParam)
{
	static HGLRC hRC;
	static HDC hDC;

	switch(message)
	{
	case WM_CREATE:
		hDC=GetDC(hWnd);
		SetDCPixelFormat(hDC);
		
		hRC=wglCreateContext(hDC);
		wglMakeCurrent(hDC,hRC);

		SetTimer(hWnd,101,1,NULL);

		Init();

		break;
	
	case WM_DESTROY:
		KillTimer(hWnd,101);

		wglMakeCurrent(hDC,NULL);
		wglDeleteContext(hRC);

		PostQuitMessage(0);
		break;
	
	case WM_SIZE:
		ChangeSize(LOWORD(lParam),HIWORD(lParam));
		break;

	case WM_TIMER:
		IdleFunction();

		InvalidateRect(hWnd,NULL,FALSE);
		break;

	case WM_PAINT:
		RenderScene();
		SwapBuffers(hDC);
		ValidateRect(hWnd,NULL);
		break;
	default:
		return(DefWindowProc(hWnd,message,wParam,lParam));
		break;
	}

	return(0);
}

int APIENTRY WinMain(HINSTANCE hInstance,
					 HINSTANCE hPrevInstance,
					 LPSTR lpCmdLine,
					 int nCmdShow)
{
	MSG msg;
	WNDCLASS wc;
	HWND hWnd;

	char lpszAppName[]="Demo";
	wc.style=CS_HREDRAW|CS_VREDRAW;
	wc.lpfnWndProc=WndProc;
    wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hInstance=hInstance;
	wc.hIcon=NULL;
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);

	wc.hbrBackground=NULL;

	wc.lpszMenuName=NULL;
	wc.lpszClassName=lpszAppName;

	if(RegisterClass(&wc)==0) 
		return(FALSE);


	hWnd=CreateWindow(
		lpszAppName,
		lpszAppName,
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
		0,0,
		windowWidth,windowHeight,

		NULL,
		NULL,
		hInstance,
		NULL);

	if(hWnd==NULL) return(FALSE);

	ShowWindow(hWnd,SW_SHOW);
	UpdateWindow(hWnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}
 
Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!