지금 팩맨 만드는중인데요 미로를 구현하는부분에서 앞에 배열주는법은 알겠는데
콜백이랑 드로우 함수에서 뭐라고 줘야 0과 1을 구분해서 적용시킬 수 있을지 모르겠어요
제발 고수형님들 살려주세요 저 F받으면 졸업못해요 ㅠ.ㅠ
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <gl/gl.h>
#include <gl/glu.h>
#define IDT_TIMER 1
// global variables
char *szClassName = "Computer Graphics";
HWND MyWND = 0;
HDC MyDC;
HGLRC MyRC;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
bool bSetupPixelFormat(void);
void Resize(const int cx, const int cy);
void DrawScene(void);
void DoColide(void);
-----------------------------------------------------------------------------------
bool maze[10][10] = {
{ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 }};
--------------------------------------------------------------------------------------
const int numRect = 2;
float centerPos[numRect][2] = {{0.1f, 0.1f}, {0.0f, 0.0f}};
float moveDirection[numRect][2] = {{0.05f, 0.0}, {0.0f, 0.05f}};
//void DrawScore(float posX, float posY);
//bool DoColide (float left, float bottom, float right, float top);
//bool DoColide (float centerX, float centerY);
//float centerPos[2] = {0.0f, 0.0f};
//float moveDirection[2] = {0.0f, 0.05f};
/*
* WinMain
*/
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nShowCmd )
{
// Registers the window class
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wc.hCursor = LoadCursor( 0, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = szClassName;
RegisterClass(&wc);
// Create the main window
MyWND = CreateWindow( szClassName,
"Simple OpenGL Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
hInstance,
0 );
// Make sure that window was created
if( !MyWND )
return false;
ShowWindow( MyWND, nShowCmd );
UpdateWindow( MyWND );
// Main message loop
MSG msg;
while( GetMessage( &msg, 0, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage (&msg );
}
UnregisterClass( szClassName, wc.hInstance );
return msg.wParam;
}
/*
* WndProc: to process messages for the main window
*/
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
RECT rect;
switch( msg )
{
case WM_CREATE:
// Initialize for the OpenGL rendering
MyDC = GetDC(hWnd);
if( !bSetupPixelFormat() )
PostQuitMessage (0);
MyRC = wglCreateContext(MyDC);
wglMakeCurrent(MyDC, MyRC);
SetTimer(hWnd, IDT_TIMER, 100, NULL);
SelectObject( MyDC, GetStockObject(SYSTEM_FONT) );
wglUseFontBitmaps( MyDC, 0, 255, 1000 );
glListBase( 1000 );
// InitStage();
break;
case WM_SIZE:
GetClientRect(hWnd, &rect);
Resize(rect.right, rect.bottom);
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_DESTROY:
// Destroy all about OpenGL
if( MyRC )
wglDeleteContext(MyRC);
if( MyDC )
ReleaseDC(hWnd, MyDC);
KillTimer(hWnd, IDT_TIMER);
glDeleteLists( 1000, 255 );
PostQuitMessage(0);
break;
case WM_PAINT:
DrawScene();
ValidateRect(hWnd, NULL);
break;
case WM_TIMER:
switch(wParam){
case IDT_TIMER:
/*
float centerX = centerPos[0] + moveDirection[0];
float centerY = centerPos[1] + moveDirection[1];
if(!DoColide( centerX-0.1f, centerY-0.1f, centerX+0.1f, centerY+0.1f)){
centerPos[0] += moveDirection[0];
centerPos[1] += moveDirection[1];
InvalidateRect(hWnd, NULL, false);
}*/
DoColide();
InvalidateRect(hWnd, NULL, false);
break;
}
break;
case WM_KEYDOWN:
/*
if(wParam == VK_LEFT){
if(!DoColide(centerPos[0]-0.2f, centerPos[1])){
centerPos[0] -= 0.2f;
InvalidateRect(hWnd, NULL, false);
}
}
else if(wParam == VK_RIGHT){
if(!DoColide(centerPos[0]+0.2f, centerPos[1])){
centerPos[0] += 0.2f;
InvalidateRect(hWnd, NULL, false);
}
}
else if(wParam == VK_UP){
if(!DoColide(centerPos[0], centerPos[1]-0.2f)){
centerPos[1] += 0.2f;
InvalidateRect(hWnd, NULL, false);
}
}
else if(wParam == VK_DOWN){
if(!DoColide(centerPos[0], centerPos[1]-0.2f)){
centerPos[1] -= 0.2f;
InvalidateRect(hWnd, NULL, false);
}
}*/
if( wParam == VK_ESCAPE )
{
DestroyWindow( MyWND );
}
break;
default:
return DefWindowProc( hWnd, msg, wParam, lParam );
}
return 0;
}
/*
* bSetupPixelFormat : to setup the format of pixel for OpenGL
*/
bool bSetupPixelFormat(void)
{
PIXELFORMATDEOR pfd;
int pixelformat;
pfd.nSize= sizeof( PIXELFORMATDEOR );
pfd.nVersion= 1;
pfd.dwFlags= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.dwLayerMask= PFD_MAIN_PLANE;
pfd.iPixelType= PFD_TYPE_RGBA;
pfd.cColorBits= 24;
pfd.cDepthBits= 16;
pfd.cAccumBits= 0;
pfd.cStencilBits= 0;
if( (pixelformat= ChoosePixelFormat( MyDC, &pfd ))==0 ) {
MessageBox( NULL, "ChoosePixelFormat failed", "Error", MB_OK );
return false;
}
if( SetPixelFormat( MyDC, pixelformat, &pfd )==false ) {
MessageBox( NULL, "SetPixelFormat failed", "Error", MB_OK );
return false;
}
return true;
}
/*
* Resize : to re-initialize
*/
void Resize(const int cx, const int cy)
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glViewport( 0, 0, cx, cy );
// 3D orthographic viewing
if( cx <= cy ) {
float ratio = (float)cy/(float)cx;
glOrtho( -1.0, 1.0, -ratio, ratio, -1.0, 1.0 );
}
else {
float ratio = (float)cx/(float)cy;
glOrtho( -ratio, ratio, -1.0, 1.0, -1.0, 1.0 );
}
return;
}
/*
* DrawScene : to draw a scene
*/
void DrawScene(void)
{
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//DrawScore( 1.1f, 0.8f );
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0f, 0.0f, 0.0f);
glRectf( centerPos[0][0]-0.1f,centerPos[0][1]-0.1f,centerPos[0][0]+0.1f,centerPos[0][1]+0.1f);
glRectf( centerPos[1][0]-0.1f,centerPos[1][1]-0.1f,centerPos[1][0]+0.1f,centerPos[1][1]+0.1f);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0f, 1.0f, 0.0f);
glRectf( centerPos[0][0]-0.1f,centerPos[0][1]-0.1f,centerPos[0][0]+0.1f,centerPos[0][1]+0.1f);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf( centerPos[1][0]-0.1f,centerPos[1][1]-0.1f,centerPos[1][0]+0.1f,centerPos[1][1]+0.1f);
glColor3f(1.0f, 1.0f, 0.5f);
glRectf(-1.5f, -1.0f, 1.5f, 1.0f);
glFlush();
SwapBuffers(MyDC);
return;
}
/*
bool DoColide(float centerX, float centerY)
{
if( centerX >= 1.0f || centerY >= 1.0 || centerX <= -1.0f || centerY <= -1.0f)
return true;
return false;
}
bool DoColide(float left, float bottom, float right, float top)
{
float bound = 1.00001f;
if( left < -bound || bottom < -bound ){
moveDirection[0] = -moveDirection[0];
return true;
}
else if(bottom < -bound || top > bound){
moveDirection[1] = -moveDirection[1];
return true;
}
return false;
}
*/
void DoColide(void)
{
for(int index=0; index<2; index++){
float centerX = centerPos[index][0]+moveDirection[index][0];
float centerY = centerPos[index][1]+moveDirection[index][1];
float left = centerX-0.1f, right = centerX+ 0.1f;
float bottom = centerY-0.1f, top = centerY+ 0.1f;
float bound = 1.00001f;
if( left < -bound || right > bound ){
moveDirection[index][0] = -moveDirection[index][0];
return;
}
else if( bottom < -bound || top > bound ){
moveDirection[index][1] = -moveDirection[index][1];
return;
}
centerPos[index][0]+=moveDirection[index][0];
centerPos[index][1]+=moveDirection[index][1];
}
return;
}
/*
void DrawScore( float posX, float posy )
{
char str[256];
sprintf_s( str, "Score: %2d", num_items );
glRasterPos2f( posX, posY );
glCallLists( strlen(str), GL_UNSIGNED_BYTE, str );
glColor3f( 0.75f, 0.75f, 0.75f );
glRectf( posX-0.02f, posY-0.02f, posX+0.23f, posY+0.05f );
return;
}
*/
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.