aboutsummaryrefslogtreecommitdiff
path: root/src/engine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine.cpp')
-rw-r--r--src/engine.cpp36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index aeae21c..91c0b80 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -10,6 +10,8 @@
#include <functional>
#include <utility>
#include <iterator>
+#include <memory>
+#include <utility>
#include <SDL.h>
#ifdef ENABLE_NCURSES
@@ -27,6 +29,7 @@
#include "o3d/camera.h"
#include "math/vector.h"
#include "math/mat4.h"
+#include "renderer.h"
#define FPS 60
@@ -50,8 +53,7 @@ static void usage_error_exit() {
std::exit(EXIT_FAILURE);
}
-template <class FB>
-static void scene_main(FB& fb, std::function<bool()> update_frame) {
+static void scene_main(engine::Renderer& renderer, engine::math::Matrix4 final_transform_mat, std::function<bool()> update_frame) {
float dist = 4.f;
float rad = 5.f;
bool cont = true;
@@ -76,7 +78,7 @@ static void scene_main(FB& fb, std::function<bool()> update_frame) {
scene.camera.rot_y += .0065f;
scene.camera.rot_z += .0080f;
- fb.clear();
+ renderer.clear();
auto transform_mat =
engine::math::Matrix4::translate(-scene.camera.loc)
* engine::math::Matrix4::rot_x(-scene.camera.rot_x)
@@ -86,15 +88,16 @@ static void scene_main(FB& fb, std::function<bool()> update_frame) {
transform_mat * engine::math::Matrix4::translate(engine::math::Vector3{-.5f * rad, -.5f * rad, -.5f * rad}) * scale_mat,
transform_mat * engine::math::Matrix4::translate(engine::math::Vector3{+.5f * rad, +.5f * rad, +.5f * rad}) * scale_mat,
}};
- auto projection_mat = engine::math::Matrix4::projection(static_cast<float>(fb.height()) / static_cast<float>(fb.width()), 2.f, 50.f);
+ auto pre_final_mat = final_transform_mat
+ * engine::math::Matrix4::projection(static_cast<float>(renderer.height()) / static_cast<float>(renderer.width()), 2.f, 50.f);
for (int i = 0; i < 2; i++) {
- auto final_mat = projection_mat * mats[i];
+ auto final_mat = pre_final_mat * mats[i];
const auto& mesh = scene.objs[i].mesh;
std::vector<engine::o3d::Vertex4> pts;
for (const auto& vert : mesh.pts)
pts.push_back({ final_mat * engine::math::Vector4{vert.point}, vert.data });
for (auto face : mesh.faces)
- fb.draw_triangle({pts[face[0]], pts[face[1]], pts[face[2]]});
+ renderer.draw_triangle({pts[face[0]], pts[face[1]], pts[face[2]]});
}
cont = update_frame();
}
@@ -121,10 +124,13 @@ static int main_term() {
int w, h;
getmaxyx(stdscr, h, w);
- engine::fb::CharacterFrameBuffer cfb{static_cast<unsigned int>(w), static_cast<unsigned int>(h)};
+ engine::Renderer renderer{
+ std::make_unique<engine::fb::CharacterFrameBuffer>(static_cast<unsigned int>(w), static_cast<unsigned int>(h))};
- scene_main(cfb, [&]() {
- mvaddnstr(0, 0, cfb.chars(), cfb.width() * cfb.height());
+ scene_main(renderer, engine::math::Matrix4::scale(engine::math::Vector3(2.f, 1.f, 1.f)), [&]() {
+ mvaddnstr(0, 0,
+ static_cast<engine::fb::CharacterFrameBuffer*>(renderer.fb.get())->chars(),
+ renderer.width() * renderer.height());
bool cont = true;
//timeout(1000 / FPS);
@@ -156,7 +162,7 @@ static int main_term() {
}
getmaxyx(stdscr, h, w);
- cfb.resize(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
+ renderer.resize(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
return cont;
});
@@ -183,17 +189,21 @@ static int main_graphical() {
}
window = SDL_CreateWindow("Engine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
+ SDL_Quit();
std::cerr << "Error: SDL_CreateWindow error: " << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
- engine::fb::PixelFrameBuffer pfb{SCREEN_WIDTH, SCREEN_HEIGHT};
+ engine::Renderer engine_renderer{
+ std::make_unique<engine::fb::PixelFrameBuffer>(SCREEN_WIDTH, SCREEN_HEIGHT)};
SDL_Event e;
- scene_main(pfb, [&]() {
- SDL_UpdateTexture(texture, NULL, pfb.pixels(), SCREEN_WIDTH * 4);
+ scene_main(engine_renderer, engine::math::Matrix4::idty(), [&]() {
+ SDL_UpdateTexture(texture, NULL,
+ static_cast<engine::fb::PixelFrameBuffer*>(engine_renderer.fb.get())->pixels(),
+ SCREEN_WIDTH * 4);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);