LOGGING!?

This commit is contained in:
Adrian Hedqvist 2017-09-18 10:39:23 +02:00
parent 96c4d4bc04
commit a2e68da071
3 changed files with 31 additions and 27 deletions

View file

@ -70,7 +70,7 @@ void init() {
hero = nullptr; hero = nullptr;
} }
SDL_Log("Creating tilemap...\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Creating tilemap...\n");
tilemap = new Tilemap(32, 32); tilemap = new Tilemap(32, 32);
int y = 0; int y = 0;
int x = 0; int x = 0;
@ -105,23 +105,23 @@ void init() {
} }
x++; x++;
} }
SDL_Log("Done.\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Done.\n");
SDL_Log("Calculating initial FOV...\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Calculating initial FOV...\n");
hero->CalcFOV(); hero->CalcFOV();
SDL_Log("Done.\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Done.\n");
} }
void load(Renderer* rend, Input* inp) { void load(Renderer* rend, Input* inp) {
renderer = rend; renderer = rend;
input = inp; input = inp;
SDL_Log("Creating ascii tileset...\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Creating ascii tileset...\n");
ascii = new Tileset(renderer, "./assets/12x12.bmp", 192, 192, 12, 12); ascii = new Tileset(renderer, "./assets/12x12.bmp", 192, 192, 12, 12);
SDL_Log("Created ascii tileset.\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Created ascii tileset.\n");
input->bindkey(SDLK_r, ACTION_RESET); input->bindkey(SDLK_r, ACTION_RESET);
input->bindkey(SDLK_SPACE, ACTION_PAUSE); input->bindkey(SDLK_SPACE, ACTION_PAUSE);
input->bindkey(SDLK_RETURN, ACTION_STEP); input->bindkey(SDLK_RETURN, ACTION_STEP);
input->bindkey(SDLK_F1, ACTION_TOGGLE_DEBUG); input->bindkey(SDLK_F1, ACTION_TOGGLE_DEBUG);
SDL_Log("Keybinds bound.\n"); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Keybinds bound.\n");
init(); init();
} }

View file

