aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/resources/textureshader.frag2
-rw-r--r--examples/simplegame/main.cpp49
-rw-r--r--tests/log/test.cpp4
-rw-r--r--yage/core/camera.cpp8
-rw-r--r--yage/core/camera.h1
-rw-r--r--yage/core/imageloader.cpp2
-rw-r--r--yage/core/logger.cpp55
-rw-r--r--yage/core/logger.h29
-rw-r--r--yage/core/loglevel.h14
-rw-r--r--yage/core/logmessage.cpp6
-rw-r--r--yage/core/logmessage.h7
-rw-r--r--yage/core/logsink.cpp33
-rw-r--r--yage/render/spritebatch.cpp2
13 files changed, 168 insertions, 44 deletions
diff --git a/examples/resources/textureshader.frag b/examples/resources/textureshader.frag
index 1292de96..ae12f4b0 100644
--- a/examples/resources/textureshader.frag
+++ b/examples/resources/textureshader.frag
@@ -12,5 +12,5 @@ void main()
{
vec4 texture_colour = texture(texture_sampler, fragment_uv);
- colour = texture_colour;
+ colour = texture_colour * fragment_colour;
}
diff --git a/examples/simplegame/main.cpp b/examples/simplegame/main.cpp
index bd5a05b9..ed6b78f3 100644
--- a/examples/simplegame/main.cpp
+++ b/examples/simplegame/main.cpp
@@ -8,6 +8,8 @@
#include <yage.cpp>
+#include <cstdlib>
+#include <ctime>
#include <iostream>
using std::cout;
@@ -16,6 +18,9 @@ using namespace yage;
int main()
{
+
+ Logger logger;
+ srand(time(nullptr));
Window window;
window.create("Simple Game", 1920, 1080);
@@ -30,7 +35,7 @@ int main()
Texture brick = ResourceManager::getTexture("examples/resources/wall.png");
- cout << "texture: " << brick.width << ", " << brick.height << '\n';
+ yLogDebug << "texture: " << brick.width << ", " << brick.height;
Camera camera(1920, 1080);
textureProgram.use();
@@ -54,23 +59,45 @@ int main()
if (window.keyPressed(yage::key::E)) {
texture = breast_plate;
}
+ if (window.keyPressed(yage::key::EQUAL)) {
+ camera.zoom(0.1f);
+ }
+ if (window.keyPressed(yage::key::MINUS)) {
+ camera.zoom(-0.1f);
+ }
+ if (window.keyPressed(yage::key::LEFT)) {
+ camera.move({-5.f, 0.f});
+ }
+ if (window.keyPressed(yage::key::RIGHT)) {
+ camera.move({5.f, 0.f});
+ }
+ if (window.keyPressed(yage::key::UP)) {
+ camera.move({0.f, 5.f});
+ }
+ if (window.keyPressed(yage::key::DOWN)) {
+ camera.move({0.f, -5.f});
+ }
camera.update(textureProgram);
- int amount = 5;
- time = glfwGetTime();
- for (int i = 0; i < 1920/amount; i++) {
- for(int j = 0; j < 1080/amount; j++)
- sp.draw({(float)(amount*i), (float)(amount*j), (float)amount, (float)amount}, {0.f, 0.f, 1.f, 1.f}, fountain.id,
- Colour(255, 255, 255, 255), 0);
+ int amount = 10;
+ time = glfwGetTime();
+ for (int i = 0; i < 1920 / amount; i++) {
+ for (int j = 0; j < 1080 / amount; j++)
+ sp.draw({(float)(amount * i), (float)(amount * j),
+ (float)amount, (float)amount},
+ {0.f, 0.f, 1.f, 1.f}, texture.id,
+ Colour(rand() % 256, rand() % 256, rand() % 256, 255),
+ 0);
}
- sp.draw({50, 50, 100, 100}, {0, 0, 1, 1}, brick.id, Colour(255, 255, 255, 255), 1);
- yLog << "draw: " << glfwGetTime() - time;
+ sp.draw({50, 50, 100, 100}, {0, 0, 1, 1}, brick.id,
+ Colour(255, 255, 255, 255), 1);
+ yLogDebug << "draw: " << glfwGetTime() - time;
time = glfwGetTime();
sp.render();
- yLog << "render: " << glfwGetTime() - time;
+ yLogDebug << "render: " << glfwGetTime() - time;
window.swapBuffer();
if (i == 0) {
@@ -78,7 +105,7 @@ int main()
diff = final_time - prev_time;
prev_time = final_time;
fps = 1 / diff * 30;
- yLog << "fps: " << fps;
+ yLogInfo << "fps: " << fps;
}
i = (i + 1) % 30;
}
diff --git a/tests/log/test.cpp b/tests/log/test.cpp
index bf087855..d7e50540 100644
--- a/tests/log/test.cpp
+++ b/tests/log/test.cpp
@@ -12,8 +12,8 @@
int main()
{
- yLog << "First message";
+ yLogError << "First message";
- yLog << "Second Message";
+ yLogError << "Second Message";
std::cout << "COUT\n";
}
diff --git a/yage/core/camera.cpp b/yage/core/camera.cpp
index c492351f..d52c8996 100644
--- a/yage/core/camera.cpp
+++ b/yage/core/camera.cpp
@@ -9,6 +9,8 @@
#include "camera.h"
#include "../render/shader.h"
+#include "logger.h"
+
#include <glad/glad.h>
#include <glm/gtc/matrix_transform.hpp>
@@ -43,4 +45,10 @@ void Camera::move(const glm::vec2 &direction)
update_matrix_ = true;
}
+void Camera::zoom(float factor)
+{
+ scale_ += factor;
+ update_matrix_ = true;
+}
+
} // namespace yage
diff --git a/yage/core/camera.h b/yage/core/camera.h
index ba07d423..63bf15ca 100644
--- a/yage/core/camera.h
+++ b/yage/core/camera.h
@@ -30,6 +30,7 @@ public:
void update(Shader &program);
void move(const glm::vec2 &direction);
+ void zoom(float factor);
};
} // namespace yage
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
index 5a60e83b..23bc9a25 100644
--- a/yage/core/imageloader.cpp
+++ b/yage/core/imageloader.cpp
@@ -46,7 +46,7 @@ Texture ImageLoader::loadPng(const std::string &file_path)
glGenerateMipmap(GL_TEXTURE_2D);
- yLog << "Successfully loaded texture: " << file_path;
+ yLogDebug << "Successfully loaded texture: " << file_path;
return texture;
}
diff --git a/yage/core/logger.cpp b/yage/core/logger.cpp
index 3c80cf1b..1ae04d0b 100644
--- a/yage/core/logger.cpp
+++ b/yage/core/logger.cpp
@@ -19,29 +19,51 @@
namespace yage
{
-Logger::Logger() : active_(Active::create())
+Logger::Logger() : active_(Active::create()), min_level_(LogLevel::INFO)
{
add(makeConsoleSink());
- add(makeFileSink("yage.log"));
}
-LogMessage Logger::operator()(const std::string &fileName, int lineNum)
+Logger::Logger(const std::string &file_path)
+ : active_(Active::create()), min_level_(LogLevel::INFO)
{
- return LogMessage(this, fileName, lineNum);
+ add(makeConsoleSink());
+ add(makeFileSink(file_path));
+}
+
+Logger::Logger(LogLevel min_level)
+ : active_(Active::create()), min_level_(min_level)
+{
+ add(makeConsoleSink());
+}
+
+Logger::Logger(LogLevel min_level, const std::string &file_path)
+ : active_(Active::create()), min_level_(min_level)
+{
+ add(makeConsoleSink());
+ add(makeFileSink(file_path));
+}
+
+LogMessage Logger::operator()(LogLevel level, const std::string &fileName,
+ int lineNum)
+{
+ return LogMessage(this, level, fileName, lineNum);
}
void Logger::flush(const LogMessage *msg)
{
- std::string asString(msg->buffer_.str());
+ if (static_cast<int>(msg->meta_.level) >= static_cast<int>(min_level_)) {
+ std::string asString(msg->buffer_.str());
- auto &&sinks = sinks_;
- auto &&meta = msg->meta_;
+ auto &&sinks = sinks_;
+ auto &&meta = msg->meta_;
- active_->send([=] {
- for (auto &&sink : sinks) {
- sink.write(meta, asString);
- }
- });
+ active_->send([=] {
+ for (auto &&sink : sinks) {
+ sink.write(meta, asString);
+ }
+ });
+ }
}
void Logger::add(const LogSink &sink)
@@ -65,9 +87,14 @@ void Logger::clear()
Logger &Logger::instance()
{
- static Logger yLogger;
+ static Logger y_logger(LogLevel::INFO, "yage.log");
+
+ return y_logger;
+}
- return yLogger;
+void Logger::setLevel(LogLevel min_level)
+{
+ min_level_ = min_level;
}
} // namespace yage
diff --git a/yage/core/logger.h b/yage/core/logger.h
index d1f75aec..2423f3c3 100644
--- a/yage/core/logger.h
+++ b/yage/core/logger.h
@@ -14,6 +14,7 @@
#include <vector>
#include "../util/active.h"
+#include "loglevel.h"
#include "logmessage.h"
#include "logsink.h"
@@ -23,9 +24,13 @@ namespace yage
class Logger
{
public:
- explicit Logger();
+ Logger();
+ explicit Logger(const std::string &file_path);
+ explicit Logger(LogLevel min_level);
+ Logger(LogLevel min_level, const std::string &file_path);
- LogMessage operator()(const std::string &fileName, int lineNum);
+ LogMessage operator()(LogLevel level = LogLevel::INFO, const std::string &fileName = "",
+ int lineNum = -1);
void flush(const LogMessage *msg);
void add(const LogSink &sink);
@@ -34,13 +39,31 @@ public:
static Logger &instance();
+ // setter for the level
+ void setLevel(LogLevel min_level);
+
private:
std::vector<LogSink> sinks_;
std::unique_ptr<Active> active_;
+ LogLevel min_level_;
};
} // namespace yage
-#define yLog (yage::Logger::instance()(__FILE__, __LINE__))
+#define yLogger (yage::Logger::instance())
+
+#define yLogDebug \
+ (yage::Logger::instance()(yage::LogLevel::DEBUG, __FILE__, __LINE__))
+
+#define yLogInfo (yage::Logger::instance()(yage::LogLevel::INFO, __FILE__, __LINE__))
+
+#define yLogWarning \
+ (yage::Logger::instance()(yage::LogLevel::WARNING, __FILE__, __LINE__))
+
+#define yLogError \
+ (yage::Logger::instance()(yage::LogLevel::ERROR, __FILE__, __LINE__))
+
+#define yLogFatal \
+ (yage::Logger::instance()(yage::LogLevel::FATAL, __FILE__, __LINE__))
#endif
diff --git a/yage/core/loglevel.h b/yage/core/loglevel.h
index eb9ff5f8..78af24af 100644
--- a/yage/core/loglevel.h
+++ b/yage/core/loglevel.h
@@ -9,10 +9,16 @@
#ifndef YAGE_CORE_LOGLEVEL_H
#define YAGE_CORE_LOGLEVEL_H
-class LogLevel
-{
-public:
- LogLevel();
+namespace yage {
+
+enum class LogLevel {
+ DEBUG,
+ INFO,
+ WARNING,
+ ERROR,
+ FATAL,
};
+}
+
#endif
diff --git a/yage/core/logmessage.cpp b/yage/core/logmessage.cpp
index 1ae16fba..641c96b6 100644
--- a/yage/core/logmessage.cpp
+++ b/yage/core/logmessage.cpp
@@ -14,9 +14,9 @@
namespace yage
{
-LogMessage::LogMessage(Logger *owner, const std::string &fileName_i,
- int lineNum_i)
- : owner_(owner), meta_({fileName_i, lineNum_i})
+LogMessage::LogMessage(Logger *owner, LogLevel level, const std::string &file_name,
+ int line_num)
+ : owner_(owner), meta_({level, file_name, line_num})
{
}
diff --git a/yage/core/logmessage.h b/yage/core/logmessage.h
index ef7fd8a5..b6929ba5 100644
--- a/yage/core/logmessage.h
+++ b/yage/core/logmessage.h
@@ -13,6 +13,8 @@
#include <sstream>
#include <string>
+#include "loglevel.h"
+
namespace yage
{
@@ -34,8 +36,9 @@ public:
LogMessage &operator<<(std::ostream &(*fn)(std::ostream &os));
struct Meta {
+ LogLevel level;
std::string fileName;
- int lineNo;
+ int line;
};
private:
@@ -45,7 +48,7 @@ private:
Logger *owner_;
Meta meta_;
- LogMessage(Logger *owner, const std::string &fileName_i, int lineNum_i);
+ LogMessage(Logger *owner, LogLevel level, const std::string &file_name, int line_num);
LogMessage(LogMessage &&msg);
};
diff --git a/yage/core/logsink.cpp b/yage/core/logsink.cpp
index 6680c773..1f026059 100644
--- a/yage/core/logsink.cpp
+++ b/yage/core/logsink.cpp
@@ -82,8 +82,37 @@ public:
auto time_t = system_clock::to_time_t(now);
auto local_time = std::localtime(&time_t);
- (*fileHandle_) << std::put_time(local_time, "[%H:%M:%S] ") << msg
- << " (" << meta.fileName << ":" << meta.lineNo << ")\n";
+ std::string level;
+
+ switch (meta.level) {
+ case LogLevel::DEBUG:
+ level = "DEBUG";
+ break;
+ case LogLevel::INFO:
+ level = "INFO";
+ break;
+ case LogLevel::WARNING:
+ level = "WARNING";
+ break;
+ case LogLevel::ERROR:
+ level = "ERROR";
+ break;
+ case LogLevel::FATAL:
+ level = "FATAL";
+ break;
+ }
+
+ (*fileHandle_) << std::put_time(local_time, "[%H:%M:%S] [") << level
+ << "] " << msg << "\n";
+ if (meta.fileName != "") {
+ (*fileHandle_) << "(" << meta.fileName;
+ if (meta.line != -1) {
+ (*fileHandle_) << ":" << meta.line << ")";
+ } else {
+ (*fileHandle_) << ")";
+ }
+ }
+ (*fileHandle_) << "\n\n";
}
private:
diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp
index 14e30ccd..90958246 100644
--- a/yage/render/spritebatch.cpp
+++ b/yage/render/spritebatch.cpp
@@ -113,7 +113,7 @@ void SpriteBatch::render()
createRenderBatches();
glActiveTexture(GL_TEXTURE0);
for (auto &&batch : render_batches_) {
- yLog << "Drawing " << batch.num_vertices
+ yLogDebug << "Drawing " << batch.num_vertices
<< " vertices bound to texture: " << batch.texture;
glBindTexture(GL_TEXTURE_2D, batch.texture);
glDrawArrays(GL_TRIANGLES, batch.offset, batch.num_vertices);