From c8288d7a253369a1e71c8beb6065dc2433235e7c Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Wed, 28 Sep 2016 11:35:56 +0200 Subject: [PATCH] renamed renderer classes. fixed shooting taking rad instead of degrees as parameter. --- game/CMakeLists.txt | 17 ++-- game/game_window.hpp | 17 ++-- game/opengl.hpp | 190 +++++++++++++++++++-------------------- game/renderer.cpp | 3 - game/renderer.hpp | 18 ++-- game/renderer_shader.cpp | 75 ---------------- game/renderer_shader.hpp | 30 ------- game/renderer_simple.cpp | 79 ---------------- game/renderer_simple.hpp | 33 ------- game/server.hpp | 46 +++++----- game/session.cpp | 50 ++++++----- game/state/commands.cpp | 7 +- game/util.cpp | 11 +++ game/util.hpp | 3 + 14 files changed, 186 insertions(+), 393 deletions(-) delete mode 100644 game/renderer_shader.cpp delete mode 100644 game/renderer_shader.hpp delete mode 100644 game/renderer_simple.cpp delete mode 100644 game/renderer_simple.hpp diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 1c510ab..f6f9a69 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -10,10 +10,11 @@ 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 game.cpp state/object.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) diff --git a/game/game_window.hpp b/game/game_window.hpp index e54f39f..9da5312 100644 --- a/game/game_window.hpp +++ b/game/game_window.hpp @@ -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(time / 1000000000.0))) { std::cout << "stopping the game..." << std::endl; stop(); diff --git a/game/opengl.hpp b/game/opengl.hpp index c9b36e9..7a8888f 100644 --- a/game/opengl.hpp +++ b/game/opengl.hpp @@ -8,100 +8,100 @@ namespace endofthejedi { -class GLWindow { - private: - //X-related stuff - Display* m_display; - Window m_rootwnd; - GLint m_attributes[23] = - { - GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, - GLX_RENDER_TYPE, GLX_RGBA_BIT, - GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR, - GLX_RED_SIZE, 8, - GLX_GREEN_SIZE, 8, - GLX_BLUE_SIZE, 8, - GLX_ALPHA_SIZE, 8, - GLX_DEPTH_SIZE, 24, - GLX_STENCIL_SIZE, 8, - GLX_DOUBLEBUFFER, True, - None + class GLWindow { + private: + //X-related stuff + Display* m_display; + Window m_rootwnd; + GLint m_attributes[23] = + { + GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, + GLX_RENDER_TYPE, GLX_RGBA_BIT, + GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR, + GLX_RED_SIZE, 8, + GLX_GREEN_SIZE, 8, + GLX_BLUE_SIZE, 8, + GLX_ALPHA_SIZE, 8, + GLX_DEPTH_SIZE, 24, + GLX_STENCIL_SIZE, 8, + GLX_DOUBLEBUFFER, True, + None + }; + + XVisualInfo* m_visualinfo; + Colormap m_colormap; + XSetWindowAttributes m_swa; + Window m_window; + GLXContext m_glcontext; + XWindowAttributes m_gwa; + + Atom m_atomWmDeleteWindow; + //End of X related stuff + + unsigned int m_width; + unsigned int m_height; + double delta = 0.0; + double sleeptime = 0.0; + timespec prev; + timespec current; + + //mainloop condition + bool m_running = false; + + double m_fps; + //if maxfps = 0 there's no fps limit + double m_maxfps; + + protected: + //ancestors shall override these methods + virtual void init(); + //called by mainloop periodically + //time shall be used for timebased movement + virtual void render(double time); + //called by handle on window resize + virtual void resize(); + //the ancestor should call the handle function in this class, too, + //as it handles the close calls & resizing + virtual void handle(XEvent event); + + virtual void handleevents(); + + public: + //initializes the X Window & creates an OpenGL context + GLWindow(unsigned int width, unsigned int height); + ~GLWindow(); + + //mainloop does event handling & calls render/swap + void poll(); + void swap(); + //stops the mainloop by setting m_running false + void stop(); + bool running(); + + //getters + unsigned int getheight() const { + return m_height; + } + + unsigned int getwidth() const { + return m_width; + } + + double get_maxfps() const { + return m_maxfps; + } + + double get_fps() const { + return m_fps; + } + + bool get_running() const { + return m_running; + } + + //setters + void set_maxfps(const double maxfps) { + m_maxfps = maxfps; + } }; - - XVisualInfo* m_visualinfo; - Colormap m_colormap; - XSetWindowAttributes m_swa; - Window m_window; - GLXContext m_glcontext; - XWindowAttributes m_gwa; - Atom m_atomWmDeleteWindow; - //End of X related stuff - - unsigned int m_width; - unsigned int m_height; - double delta = 0.0; - double sleeptime = 0.0; - timespec prev; - timespec current; - - //mainloop condition - bool m_running = false; - - double m_fps; - //if maxfps = 0 there's no fps limit - double m_maxfps; - - protected: - //ancestors shall override these methods - virtual void init(); - //called by mainloop periodically - //time shall be used for timebased movement - virtual void render(double time); - //called by handle on window resize - virtual void resize(); - //the ancestor should call the handle function in this class, too, - //as it handles the close calls & resizing - virtual void handle(XEvent event); - - virtual void handleevents(); - - public: - //initializes the X Window & creates an OpenGL context - GLWindow(unsigned int width, unsigned int height); - ~GLWindow(); - - //mainloop does event handling & calls render/swap - void poll(); - void swap(); - //stops the mainloop by setting m_running false - void stop(); - bool running(); - - //getters - unsigned int getheight() const { - return m_height; - } - - unsigned int getwidth() const { - return m_width; - } - - double get_maxfps() const { - return m_maxfps; - } - - double get_fps() const { - return m_fps; - } - - bool get_running() const { - return m_running; - } - - //setters - void set_maxfps(const double maxfps) { - m_maxfps = maxfps; - } -}; - } diff --git a/game/renderer.cpp b/game/renderer.cpp index cdc854a..0f76e69 100644 --- a/game/renderer.cpp +++ b/game/renderer.cpp @@ -1,4 +1 @@ #include "renderer.hpp" - -void endofthejedi::Renderer::render(const game::State *state) { -} diff --git a/game/renderer.hpp b/game/renderer.hpp index 88c0b45..fcea0ac 100644 --- a/game/renderer.hpp +++ b/game/renderer.hpp @@ -9,11 +9,17 @@ namespace endofthejedi { -class Renderer { - private: - protected: - public: - virtual void render(const game::State *state); -}; + /** + * 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) = 0; + }; } diff --git a/game/renderer_shader.cpp b/game/renderer_shader.cpp deleted file mode 100644 index 47beaf5..0000000 --- a/game/renderer_shader.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "renderer_shader.hpp" - -#include -#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 positions; - std::vector colors; - std::vector radii; - std::vector 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(); -} diff --git a/game/renderer_shader.hpp b/game/renderer_shader.hpp deleted file mode 100644 index 2287740..0000000 --- a/game/renderer_shader.hpp +++ /dev/null @@ -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; - }; - -} diff --git a/game/renderer_simple.cpp b/game/renderer_simple.cpp deleted file mode 100644 index 197001c..0000000 --- a/game/renderer_simple.cpp +++ /dev/null @@ -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); - } - } -} diff --git a/game/renderer_simple.hpp b/game/renderer_simple.hpp deleted file mode 100644 index b62a650..0000000 --- a/game/renderer_simple.hpp +++ /dev/null @@ -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; -}; - -} diff --git a/game/server.hpp b/game/server.hpp index dc33e08..61c1089 100644 --- a/game/server.hpp +++ b/game/server.hpp @@ -8,31 +8,31 @@ using asio::ip::tcp; class Server { -public: - Server(asio::io_service& io_service,game::State* s, short port) - : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), - socket_(io_service) - { - state=s; - do_accept(); - } + public: + Server(asio::io_service& io_service,game::State* s, short port) + : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), + socket_(io_service) + { + state=s; + do_accept(); + } -private: - void do_accept() - { - acceptor_.async_accept(socket_, - [this](std::error_code ec) + private: + void do_accept() { - if (!ec) - { - std::make_shared(std::move(socket_),state)->start(); - } + acceptor_.async_accept(socket_, + [this](std::error_code ec) + { + if (!ec) + { + std::make_shared(std::move(socket_),state)->start(); + } - do_accept(); - }); - } + do_accept(); + }); + } - tcp::acceptor acceptor_; - tcp::socket socket_; - game::State* state; + tcp::acceptor acceptor_; + tcp::socket socket_; + game::State* state; }; diff --git a/game/session.cpp b/game/session.cpp index ef62b5a..e8727e8 100644 --- a/game/session.cpp +++ b/game/session.cpp @@ -11,24 +11,29 @@ using namespace std; bool Session::parse(string s) { s.erase(std::find_if(s.rbegin(), s.rend(), - std::not1(std::ptr_fun(std::isspace))).base(), s.end()); + std::not1(std::ptr_fun(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,36 +46,35 @@ 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), - [this, self](std::error_code ec, std::size_t length) - { - if (!ec) + m_socket.async_read_some( + asio::buffer(m_rcv_data, max_length), + [this, self](std::error_code ec, std::size_t length) { - std::string s(m_rcv_data,length); - if(parse(s)){ - char c[]="> "; - do_write(c,strlen(c)); + if (!ec) { + std::string s(m_rcv_data,length); + if(parse(s)){ + char c[]="> "; + do_write(c,strlen(c)); + } } - } - }); + }); } void Session::do_write(char m_snd_data[], std::size_t length) { auto self(shared_from_this()); asio::async_write(m_socket, asio::buffer(m_snd_data, length), - [this, self](std::error_code ec, std::size_t /*length*/) - { - if (!ec) + [this, self](std::error_code ec, std::size_t /*length*/) { - do_read(); - } - }); + if (!ec) { + do_read(); + } + }); } diff --git a/game/state/commands.cpp b/game/state/commands.cpp index 1a64d89..5e09db3 100644 --- a/game/state/commands.cpp +++ b/game/state/commands.cpp @@ -1,9 +1,10 @@ #include "commands.hpp" -#include "trace.hpp" - #include +#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; diff --git a/game/util.cpp b/game/util.cpp index 99970d0..4f758b8 100644 --- a/game/util.cpp +++ b/game/util.cpp @@ -1,5 +1,6 @@ #include "util.hpp" +#include #include 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; + } } diff --git a/game/util.hpp b/game/util.hpp index 041968b..c99bb8d 100644 --- a/game/util.hpp +++ b/game/util.hpp @@ -9,4 +9,7 @@ namespace util { glm::vec2 randv2_m1_1(); glm::vec2 randv2_0_1(); + + float deg2rad(float deg); + float rad2deg(float rad); }