´ë¹®    
FindPage  |  TitleIndex  |  UserPreferences  |  [http]me2day  |  redpixel
RecentChanges
 


win32api/ÅÊÅ©µ¥¸ð1

Contents

1 Áغñ
2 ¼Ò½º
2.1 tank.hpp
2.2 tank.cpp

1 Áغñ #

  1. win32api/±âº»±¸Á¶ÀÇ »À´ë¼Ò½º¸¦ ±âÃÊ·Î »õ·Î¿î ÇÁ·ÎÁ§Æ®¸¦ Çϳª ¸¸µì´Ï´Ù.
  2. ¾Æ·¡ÀÇ tank.hpp, tank.cpp¸¦ »õ·Î ÀÛ¼ºÇØ Ãß°¡ÇÕ´Ï´Ù.
  3. main.cpp »ó´Ü ¼±¾ðÀ» ´ÙÀ½°ú °°ÀÌ °íĨ´Ï´Ù.
    #include <windows.h>
    #include "tank.hpp"
    
    #define WM_TANK_MOVE (WM_USER+1)
    #define WM_TANK_STEP (WM_USER+2)
    
  4. main.cppºÎºÐ¿¡¼­ winmain()¾ÈÀÇ ¸Þ¼¼Áö ·çÇÁ ºÎºÐÀ» ´ÙÀ½°ú °°ÀÌ °íĨ´Ï´Ù. ( while(GetMessage(...)) {} ºÎºÐÀÔ´Ï´Ù )
        static DWORD prevtime_ = GetTickCount();
    		while (1) { 
      		if (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)) {
      			if (messages.message == WM_QUIT) break;
       			TranslateMessage(&messages);
         		DispatchMessage(&messages);
        	}
        	
        	if ( (GetTickCount() - prevtime_) > 100 ) {
     				SendMessage( hwnd, WM_TANK_STEP, (WPARAM) NULL, (LPARAM) NULL );
      			prevtime_ = GetTickCount(); 		
       		}
    		}
    
    


  5. main.cppÀÇ À©µµ¿ì ÇÁ·Î½ÃÁ®ºÎºÐÀ» ´ÙÀ½°ú °°ÀÌ °íĨ´Ï´Ù.
    LRESULT CALLBACK GameWindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* ¸Þ¼¼Áö¿¡ µû¶ó ºÐ±âÇÕ´Ï´Ù */
        {
        		case WM_CREATE:
    						tank_init( hwnd );
        				break;
    				case WM_SIZE:
    						tank_update();
    						InvalidateRect( hwnd, NULL, TRUE );
    						break;
    				case WM_MOUSEMOVE:
    						tank_cannon_realign(LOWORD(lParam), HIWORD(lParam));
    						break;
    				case WM_TANK_STEP:
    						tank_step();
    						InvalidateRect( hwnd, NULL, TRUE );
    						break;
    				case WM_LBUTTONDOWN:
    						tank_move_target(LOWORD(lParam), HIWORD(lParam));
    						break;
    				case WM_PAINT:
    						{
    							PAINTSTRUCT ps;
    							HDC hdc_ = BeginPaint( hwnd, &ps );
    							tank_render_all( hdc_ );
    					 		EndPaint( hwnd, &ps );
    						}
    						break;
            case WM_DESTROY:
                PostQuitMessage (0);       /* ¸Þ¼¼Áö Å¥¿¡ WM_QUIT ¸Þ¼¼Áö¸¦ º¸³À´Ï´Ù. */
                break;
            default:                      /* ´Ù·çÁö ¾ÊÀº ¸Þ¼¼Áö´Â ¸ðµÎ ¿î¿µÃ¼°è¿¡°Ô ³Ñ±é´Ï´Ù. */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

2 ¼Ò½º #

2.1 tank.hpp #

#ifndef __TANK_HPP__
#define __TANK_HPP__

#include <windows.h> 

#define TANK_BODY_SIZE 0.05f
#define TANK_CANNON_LENGTH 0.027f
#define TANK_SPEED 0.02f

// ÅÊÅ©Á¤º¸
typedef struct {
	float x, y;									// ÇöÀç À§Ä¡ 
	float cannon_dx, cannon_dy;	// Æ÷´ë ¹æÇâ
	float move_dx, move_dy; 		// ¿òÁ÷ÀÓ ¹æÇâ
} TANK;

// ÅÊÅ©¸¦ ÃʱâÈ­ÇÕ´Ï´Ù. (WM_CREATE)
void tank_init( HWND hwnd_ );

// ³»ºÎ rect°ªÀ» °»½ÅÇÕ´Ï´Ù. (WM_SIZE)
void tank_update( );

// Æ÷´ë¹æÇâÀ» °»½ÅÇÕ´Ï´Ù. (WM_MOUSEMOVE)
void tank_cannon_realign(int mx, int my);

// ÅÊÅ©¸¦ ÇÑ ½ºÅǾ¿ À̵¿½Ãŵ´Ï´Ù. (¸Þ¼¼Áö·çÇÁ ºÎºÐÀÇ Å¸ÀÌ¸Ó Ã³¸®)
void tank_step();

// ÅÊÅ©°¡ À̵¿ÇÒ ÁÂÇ¥¸¦ ¼³Á¤ÇÕ´Ï´Ù. (WM_LBUTTONDOWN)
void tank_move_target( int tx, int ty );

// ¿î¼®µé°ú Ç÷¹À̾ ÇöÀç À©µµ¿ì¿¡ ±×¸³´Ï´Ù. (WM_PAINT)
void tank_render_all( HDC hdc_ );

#endif

2.2 tank.cpp #

#include <time.h>
#include <math.h>
#include "tank.hpp"

static RECT g_client_rect;					// ¿Ü°û °æ°è RECT°ª
static HWND g_hwnd;								// °ÔÀÓ Ãâ·Â À©µµ¿ì ÇÚµé
static TANK g_tank;

// ÅÊÅ©¸¦ ÃʱâÈ­ÇÕ´Ï´Ù.
void tank_init( HWND hwnd_ ) {
	srand((unsigned) time(NULL) ); // ³­¼öÇ¥ ÃʱâÈ­ 
	g_hwnd = hwnd_;
	
  g_tank.x = 0.5f;
  g_tank.y = 0.5f;
  
	g_tank.cannon_dx = 0.0f;
	g_tank.cannon_dy = TANK_CANNON_LENGTH;
	g_tank.move_dx = 0.0f;
	g_tank.move_dy = 0.0f;
}

// ¿Ü°û rect °ªÀ» °»½ÅÇÕ´Ï´Ù.
void tank_update( ) {
	GetClientRect( g_hwnd, &g_client_rect );
}

// ÅÊÅ©¸¦ ÇÑ ½ºÅǾ¿ À̵¿½Ãŵ´Ï´Ù. (¸Þ¼¼Áö·çÇÁ ºÎºÐÀÇ Å¸ÀÌ¸Ó Ã³¸®)
void tank_step() {
	g_tank.x += g_tank.move_dx;
	g_tank.y += g_tank.move_dy;
}

// ÅÊÅ©°¡ À̵¿ÇÒ ÁÂÇ¥¸¦ ¼³Á¤ÇÕ´Ï´Ù.
void tank_move_target( int tx, int ty ) {
	float target_x = (float)tx / (float)g_client_rect.right;
	float target_y = (float)ty / (float)g_client_rect.bottom;
	float theta_ = atan2( (target_y - g_tank.y), (target_x - g_tank.x) );
	g_tank.move_dx = TANK_SPEED * cos(theta_);
	g_tank.move_dy = TANK_SPEED * sin(theta_);
}

// Æ÷´ë¹æÇâÀ» °»½ÅÇÕ´Ï´Ù. (WM_MOUSEMOVE)
void tank_cannon_realign(int mx, int my) {
	float target_x = (float)mx / (float)g_client_rect.right;
	float target_y = (float)my / (float)g_client_rect.bottom;
	float theta_ = atan2( (target_y - g_tank.y), (target_x - g_tank.x) );
	g_tank.cannon_dx = TANK_CANNON_LENGTH * cos(theta_);
	g_tank.cannon_dy = TANK_CANNON_LENGTH * sin(theta_);
}

// ¿î¼®µé°ú Ç÷¹À̾ ÇöÀç À©µµ¿ì¿¡ ±×¸³´Ï´Ù. (ÁÖÀÇ:WM_PAINT³»¿¡¼­¸¸ ½ÇÇàÇØ¾ßÇÕ´Ï´Ù)
void tank_render_all( HDC hdc_ ) {
	Ellipse( hdc_, 
		(int)((g_tank.x - (TANK_BODY_SIZE / 2.0f)) * g_client_rect.right), 
		(int)((g_tank.y - (TANK_BODY_SIZE / 2.0f)) * g_client_rect.bottom), 
		(int)((g_tank.x + (TANK_BODY_SIZE / 2.0f)) * g_client_rect.right), 
		(int)((g_tank.y + (TANK_BODY_SIZE / 2.0f)) * g_client_rect.bottom)
	);
	int x1 = (int)(g_tank.x * g_client_rect.right);
	int y1 = (int)(g_tank.y * g_client_rect.bottom);
	int x2 = (int)((g_tank.x + g_tank.cannon_dx) * g_client_rect.right);
	int y2 = (int)((g_tank.y + g_tank.cannon_dy) * g_client_rect.bottom);
	MoveToEx(hdc_, x1, y1, NULL );
	LineTo(hdc_, x2, y2);
}


EditText | FindPage | DeletePage | LikePages

Powered by MoniWiki
xhtml1 | css2 | any browser | rss
Last modified 2004-04-13 09:15:58
Loading 0.4327 sec