Ver código fonte

Lesson 6.1 - 6.2

Godzil 3 anos atrás
pai
commit
a44187fc1b
3 arquivos alterados com 101 adições e 6 exclusões
  1. 8 6
      source/Game.cpp
  2. 71 0
      source/Logger.cpp
  3. 22 0
      source/include/Logger.h

+ 8 - 6
source/Game.cpp

@@ -6,14 +6,16 @@
  *
  * Created by Manoël Trapier on 09/02/2021.
  */
-#include <Game.h>
-#include <stdio.h>
 #include <SDL.h>
 #include <SDL_image.h>
 #include <glm/glm.hpp>
 
+#include <Game.h>
+#include <Logger.h>
+
 Game::Game()
 {
+    Logger::Log("It's a me!");
     this->isRunning = false;
     this->windowsHeight = 0;
     this->windowsWidth = 0;
@@ -21,7 +23,7 @@ Game::Game()
 
 Game::~Game()
 {
-
+    Logger::Log("Mama mia!");
 }
 
 void Game::Initialize()
@@ -32,7 +34,7 @@ void Game::Initialize()
     ret = SDL_Init(SDL_INIT_EVERYTHING);
     if (ret)
     {
-        fprintf(stderr, "SDL Initialisation error.\n");
+        Logger::Error("SDL Initialisation error.");
         return;
     }
 
@@ -44,14 +46,14 @@ void Game::Initialize()
                                     this->windowsWidth, this->windowsHeight, SDL_WINDOW_BORDERLESS);
     if (!window)
     {
-        fprintf(stderr, "SDL window creation error.\n");
+        Logger::Error("SDL window creation error.");
         return;
     }
 
     this->renderer = SDL_CreateRenderer(window, -1, 0);
     if (!renderer)
     {
-        fprintf(stderr, "SDL renderer creation error.\n");
+        Logger::Error("SDL renderer creation error.");
         return;
     }
 

+ 71 - 0
source/Logger.cpp

@@ -0,0 +1,71 @@
+/*
+ * 2D Game Engine 
+ * Logger.cpp.c: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/02/2021.
+ */
+#include <stdio.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <time.h>
+#include <Logger.h>
+
+static void printHeader(const char *type, uint8_t colour)
+{
+    time_t curtime = time(NULL);
+    char buf[30];
+    struct tm * p = localtime(&curtime);
+    strftime(buf, 38, "%d-%b-%Y %H:%M:%S", p);
+
+    fprintf(stderr, "\x1B[%d;1m%-5s | %-20s - ", colour, type, buf);
+}
+
+void Logger::Log(const char *message, ...)
+{
+    va_list list;
+    va_start(list, message);
+
+    printHeader("LOG", 32);
+    vfprintf(stderr, message, list);
+    fprintf(stderr, "\x1B[0m\n");
+
+    va_end(list);
+}
+
+void Logger::Error(const char *message, ...)
+{
+    va_list list;
+    va_start(list, message);
+
+    printHeader("ERROR", 31);
+    vfprintf(stderr, message, list);
+    fprintf(stderr, "\x1B[0m\n");
+
+    va_end(list);
+}
+
+void Logger::Warning(const char *message, ...)
+{
+    va_list list;
+    va_start(list, message);
+
+    printHeader("WARN", 33);
+    vfprintf(stderr, message, list);
+    fprintf(stderr, "\x1B[0m\n");
+
+    va_end(list);
+}
+
+void Logger::Info(const char *message, ...)
+{
+    va_list list;
+    va_start(list, message);
+
+    printHeader("INFO", 36);
+    vfprintf(stderr, message, list);
+    fprintf(stderr, "\x1B[0m\n");
+
+    va_end(list);
+}

+ 22 - 0
source/include/Logger.h

@@ -0,0 +1,22 @@
+/*
+ * 2D Game Engine 
+ * Logger.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/02/2021.
+ */
+
+#ifndef GAMEENGINE_LOGGER_H
+#define GAMEENGINE_LOGGER_H
+
+class Logger
+{
+public:
+    static void Log(const char *message, ...);
+    static void Error(const char *message, ...);
+    static void Warning(const char *message, ...);
+    static void Info(const char *message, ...);
+};
+
+#endif /* GAMEENGINE_LOGGER_H */