U E D R S I H C RSS
ID
Password
Join
๊ฑฐ์ง“๋ง์„ ํ•˜๊ธฐ๋Š” ์‰ฝ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ ํ•œ๋ฒˆ๋งŒ ๊ฑฐ์ง“๋ง์„ ํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค. โ€•ใ€Œํ…์‚ฌ์Šค ๋‰ด์Šคใ€่ชŒ

๏ปฟ

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);
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-10-28 12:42:54
Processing time 0.5858 sec