| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #include "Crown.h"
- #include <cstdlib>
- #include <GL/glew.h>
- using namespace Crown;
- void DrawCircle(const Circle& circle);
- void DrawLine(const Vec2& start, const Vec2& end);
- void DrawRectangle(const Crown::Rect& rect, const Color4& color);
- class WndCtrl: public KeyboardListener
- {
- public:
- WndCtrl()
- {
- GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
- }
- virtual void KeyReleased(const KeyboardEvent& event)
- {
- if (event.key == KC_ESCAPE)
- {
- GetDevice()->StopRunning();
- }
- }
- };
- class MainScene: public Scene, public MouseListener//, public KeyboardListener
- {
- public:
- MainScene(uint windowWidth, uint windowHeight) :
- c1(Vec2::ZERO, 0.0f),
- c2(Vec2::ZERO, 0.0f)
- {
- GetDevice()->GetInputManager()->RegisterMouseListener(this);
- //GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
- }
- virtual ~MainScene()
- {
- }
- virtual void ButtonPressed(const MouseEvent& event)
- {
-
- }
- virtual void OnLoad()
- {
- Crown::Renderer* renderer = Crown::GetDevice()->GetRenderer();
- renderer->SetClearColor(Crown::Color4(0.6f, 0.6f, 0.6f, 1.0f));
- c1.SetCenter(Vec2(300.0f, 300.0f));
- c1.SetRadius(32.0f);
- c2.SetCenter(Vec2(500.0f, 700.0f));
- c2.SetRadius(16.0f);
- ortho.BuildProjectionOrtho2dRH(1000, 625, 0, 10);
- }
- virtual void RenderScene()
- {
- Renderer* renderer = GetDevice()->GetRenderer();
- renderer->_SetLighting(false);
- renderer->_SetBlending(false);
- renderer->_SetAlphaTest(false);
- renderer->_SetBackfaceCulling(false);
- renderer->_SetTexturing(0, false);
- renderer->_SetTexturing(1, false);
- renderer->_SetTexturing(2, false);
- Scene::RenderScene();
- renderer->SetMatrix(MT_PROJECTION, ortho);
- glDisable(GL_CULL_FACE);
- glColor4f(0, 1, 0, 1);
- Crown::Rect r1;
- r1.SetFromCenterAndDimensions(Vec2(400, 400), 100, 80);
- c1.SetCenter(GetDevice()->GetInputManager()->GetMouse()->GetCursorXY().ToVec2());
- Color4 color = Color4::LIME;
- Vec2 penetration;
- if (Intersection::TestCircleRect(c1, r1, penetration))
- {
- color = Color4::RED;
- }
- Circle cr1 = r1.ToCircle();
- DrawCircle(cr1);
- Rect rc1 = c1.ToRect();
- DrawRectangle(rc1, color);
- DrawRectangle(r1, color);
- DrawCircle(c1);
- glColor4f(0, 0, 1, 1);
- DrawLine(c1.GetCenter(), c1.GetCenter() + penetration);
- //DrawCircle(c2);
- //Vec2 c1vel = GetDevice()->GetInputManager()->GetMouse()->GetCursorXY().ToVec2() - c1.GetCenter();
- //Vec2 c2vel = Vec2(250.0f, -200.0f);
- //DrawLine(c1.GetCenter(), c1.GetCenter() + c1vel);
- //DrawLine(c2.GetCenter(), c2.GetCenter() + c2vel);
- //real ct;
- //if (Intersection::TestDynamicCircleCircle(c1, c1vel, c2, c2vel, ct))
- //{
- // glColor4f(1, 0, 0, 1);
- // Circle cc1(c1.GetCenter() + c1vel * ct, c1.GetRadius());
- // Circle cc2(c2.GetCenter() + c2vel * ct, c2.GetRadius());
- // DrawCircle(cc1);
- // DrawCircle(cc2);
- //}
- }
- private:
- Circle c1;
- Circle c2;
- Mat4 ortho;
- };
- int main(int argc, char** argv)
- {
- int wndW = 1000;
- int wndH = 625;
- if (argc == 3)
- {
- wndW = atoi(argv[1]);
- wndH = atoi(argv[2]);
- }
- Device* mDevice = GetDevice();
- if (!mDevice->Init(wndW, wndH, 32, false))
- {
- return 0;
- }
- WndCtrl ctrl;
- MainScene* mainScene = new MainScene(wndW, wndH);
- GetDevice()->GetSceneManager()->SelectNextScene(mainScene);
- mDevice->GetMainWindow()->SetTitle("Crown Engine v0.1 - Collision 2D Test");
- mDevice->Run();
- mDevice->Shutdown();
- return 0;
- }
- void DrawCircle(const Circle& circle)
- {
- float step = (360.0f / 16.0f) * Math::DEG_TO_RAD;
- glBegin(GL_TRIANGLE_FAN);
- glVertex2fv(circle.GetCenter().ToFloatPtr());
- for (int i = 0; i <= 16; i++)
- {
- Vec2 vert = circle.GetCenter() + (Vec2(Math::Cos((float)i * step), Math::Sin((float)i * step)) * circle.GetRadius());
- glVertex2fv(vert.ToFloatPtr());
- }
- glEnd();
- }
- void DrawLine(const Vec2& start, const Vec2& end)
- {
- glBegin(GL_LINES);
- glVertex2fv(start.ToFloatPtr());
- glVertex2fv(end.ToFloatPtr());
- glEnd();
- }
- void DrawRectangle(const Crown::Rect& rect, const Color4& color)
- {
- Renderer* r = GetDevice()->GetRenderer();
-
- Vec2 min = rect.GetVertext(0);
- Vec2 size = rect.GetSize();
- r->DrawRectangle(Point2(min.x, min.y), Point2(size.x, size.y), DM_BORDER, color);
- }
|