include pch include App include dinput include zp user_interface_pc Wi

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "pch.h"
#include "App.h"
#include "dinput.h"
#include <zp/user_interface_pc/Window.h>
#include <zp/graphics/display_adapter.h>
#include <zp/graphics/GraphicsDeviceCreationParameters.h>
#include <zp/graphics/GraphicsDevice.h>
#include <zp/graphics/ShaderResourceContext.h>
#include <zp/graphics/TextureResourceContext.h>
#include <zp/graphics_dx9/GraphicsDeviceDx9.h>
#include <zp/render_system/RenderContext.h>
#include <zp/render_system/RenderInfo.h>
#include <zp/render_system/ShapesRenderer.h>
#include <zp/render_system/EffectResourceContext.h>
#include <zp/render_system/MaterialResourceContext.h>
#include <zp/render_system/FontResourceContext.h>
#include <zp/render_system/QuadShape.h>
#include <zp/render_system/TextShape.h>
#include <zp/scene_graph/Node.h>
#include <zp/resource_manager/ResourceContextManager.h>
#include <zp/input_output/FileInputStream.h>
#include <zp/collada_scene_loader/ColladaSceneLoader.h>
#include <zp/collada_scene_loader/ColladaSceneDocument.h>
#include <zp/graphics/resource_types.h>
#include <zp/render_system/resource_types.h>
#include <zp/input_system/InputSystem.h>
#include <zp/input_system_pc/uids.h>
#include <zp/input_output/FileInputStream.h>
#include <zp/input_output/Path.h>
#include <zp/input_output/fs/FilesFileSystem.h>
#include <zp/particles/CommonEmitPolicy.h>
#include <zp/particles/RandomLifeInitPolicy.h>
#include <zp/particles/PointPositionInitPolicy.h>
#include <zp/particles/RenderUpdaterInitPolicy.h>
#include <zp/particles/ConeVelocityInitPolicy.h>
#include <zp/particles/CommonLifeUpdatePolicy.h>
#include <zp/particles/PositionUpdatePolicy.h>
#include <zp/particles/DeathParticleController.h>
#include <zp/particles/ParticleSystemRenderable.h>
#include <zp/particles/PointRenderer.h>
#include <zp/particles/PointRendererRenderUpdater.h>
#include <zp/particles/CommonRenderUpdatePolicy.h>
#include "EditorResourceStreamManager.h"
namespace zp {
namespace zp_test {
namespace {
const bool save_resources = false;
const String animations_path("D:/work/assets/tmp/");
} //namespace
AppInputEventListener::AppInputEventListener()
{
}
AppInputEventListener::AppInputEventListener( App* app, const Float speed )
: m_app(app)
, m_speed(speed)
{
}
AppInputEventListener::~AppInputEventListener()
{
}
void AppInputEventListener::on_event( const input_system::InputEvent& event_data )
{
using namespace input_system;
using namespace input_system_pc;
if(event_data.value() == mouse_axe_x)
{
Int x = event_data.offset();
m_app->set_mouse_x(x);
}
if(event_data.value() == mouse_axe_y)
{
Int y = event_data.offset();
m_app->set_mouse_y(y);
}
}
AppInputEventListenerAim::AppInputEventListenerAim()
{
}
AppInputEventListenerAim::AppInputEventListenerAim( App* app, const Bool aim )
: m_app(app)
, m_aim(aim)
{
}
AppInputEventListenerAim::~AppInputEventListenerAim()
{
}
void AppInputEventListenerAim::on_event( const input_system::InputEvent& event_data )
{
}
App::App()
: m_time(boost::numeric_cast<Float>(time(NULL)) / 1000.0f)
, m_mouse_x(0)
, m_mouse_y(0)
{
SharedPtr<EditorResourceStreamManager> stream_manager(new EditorResourceStreamManager());
resource_manager::create_resource_context_manager(stream_manager);
SharedPtr<resource_manager::BaseResourceContext> shaders_resource_context(new graphics::ShaderResourceContext(stream_manager));
resource_manager::resource_context_manager().add_resource_context(graphics::pixel_shader_resource_type(), shaders_resource_context);
resource_manager::resource_context_manager().add_resource_context(graphics::vertex_shader_resource_type(), shaders_resource_context);
SharedPtr<resource_manager::BaseResourceContext> textures_resource_context(new graphics::TextureResourceContext(stream_manager));
resource_manager::resource_context_manager().add_resource_context(graphics::texture_resource_type(), textures_resource_context);
SharedPtr<resource_manager::BaseResourceContext> effects_resource_context(new render_system::EffectResourceContext(stream_manager));
resource_manager::resource_context_manager().add_resource_context(render_system::effect_resource_type(), effects_resource_context);
SharedPtr<resource_manager::BaseResourceContext> material_resource_context(new render_system::MaterialResourceContext(stream_manager));
resource_manager::resource_context_manager().add_resource_context(render_system::material_resource_type(), material_resource_context);
SharedPtr<resource_manager::BaseResourceContext> font_resource_context(new render_system::FontResourceContext(stream_manager));
resource_manager::resource_context_manager().add_resource_context(render_system::font_resource_type(), font_resource_context);
resource_manager::resource_context_manager().add_resource_context(render_system::font_implementation_resource_type(), font_resource_context);
//SharedPtr<resource_manager::BaseResourceContext> skeletal_animation_resource_context(new animation::SkeletalAnimationResourceContext(stream_manager));
//resource_manager::resource_context_manager().add_resource_context(animation::skeletal_animation_resource_type(), skeletal_animation_resource_context);
//SharedPtr<resource_manager::BaseResourceContext> track_resource_context(new animation::TrackResourceContext(stream_manager));
//resource_manager::resource_context_manager().add_resource_context(animation::track_resource_type(), track_resource_context);
//SharedPtr<resource_manager::BaseResourceContext> animation_mask_resource_context(new animation::AnimationMaskResourceContext(stream_manager));
//resource_manager::resource_context_manager().add_resource_context(animation::animation_mask_resource_type(), animation_mask_resource_context);
}
App::~App()
{
}
SharedPtr<graphics::GraphicsDevice> App::create_device(void* window_handle, const mathematics::Dimensions2I& window_size, const graphics::DisplayAdapter display_adapter)
{
graphics::GraphicsDeviceCreationParameters creation_parameters;
creation_parameters.set_hwnd(reinterpret_cast<const HWND>(window_handle));
creation_parameters.set_dimensions(window_size);
creation_parameters.set_display_adapter(display_adapter);
SharedPtr<graphics::GraphicsDevice> device(new graphics_dx9::GraphicsDevice());
if (!device->create(creation_parameters))
return SharedPtr<graphics::GraphicsDevice>();
device->set_clear_color(image::Color4F(0, 0, 0, 0));
return device;
}
bool App::on_create()
{
m_window.reset(new user_interface_pc::Window("zp"));
m_window->create(math::Vector2I(0, 0), math::Dimensions2I(800, 600), window::window_style_normal);
m_device = create_device(m_window->hwnd(), m_window->dimensions(), graphics::da_primary);
m_render_context.reset(new render_system::RenderContext(m_device));
m_input_system.reset(new input_system::InputSystem(m_window->hwnd()));
// Input system
SharedPtr<input_system::InputContext> input_context = m_input_system->create_context(0);
if(input_context)
{
using namespace input_system;
using namespace input_system_pc;
m_input_event_listener.reset(new AppInputEventListener(this, 0.0f));
input_context->register_listener(
InputEventUID(idid_win_mouse, ie_on_change, mouse_axe_x), m_input_event_listener);
input_context->register_listener(
InputEventUID(idid_win_mouse, ie_on_change, mouse_axe_y), m_input_event_listener);
m_input_event_listener.reset(new AppInputEventListenerAim(this, false));
input_context->register_listener(
InputEventUID(idid_dx_keyboard, ie_on_button_down, DIK_A), m_input_event_listener);
m_input_event_listener.reset(new AppInputEventListenerAim(this, true));
input_context->register_listener(
InputEventUID(idid_dx_keyboard, ie_on_button_down, DIK_S), m_input_event_listener);
}
m_renderer.reset(new render_system::ShapesRenderer(m_render_context.get()));
// Particle System
m_particle_system.reset(new particles::ParticleSystem());
SharedPtr<particles::ParticleSystemRenderable> renderable(new particles::ParticleSystemRenderable());
renderable->set_particle_system(m_particle_system);
m_particle_system->set_renderable(renderable);
m_particle_container.reset(new particles::ParticleContainer());
m_particle_emiter.reset(new particles::Emiter());
m_particle_system->add_container(m_particle_container);
m_particle_system->add_emiter(m_particle_emiter);
SharedPtr<particles::PointRenderer> renderer(new particles::PointRenderer());
renderer->set_renderer(m_renderer);
m_particle_system->add_renderer(renderer);
SharedPtr<particles::PointRendererRenderUpdater> render_updater(new particles::PointRendererRenderUpdater());
render_updater->set_particle_container(m_particle_container);
render_updater->set_renderer(renderer);
SharedPtr<particles::RenderUpdaterInitPolicy> render_updater_init_policy(new particles::RenderUpdaterInitPolicy());
render_updater_init_policy->set_render_updater(render_updater);
m_particle_emiter->add_init_policy(render_updater_init_policy);
SharedPtr<particles::CommonEmitPolicy> emit_policy(new particles::CommonEmitPolicy());
emit_policy->set_emit_rate(100.0f);
m_particle_emiter->add_emit_policy(emit_policy);
SharedPtr<particles::RandomLifeInitPolicy> life_init_policy(new particles::RandomLifeInitPolicy());
life_init_policy->set_max_life(2.0f);
m_particle_emiter->add_init_policy(life_init_policy);
SharedPtr<particles::PointPositionInitPolicy> position_init_policy(new particles::PointPositionInitPolicy());
m_particle_emiter->add_init_policy(position_init_policy);
SharedPtr<particles::ConeVelocityInitPolicy> velocity_init_policy(new particles::ConeVelocityInitPolicy());
velocity_init_policy->set_min_z_angle(-math::pi * 0.25f);
velocity_init_policy->set_max_z_angle(math::pi * 0.25f);
velocity_init_policy->set_min_speed(0.1f);
velocity_init_policy->set_max_speed(0.5f);
m_particle_emiter->add_init_policy(velocity_init_policy);
m_particle_emiter->set_particle_container(m_particle_container);
SharedPtr<particles::BaseUpdatePolicy> position_update_policy(new particles::PositionUpdatePolicy());
m_particle_container->add_update_policy(position_update_policy);
SharedPtr<particles::BaseUpdatePolicy> life_update_policy(new particles::CommonLifeUpdatePolicy());
m_particle_container->add_update_policy(life_update_policy);
m_gravity_velocity_policy.reset(new particles::GravityVelocityUpdatePolicy());
m_particle_container->add_update_policy(m_gravity_velocity_policy);
SharedPtr<particles::CommonRenderUpdatePolicy> common_render_update_policy(new particles::CommonRenderUpdatePolicy());
m_particle_container->add_render_update_policy(common_render_update_policy);
SharedPtr<particles::DeathParticleController> death_particle_controller(new particles::DeathParticleController());
m_particle_container->add_particle_controller(death_particle_controller);
m_particle_position_accessor.set_container(m_particle_container);
m_particle_container->reserve(10000);
return true;
}
void App::on_idle()
{
Base::on_idle();
m_window->process_messages();
m_input_system->update();
Float current_time = boost::numeric_cast<Float>(time(NULL)) / 1000.0f;
Float dt = 1.0f / 15.0f;
m_time = current_time;
m_particle_system->update(dt);
m_particle_system->renderable()->update(dt);
render();
}
bool App::is_valid() const
{
return m_window->is_valid();
}
void App::render()
{
m_device->clear(graphics::GraphicsDevice::cf_back_buffer | graphics::GraphicsDevice::cf_depth_buffer, 1.f, 0);
m_device->begin_scene();
m_particle_system->renderable()->render(render_system::RenderInfo(m_render_context.get()));
//m_device->render(render_system::RenderInfo(render_context.get()));
m_renderer->render(render_system::RenderInfo(m_render_context.get()));
m_device->end_scene();
m_device->present();
}
void App::render_particle_system()
{
for(Size particle_n = 0; particle_n != m_particle_container->particles_count(); ++particle_n)
{
const Float x_coof = 2.0f;
const Float y_coof = 1.5f;
const float square_2 = 0.003f;
math::Vector3F position =
m_particle_position_accessor.attribute(m_particle_container->particle(particle_n)).position();
position.set(position.x() / x_coof, position.y() / y_coof, 0);
SharedPtr<render_system::QuadShape> test_shape(new render_system::QuadShape(
math::Vector3F(position.x() - square_2, position.y() - square_2, 0.0f),
math::Vector3F(position.x() + square_2, position.y() - square_2, 0.0f),
math::Vector3F(position.x() + square_2, position.y() + square_2, 0.0f),
math::Vector3F(position.x() - square_2, position.y() + square_2, 0.0f),
image::Color4F(1.0f, 1.0f, 1.0f, 1.0f),
m_render_context.get()));
m_renderer->add_shape(test_shape);
}
}
const SharedPtr<render_system::RenderContext>& App::render_context() const
{
return m_render_context;
}
void App::on_change_mouse_position()
{
math::Vector3F gravity(zp_numeric_cast<Float>(m_mouse_x - 400), -zp_numeric_cast<Float>(m_mouse_y - 300), 0);
gravity *= 1.0f / 100.0f;
m_gravity_velocity_policy->set_gravity(gravity);
}
void App::set_mouse_x( Int x )
{
m_mouse_x = x;
on_change_mouse_position();
}
void App::set_mouse_y( Int y )
{
m_mouse_y = y;
on_change_mouse_position();
}
} // namespace zp_test
} // namespace zp