KlassischeKeplerKriege/game/game_window.hpp
2016-09-28 03:42:21 +02:00

118 lines
3.4 KiB
C++

#pragma once
#include "opengl.hpp"
#include "renderer.hpp"
#include "game.hpp"
#include "state/trace.hpp"
#include "state/object.hpp"
#include "state/missile.hpp"
#include "state/player.hpp"
#include "state/planet.hpp"
#include "state/ship.hpp"
#include "state/explosion.hpp"
class GameWindow : public endofthejedi::GLWindow {
private:
protected:
void init() override {
glClearColor(0.5f, 0.6f, 0.7f, 1.0f);
resize();
glEnable(GL_DEPTH_TEST);
}
void render(double time) override {
//static bool once = false;
//if (!once) {
// once = true;
// for (int i=0; i<1000; i++) {
// m_game->cycle(time);
// }
//}
if (!m_game->cycle(static_cast<float>(time/1000000000.0))) {
std::cout<<"stopping the game..." << std::endl;
stop();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (const game::Planet *planet : m_game->state()->planets) {
drawPlanet(planet->position, planet->radius);
}
for (const game::Trace *trace : m_game->state()->traces) {
drawTrace(trace);
}
for (const game::Explosion *explosion : m_game.state()->explosions) {
drawExplosion(explosion);
}
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()); }
void drawShip(const glm::vec2 &pos)
{
//std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
float radius = m_game->state()->shipRadius();
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 12);
}
void drawPlanet(const glm::vec2 &pos, float radius)
{
glm::vec3 color = glm::vec3(0.7, 0.1, 0.2);
//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);
}
void drawMissile(const glm::vec2 &pos)
{
glm::vec3 color = glm::vec3(1.0, 1.0, 0.3);
m_renderer.drawCircle(pos.x, pos.y, 0.01, color.x, color.y, color.z, 6);
}
void drawTrace(const game::Trace *trace)
{
for (const game::Trace::TracePoint &p : trace->points) {
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);
}
}
void drawExplosion(const game::Explosion *explosion)
{
// TODO: transparent
// TODO: with glow in the middle
float r = explosion->maxRadius * (explosion->age / explosion->maxAge);
// TODO: transparency?
glm::vec3 color = glm::vec3(1.0, 0.9, 0.1);
m_renderer.drawCircle(explosion->position.x, explosion->position.y, r, color.x, color.y, color.z, 64);
}
public:
GameWindow(unsigned int width, unsigned int height, Game* game)
: endofthejedi::GLWindow(width, height) { m_game = game; }
private:
Game *m_game;
endofthejedi::Renderer m_renderer;
};