๊ฑฐ์ง๋ง์ ํ๊ธฐ๋ ์ฝ๋ค. ๊ทธ๋ฌ๋ ๋จ ํ๋ฒ๋ง ๊ฑฐ์ง๋ง์ ํ๊ธฐ๋ ์ด๋ ต๋ค. โใํ
์ฌ์ค ๋ด์คใ่ช
1 ์ค๋น #
- win32api/๊ธฐ๋ณธ๊ตฌ์กฐ์ ๋ผ๋์์ค๋ฅผ ๊ธฐ์ด๋ก ์๋ก์ด ํ๋ก์ ํธ๋ฅผ ํ๋ ๋ง๋ญ๋๋ค.
- ์๋์ tank.hpp, tank.cpp๋ฅผ ์๋ก ์์ฑํด ์ถ๊ฐํฉ๋๋ค.
- main.cpp ์๋จ ์ ์ธ์ ๋ค์๊ณผ ๊ฐ์ด ๊ณ ์นฉ๋๋ค.
#include <windows.h> #include "tank.hpp" #define WM_TANK_MOVE (WM_USER+1) #define WM_TANK_STEP (WM_USER+2)
- 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(); } }
- 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.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);
}









![[http]](/wiki/imgs/http.png)
