diff --git a/src/core/renderer.cpp b/src/core/renderer.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/core/renderer.hpp b/src/core/renderer.hpp new file mode 100644 index 0000000..0846022 --- /dev/null +++ b/src/core/renderer.hpp @@ -0,0 +1,8 @@ +#ifndef INFERNO_CORE_RENDERER_H_ +#define INFERNO_CORE_RENDERER_H_ + +class Renderer { + // Function initProgressive or whatever takes a pointer to the display +} + +#endif diff --git a/src/display/display.cpp b/src/display/display.cpp index e69de29..a9b21d0 100644 --- a/src/display/display.cpp +++ b/src/display/display.cpp @@ -0,0 +1,96 @@ +#include "display.hpp" + +#include + +#include "../pixel.hpp" + +Display::Display() { + +} + +bool Display::InitVideoDisplay(std::string title, int x, int y) { + this->XRes = x; this->YRes = y; + + m_window = SDL_CreateWindow( + title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + this->XRes * this->Scale, this->YRes * this->Scale, + SDL_WINDOW_ALLOW_HIGHDPI + ); + + if (!m_window) { + // Add error + std::cout << "ERROR: COULD NOT CREATE SDL WINDOW" << std::endl; + return false; + } + + m_renderer = SDL_CreateRenderer( + m_window, -1, SDL_RENDERER_ACCELERATED + ); + + if (!m_renderer) { + // Add error + std::cout << "ERROR: COULD NOT CREATE SDL RENDERER" << std::endl; + return false; + } + + m_texture = SDL_CreateTexture( + m_renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, this->XRes, this->YRes + ); + + if (!m_texture) { + // Add error + std::cout << "ERROR: COULD NOT CREATE SDL TEXTURE" << std::endl; + return false; + } + + Framebuffer = (uint32_t*)malloc(this->XRes * this->YRes * sizeof(uint32_t)); + + if (!Framebuffer) { + // Add error + std::cout << "ERROR: COULD NOT ALLOCATE FRAMEBUFFER" << std::endl; + return false; + } + + return true; +} + + +void Display::SetPixel(int x, int y, Pixel p) { + Framebuffer[y * this->XRes + x] = p.rgb(); +} + +void Display::SetPixel(int x, int y, uint32_t p) { + Framebuffer[y * this->XRes + x] = p; +} + +void Display::SetPixelSafe(int x, int y, Pixel p) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + Framebuffer[y * this->XRes + x] = p.rgb(); + } +} + +void Display::SetPixelSafe(int x, int y, uint32_t p) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + Framebuffer[y * this->XRes + x] = p; + } +} + +void Display::SetFramebuffer(uint32_t* fb) { + Framebuffer = nullptr; + Framebuffer = fb; +} + +void Display::Refresh() { + SDL_UpdateTexture(m_texture, NULL, Framebuffer, this->XRes * sizeof(uint32_t)); + SDL_RenderCopy(m_renderer, m_texture, NULL, NULL); + SDL_RenderPresent(m_renderer); +} + +void Display::CloseDisplay() { + +} + +Display::~Display() { + +} diff --git a/src/display/display.hpp b/src/display/display.hpp index e69de29..f587ad7 100644 --- a/src/display/display.hpp +++ b/src/display/display.hpp @@ -0,0 +1,43 @@ +#ifndef INFERNO_DISPLAY_DISPLAY_H_ +#define INFERNO_DISPLAY_DISPLAY_H_ + +#include + +#include + +class Pixel; + +class Display { +public: + Display(); + + bool InitVideoDisplay(std::string title, int x, int y); + + int XRes, YRes; + std::string Title; + unsigned int Scale = 1; + + void SetPixel(int x, int y, Pixel p); + void SetPixel(int x, int y, uint32_t p); + void SetPixelSafe(int x, int y, Pixel p); + void SetPixelSafe(int x, int y, uint32_t p); + + uint32_t* Framebuffer; + void SetFramebuffer(uint32_t* fb); + + void Refresh(); + + // Error and warn + + void CloseDisplay(); + + virtual ~Display(); +private: + // SDL Internals + SDL_Window* m_window; + SDL_Renderer* m_renderer; + SDL_Texture* m_texture; + +}; + +#endif diff --git a/src/inferno.cpp b/src/inferno.cpp index e69de29..b9bc943 100644 --- a/src/inferno.cpp +++ b/src/inferno.cpp @@ -0,0 +1,66 @@ +#include "inferno.hpp" + +#include +#include "pixel.hpp" + +#include "display/display.hpp" + +InfernoEngine::InfernoEngine() { + m_initialized = false; +} + +void InfernoEngine::SetMode(OperationMode mode) { + if (!m_initialized) { + // warn = + return; + } + + m_mode = mode; +} + +bool InfernoEngine::InitWindow(int xRes, int yRes) { + if (!m_initialized) { + // warn = + return true; + } + + m_display = new Display(); + + if (!m_display->InitVideoDisplay("Inferno Engine", xRes, yRes)) { + return false; + } + + m_initialized = true; + + return true; +} + +void InfernoEngine::Ready() { + if (!m_initialized) m_initialized = true; + + while (1) { + SDL_Event e; + while (SDL_PollEvent(&e) == SDL_TRUE) + if (e.type == SDL_QUIT) m_display->CloseDisplay(); + + for (int i = 0; i < 360000; i++) { + m_display->SetPixel(rand() % m_display->XRes, + rand() % m_display->YRes, + rgb888(rand() % 255, rand() % 255, rand() % 255)); + } + + m_display->Refresh(); + } +} + +std::string InfernoEngine::LastError() { + return ""; +} + +std::string InfernoEngine::LastWarn() { + return ""; +} + +InfernoEngine::~InfernoEngine() { + +} diff --git a/src/inferno.hpp b/src/inferno.hpp index 30146a7..f87133a 100644 --- a/src/inferno.hpp +++ b/src/inferno.hpp @@ -2,8 +2,12 @@ #define INFERNO_INFERNO_H_ #include +#include +// TODO: this errored for some reason lol +// #include class Display; +class Renderer; enum OperationMode { MODE_PROGRESSIVE_GUI, @@ -18,24 +22,39 @@ enum OperationMode { class InfernoEngine { public: + InfernoEngine(); void SetMode(OperationMode mode); // Initializes the SDL framebuffer with OpenGL - bool InitWindow(); + bool InitWindow(int xRes, int yRes); + + void Ready(); // Queries the modules, if one of them errored it finds their error string - // and returns it to the main execution code + // and returns it to the main execution code, the same happens for warnings + // each module class should have an "err" and "warn" a string, timestamp + // pair as an atribute std::string LastError(); + std::string LastWarn(); + // Error and warning + // std::pair err; + // std::pair warn; + + virtual ~InfernoEngine(); private: OperationMode m_mode = MODE_PROGRESSIVE_GUI; + // Initialized flag - core engine features can't + // be changed while this flag is set to true + bool m_initialized = false; + // Gotta be a pointer so that if the display // mode is not selected then it is nothing Display* m_display = nullptr; - + Renderer* m_renderer = nullptr; }; #endif diff --git a/src/pixel.hpp b/src/pixel.hpp new file mode 100644 index 0000000..86fbc64 --- /dev/null +++ b/src/pixel.hpp @@ -0,0 +1,28 @@ +#ifndef INFERNO_PIXEL_H_ +#define INFERNO_PIXEL_H_ + +struct Pixel { + uint8_t a; + uint8_t r; + uint8_t g; + uint8_t b; + + inline uint32_t argb() { + return a << 24 | r << 16 | g << 8| b; + } + + inline uint32_t rgb() { + return 0xFF000000 | r << 16 | g << 8| b; + } +}; + + +inline uint32_t argb8888(uint8_t a, uint8_t r, uint8_t g, uint8_t b) { + return a << 24 | r << 16 | g << 8| b; +} + +inline uint32_t rgb888(uint8_t r, uint8_t g, uint8_t b) { + return 0xFF000000 | r << 16 | g << 8| b; +} + +#endif diff --git a/src/primatives/sphere.cpp b/src/primatives/sphere.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/primatives/sphere.hpp b/src/primatives/sphere.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/util/types.hpp b/src/util/types.hpp new file mode 100644 index 0000000..bf81711 --- /dev/null +++ b/src/util/types.hpp @@ -0,0 +1,13 @@ +// #ifndef INFERNO_UTIL_TYPES_H_ +// #define INFERNO_UTIL_TYPES_H_ + +// typedef signed char int8_t; +// typedef short int int16_t; +// typedef int int32_t; +// typedef long long int int64_t; +// typedef unsigned char uint8_t; +// typedef unsigned short int uint16_t; +// typedef unsigned int uint32_t; +// typedef unsigned long long int uint64_t; + +// #endif diff --git a/test/main.cpp b/test/main.cpp index dca59cc..2cb0981 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -5,10 +5,11 @@ int main(int argc, char** argv) { InfernoEngine inferno; inferno.SetMode(MODE_PROGRESSIVE_GUI); - bool status = inferno.InitWindow(); - if (!status { + bool status = inferno.InitWindow(600, 600); + if (!status) { std::cout << "Error initializing window: " << inferno.LastError() << std::endl; } + inferno.Ready(); }