diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-11-12 22:30:20 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-11-12 22:30:20 +0000 |
commit | e68759a4101567a27e306eae0a907baa759ae80c (patch) | |
tree | c90733351cb7a8a02f91725118593c1c4f19c563 | |
parent | 5fb7c972d44a3ce0a067101885d2e0c1966e7c89 (diff) | |
download | YAGE-e68759a4101567a27e306eae0a907baa759ae80c.tar.gz YAGE-e68759a4101567a27e306eae0a907baa759ae80c.zip |
Designing simple preview game
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | resources/default_shader.frag | 7 | ||||
-rw-r--r-- | resources/default_shader.vert | 22 | ||||
-rw-r--r-- | tests/logtest.cpp | 15 | ||||
-rw-r--r-- | tests/resources/dngn_blood_fountain.png | bin | 0 -> 955 bytes | |||
-rw-r--r-- | tests/simplegame.cpp | 14 | ||||
-rw-r--r-- | tests/threadtest.cpp | 23 | ||||
-rw-r--r-- | yage/core/logger.cpp | 39 | ||||
-rw-r--r-- | yage/core/logger.h | 18 | ||||
-rw-r--r-- | yage/core/loglevel.h | 10 | ||||
-rw-r--r-- | yage/core/logmessage.cpp | 7 | ||||
-rw-r--r-- | yage/core/logmessage.h | 3 | ||||
-rw-r--r-- | yage/core/logsink.cpp | 31 | ||||
-rw-r--r-- | yage/core/logsink.h | 9 | ||||
-rw-r--r-- | yage/core/resourcemanager.cpp | 2 | ||||
-rw-r--r-- | yage/core/spritebatch.h | 1 | ||||
-rw-r--r-- | yage/util/active.h | 11 |
17 files changed, 194 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d8ae083..f003b103 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,4 +47,5 @@ if($ENV{UNIT_TESTS}) make_test(vector3test ${SIMULATION_RUNS}) make_test(vector4test ${SIMULATION_RUNS}) make_test(logtest 1) + make_test(threadtest 1) endif() diff --git a/resources/default_shader.frag b/resources/default_shader.frag new file mode 100644 index 00000000..93db96da --- /dev/null +++ b/resources/default_shader.frag @@ -0,0 +1,7 @@ +#version 130 + +in vec2 fragment_position; +in vec4 fragment_colour; +in vec2 fragment_uv; + +out vec4 colour; diff --git a/resources/default_shader.vert b/resources/default_shader.vert new file mode 100644 index 00000000..d591e4bf --- /dev/null +++ b/resources/default_shader.vert @@ -0,0 +1,22 @@ +#version 130 + +in vec2 vertex_position; +in vec4 vertex_colour; +in vec2 vertex_uv; + +out vec2 fragment_position; +out vec4 fragment_colour; +out vec2 fragment_uv; + +uniform mat4 P; + +void main() +{ + gl_Position.xy = (P*vec4(vertex_position, 0.0, 1.0)).xy; + gl_Position.z = 0.0; + gl_Position.w = 1.0; + + fragment_position = vertex_position; + fragment_colour = vertex_colour; + fragment_uv = vec2(vertex_uv.x, 1-vertex_uv.y); +} diff --git a/tests/logtest.cpp b/tests/logtest.cpp index 9cd5288a..359311ec 100644 --- a/tests/logtest.cpp +++ b/tests/logtest.cpp @@ -1,3 +1,11 @@ +/* ---------------------------------------------------------------------------- + * logtest.cpp + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License + * See file LICENSE for more details + * ---------------------------------------------------------------------------- + */ + #include <yage.h> #include <iostream> @@ -5,10 +13,7 @@ int main() { gLog << "Hello World"; - yage::Logger l; - l() << "Hello my name is Yann"; - std::cout << "#####################\n"; - gLog << "Yanananan"; - gLog << "Remove the top element"; + gLog << "This is Yann"; + std::cout << "Hello\n"; } diff --git a/tests/resources/dngn_blood_fountain.png b/tests/resources/dngn_blood_fountain.png Binary files differnew file mode 100644 index 00000000..7214fd47 --- /dev/null +++ b/tests/resources/dngn_blood_fountain.png diff --git a/tests/simplegame.cpp b/tests/simplegame.cpp index 0692574a..7ca8db77 100644 --- a/tests/simplegame.cpp +++ b/tests/simplegame.cpp @@ -5,14 +5,26 @@ using namespace yage; int main() { Window window; + SpriteBatch sp; + GlslProgram program; window.create("Simple Game", 800, 640); + sp.init(); + + program.compileShaders("/home/yannherklotz/Github/YAGE/tests/resources/simplegame.vert", "/home/yannherklotz/Github/YAGE/tests/resources/simplegame.vert"); + program.addAttribute("vertex_position"); + program.addAttribute("vertex_color"); + program.addAttribute("vertex_uv"); + program.linkShaders(); + + Texture fountain = ResourceManager::getTexture("/home/yannherklotz/Github/YAGE/tests/resources/dngn_blood_fountain.png"); while(!window.shouldClose()) { window.clearBuffer(); - SpriteBatch sp; sp.begin(); + sp.draw(std::vector<float>({0, 0, 50, 50}), std::vector<float>({0, 0, 1, 1}), fountain.id, Color(), 0); + sp.render(); window.pollEvents(); window.swapBuffer(); diff --git a/tests/threadtest.cpp b/tests/threadtest.cpp new file mode 100644 index 00000000..dd1877e5 --- /dev/null +++ b/tests/threadtest.cpp @@ -0,0 +1,23 @@ +#include <yage.h> + +#include <iostream> +#include <thread> + +int main() +{ + int n = 5; + + std::cout << "n before: " << n << "\n"; + + auto f = [&] () { + n = 8; + }; + + std::thread t1(f); + + std::cout << "n after: " << n << "\n"; + + t1.join(); + + std::cout << "n after thread: " << n << "\n"; +} diff --git a/yage/core/logger.cpp b/yage/core/logger.cpp index d289c0e6..9fba2239 100644 --- a/yage/core/logger.cpp +++ b/yage/core/logger.cpp @@ -7,25 +7,52 @@ */ #include "logger.h" +#include "logmessage.h" +#include "logsink.h" +#include <algorithm> #include <iostream> #include <string> namespace yage { -LogMessage Logger::operator()() +Logger::Logger() { - return LogMessage(this); + add(makeConsoleSink()); +} + +LogMessage Logger::operator()(const std::string &fileName, int lineNum) +{ + return LogMessage(this, fileName, lineNum); } void Logger::flush(const LogMessage *msg) { - using std::string; - using std::cout; + std::string asString(msg->buffer_.str()); - string asString(msg->buffer_.str()); - cout << asString << "\n"; + for (auto &&sink : sinks_) { + sink.write(msg->meta_, asString); + } +} + +void Logger::add(const LogSink &sink) +{ + sinks_.push_back(sink); +} + +void Logger::remove(const LogSink &sink) +{ + auto it = std::find(std::begin(sinks_), std::end(sinks_), sink); + + if(it != std::end(sinks_)) { + sinks_.erase(it); + } +} + +void Logger::clear() +{ + sinks_.clear(); } Logger &Logger::instance() diff --git a/yage/core/logger.h b/yage/core/logger.h index 6829327e..2c70fd04 100644 --- a/yage/core/logger.h +++ b/yage/core/logger.h @@ -9,23 +9,35 @@ #ifndef YAGE_CORE_LOGGER_H #define YAGE_CORE_LOGGER_H -#include "logmessage.h" +#include <string> +#include <vector> namespace yage { +class LogMessage; +class LogSink; + class Logger { public: - LogMessage operator()(); + explicit Logger(); + + LogMessage operator()(const std::string &fileName, int lineNum); void flush(const LogMessage *msg); + void add(const LogSink &sink); + void remove(const LogSink &sink); + void clear(); static Logger &instance(); + +private: + std::vector<LogSink> sinks_; }; } // namespace yage -#define gLog yage::Logger::instance()() +#define gLog (yage::Logger::instance()(__FILE__, __LINE__)) #endif diff --git a/yage/core/loglevel.h b/yage/core/loglevel.h new file mode 100644 index 00000000..5e11ed83 --- /dev/null +++ b/yage/core/loglevel.h @@ -0,0 +1,10 @@ +#ifndef YAGE_CORE_LOGLEVEL_H +#define YAGE_CORE_LOGLEVEL_H + +class LogLevel +{ +public: + LogLevel(); +}; + +#endif diff --git a/yage/core/logmessage.cpp b/yage/core/logmessage.cpp index 82b63d2e..9f460bec 100644 --- a/yage/core/logmessage.cpp +++ b/yage/core/logmessage.cpp @@ -9,13 +9,16 @@ #include "logmessage.h" #include "logger.h" - #include <iostream> namespace yage { -LogMessage::LogMessage(Logger *owner) : owner_(owner) {} +LogMessage::LogMessage(Logger *owner, const std::string &fileName_i, + int lineNum_i) + : owner_(owner), meta_({fileName_i, lineNum_i}) +{ +} LogMessage::LogMessage(LogMessage &&msg) : owner_(std::move(msg.owner_)) {} diff --git a/yage/core/logmessage.h b/yage/core/logmessage.h index ded40c19..8080f914 100644 --- a/yage/core/logmessage.h +++ b/yage/core/logmessage.h @@ -43,8 +43,9 @@ private: std::ostringstream buffer_; Logger *owner_; + Meta meta_; - LogMessage(Logger *owner); + LogMessage(Logger *owner, const std::string &fileName_i, int lineNum_i); LogMessage(LogMessage &&msg); }; diff --git a/yage/core/logsink.cpp b/yage/core/logsink.cpp index 1cc8b139..987a260d 100644 --- a/yage/core/logsink.cpp +++ b/yage/core/logsink.cpp @@ -8,12 +8,43 @@ #include "logsink.h" +#include <iostream> + namespace yage { +LogSink::LogSink(const LogSink &sink) : wrapper_(sink.wrapper_->clone()) {} + +LogSink::LogSink(LogSink &&sink) : wrapper_(std::move(sink.wrapper_)) {} + +LogSink &LogSink::operator=(const LogSink &sink) +{ + wrapper_.reset(sink.wrapper_->clone()); + return *this; +} + +LogSink &LogSink::operator=(LogSink &&sink) +{ + wrapper_ = std::move(sink.wrapper_); + return *this; +} + +bool LogSink::operator==(const LogSink &sink) +{ + return (wrapper_.get() == sink.wrapper_.get()); +} + void LogSink::write(const LogMessage::Meta &meta, const std::string &msg) { wrapper_->write(meta, msg); } +LogSink makeConsoleSink() +{ + return [](const LogMessage::Meta &meta, const std::string &msg) { + std::cout << msg << " (" << meta.fileName << ":" << meta.lineNo + << ")\n"; + }; +} + } // namespace yage diff --git a/yage/core/logsink.h b/yage/core/logsink.h index e898d15f..f18a6d37 100644 --- a/yage/core/logsink.h +++ b/yage/core/logsink.h @@ -23,6 +23,13 @@ public: template <typename T> LogSink(T impl); + LogSink(const LogSink &sink); + LogSink(LogSink &&sink); + + LogSink &operator=(const LogSink &sink); + LogSink &operator=(LogSink &&sink); + bool operator==(const LogSink &sink); + void write(const LogMessage::Meta &meta, const std::string &msg); private: @@ -47,6 +54,8 @@ private: std::unique_ptr<Concept> wrapper_; }; +LogSink makeConsoleSink(); + /* ----------------------------------------------------------------------------- * Template Implementation * ----------------------------------------------------------------------------- diff --git a/yage/core/resourcemanager.cpp b/yage/core/resourcemanager.cpp index cf70eae8..aa234c9f 100644 --- a/yage/core/resourcemanager.cpp +++ b/yage/core/resourcemanager.cpp @@ -6,7 +6,7 @@ * ---------------------------------------------------------------------------- */ -#include <yage/core/resourcemanager.h> +#include "resourcemanager.h" namespace yage { diff --git a/yage/core/spritebatch.h b/yage/core/spritebatch.h index 953055f6..2c525ba7 100644 --- a/yage/core/spritebatch.h +++ b/yage/core/spritebatch.h @@ -92,6 +92,7 @@ public: void init(); void begin(); void end(); + // adds a sprite to the sprite batch to be rendered later void draw(const yage::Vector4f &destination_rect, const yage::Vector4f &uv_rect, GLuint texture, const Color &color, diff --git a/yage/util/active.h b/yage/util/active.h new file mode 100644 index 00000000..877ab75e --- /dev/null +++ b/yage/util/active.h @@ -0,0 +1,11 @@ +#ifndef YAGE_UTIL_ACTIVE_H +#define YAGE_UTIL_ACTIVE_H + +class Active +{ +public: + Active(); + virtual ~Active(); +}; + +#endif |