Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/code clean up #39

Merged
merged 3 commits into from
Nov 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions include/Engine/FileReader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include "Engine/Log.hpp"

#include <cstdio>

class FileReader
{
protected:
char* buffer = nullptr;

public:
FileReader(const char* filename)
{
size_t string_size, read_size;
FILE* handler;
errno_t err;

err = fopen_s(&handler, filename, "rb");
if (err != 0)
{
logf("The file '%s' was not opened\n", filename);
}

if (handler)
{
// Seek the last byte of the file
fseek(handler, 0, SEEK_END);
// Offset from the first to the last byte, or in other words, filesize
string_size = ftell(handler);
// go back to the start of the file
rewind(handler);

// Allocate a string that can hold it all
buffer = (char*)malloc(sizeof(char) * (string_size + 1));

// Read it all in one operation
read_size = fread(buffer, sizeof(char), string_size, handler);

// fread doesn't set it so put a \0 in the last position
// and buffer is now officially a string
buffer[string_size] = '\0';

if (string_size != read_size)
{
// Something went wrong, throw away the memory and set
// the buffer to NULL
free(buffer);
buffer = NULL;
}

// Always remember to close the file.
fclose(handler);
}
}

~FileReader()
{
free(buffer);
buffer = NULL;
}

const char* get()
{
return buffer;
}
};
44 changes: 44 additions & 0 deletions include/Engine/Framebuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include "Engine/Texture.hpp"
#include "Engine/Log.hpp"

#include <glad/glad.h>

class Framebuffer
{
protected:
unsigned int ID;

public:
Framebuffer()
{
glGenFramebuffers(1, &ID);
}

~Framebuffer()
{
glDeleteFramebuffers(1, &ID);
}

void bind()
{
glBindFramebuffer(GL_FRAMEBUFFER, ID);
}

void attachTexture(const Texture& texture)
{
// Set "renderedTexture" as our colour attachement #0
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.getID(), 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
log("Framebuffer error");
}
}

static void bindScreen()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
};
39 changes: 39 additions & 0 deletions include/Engine/Log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "boxer/boxer.h"

#include <stdio.h>
#include <cstdarg>
#include <string>

void log(const char* buffer)
{
#if _DEBUG
// log into console
fputs(buffer, stderr);
#endif
}

void logf(char const* const format, ...)
{
va_list arglist;
va_start(arglist, format);
#if _DEBUG
// log into console
vfprintf(stderr, format, arglist);
#endif
va_end(arglist);
}

void errorAndExit(const std::string& msg)
{
boxer::Selection selection =
boxer::show(msg.c_str(), PROJECT_NAME " error", boxer::Style::Error, boxer::Buttons::OK);
exit(-1);
}

void warning(const std::string& msg)
{
boxer::show(msg.c_str(), PROJECT_NAME " warning", boxer::Style::Warning, boxer::Buttons::OK);
exit(-1);
}
Loading