2016-09-27 16:23:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-09-27 17:01:21 +00:00
|
|
|
#include "opengl.hpp"
|
|
|
|
#include "renderer.hpp"
|
2016-09-27 16:23:58 +00:00
|
|
|
|
|
|
|
#include "game.hpp"
|
|
|
|
|
2016-09-27 18:23:33 +00:00
|
|
|
#include "state/trace.hpp"
|
2016-09-27 16:23:58 +00:00
|
|
|
#include "state/object.hpp"
|
|
|
|
#include "state/missile.hpp"
|
|
|
|
#include "state/player.hpp"
|
|
|
|
#include "state/planet.hpp"
|
|
|
|
#include "state/ship.hpp"
|
|
|
|
|
|
|
|
class GameWindow : public endofthejedi::GLWindow {
|
2016-09-27 18:56:17 +00:00
|
|
|
private:
|
|
|
|
protected:
|
2016-09-27 16:23:58 +00:00
|
|
|
void init() override {
|
|
|
|
glClearColor(0.5f, 0.6f, 0.7f, 1.0f);
|
|
|
|
resize();
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
void render(double time) override {
|
2016-09-27 18:56:17 +00:00
|
|
|
// static bool once = false;
|
|
|
|
// if (!once) {
|
2016-09-27 16:23:58 +00:00
|
|
|
// once = true;
|
|
|
|
// for (int i=0; i<1000; i++) {
|
|
|
|
// m_game.cycle(time);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
if (!m_game.cycle(static_cast<float>(time / 1000000000.0))) {
|
|
|
|
std::cout << "stopping the game..." << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
for (const game::Planet *planet : m_game.state()->planets) {
|
|
|
|
drawPlanet(planet->position, planet->radius);
|
|
|
|
}
|
|
|
|
|
2016-09-27 18:23:33 +00:00
|
|
|
for (const game::Trace *trace : m_game.state()->traces) {
|
|
|
|
drawTrace(trace);
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:23:58 +00:00
|
|
|
for (const game::Ship *ship : m_game.state()->ships) {
|
|
|
|
drawShip(ship->position);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const game::Player *player : m_game.state()->players) {
|
|
|
|
for (const game::Missile *missile : player->missiles) {
|
|
|
|
drawMissile(missile->position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize() override { glViewport(0, 0, getwidth(), getheight()); }
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
void drawShip(const glm::vec2 &pos) {
|
|
|
|
// std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
|
|
|
|
|
|
|
|
float radius = m_game.state()->shipRadius();
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z,
|
|
|
|
12);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
void drawPlanet(const glm::vec2 &pos, float radius) {
|
2016-09-27 16:23:58 +00:00
|
|
|
glm::vec3 color = glm::vec3(0.7, 0.1, 0.2);
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
// std::cout<<"draw planet @ " << pos.x << ", " << pos.y << std::endl;
|
|
|
|
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z,
|
|
|
|
32);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawMissile(const glm::vec2 &pos)
|
2016-09-27 18:23:33 +00:00
|
|
|
|
2016-09-27 16:23:58 +00:00
|
|
|
{
|
|
|
|
glm::vec3 color = glm::vec3(1.0, 1.0, 0.3);
|
2016-09-27 18:23:33 +00:00
|
|
|
m_renderer.drawCircle(pos.x, pos.y, 0.01, color.x, color.y, color.z, 6);
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:28:26 +00:00
|
|
|
void drawTrace(const game::Trace *trace) {
|
2016-09-27 18:23:33 +00:00
|
|
|
for (const game::Trace::TracePoint &p : trace->points) {
|
2016-09-27 19:28:26 +00:00
|
|
|
glm::vec3 color =
|
|
|
|
glm::vec3(0.1, 0.3, 1.0) / (1.0f + 500.0f * p.speed);
|
|
|
|
m_renderer.drawCircle(p.position.x, p.position.y, 0.005, color.x,
|
|
|
|
color.y, color.z, 3);
|
2016-09-27 18:23:33 +00:00
|
|
|
}
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
public:
|
2016-09-27 16:23:58 +00:00
|
|
|
GameWindow(unsigned int width, unsigned int height)
|
|
|
|
: endofthejedi::GLWindow(width, height) {}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
private:
|
|
|
|
Game m_game;
|
2016-09-27 16:23:58 +00:00
|
|
|
endofthejedi::Renderer m_renderer;
|
|
|
|
};
|