@ -24,6 +24,8 @@ int main(int argc, char* argv[]) {
Renderer* renderer; Renderer* renderer;
Input* input; Input* input;
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
Config cfg = Config("dungeon.cfg"); Config cfg = Config("dungeon.cfg");
cfg.load(); cfg.load();
@ -38,7 +40,7 @@ int main(int argc, char* argv[]) {
err = SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO); err = SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
if (err != 0) { if (err != 0) {
const char* error = SDL_GetError(); const char* error = SDL_GetError();
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Error: %s", error); SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "%s", error);
SDL_Quit(); SDL_Quit();
return 1; return 1;
} }
@ -47,7 +49,7 @@ int main(int argc, char* argv[]) {
renderer = new Renderer(); renderer = new Renderer();
if (!renderer->Init("Dungeon", windowWidth, windowHeight)) { if (!renderer->Init("Dungeon", windowWidth, windowHeight)) {
const char* error = SDL_GetError(); const char* error = SDL_GetError();
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Error: %s", error); SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "%s", error);
SDL_ShowSimpleMessageBox(0, "Error", error, nullptr); SDL_ShowSimpleMessageBox(0, "Error", error, nullptr);
SDL_Quit(); SDL_Quit();
return 1; return 1;
@ -127,7 +129,7 @@ int main(int argc, char* argv[]) {
} }
delete renderer; delete renderer;
SDL_Quit(); SDL_Quit();
SDL_Log("Quit."); SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Quit.");
return 0; return 0;
} }

View file

@ -30,7 +30,7 @@ GLuint LoadShader(const char* path, GLenum shadertype) {
shaderstream.close(); shaderstream.close();
} }
else { else {
SDL_Log("ERROR: Could not open shader file \"%s\".\n", path); SDL_LogError(SDL_LOG_CATEGORY_RENDER, "ERROR: Could not open shader file \"%s\".\n", path);
getchar(); getchar();
return 0; return 0;
} }
@ -48,14 +48,14 @@ GLuint LoadShader(const char* path, GLenum shadertype) {
if (infologlength > 0) { if (infologlength > 0) {
std::vector<char> errorlog(infologlength + 1); std::vector<char> errorlog(infologlength + 1);
glGetShaderInfoLog(shaderId, infologlength, NULL, &errorlog[0]); glGetShaderInfoLog(shaderId, infologlength, NULL, &errorlog[0]);
SDL_Log("Errors in \"%s\":\n%s\n", path, &errorlog[0]); SDL_LogWarn(SDL_LOG_CATEGORY_RENDER, "Errors in \"%s\":\n%s\n", path, &errorlog[0]);
} }
SDL_Log("Done."); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Done.");
return shaderId; return shaderId;
} }
GLuint CreateShaderProgram(GLuint vertshader, GLuint fragshader) { GLuint CreateShaderProgram(GLuint vertshader, GLuint fragshader) {
SDL_Log("Linking shader program...\n"); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Linking shader program...\n");
GLuint programId = glCreateProgram(); GLuint programId = glCreateProgram();
glAttachShader(programId, vertshader); glAttachShader(programId, vertshader);
glAttachShader(programId, fragshader); glAttachShader(programId, fragshader);
@ -69,12 +69,12 @@ GLuint CreateShaderProgram(GLuint vertshader, GLuint fragshader) {
if (infologlength > 0) { if (infologlength > 0) {
std::vector<char> errorlog(infologlength + 1); std::vector<char> errorlog(infologlength + 1);
glGetShaderInfoLog(programId, infologlength, NULL, &errorlog[0]); glGetShaderInfoLog(programId, infologlength, NULL, &errorlog[0]);
SDL_Log("Errors when creating shader program:\n%s\n", &errorlog[0]); SDL_LogWarn(SDL_LOG_CATEGORY_RENDER, "Errors when creating shader program:\n%s\n", &errorlog[0]);
} }
glDetachShader(programId, vertshader); glDetachShader(programId, vertshader);
glDetachShader(programId, fragshader); glDetachShader(programId, fragshader);
SDL_Log("Done."); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Done.");
return programId; return programId;
} }
@ -99,7 +99,7 @@ Renderer::~Renderer() {
SDL_GL_DeleteContext(context); SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
window = nullptr; window = nullptr;
SDL_Log("Renderer destroyed."); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Renderer destroyed.");
} }
GLuint shaderProg; GLuint shaderProg;
@ -116,22 +116,24 @@ bool Renderer::Init(std::string title, int width, int height) {
window = SDL_CreateWindow(title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL); window = SDL_CreateWindow(title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
if (window == NULL) { if (window == NULL) {
SDL_LogCritical(SDL_LOG_CATEGORY_RENDER,"Failed to create a window!\n");
return false; return false;
} }
context = SDL_GL_CreateContext(window); context = SDL_GL_CreateContext(window);
if (context == NULL) { if (context == NULL) {
SDL_LogCritical(SDL_LOG_CATEGORY_RENDER,"Failed to create OpenGL context!\n");
return false; return false;
} }
GLenum err = glewInit(); GLenum err = glewInit();
if (err != GLEW_OK) { if (err != GLEW_OK) {
fprintf(stderr, "failed to initialize OpenGl: %s\n", glewGetErrorString(err)); SDL_LogCritical(SDL_LOG_CATEGORY_RENDER, "Failed to initialize OpenGl: %s\n", glewGetErrorString(err));
return false; return false;
} }
if (!GLEW_VERSION_3_2) { if (!GLEW_VERSION_3_2) {
fprintf(stderr, "OpenGl 3.2 is not supported, please try updating your drivers!\n"); SDL_LogCritical(SDL_LOG_CATEGORY_RENDER,"OpenGl 3.2 is not supported, please try updating your drivers!\n");
return false; return false;
} }
@ -164,7 +166,7 @@ bool Renderer::Init(std::string title, int width, int height) {
SetWindowSize(width, height); SetWindowSize(width, height);
SDL_Log("Renderer initialized.\n"); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Renderer initialized.\n");
return true; return true;
} }
@ -193,7 +195,7 @@ void Renderer::SetWindowSize(int width, int height) {
glm::mat4 view = glm::scale(glm::vec3(widthmult, heightmult, 1)) * glm::translate(glm::vec3(-windowwidth / 2, -windowheight / 2, 0)) * glm::mat4(1); glm::mat4 view = glm::scale(glm::vec3(widthmult, heightmult, 1)) * glm::translate(glm::vec3(-windowwidth / 2, -windowheight / 2, 0)) * glm::mat4(1);
screenVPmat = projection * view; screenVPmat = projection * view;
SDL_Log("Window size set to %dx%d.\n", width, height); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Window size set to %dx%d.\n", width, height);
} }
void Renderer::SetClearColor(float r, float g, float b, float a) { void Renderer::SetClearColor(float r, float g, float b, float a) {
@ -237,7 +239,7 @@ void Renderer::ImguiNewFrame() {
Texture * Renderer::LoadTexture(std::string path) { Texture * Renderer::LoadTexture(std::string path) {
auto it = textures.find(path); auto it = textures.find(path);
if (it == textures.end()) { if (it == textures.end()) {
SDL_Log("Loading texture: %s\n", path.c_str()); SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Loading texture: %s\n", path.c_str());
// Texture not loaded, let's load it. // Texture not loaded, let's load it.
SDL_Surface* surface = SDL_LoadBMP(path.c_str()); SDL_Surface* surface = SDL_LoadBMP(path.c_str());
SDL_Log("Loaded surface."); SDL_Log("Loaded surface.");
@ -256,9 +258,9 @@ Texture * Renderer::LoadTexture(std::string path) {
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
SDL_Log("Sending %dx%d texture to GPU...", surface->w, surface->h); SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Sending %dx%d texture to GPU...", surface->w, surface->h);
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels); glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);
SDL_Log("Loaded texture to GPU."); SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Loaded texture to GPU.");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -269,13 +271,13 @@ Texture * Renderer::LoadTexture(std::string path) {
t.h = surface->h; t.h = surface->h;
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
SDL_Log("Freed surface."); SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Freed surface.");
textures.insert(std::pair<std::string, Texture>(path, t)); textures.insert(std::pair<std::string, Texture>(path, t));
SDL_Log("Loaded texture \"%s\"\n", path.c_str()); SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "Loaded texture \"%s\"\n", path.c_str());
return &textures[path]; return &textures[path];
} }
else { else {
SDL_Log("Error: Could not load texture \"%s\"\n\t%s\n", path.c_str(), SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Could not load texture \"%s\"\n\t%s\n", path.c_str(), SDL_GetError());
return nullptr; return nullptr;
} }
} }