From a89dac5c30bb0063076350a5c8a77628e59d2518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Mon, 29 Sep 2014 05:22:12 +0000 Subject: [PATCH] Fixed some stuff and added some small tweaks --- src/camera.cpp | 2 +- src/debug/lines.cpp | 64 +++++++++++++++++++++++++++---- src/easymesh/easymesh.cpp | 2 + src/easymesh/easymesh.h | 6 --- src/image/codec/gdiplus-image.cpp | 3 +- src/image/color/color.cpp | 19 +++++---- src/input/controller.h | 6 ++- src/lol/algorithm/sort.h | 12 ++++++ src/lol/base/array.h | 1 + src/lol/debug/lines.h | 5 +++ src/lol/image/color.h | 2 +- src/lol/math/geometry.h | 12 ++++++ src/ticker.cpp | 11 ++++-- src/worldentity.cpp | 4 -- src/worldentity.h | 8 ++-- 15 files changed, 120 insertions(+), 37 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 9be3d593..cc1aaca7 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -173,7 +173,7 @@ void Camera::SetPosition(vec3 pos, bool keep_target) if (keep_target) SetView(pos, GetTarget(), GetUp()); else - SetView(GetView() * mat4::translate(pos - m_position)); + SetView(GetView() * mat4::translate(m_position - pos)); m_position = pos; } diff --git a/src/debug/lines.cpp b/src/debug/lines.cpp index 7176b0ae..6f1ca147 100644 --- a/src/debug/lines.cpp +++ b/src/debug/lines.cpp @@ -98,7 +98,8 @@ vec3 Debug::ScreenToWorld(vec3 pos, mat4 view, mat4 proj) } //Draw stuff in World -//-- LINE: 3D -2D - 3D_to_2D +//-- LINE: 3D -2D - 3D_to_2D -------------------------------------------------- + void Debug::DrawLine(vec3 a, vec3 b) { Debug::DrawLine(a, b, g_scene->GetLineColor()); } void Debug::DrawLine(vec2 a, vec3 b, float az) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az); } void Debug::DrawLine(vec2 a, vec2 b, float az, float bz) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az, bz); } @@ -106,7 +107,56 @@ void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { g_scene->A void Debug::DrawLine(vec2 a, vec3 b, vec4 color, float az) { g_scene->AddLine(a, b, color, az); } void Debug::DrawLine(vec2 a, vec2 b, vec4 color, float az, float bz){ g_scene->AddLine(a, b, color, az, bz); } -//-- ARROW: 3D -2D - 3D_to_2D +//-- GIZMO -------------------------------------------------------------------- +void Debug::DrawGizmo(vec3 pos, vec3 x, vec3 y, vec3 z, float size) +{ + Debug::DrawLine(pos, pos + x * size, Color::red); + Debug::DrawLine(pos, pos + y * size, Color::green); + Debug::DrawLine(pos, pos + z * size, Color::blue); +} +void Debug::DrawGizmo(vec2 pos, vec3 x, vec3 y, vec3 z, float size, float posz) +{ + Debug::DrawLine(pos, pos + x.xy * size, Color::red, posz, posz + x.z * size); + Debug::DrawLine(pos, pos + y.xy * size, Color::green, posz, posz + y.z * size); + Debug::DrawLine(pos, pos + z.xy * size, Color::blue, posz, posz + z.z * size); +} + +//-- GRID --------------------------------------------------------------------- +void Debug::DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d) +{ + float seg_sz = g_scene->GetLineSegmentSize(); + int seg_nb = lol::max((int)(size / seg_sz), 1); + seg_sz = size / (float)seg_nb; + + //Draw Gizmo + Debug::DrawGizmo(pos, x, y, z, size); + Debug::DrawLine(pos, pos - x * size, Color::gray); + Debug::DrawLine(pos, pos - y * size, Color::gray); + Debug::DrawLine(pos, pos - z * size, Color::gray); + + //grid + for (float i = -seg_nb; i <= seg_nb; i++) + { + if (i != 0) + { + float iter = seg_sz * ((float)i); + Debug::DrawLine(vec3(-size, 0, iter), vec3(size, 0, iter), Color::gray_dark); + Debug::DrawLine(vec3(iter, 0, -size), vec3(iter, 0, size), Color::gray_dark); + + if (draw_3d) + { + Debug::DrawLine(vec3(0, -size, iter), vec3(0, size, iter), Color::gray_dark); + Debug::DrawLine(vec3(0, iter, -size), vec3(0, iter, size), Color::gray_dark); + + Debug::DrawLine(vec3(-size, iter, 0), vec3(size, iter, 0), Color::gray_dark); + Debug::DrawLine(vec3(iter, -size, 0), vec3(iter, size, 0), Color::gray_dark); + } + } + } + +} + +//-- ARROW: 3D -2D - 3D_to_2D ------------------------------------------------- void Debug::DrawArrow(vec3 a, vec3 b, vec2 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y)); } void Debug::DrawArrow(vec2 a, vec3 b, vec2 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), az, b.z); } void Debug::DrawArrow(vec2 a, vec2 b, vec2 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), az, bz); } @@ -161,7 +211,7 @@ void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz) Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, color, bz - (z + y).z, bz - (z - y).z); } -//-- BOX: 3D -2D - 3D_to_2D +//-- BOX: 3D -2D - 3D_to_2D --------------------------------------------------- void Debug::DrawBox(box3 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); } void Debug::DrawBox(box2 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); } void Debug::DrawBox(box3 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); } @@ -219,7 +269,7 @@ void Debug::DrawBox(vec2 a, float s, mat2 transform, vec4 color) Debug::DrawBox(a - b, a + b, transform, color); } -//-- CIRCLE +//-- CIRCLE ------------------------------------------------------------------- void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); } void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, g_scene->GetLineColor()); } void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); } @@ -274,7 +324,7 @@ void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color) } } -//-- SPHERE +//-- SPHERE ------------------------------------------------------------------- void Debug::DrawSphere(vec3 a, float s) { Debug::DrawSphere(a, s, g_scene->GetLineColor()); } void Debug::DrawSphere(vec3 a, float s, vec4 color) { Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, color); } void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z) { Debug::DrawSphere(a, x, y, z, g_scene->GetLineColor()); } @@ -291,7 +341,7 @@ void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color) Debug::DrawCircle(a, y, (z - x) * .707f, color); } -//-- CAPSULE +//-- CAPSULE ------------------------------------------------------------------ void Debug::DrawCapsule(vec3 a, float s, vec3 h) { Debug::DrawCapsule(a, s, h, g_scene->GetLineColor()); } void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h) { Debug::DrawCapsule(a, x, y, z, h, g_scene->GetLineColor()); } void Debug::DrawCapsule(vec3 a, float s, vec3 h, vec4 color) @@ -327,7 +377,7 @@ void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color) } } -//-- VIEW PROJ +//-- VIEW PROJ ---------------------------------------------------------------- void Debug::DrawViewProj(mat4 view, mat4 proj) { Debug::DrawViewProj(view, proj, g_scene->GetLineColor()); } void Debug::DrawViewProj(mat4 view_proj) { Debug::DrawViewProj(view_proj, g_scene->GetLineColor()); } void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color) diff --git a/src/easymesh/easymesh.cpp b/src/easymesh/easymesh.cpp index 63dcb8c2..fc9d516e 100644 --- a/src/easymesh/easymesh.cpp +++ b/src/easymesh/easymesh.cpp @@ -103,6 +103,8 @@ void EasyMesh::MeshConvert() m_submeshes.Push(new SubMesh(shader, vdecl)); m_submeshes.Last()->SetIndexBuffer(ibo); m_submeshes.Last()->SetVertexBuffer(0, vbo); + + m_state = MeshRender::CanRender; } //----------------------------------------------------------------------------- diff --git a/src/easymesh/easymesh.h b/src/easymesh/easymesh.h index c679c9d7..f7e1ad27 100644 --- a/src/easymesh/easymesh.h +++ b/src/easymesh/easymesh.h @@ -33,12 +33,6 @@ LOL_SAFE_ENUM(CSGUsage, Xor ); -LOL_SAFE_ENUM(Axis, - X, - Y, - Z -); - LOL_SAFE_ENUM(MeshTransform, Taper, Twist, diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index 6995792c..0566eaf3 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -207,11 +207,12 @@ bool GdiPlusImageCodec::Save(Image *image, char const *path) } u8vec4 *psrc = image->Lock(); + u8vec4 *psrc0 = psrc; u8vec4 *pdst = static_cast(bdata.Scan0); for (int y = 0; y < size.y; y++) for (int x = 0; x < size.x; x++) *pdst++ = (*psrc++).bgra; - image->Unlock(pdst); + image->Unlock(psrc0); b->UnlockBits(&bdata); if (b->Save(wpath, &clsid, nullptr) != Gdiplus::Ok) diff --git a/src/image/color/color.cpp b/src/image/color/color.cpp index e3e726aa..afe52ebd 100644 --- a/src/image/color/color.cpp +++ b/src/image/color/color.cpp @@ -16,14 +16,17 @@ namespace lol /* * Hardcoded constants for colours */ -vec4 const Color::black = vec4(0, 0, 0, 1); -vec4 const Color::red = vec4(1, 0, 0, 1); -vec4 const Color::green = vec4(0, 1, 0, 1); -vec4 const Color::yellow = vec4(1, 1, 0, 1); -vec4 const Color::blue = vec4(0, 0, 1, 1); -vec4 const Color::magenta = vec4(1, 0, 1, 1); -vec4 const Color::cyan = vec4(0, 1, 1, 1); -vec4 const Color::white = vec4(1, 1, 1, 1); +vec4 const Color::black = vec4(0, 0, 0, 1); +vec4 const Color::red = vec4(1, 0, 0, 1); +vec4 const Color::green = vec4(0, 1, 0, 1); +vec4 const Color::yellow = vec4(1, 1, 0, 1); +vec4 const Color::blue = vec4(0, 0, 1, 1); +vec4 const Color::magenta = vec4(1, 0, 1, 1); +vec4 const Color::cyan = vec4(0, 1, 1, 1); +vec4 const Color::white = vec4(1, 1, 1, 1); +vec4 const Color::gray_dark = vec4(.25f, .25f, .25f, 1); +vec4 const Color::gray = vec4(.5f, .5f, .5f, 1); +vec4 const Color::gray_light= vec4(.75f, .75f, .75f, 1); /* * Conversion from colours to hexadecimal diff --git a/src/input/controller.h b/src/input/controller.h index 80342011..feb18536 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -114,7 +114,11 @@ public: protected: - void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; } + void Update() + { + m_previous = m_current; + m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; + } float RetrieveCurrentValue(); /** m1 is the InputDevice, m2 is the axis index on the InputDevice, m3 and m4 are an optional key indices to bind one or two keys over the axis */ diff --git a/src/lol/algorithm/sort.h b/src/lol/algorithm/sort.h index 6996497a..d47c22b8 100644 --- a/src/lol/algorithm/sort.h +++ b/src/lol/algorithm/sort.h @@ -16,6 +16,18 @@ namespace lol { +template +void array_base::Shuffle() +{ + auto n = Count(); + auto ni = n; + while (n > 0) + { + ni = lol::rand(n--) | 0; + Swap(ni, n); + } +} + template void array_base::Sort(int sort) { diff --git a/src/lol/base/array.h b/src/lol/base/array.h index c3a3ba90..db76941a 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -368,6 +368,7 @@ public: m_reserved = toreserve; } + void Shuffle(); void Sort(int sort); void SortQuickSwap(ptrdiff_t start, ptrdiff_t stop); diff --git a/src/lol/debug/lines.h b/src/lol/debug/lines.h index 23e04758..e62d0275 100644 --- a/src/lol/debug/lines.h +++ b/src/lol/debug/lines.h @@ -51,6 +51,11 @@ void DrawLine(vec2 a, vec2 b, float az=-1.f, float bz=-1.f); void DrawLine(vec3 a, vec3 b, vec4 color); void DrawLine(vec2 a, vec3 b, vec4 color, float az=-1.f); void DrawLine(vec2 a, vec2 b, vec4 color, float az=-1.f, float bz=-1.f); +//-- GIZMO +void DrawGizmo(vec3 pos, vec3 x, vec3 y, vec3 z, float size); +void DrawGizmo(vec2 pos, vec3 x, vec3 y, vec3 z, float size, float posz = -1.f); +//-- GRID +void DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d=false); //-- ARROW void DrawArrow(vec3 a, vec3 b, vec2 s); void DrawArrow(vec2 a, vec3 b, vec2 s, float az=-1.f); diff --git a/src/lol/image/color.h b/src/lol/image/color.h index 393713d8..1a00124a 100644 --- a/src/lol/image/color.h +++ b/src/lol/image/color.h @@ -327,7 +327,7 @@ public: /* * Some predefined colours */ - static const vec4 black, red, green, yellow, blue, magenta, cyan, white; + static const vec4 black, red, green, yellow, blue, magenta, cyan, white, gray_dark, gray, gray_light; }; } /* namespace lol */ diff --git a/src/lol/math/geometry.h b/src/lol/math/geometry.h index 98e475e9..5ae5a337 100644 --- a/src/lol/math/geometry.h +++ b/src/lol/math/geometry.h @@ -26,6 +26,18 @@ namespace lol { +struct AxisBase +{ + enum Type + { + X = 0, Y, Z, MAX, XY = 2, XYZ = 3, + }; +protected: + static inline char const *GetDescription() { return "X,Y,Z,MAX,XY,XYZ"; } + static inline char const **GetCustomString() { return nullptr; } +}; +typedef SafeEnum Axis; + #define LOL_BOX_TYPEDEFS(tname, suffix) \ template struct tname; \ typedef tname suffix; \ diff --git a/src/ticker.cpp b/src/ticker.cpp index 91fcfa12..caac46c3 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -339,8 +339,9 @@ void TickerData::GameThreadTick() } /* Tick objects for the game loop */ - for (int g = Entity::GAMEGROUP_BEGIN; g < Entity::GAMEGROUP_END; ++g) - for (int i = 0; i < data->m_list[g].Count(); ++i) + for (int g = Entity::GAMEGROUP_BEGIN; g < Entity::GAMEGROUP_END && !data->quit /* Stop as soon as required */; ++g) + { + for (int i = 0; i < data->m_list[g].Count() && !data->quit /* Stop as soon as required */; ++i) { Entity *e = data->m_list[g][i]; @@ -361,6 +362,7 @@ void TickerData::GameThreadTick() #endif } } + } Profiler::Stop(Profiler::STAT_TICK_GAME); } @@ -370,7 +372,7 @@ void TickerData::DrawThreadTick() Profiler::Start(Profiler::STAT_TICK_DRAW); /* Tick objects for the draw loop */ - for (int g = Entity::DRAWGROUP_BEGIN; g < Entity::DRAWGROUP_END; ++g) + for (int g = Entity::DRAWGROUP_BEGIN; g < Entity::DRAWGROUP_END && !data->quit /* Stop as soon as required */; ++g) { switch (g) { @@ -382,7 +384,8 @@ void TickerData::DrawThreadTick() break; } - for (int i = 0; i < data->m_list[g].Count(); ++i) + //Stop as soon as required + for (int i = 0; i < data->m_list[g].Count() && !data->quit /* Stop as soon as required */; ++i) { Entity *e = data->m_list[g][i]; diff --git a/src/worldentity.cpp b/src/worldentity.cpp index f6544875..55cb56e6 100644 --- a/src/worldentity.cpp +++ b/src/worldentity.cpp @@ -21,10 +21,6 @@ namespace lol WorldEntity::WorldEntity() { - m_position = vec3::zero; - m_rotation = quat(1); - m_velocity = vec3::zero; - m_rotation_velocity = vec3::zero; m_bbox[0] = m_bbox[1] = vec3::zero; } diff --git a/src/worldentity.h b/src/worldentity.h index 61efa412..f3990918 100644 --- a/src/worldentity.h +++ b/src/worldentity.h @@ -28,10 +28,10 @@ public: virtual char const *GetName(); public: - vec3 m_position; - vec3 m_velocity; - quat m_rotation; - vec3 m_rotation_velocity; + vec3 m_position = vec3::zero; + vec3 m_velocity = vec3::zero; + quat m_rotation = quat(1); + vec3 m_rotation_velocity = vec3::zero; vec3 m_bbox[2]; protected: