我正在尝试为我的程序(C ++ Win32)实现FPS和DeltaTime。以下是我当前的代码。 FPS和DeltaTime应该以正确的方式实施。如果没有,请告诉我如何解决我的问题。
我面临的当前问题是我应该如何使用DeltaTime。那就是更新和渲染。是的,我确实使用了包装类。 在我实现之前,我使用WindowProcedure来处理我的消息,我没有问题。但现在,试图实现这一点正在倾向于我。所以在我使用后台缓冲区和WM_PAINT绘制之前我必须接受hwnd才能绘制。并且更新是通过WindowProcedure的输入,它必须接受像LPARAM和WPARAM这样的参数但是在阅读了关于这个主题的文章和论坛之后。需要更新和渲染,但他们没有必要接受渲染方法的hwnd。至于更新,他们不必接受它。
所以基本上我只是想知道如何编写更新和渲染方法?
bool BaseWindow::HandleMessages() {
// Counts Per Second
INT64 counts_per_sec = 0;
QueryPerformanceFrequency( ( LARGE_INTEGER* ) &counts_per_sec );
// Seconds Per Count
float sec_per_count = 1.0f / ( float ) counts_per_sec;
// Pervious Time
INT64 prev_time = 0;
QueryPerformanceCounter( ( LARGE_INTEGER* ) &prev_time );
MSG message = { 0 };
if ( PeekMessage( &message, NULL, 0, 0, PM_REMOVE )) {
TranslateMessage( &message );
DispatchMessage( &message );
if ( message.message == WM_QUIT ) {
OnDestroy();
return false;
}
}
else {
// Get current count
INT64 current_time = 0;
QueryPerformanceCounter( ( LARGE_INTEGER* ) ¤t_time );
// DeltaTime
float delta_time = ( current_time - prev_time ) * sec_per_count;
// Update
// Render
}
return true;
}
更新 的 Main.cpp的
#include "BaseWindow.h"
#include "ChildWindow.h"
int APIENTRY WinMain( HINSTANCE h_instance, HINSTANCE h_prev_instance, LPSTR lp_cmd_line, int n_cmd_show ) {
ChildWindow child_window( h_instance, TEXT( "Child Window" ) );
BaseWindow base_window( TEXT( "Base Window" ), child_window.ClassName() );
while ( base_window.HandleMessages() );
return 0;
}
AbstractWindow.h
#ifndef __ABSTRACTWINDOW_H__
#define __ABSTRACTWINDOW_H__
#pragma once
#include <Windows.h>
class AbstractWindow {
#pragma region Methods
public:
AbstractWindow();
~AbstractWindow();
virtual bool Create();
static LRESULT CALLBACK MessageHandler( HWND, UINT, WPARAM, LPARAM );
protected:
virtual LRESULT CALLBACK WindowProcedure( HWND, UINT, WPARAM, LPARAM ) = 0;
#pragma endregion
#pragma region Variables
protected:
HWND hwnd_;
DWORD style_ex_;
LPCTSTR class_name_;
LPCTSTR window_name_;
DWORD style_;
int x_;
int y_;
int width_;
int height_;
HWND parent_;
HMENU menu_;
HINSTANCE instance_;
#pragma endregion
};
#endif // !__ABSTRACTWINDOW_H__
AbstractWindow.cpp
#include "AbstractWindow.h"
AbstractWindow::AbstractWindow() {}
AbstractWindow::~AbstractWindow() {}
bool AbstractWindow::Create() {
// Default Create Method
hwnd_ = CreateWindowEx(
style_ex_,
class_name_,
window_name_,
style_,
x_,
y_,
width_,
height_,
parent_,
menu_,
instance_,
this
);
return ( hwnd_ ? true : false );
}
LRESULT CALLBACK AbstractWindow::MessageHandler( HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param ) {
AbstractWindow* abstract_window = 0;
if ( message == WM_NCCREATE ) {
abstract_window = ( AbstractWindow* ) ( ( LPCREATESTRUCT( l_param ) )->lpCreateParams );
SetWindowLong( hwnd, GWL_USERDATA, long( abstract_window ) );
}
abstract_window = ( AbstractWindow * ) ( GetWindowLong( hwnd, GWL_USERDATA ) );
if ( abstract_window ) {
return abstract_window->WindowProcedure( hwnd, message, w_param, l_param );
}
else {
return DefWindowProc( hwnd, message, w_param, l_param );
}
}
BaseWindow.h
#ifndef __BASEWINDOW_H__
#define __BASEWINDOW_H__
#pragma once
#include "AbstractWindow.h"
#include "ChildWindow.h"
class BaseWindow : public AbstractWindow {
#pragma region Test Methods
private:
void Update();
void Render();
#pragma endregion
#pragma region Methods
public:
BaseWindow();
~BaseWindow();
bool HandleMessages();
BaseWindow( const TCHAR*, const TCHAR* );
void Show();
virtual LRESULT CALLBACK WindowProcedure( HWND, UINT, WPARAM, LPARAM );
#pragma region Handles
private:
bool CreateBackBuffer( HWND );
bool OnPaint( HWND );
bool PaintManager();
bool OnDestroy();
#pragma endregion
#pragma endregion
#pragma region Variables
private:
RECT window_rect_; // Structure for window width and height
int client_width_;
int client_height_;
POINT mouse_pos_;
#pragma region Back Buffer
HDC hdc_; // Handle to Device Context
HDC back_buffer_; // Back Buffer
HBITMAP bitmap_; // Current bitmap
#pragma endregion
#pragma endregion
};
#endif // !__BASEWINDOW_H__
BaseWindow.cpp
#include "BaseWindow.h"
BaseWindow::BaseWindow() {}
BaseWindow::~BaseWindow() {}
BaseWindow::BaseWindow( const TCHAR* window_name, const TCHAR* class_name ) {
style_ex_ = NULL;
class_name_ = class_name;
window_name_ = window_name;
style_ = WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
x_ = CW_USEDEFAULT;
y_ = CW_USEDEFAULT; CW_USEDEFAULT;
width_ = CW_USEDEFAULT;
height_ = CW_USEDEFAULT;
parent_ = NULL;
menu_ = NULL;
instance_ = GetModuleHandle( NULL );
Create();
Show();
}
void BaseWindow::Show() {
ShowWindow( hwnd_, SW_SHOW );
UpdateWindow( hwnd_ );
}
LRESULT CALLBACK BaseWindow::WindowProcedure( HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param ) {
switch ( message ) {
case WM_CREATE:
CreateBackBuffer(hwnd);
return true;
case WM_ERASEBKGND:
return true;
case WM_DESTROY:
return OnDestroy();
default:
return DefWindowProc( hwnd, message, w_param, l_param );
}
}
bool BaseWindow::HandleMessages() {
// Counts Per Second
INT64 counts_per_sec = 0;
QueryPerformanceFrequency( ( LARGE_INTEGER* ) &counts_per_sec );
// Seconds Per Count
float sec_per_count = 1.0f / ( float ) counts_per_sec;
// Pervious Time
INT64 prev_time = 0;
QueryPerformanceCounter( ( LARGE_INTEGER* ) &prev_time );
MSG message = { 0 };
if ( PeekMessage( &message, NULL, 0, 0, PM_REMOVE ) ) {
TranslateMessage( &message );
DispatchMessage( &message );
if ( message.message == WM_QUIT ) {
OnDestroy();
return false;
}
}
else {
// Get current count
INT64 current_time = 0;
QueryPerformanceCounter( ( LARGE_INTEGER* ) ¤t_time );
// DeltaTime
float delta_time = ( current_time - prev_time ) * sec_per_count;
// Update
// Render
// I need to call for OnPaint()
// I need to take in hwnd
// But how ?
// Or do i not take in hwnd ?
// But hoe ?
}
return true;
}
#pragma region Handles
bool BaseWindow::CreateBackBuffer( HWND hwnd ) {
GetClientRect( hwnd, &window_rect_ );
client_width_ = window_rect_.right;
client_height_ = window_rect_.bottom;
back_buffer_ = CreateCompatibleDC( NULL ); // Create Back Buffer
hdc_ = GetDC( hwnd ); // Get the Device Context
bitmap_ = CreateCompatibleBitmap( hdc_, client_width_, client_height_ ); // Create Bitmap
SelectObject( back_buffer_, bitmap_ ); // Select Bitmap
ReleaseDC( hwnd, hdc_ ); // Release
return true;
}
bool BaseWindow::OnPaint( HWND hwnd ) {
PAINTSTRUCT paint_struct;
hdc_ = BeginPaint( hwnd_, &paint_struct ); // Get the Device Context
BitBlt( back_buffer_, 0, 0, client_width_, client_height_, NULL, NULL, NULL, WHITENESS );
// Paint
PaintManager();
BitBlt( hdc_, 0, 0, client_width_, client_height_, back_buffer_, 0, 0, SRCCOPY ); // Display the back buff
InvalidateRect( hwnd, NULL, true ); // Repaint the screen
EndPaint( hwnd, &paint_struct );
return true;
}
bool BaseWindow::PaintManager() {
HBRUSH brush = ( HBRUSH ) ( GetStockObject( WHITE_BRUSH ) );
SelectObject( back_buffer_, brush ); // Select Brush
Rectangle( back_buffer_, 200, 200, 500, 500 );
DeleteObject( brush );
return true;
}
bool BaseWindow::OnDestroy() {
PostQuitMessage( 0 );
return true;
}
#pragma endregion
ChildWindow.h
#ifndef __CHILDWINDOW_H__
#define __CHILDWINDOW_H__
#pragma once
#include "AbstractWindow.h"
#include "BaseWindow.h"
class ChildWindow : protected WNDCLASSEX {
#pragma region Methods
public:
ChildWindow();
~ChildWindow();
ChildWindow( HINSTANCE, const TCHAR* );
bool Register();
const TCHAR* ClassName() const;
#pragma endregion
};
#endif // !__CHILDWINDOW_H__
ChildWindow.cpp
#include "ChildWindow.h"
ChildWindow::ChildWindow() {}
ChildWindow::~ChildWindow() {}
ChildWindow::ChildWindow( HINSTANCE h_instance, const TCHAR* class_name ) {
cbSize = sizeof( WNDCLASSEX );
style = NULL;
lpfnWndProc = AbstractWindow::MessageHandler;
cbClsExtra = NULL;
cbWndExtra = NULL;
hInstance = h_instance;
hIcon = LoadIcon( NULL, IDI_APPLICATION );
hCursor = LoadCursor( NULL, IDC_ARROW );
//hbrBackground = ( HBRUSH ) ( GetStockObject( DKGRAY_BRUSH ) );
hbrBackground = ( HBRUSH ) NULL;
lpszMenuName = NULL;
lpszClassName = class_name;
hIconSm = LoadIcon( NULL, IDI_APPLICATION );
Register();
}
bool ChildWindow::Register() {
return ( ( RegisterClassEx( this ) ) ? true : false );
}
const TCHAR* ChildWindow::ClassName() const {
return lpszClassName;
}
答案 0 :(得分:0)
我之前已经回答了这个问题,但你忽略了我的评论。您不应该从InvalidateRect
致电WM_PAINT
。您必须从InvalidateRect
中删除OnPaint
。
甚至没有调用OnPaint
函数。您必须添加以下行:
case WM_PAINT:
OnPaint();
return 0;
你在主消息循环中做了奇怪的事情。这非常危险。只需完全删除HandleMessages
即可。用简单的消息循环替换它:
int APIENTRY WinMain(HINSTANCE h_instance, HINSTANCE, LPSTR, int)
{
ChildWindow child_window(h_instance, TEXT("Child Window"));
BaseWindow base_window(TEXT("Base Window"), child_window.ClassName());
//main message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
单独保留主消息循环。您可以稍后在消息循环中处理加速器,但这是关于它的。
现在假设您要添加动画。首先,让我们从一些简单的动画开始。声明全局变量int g_counter = 0;
然后像这样更改PaintManager
:
bool BaseWindow::PaintManager()
{
HBRUSH brush = (HBRUSH)(GetStockObject(WHITE_BRUSH));
SelectObject(back_buffer_, brush); // Select Brush
Rectangle(back_buffer_, 200, 200, 500, 500);
DeleteObject(brush);
TCHAR buf[100];
wsprintf(buf, TEXT("%d"), g_counter);
TextOut(back_buffer_, 0, 0, buf, strlen(buf));
return true;
}
为信号动画添加内容:
case WM_LBUTTONDOWN:
animate();
return 0;
制作动画功能:
void BaseWindow::animate()
{
for (int i = 0; i < 10000; i++)
{
g_counter++;
InvalidateRect(hwnd_, 0, FALSE);
//this code allows window to refresh itself, use it as is
//don't mess around with it
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
PostQuitMessage(0);
return;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
您可以在for
循环中放置一个计时器以减慢它...如果您不需要高精度计时器,也可以查看WM_TIMER
。