Example #1
0
void					Window::loop()
{
	sf::RectangleShape	rect(sf::Vector2f(10, 30));

	while (m_alive)
	{
		//rect.setPosition(mousePos().x, mousePos().y);
		//m_window.draw(rect);
		m_window.clear();
		procEvent();
		act();
		display();
		m_window.display();
	}
}
Example #2
0
void Test::run()
{
	
	bool running = true;
	int lastTime = SDL_GetTicks();

	m_physicsTimer = 0.0f;
	float fps = 0.0f;
	int fpsCount = 1;
	while (running)
	{
		int thisTime = SDL_GetTicks();
		float dt = (thisTime - lastTime) / 1000.0f;
		lastTime = thisTime;

		if (dt >= 1.0f)
			continue;


		fps += dt;
		fpsCount++;
		if (fps >= 1.0f)
		{
			fps -= 1.0f;
			printf("FPS: %d\n", fpsCount);
			fpsCount = 0;
		}
		

		bool step = false;

		SDL_Event event;
		while (SDL_PollEvent(&event))
		{
			if (procEvent(event))
				continue;


			switch (event.type)
			{
			case SDL_QUIT:
				running = false;
				break;
			case SDL_KEYDOWN:
				if (event.key.keysym.scancode == SDL_SCANCODE_K)
					step = true;
				else if ((event.key.keysym.scancode == SDL_SCANCODE_SPACE))
					m_stepping = !m_stepping;
				else if ((event.key.keysym.scancode == SDL_SCANCODE_R))
				{
					m_entities.clear();
					delete m_world;
					gBox = ShapePtr();
					gCapsule = ShapePtr();
					gFloor = ShapePtr();
					gSlope = ShapePtr();
					gSphere = ShapePtr();
					init();
				}
				else if (event.key.keysym.scancode == SDL_SCANCODE_V)
				{
					m_playerSpec = !m_playerSpec;
				}
				break;
			case SDL_MOUSEWHEEL:
				m_eye.p += rotate(vec3(0, 0, event.wheel.y), m_eye.q);
				break;
			case SDL_MOUSEMOTION:


				if (SDL_GetMouseState(0, 0) & SDL_BUTTON(SDL_BUTTON_LEFT))
				{
					m_eye.q = QuatFromAxisAngle(vec3(0, 1, 0), event.motion.xrel / 4.0f * 3.14 / 180.0f) * m_eye.q;
					m_eye.q = QuatFromAxisAngle(rotate(vec3(1, 0, 0), m_eye.q), event.motion.yrel / 4.0f * 3.14 / 180.0f) * m_eye.q;
				}

				if (SDL_GetMouseState(0, 0) & SDL_BUTTON(SDL_BUTTON_MIDDLE))
				{
					m_eye.p += rotate(vec3(-event.motion.xrel / 80.0f, event.motion.yrel / 80.0f, 0.0f), m_eye.q);
				}
			}

		}




		if (m_stepping)
		{
			if (step)
			{
				int numSteps = 1;
				for (int i = 0; i < numSteps; ++i)
				{
					printf("%d\n", m_numSteps);
					m_world->step(1.0f / 60.0f);
					++m_numSteps;
				}
			}
		}
		else
		{
			stepPhysics(dt);
		}
		
		std::vector<Entity*> deadEntities;

		for (int i = 0; i < m_entities.size(); ++i)
		{
			m_entities[i]->update(dt);
			if (m_entities[i]->isDead())
			{
				if (m_entities[i] == m_player)
					m_player = 0;

				//m_entities[i]->destroy(m_toAdd);

				//delete m_entities[i];
				m_entities[i] = m_entities.back();
				m_entities.pop_back();
				--i;
			}
		}

		for (Entity* entity : m_toAdd)
		{
			m_entities.push_back(entity);
		}
		m_toAdd.clear();


		update(dt);
		// render


		//
		glClear(GL_COLOR_BUFFER_BIT);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0f, 800.0f / 600.0f, 0.1, 1000.0f);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();


		//gluLookAt(eye.x, eye.y, eye.z,
		//	0.0f, 0.0f, 0.0f,
		//	0.0f, 1.0f, 0.0f);
		float view[16];
		if (m_playerSpec && m_player)
			getViewMat(m_player->getView(), view);
		else
			getViewMat(m_eye, view);
		glMultMatrixf(view);



		for (Entity* e : m_entities)
		{
			
			e->render(m_colorLocation);
		}

		render();

		SDL_GL_SwapWindow(m_window);


	}
}