renamed renderer classes. fixed shooting taking rad instead of degrees as parameter.
This commit is contained in:
parent
74b4573c3a
commit
c8288d7a25
14 changed files with 186 additions and 393 deletions
|
@ -10,8 +10,9 @@ set(GAME_SRC
|
|||
glclasses.cpp
|
||||
renderer.cpp
|
||||
game_window.cpp
|
||||
renderer_simple.cpp
|
||||
renderer_shader.cpp
|
||||
renderer_polygon_2d.cpp
|
||||
renderer_polygon_3d.cpp
|
||||
renderer_ray_tracer.cpp
|
||||
session.cpp
|
||||
|
||||
util.cpp
|
||||
|
@ -27,20 +28,12 @@ set(GAME_SRC
|
|||
state/state.cpp
|
||||
)
|
||||
|
||||
set(GAME_HEADERS
|
||||
opengl.hpp
|
||||
glclasses.hpp
|
||||
renderer.hpp
|
||||
renderer_simple.hpp
|
||||
renderer_shader.hpp
|
||||
)
|
||||
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
include_directories(${OPENGL_INCLUDE_DIR})
|
||||
include_directories(${CMAKE_SOURCE_DIR}/libs/glm/)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/libs/asio/asio/include/)
|
||||
|
||||
add_executable(game ${GAME_SRC} ${GAME_HEADERS})
|
||||
add_executable(game ${GAME_SRC})
|
||||
setup_target(game)
|
||||
target_link_libraries(game X11 epoxy pthread)
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
#include "opengl.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "renderer_simple.hpp"
|
||||
#include "renderer_shader.hpp"
|
||||
#include "renderer_polygon_2d.hpp"
|
||||
#include "renderer_polygon_3d.hpp"
|
||||
#include "renderer_ray_tracer.hpp"
|
||||
|
||||
#include "game.hpp"
|
||||
|
||||
|
@ -18,7 +19,9 @@
|
|||
class GameWindow : public endofthejedi::GLWindow {
|
||||
private:
|
||||
Game* m_game;
|
||||
endofthejedi::RendererSimple m_renderer;
|
||||
endofthejedi::RendererPolygon2d m_renderer;
|
||||
//endofthejedi::RendererPolygon3d m_renderer;
|
||||
//endofthejedi::RendererRayTracer m_renderer;
|
||||
|
||||
protected:
|
||||
void init() override {
|
||||
|
@ -29,14 +32,6 @@ class GameWindow : public endofthejedi::GLWindow {
|
|||
}
|
||||
|
||||
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();
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
namespace endofthejedi {
|
||||
|
||||
class GLWindow {
|
||||
class GLWindow {
|
||||
private:
|
||||
//X-related stuff
|
||||
Display* m_display;
|
||||
|
@ -34,6 +34,7 @@ class GLWindow {
|
|||
Window m_window;
|
||||
GLXContext m_glcontext;
|
||||
XWindowAttributes m_gwa;
|
||||
|
||||
Atom m_atomWmDeleteWindow;
|
||||
//End of X related stuff
|
||||
|
||||
|
@ -102,6 +103,5 @@ class GLWindow {
|
|||
void set_maxfps(const double maxfps) {
|
||||
m_maxfps = maxfps;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,4 +1 @@
|
|||
#include "renderer.hpp"
|
||||
|
||||
void endofthejedi::Renderer::render(const game::State *state) {
|
||||
}
|
||||
|
|
|
@ -9,11 +9,17 @@
|
|||
|
||||
namespace endofthejedi {
|
||||
|
||||
class Renderer {
|
||||
/**
|
||||
* Base class for the render.
|
||||
*
|
||||
* Implementations should render the whole scene (state) on the screen with
|
||||
* opengl.
|
||||
*/
|
||||
class Renderer {
|
||||
private:
|
||||
protected:
|
||||
public:
|
||||
virtual void render(const game::State *state);
|
||||
};
|
||||
virtual void render(const game::State *state) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
#include "renderer_shader.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include "glm/glm.hpp"
|
||||
#include "glm/vec2.hpp"
|
||||
#include "glm/vec3.hpp"
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
|
||||
endofthejedi::RendererShader::RendererShader() {
|
||||
m_shader.load(vss, GL_VERTEX_SHADER);
|
||||
m_shader.load(fss, GL_FRAGMENT_SHADER);
|
||||
}
|
||||
|
||||
void endofthejedi::RendererShader::render(const game::State *state) {
|
||||
m_shader.bind();
|
||||
|
||||
std::vector<glm::vec3> positions;
|
||||
std::vector<glm::vec4> colors;
|
||||
std::vector<GLfloat> radii;
|
||||
std::vector<GLfloat> reflections;
|
||||
|
||||
for (const game::Planet *planet : state->planets) {
|
||||
positions.push_back(glm::vec3(planet->position, -5.0f));
|
||||
radii.push_back(planet->radius);
|
||||
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
|
||||
}
|
||||
|
||||
for (const game::Ship *ship : state->ships) {
|
||||
positions.push_back(glm::vec3(ship->position, -3.0f));
|
||||
radii.push_back(ship->radius);
|
||||
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
|
||||
}
|
||||
|
||||
for (const game::Player *player : state->players) {
|
||||
for (const game::Missile *missile : player->missiles) {
|
||||
positions.push_back(glm::vec3(missile->position, -3.0f));
|
||||
radii.push_back(0.01f);
|
||||
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &pos : positions) {
|
||||
pos.x *= 5;
|
||||
pos.y *= 5;
|
||||
}
|
||||
for (auto &rad : radii) {
|
||||
rad *= 5;
|
||||
}
|
||||
|
||||
/*
|
||||
for (const game::Trace *trace : state->traces) {
|
||||
positions.push_back(glm::vec3(trace->position, -3.0f));
|
||||
radii.push_back(trace->radius);
|
||||
colors.push_back(glm::vec4(0.0f, 1.0f, 0.0f, 0.5f));
|
||||
}*/
|
||||
|
||||
glUniform1f(m_shader.location("aspectratio"), 1.0f);
|
||||
glUniform1i(m_shader.location("reflections"), 1);
|
||||
glUniform3f(m_shader.location("l_position"), 0.6f, 0.1f, 0.0f);
|
||||
glUniform1f(m_shader.location("l_radius"), 1.0f);
|
||||
glUniform3fv(m_shader.location("s_positions"), positions.size(),
|
||||
glm::value_ptr(positions[0]));
|
||||
glUniform4fv(m_shader.location("s_colors"), colors.size(),
|
||||
glm::value_ptr(colors[0]));
|
||||
glUniform1fv(m_shader.location("s_radii"), radii.size(), &radii[0]);
|
||||
glUniform1i(m_shader.location("s_length"), positions.size());
|
||||
|
||||
// render fullscreen quad with legacy opengl (too lazy, sorry)
|
||||
glBegin(GL_QUADS);
|
||||
glVertex2f(-1.0f, -1.0f);
|
||||
glVertex2f(1.0f, -1.0f);
|
||||
glVertex2f(1.0f, 1.0f);
|
||||
glVertex2f(-1.0f, 1.0f);
|
||||
glEnd();
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
static const char *vss =
|
||||
#include "raycaster.vs"
|
||||
;
|
||||
static const char *fss =
|
||||
#include "raycaster.fs"
|
||||
;
|
||||
|
||||
#include "renderer.hpp"
|
||||
|
||||
#include "glclasses.hpp"
|
||||
#include "game.hpp"
|
||||
#include "state/planet.hpp"
|
||||
#include "state/trace.hpp"
|
||||
#include "state/player.hpp"
|
||||
#include "state/ship.hpp"
|
||||
|
||||
namespace endofthejedi {
|
||||
|
||||
class RendererShader : Renderer {
|
||||
private:
|
||||
Shader m_shader;
|
||||
protected:
|
||||
public:
|
||||
RendererShader();
|
||||
void render(const game::State *state) override;
|
||||
};
|
||||
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
#include "renderer_simple.hpp"
|
||||
|
||||
void endofthejedi::RendererSimple::drawCircle(float x, float y, float radius,
|
||||
float r, float g, float b,
|
||||
int numSides) {
|
||||
glBegin(GL_TRIANGLE_FAN);
|
||||
glColor3f(r, g, b);
|
||||
glVertex2f(x, y); // center of circle
|
||||
for (int i = 0; i <= numSides; i++) {
|
||||
glVertex2f(x + (radius * cos(i * 2 * M_PI / numSides)),
|
||||
y + (radius * sin(i * 2 * M_PI / numSides)));
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void endofthejedi::RendererSimple::drawShip(const glm::vec2 &pos,
|
||||
float radius) {
|
||||
// std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
|
||||
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
|
||||
|
||||
drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 12);
|
||||
}
|
||||
|
||||
void endofthejedi::RendererSimple::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;
|
||||
drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 32);
|
||||
}
|
||||
|
||||
void endofthejedi::RendererSimple::drawMissile(const glm::vec2 &pos) {
|
||||
glm::vec3 color = glm::vec3(1.0, 1.0, 0.3);
|
||||
drawCircle(pos.x, pos.y, 0.01, color.x, color.y, color.z, 6);
|
||||
}
|
||||
|
||||
void endofthejedi::RendererSimple::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);
|
||||
drawCircle(p.position.x, p.position.y, 0.005, color.x, color.y, color.z,
|
||||
3);
|
||||
}
|
||||
}
|
||||
|
||||
void endofthejedi::RendererSimple::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);
|
||||
drawCircle(explosion->position.x, explosion->position.y, r, color.x,
|
||||
color.y, color.z, 64);
|
||||
}
|
||||
|
||||
void endofthejedi::RendererSimple::render(const game::State *state) {
|
||||
for (const game::Planet *planet : state->planets) {
|
||||
drawPlanet(planet->position, planet->radius);
|
||||
}
|
||||
|
||||
for (const game::Trace *trace : state->traces) {
|
||||
drawTrace(trace);
|
||||
}
|
||||
|
||||
for (const game::Explosion *explosion : state->explosions) {
|
||||
drawExplosion(explosion);
|
||||
}
|
||||
|
||||
for (const game::Ship *ship : state->ships) {
|
||||
drawShip(ship->position, ship->radius);
|
||||
}
|
||||
|
||||
for (const game::Player *player : state->players) {
|
||||
for (const game::Missile *missile : player->missiles) {
|
||||
drawMissile(missile->position);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "renderer.hpp"
|
||||
|
||||
#include "glclasses.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"
|
||||
|
||||
namespace endofthejedi {
|
||||
|
||||
class RendererSimple : Renderer {
|
||||
private:
|
||||
void drawCircle(float x, float y, float radius, float r, float g, float b,
|
||||
int numSides = 12);
|
||||
void drawShip(const glm::vec2 &pos, float radius);
|
||||
void drawPlanet(const glm::vec2 &pos, float radius);
|
||||
void drawMissile(const glm::vec2 &pos);
|
||||
void drawTrace(const game::Trace *trace);
|
||||
void drawExplosion(const game::Explosion *explosion);
|
||||
|
||||
protected:
|
||||
public:
|
||||
void render(const game::State *state) override;
|
||||
};
|
||||
|
||||
}
|
|
@ -8,7 +8,7 @@ using asio::ip::tcp;
|
|||
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
public:
|
||||
Server(asio::io_service& io_service,game::State* s, short port)
|
||||
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
|
||||
socket_(io_service)
|
||||
|
@ -17,7 +17,7 @@ public:
|
|||
do_accept();
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
void do_accept()
|
||||
{
|
||||
acceptor_.async_accept(socket_,
|
||||
|
|
|
@ -13,22 +13,27 @@ bool Session::parse(string s)
|
|||
s.erase(std::find_if(s.rbegin(), s.rend(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||
|
||||
if(s.size()==1&&s[0]=='q'){
|
||||
// TODO:
|
||||
// from knoc:
|
||||
// ALWAYS use commands here, no direct input!
|
||||
// thats important!!!!
|
||||
// TODO
|
||||
if (s.size()==1&&s[0]=='q'){
|
||||
//quit
|
||||
m_socket.close();
|
||||
m_state->quitPlayer(m_pid);
|
||||
return false;
|
||||
}else if(s.size()==1&&s[0]=='c'){
|
||||
} else if (s.size()==1&&s[0]=='c') {
|
||||
//clear
|
||||
m_state->clear(m_pid);
|
||||
}else if(s.size()>=3&&s[0]=='n'&&s[1]==' '){
|
||||
} else if (s.size()>=3&&s[0]=='n'&&s[1]==' ') {
|
||||
//set name
|
||||
m_state->setName(m_pid, s.substr(2));
|
||||
}else if(s.size()>=3&&s[0]=='v'&&s[1]==' '){
|
||||
} else if (s.size()>=3&&s[0]=='v'&&s[1]==' ') {
|
||||
//set speed
|
||||
double speed = atof(s.substr(2).c_str());
|
||||
m_state->setSpeed(m_pid, speed);
|
||||
}else{
|
||||
} else {
|
||||
//shoot
|
||||
double angle = atof(s.c_str());
|
||||
m_state->commandForPlayer(m_pid, new game::ShootCommand(angle));
|
||||
|
@ -41,18 +46,18 @@ void Session::start()
|
|||
if(m_started) return;
|
||||
m_started=true;
|
||||
m_pid = m_state->addPlayer();
|
||||
char hello[] = "\nUse \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n> ";
|
||||
char hello[] = "\nWeclome to KlassischeKeplerKriege v0.1\n Use \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n> ";
|
||||
do_write(hello, strlen(hello));
|
||||
}
|
||||
|
||||
void Session::do_read()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
m_socket.async_read_some(asio::buffer(m_rcv_data, max_length),
|
||||
m_socket.async_read_some(
|
||||
asio::buffer(m_rcv_data, max_length),
|
||||
[this, self](std::error_code ec, std::size_t length)
|
||||
{
|
||||
if (!ec)
|
||||
{
|
||||
if (!ec) {
|
||||
std::string s(m_rcv_data,length);
|
||||
if(parse(s)){
|
||||
char c[]="> ";
|
||||
|
@ -68,8 +73,7 @@ void Session::do_write(char m_snd_data[], std::size_t length)
|
|||
asio::async_write(m_socket, asio::buffer(m_snd_data, length),
|
||||
[this, self](std::error_code ec, std::size_t /*length*/)
|
||||
{
|
||||
if (!ec)
|
||||
{
|
||||
if (!ec) {
|
||||
do_read();
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
#include "commands.hpp"
|
||||
|
||||
#include "trace.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "trace.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
namespace game {
|
||||
void ShootCommand::apply(Player *player, State *state) const
|
||||
{
|
||||
|
@ -12,7 +13,7 @@ namespace game {
|
|||
// TODO: idea
|
||||
// shoot multiple rockets at once or from different positions after
|
||||
// level up / upgrade ...
|
||||
Missile *missile = new Missile(player, player->ship->position, m_angle, player->speed);
|
||||
Missile *missile = new Missile(player, player->ship->position, util::deg2rad(m_angle), player->speed);
|
||||
|
||||
Trace *trace = new Trace(missile);
|
||||
missile->trace = trace;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "util.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace util {
|
||||
|
@ -22,4 +23,14 @@ namespace util {
|
|||
{
|
||||
return glm::vec2(randf_0_1(), randf_0_1());
|
||||
}
|
||||
|
||||
float deg2rad(float deg)
|
||||
{
|
||||
return M_PI*deg/180.0;
|
||||
}
|
||||
|
||||
float rad2deg(float rad)
|
||||
{
|
||||
return 180.0 * rad / M_PI;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,4 +9,7 @@ namespace util {
|
|||
|
||||
glm::vec2 randv2_m1_1();
|
||||
glm::vec2 randv2_0_1();
|
||||
|
||||
float deg2rad(float deg);
|
||||
float rad2deg(float rad);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue