HGE tutorial 04 学习笔记及摘录

本文介绍如何在游戏引擎中使用渲染目标技术。通过创建渲染目标和关联精灵,可以将绘制的内容渲染到纹理上,并进一步将纹理渲染到屏幕的不同位置,实现特殊视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tutorial 04 - Rendering to texture

In this tutorial we will learn how to use render targets. 



First, we declare the variables to handle the render target and the sprite that we will associate with it:

hgeSprite*   tar;

HTARGET      target;

During some events like video mode switching, the render target's texture handle may change. So, we write the GfxRestoreFunc function in which we obtain the new render target's texture:

bool GfxRestoreFunc()
{
  if(tar && target)
       tar->SetTexture(hge->Target_GetTexture(target));

  return false;
}

In the RenderFunc we first render all our stuff onto the texture, specifying our render target in the Gfx_BeginScene call:

 
  hge->Gfx_BeginScene(target);
  hge->Gfx_Clear(0);
  par->Render();
  spr->Render(x, y);
  hge->Gfx_EndScene();

Then we render several instances of the texture to the screen:

  
  hge->Gfx_BeginScene();
  hge->Gfx_Clear(0);

  for(i=0;i<6;i++)
  {
    tar->SetColor(0xFFFFFF | (((5-i)*40+55)<<24));
    tar->RenderEx(i*100.0f, i*50.0f, i*M_PI/8, 1.0f-i*0.1f);
  }

  fnt->printf(5, 5 ,HGETEXT_LEFT, "dt:%.3f\nFPS:%d",
              hge->Timer_GetDelta(), hge->Timer_GetFPS());
  hge->Gfx_EndScene();

In the WinMain function we attach our GfxRestoreFunc function:

  
hge->System_SetState(HGE_GFXRESTOREFUNC, GfxRestoreFunc);

Then, after HGE initiation, we create a render target and a sprite that we will use to make rendering of the created texture easier:

 
   target=hge->Target_Create(512,512,false);

    tar=new hgeSprite(
           hge->Target_GetTexture(target),0,0,512,512);
    tar->SetBlendMode(
           BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);

During shutdown we delete the sprite that we used to render the generated texture and the render target itself:

   
 delete tar;
 hge->Target_Free(target);

The rest of shutdown process is identical to the one demonstrated in previous tutorials. 

The complete source code with detailed comments for this tutorial you may find in the folder tutorials\tutorial04. The required media files you'll find in the folder tutorials\precompiled.


源码及简单注释

/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hge_tut04 - Using render targets
*/


// Copy the files "particles.png", "menu.wav",
// "font1.fnt", "font1.png" and "trail.psi" from
// the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.


#include "..\..\include\hge.h"
#include "..\..\include\hgesprite.h"
#include "..\..\include\hgefont.h"
#include "..\..\include\hgeparticle.h"


HGE *hge=0;


hgeSprite*			spr;
hgeSprite*			spt;
hgeSprite*			tar;
hgeFont*			fnt;
hgeParticleSystem*	par;

HTEXTURE			tex;
HEFFECT				snd;

// HGE render target handle
HTARGET				target;

float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

const float speed=90;
const float friction=0.98f;

void boom() {
	int pan=int((x-256)/2.56f);
	float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
	hge->Effect_PlayEx(snd,100,pan,pitch);
}

// This function will be called by HGE when
// render targets were lost and have been just created
// again. We use it here to update the render
// target's texture handle that changes during recreation.
bool GfxRestoreFunc()//当窗口焦点失去再重新获得焦点时,会丢失场景数据,这里重新获得数据
{
	if(tar && target) tar->SetTexture(hge->Target_GetTexture(target));
	return false;
}


bool FrameFunc()
{
	float dt=hge->Timer_GetDelta();

	// Process keys
	if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
	if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt;
	if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt;
	if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt;
	if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt;

	// Do some movement calculations and collision detection	
	dx*=friction; dy*=friction; x+=dx; y+=dy;
	if(x>496) {x=496-(x-496);dx=-dx;boom();}
	if(x<16) {x=16+16-x;dx=-dx;boom();}
	if(y>496) {y=496-(y-496);dy=-dy;boom();}
	if(y<16) {y=16+16-y;dy=-dy;boom();}

	// Update particle system
	par->info.nEmission=(int)(dx*dx+dy*dy);//速度越快,放射的粒子数越多
	par->MoveTo(x,y);//将粒子系统移到该位置
	par->Update(dt);//更新粒子系统

	return false;
}


bool RenderFunc()
{
	int i;

	// Render graphics to the texture
	hge->Gfx_BeginScene(target);
	hge->Gfx_Clear(0);
	par->Render();
	spr->Render(x, y);
	
	hge->Gfx_EndScene();

	// Now put several instances of the rendered texture to the screen
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	for(i=0;i<6;i++)
	{
		tar->SetColor(0xFFFFFF | (((5-i)*40+55)<<24)); //颜色变化
		//参数3 旋转弧度 参数4 大小变化
		tar->RenderEx(i*100.0f, i*50.0f, i*M_PI/8,1.0f-i*0.06f);

	}
	fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", hge->Timer_GetDelta(), hge->Timer_GetFPS());
	hge->Gfx_EndScene();

	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "hge_tut04.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_GFXRESTOREFUNC, GfxRestoreFunc);//??
	hge->System_SetState(HGE_TITLE, "HGE Tutorial 04 - Using render targets");
	hge->System_SetState(HGE_FPS, 100);
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);

	tar=0;
	target=0;

	if(hge->System_Initiate()) {
		snd=hge->Effect_Load("menu.wav");
		tex=hge->Texture_Load("particles.png");
		if(!snd || !tex)
		{
			// If one of the data files is not found, display
			// an error message and shutdown.
			MessageBox(NULL, "Can't load one of the following files:\nMENU.WAV, PARTICLES.PNG, FONT1.FNT, FONT1.PNG, TRAIL.PSI", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
			hge->System_Shutdown();
			hge->Release();
			return 0;
		}
		//hgeSprite(tex,x,y,w,h) 纹理x,y坐标  w,h 精灵宽,高,不同的坐标能调整出不同的精灵图案
		spr=new hgeSprite(tex, 96, 64, 32, 32);//纹理精灵
		spr->SetColor(0xFFFFA000);

		//设置精灵锚点,锚点是缩放,旋转和运转位置的中心,精灵出现的位置,相对与精灵方块(16,16)为中心
		//(0,0)为左上角
		spr->SetHotSpot(16,16);

		fnt=new hgeFont("font1.fnt");

		spt=new hgeSprite(tex, 64, 64, 32, 32); //粒子精灵
		spt->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
		spt->SetHotSpot(16,16);
		par=new hgeParticleSystem("trail.psi",spt);//加载粒子系统
		par->Fire();

		// Create a render target and a sprite for it
		target=hge->Target_Create(512,512,false);
		tar=new hgeSprite(hge->Target_GetTexture(target),0,0,512,512);
		tar->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);

		// Let's rock now!
		hge->System_Start();

		// Delete created objects and free loaded resources
		delete par;
		delete fnt;
		delete spt;
		delete spr;
		delete tar;
		hge->Target_Free(target);
		hge->Texture_Free(tex);
		hge->Effect_Free(snd);
	}

	// Clean up and shutdown
	hge->System_Shutdown();
	hge->Release();
	return 0;
}



内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值