More on premake refactoring

This commit is contained in:
Balhau 2025-01-02 17:57:04 +00:00
parent d8af125817
commit e8e009e42d
9 changed files with 187 additions and 139 deletions

View file

@ -1,12 +1,11 @@
#include "../src/bmath/complex.hpp"
#include <iostream>
#include "../bmath/complex.hpp"
using BMath::Complex;
int main(void)
{
int main(void) {
std::cout << "Complex numbers " << std::endl;
Complex c1(1,1);
Complex c2(2,2);
std::cout << c1 << c2 << c2-c1 << c2+c1 <<std::endl;
}
Complex c1(1, 1);
Complex c2(2, 2);
std::cout << c1 << c2 << c2 - c1 << c2 + c1 << std::endl;
}

View file

@ -1,78 +1,74 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <signal.h>
#include <sstream>
#include <stdlib.h>
#include <string>
#include "../opengl/Shader.h"
#include "../opengl/Renderer.h"
#include "../opengl/VertexBuffer.h"
#include "../opengl/IndexBuffer.h"
#include "../opengl/VertexArray.h"
#include "../opengl/utils.h"
#include "../src/opengl/IndexBuffer.h"
#include "../src/opengl/Renderer.h"
#include "../src/opengl/Shader.h"
#include "../src/opengl/VertexArray.h"
#include "../src/opengl/VertexBuffer.h"
#include "../src/opengl/utils.h"
const std::string SHADERS_PATH = "src/opengl/res/shaders/Basic.shader";
static void error_callback(int error, const char *description)
{
fputs(description, stderr);
static void error_callback(int error, const char *description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
GLFWwindow *window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
int major, minor, revision;
glfwGetVersion(&major, &minor, &revision);
printf("Running against GLFW %i.%i.%i\n", major, minor, revision);
LOG(glfwGetVersionString());
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
int main(void) {
GLFWwindow *window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_SUCCESS);
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
int major, minor, revision;
glfwGetVersion(&major, &minor, &revision);
printf("Running against GLFW %i.%i.%i\n", major, minor, revision);
LOG(glfwGetVersionString());
while (!glfwWindowShouldClose(window)) {
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}

View file

@ -1,51 +1,39 @@
#include "../src/platform/dsl/pt.h"
#include <iostream>
using namespace std;
#define CALLBACK(init,end) \
{ \
.init_cena = init, \
.end_cena = end \
}
#define CALLBACK(init, end) \
{ .init_cena = init, .end_cena = end }
#define ATTRIBUTE_NO_RETURN __attribute__((__noreturn__))
#define CALLBACK_ARRAY_SIZE(array) sizeof(array)/sizeof(Callback)
#define CALLBACK_ARRAY_SIZE(array) sizeof(array) / sizeof(Callback)
typedef struct Callback {
void (*init_cena)(void);
void (*end_cena)(void);
} Callback;
void end_cena_cool(){
std::cout << "End cool stuff" << std::endl;
}
void end_cena_cool() { std::cout << "End cool stuff" << std::endl; }
void end_cena_not_cool(){
std::cout << "End not cool stuff" << std::endl;
}
void end_cena_not_cool() { std::cout << "End not cool stuff" << std::endl; }
void init_cena_cool(){
std::cout<< "Init cool stuff" << std::endl;
}
void init_cena_cool() { std::cout << "Init cool stuff" << std::endl; }
void init_cena_not_cool(){
std::cout << "Init not cool stuff" << std::endl;
}
void init_cena_not_cool() { std::cout << "Init not cool stuff" << std::endl; }
void void_func(){
std::cout << "Void function 1" << std::endl;
}
void void_func() { std::cout << "Void function 1" << std::endl; }
void void_func2() ATTRIBUTE_NO_RETURN;
void void_func2(){
exit(0);
}
void void_func2() { exit(0); }
#define COLOR_ARRAY 3
typedef union Color{
enum Colors { RED, GREEN, BLUE };
typedef union Color {
struct {
int r;
int g;
@ -54,38 +42,67 @@ typedef union Color{
int color_array[COLOR_ARRAY];
} Color;
int main(int argc, char** argv){
Callback calls[]={
CALLBACK(init_cena_cool, end_cena_cool),
CALLBACK(init_cena_not_cool,end_cena_not_cool),
CALLBACK(init_cena_cool, end_cena_not_cool),
CALLBACK(init_cena_not_cool, end_cena_cool)
};
void printColor(const Color &color) {
std::cout << "[" << color.r << "," << color.g << "," << color.b << "]"
<< std::endl;
}
Color red = {{
.r = 1,
.g = 0,
.b = 0,
}};
Color green = {{
.r = 0,
.g = 1,
.b = 0,
}};
Color blue = {{
.r = 0,
.g = 0,
.b = 1,
}};
Callback empty_calls[]={};
Color colors[] = {[RED] = red, [GREEN] = green, [BLUE] = blue};
int main(int argc, char **argv) {
Callback callbacks[] = {CALLBACK(init_cena_cool, end_cena_cool),
CALLBACK(init_cena_not_cool, end_cena_not_cool),
CALLBACK(init_cena_cool, end_cena_not_cool),
CALLBACK(init_cena_not_cool, end_cena_cool)};
Callback empty_callbacks[] = {};
std::cout << "Hello cenas" << std::endl;
std::cout << "Size of Callback entry bytes:" << sizeof(calls[0]) << std::endl;
std::cout << "Size of Callback array bytes: " << sizeof(calls) << std::endl;
std::cout << "Number of Callback entries: " << sizeof(calls)/sizeof(Callback) << std::endl;
std::cout << "Empty calls size: "<< CALLBACK_ARRAY_SIZE(empty_calls) << std::endl;
std::cout << "Size of Callback entry bytes:" << sizeof(callbacks[0])
<< std::endl;
std::cout << "Size of Callback array bytes: " << sizeof(callbacks)
<< std::endl;
std::cout << "Number of Callback entries: "
<< sizeof(callbacks) / sizeof(Callback) << std::endl;
std::cout << "Empty callbacks size: " << CALLBACK_ARRAY_SIZE(empty_callbacks)
<< std::endl;
int array_size = CALLBACK_ARRAY_SIZE(calls);
int i=0;
for(i=0;i<array_size;i++){
calls[i].init_cena();
calls[i].end_cena();
int array_size = CALLBACK_ARRAY_SIZE(callbacks);
int i = 0;
for (i = 0; i < array_size; i++) {
callbacks[i].init_cena();
callbacks[i].end_cena();
}
Color color;
color.color_array[0]=10;
color.color_array[1]=20;
color.color_array[2]=30;
std::cout << "R: " << color.r << " G: " << color.g << " B: " << color.b << std::endl;
color.color_array[0] = 10;
color.color_array[1] = 20;
color.color_array[2] = 30;
std::cout << "R: " << color.r << " G: " << color.g << " B: " << color.b
<< std::endl;
std::cout << sizeof(color) << std::endl;
se(0 == 0) { std::cout << "SIm" << std::endl; }
senao {}
printColor(colors[RED]);
printColor(colors[GREEN]);
printColor(colors[BLUE]);
}

View file

@ -9,3 +9,4 @@ include("premake/maths.lua")
include("premake/clock.lua")
include("premake/allocation.lua")
include("premake/engine.lua")
include("premake/fpointers.lua")

View file

@ -134,9 +134,9 @@ bool matrix3d::equals(const matrix3d &first, const matrix3d &second) {
}
float32 matrix3d::det(const matrix3d &m) {
return m(0, 0) * Det(m(1, 1), m(1, 2), m(2, 1), m(2, 2)) -
m(0, 1) * Det(m(1, 0), m(1, 2), m(2, 0), m(2, 2)) +
m(0, 2) * Det(m(1, 0), m(1, 1), m(2, 0), m(2, 1));
return m(0, 0) * DET(m(1, 1), m(1, 2), m(2, 1), m(2, 2)) -
m(0, 1) * DET(m(1, 0), m(1, 2), m(2, 0), m(2, 2)) +
m(0, 2) * DET(m(1, 0), m(1, 1), m(2, 0), m(2, 1));
};
matrix3d matrix3d::inv(const matrix3d &m) {
@ -153,7 +153,7 @@ matrix3d matrix3d::inv(const matrix3d &m) {
// determinant inverse
float32 idet = 1.0f / vector3d::dot(r2, c);
return matrix3d(Idet(r0, idet), Idet(r1, idet), Idet(r2, idet));
return matrix3d(INVDET(r0, idet), INVDET(r1, idet), INVDET(r2, idet));
};
matrix3d matrix3d::rotX(float32 angle) {
@ -197,13 +197,17 @@ matrix3d matrix3d::scale(float32 scalex, float32 scaley, float32 scalez) {
return matrix3d(scalex, 0, 0, 0, scaley, 0, 0, 0, scalez);
};
matrix3d matrix3d::scale(const vector3d &v) {
return matrix3d(v.x, 0, 0, 0, v.y, 0, 0, 0, v.z);
}
matrix3d matrix3d::scale(float32 scale, const vector3d &vector) {
scale -= 1;
float32 x = vector[0] * scale;
float32 y = vector[1] * scale;
float32 z = vector[2] * scale;
float vx_vy = x * vector[1];
float vx_vz = x * vector[1];
float vx_vz = x * vector[2];
float vy_vz = y * vector[2];
return matrix3d(x * vector[0] + 1, vx_vy, vx_vz, vx_vy, y * vector[1] + 1,
vx_vz, vx_vz, vy_vz, z * vector[2] + 1);
@ -218,9 +222,35 @@ matrix3d matrix3d::skew(float32 angle, const vector3d v1, const vector3d &v2) {
return matrix3d();
};
matrix3d matrix3d::reflect(const vector3d &v) {
float32 x = v.x * -2.0;
float32 y = v.y * -2.0;
float32 z = v.z * -2.0;
float32 vx_vy = x * v.y;
float32 vx_vz = x * v.z;
float32 vy_vz = y * v.z;
return matrix3d(x * v.x + 1, vx_vy, vx_vz, vx_vy, y * v.y + 1, vy_vz, vx_vz,
vy_vz, z * v.z + 1);
};
matrix3d matrix3d::involution(const vector3d &v) {
float32 x = v.x * 2.0;
float32 y = v.y * 2.0;
float32 z = v.z * 2.0;
float32 vx_vy = x * v.y;
float32 vx_vz = x * v.z;
float32 vy_vz = y * v.z;
return matrix3d(x * v.x - 1, vx_vy, vx_vz, vx_vy, y * v.y - 1, vy_vz, vx_vz,
vy_vz, z * v.z - 1);
};
// Overload of + operator. This will add 2 matrix3d instances
matrix3d operator+(const matrix3d &left, const matrix3d &right) {
Matrix(add);
MATRIX(add, 3);
add[0][0] = left[0][0] + right[0][0];
add[0][1] = left[0][1] + right[0][1];
add[0][2] = left[0][2] + right[0][2];
@ -237,7 +267,7 @@ matrix3d operator+(const matrix3d &left, const matrix3d &right) {
};
matrix3d operator-(const matrix3d &left, const matrix3d &right) {
Matrix(m_temp);
MATRIX(m_temp, 3);
m_temp[0][0] = left[0][0] - right[0][0];
m_temp[0][1] = left[0][1] - right[0][1];
m_temp[0][2] = left[0][2] - right[0][2];

View file

@ -2,16 +2,12 @@
#include "../../platform/base.hpp"
#include "math.h"
#include "utils.hpp"
#include "vector3d.hpp"
// Defines a matrix in termos of float32
#define Matrix(n) float32 n[3][3]
// Computes determinant sub block
#define Det(a, b, c, d) (a * d - b * c)
#define Idet(row, idet) row[0] * idet, row[1] * idet, row[2] * idet
namespace engine::math {
struct matrix3d {
Matrix(_m);
MATRIX(_m, 3);
void setFloatArray(const float32[3][3]);
void setAll(float32 v);
@ -51,10 +47,14 @@ struct matrix3d {
static matrix3d rot(float32 angle, const vector3d &axis);
// Scaling matrix
static matrix3d scale(float32 scale_x, float32 scale_y, float32 scale_z);
static matrix3d scale(const vector3d &v);
// Scaling matrix in relation to a given vector
static matrix3d scale(float32 scale, const vector3d &vector);
// Skew matrix based on an angle and two vector3d
static matrix3d skew(float32 angle, const vector3d v1, const vector3d &v2);
static matrix3d reflect(const vector3d &v);
static matrix3d involution(const vector3d &v);
;
};
// Non instance operator overloading

View file

@ -7,12 +7,14 @@ namespace engine::math {
struct point3d : vector3d {
point3d() = default;
point3d(float32 x, float32 y, float32 z) : vector3d(x, y, z){};
inline point3d operator+(const vector3d &v) {
return point3d(this->x + v.x, this->y + v.y, this->z + v.z);
};
inline vector3d operator-(const point3d &b) {
return vector3d(b.x - this->x, b.y - this->y, b.z - this->z);
}
};
inline point3d operator+(const point3d &a, const vector3d &b) {
return point3d(a.x + b.x, a.y + b.y, a.z + b.z);
};
inline vector3d operator-(const point3d &a, const point3d &b) {
return vector3d(b.x - a.x, b.y - a.y, b.z - a.z);
}
}; // namespace engine::math

View file

@ -2,4 +2,5 @@
#include "platform.hpp"
#include "types.hpp"
#include <iostream>
#include <ostream>

View file

@ -11,6 +11,8 @@ typedef long int64;
typedef char int8;
typedef short int16;
#define f_pointer(fp_name, ret_type, ...) ret_type (*fp_name)(__VA_ARGS__)
typedef union {
struct {
int32 value : 31;