feat: switched to qt and refactored

This commit is contained in:
2025-11-12 10:41:40 +01:00
parent adafae2d84
commit e9e4eed79b
3 changed files with 124 additions and 14 deletions

View File

@@ -1,12 +1,18 @@
#include "fastmath.hpp" #include "fastmath.hpp"
#include "polygon.hpp" #include "polygon.hpp"
#include "renderer.hpp"
#include "rendertarget.hpp"
#include <QApplication>
#include <QImage>
#include <QLabel>
#include <QPixmap>
#include <functional>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#define HIGHT 64 #define HIGHT 64
#define WIDTH 64 #define WIDTH 64
@@ -56,24 +62,54 @@ void drawImage(unsigned short *img) {
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] = img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
(unsigned short)23; // (((-normal[1]+(1 << (unsigned short)23; // (((-normal[1]+(1 <<
// SHIFT_AMOUNT))*14)>>SHIFT_AMOUNT); // SHIFT_AMOUNT))*14)>>SHIFT_AMOUNT);
} else {
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
(unsigned short)0;
} }
} }
} }
printf("done writing %d \n", *(img + sizeof(unsigned short) * 32 * 32)); printf("done writing %d \n", *(img + sizeof(unsigned short) * 32 * 32));
} }
int main() { int main(int argc, char *argv[]) {
unsigned short *img = Rendertarget target(128, 128);
(unsigned short *)malloc(128 * 128 * sizeof(unsigned short)); Renderer renderer;
while (true) { renderer.target = &target;
drawImage(img);
printf("%s", drawToString(img)); polygon poly =
return 0; polygon(vec3(0.9, 0.9, 0.0), vec3(0.5, 0.1, 0.0), vec3(0.1, 0.9, 0.0));
// sleep(0.001);
renderer.render(&poly);
QApplication a(argc, argv);
uint8_t *pixel = new uint8_t[64 * 64 * 3];
/*for (int i = 0; i < 64 * 64 * 3; i++) {
pixel[i] = target.pixels[i].i >> SHIFT_AMOUNT;
}*/
std::function<void(int, uint32_t *)> addTo = [&target](int start,
uint32_t *arr) {
for (int c = 0; c < 3; c++) {
arr[c] += target.pixels[start + c].i >> SHIFT_AMOUNT;
}
};
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
uint32_t result[3] = {};
addTo((target.width * y * 2 + x * 2) * 3, result);
addTo((target.width * (y * 2 + 1) + x * 2) * 3, result);
addTo((target.width * y * 2 + (x * 2 + 1)) * 3, result);
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
for (int c = 0; c < 3; c++) {
pixel[(WIDTH * y + x) * 3 + c] = result[c] >> 2; /*
target.pixels[(target.width * y * 2 + x * 2) * 3 + c].i >>
SHIFT_AMOUNT;*/
}
}
} }
// printf("%s", test); QWidget widget;
return 0; widget.setAutoFillBackground(true);
widget.setGeometry(0, 0, 500, 500);
QLabel display(&widget);
QImage img((unsigned char *)pixel, 64, 64, QImage::Format_RGB888);
display.setPixmap(QPixmap::fromImage(img).scaled(widget.size()));
widget.show();
return a.exec();
} }

42
renderer.hpp Normal file
View File

@@ -0,0 +1,42 @@
#ifndef RENDERER_H
#define RENDERER_H
#include "fastmath.hpp"
#include "polygon.hpp"
#include "rendertarget.hpp"
#include <memory.h>
class Renderer {
public:
Rendertarget *target;
bool clearTarget = true;
Renderer() : clearTarget(false) {}
void render(polygon *p) {
decimal widthScale = decimal(1.0) / decimal((float)target->width);
decimal heightScale = decimal(1.0) / decimal((float)target->height);
// std::cout << widthScale << std::endl;
vec3 pos = vec3();
if (clearTarget) {
memset((wchar_t *)target->pixels, 0,
target->height * target->width * sizeof(target[0]));
}
for (int x = 0; x < target->height; x++) {
for (int y = 0; y < target->height; y++) {
if (p->contains(pos)) {
target->set(x, y, vec3(0.0, 255.0, 0.0));
}
pos.y() += heightScale;
}
pos.y() = decimal(0);
pos.x() += widthScale;
}
}
};
#endif

32
rendertarget.hpp Normal file
View File

@@ -0,0 +1,32 @@
#ifndef RENDERTARGET_H
#define RENDERTARGET_H
#include "fastmath.hpp"
class Rendertarget {
public:
const int width;
const int height;
Rendertarget(int width, int height) : width(width), height(height) {
pixels = new decimal[width * height * 3];
}
void set(int x, int y, vec3 val) {
int start = (width * y + x) * 3;
for (int i = 0; i < 3; i++) {
pixels[start + i] = val[i];
}
}
void get(int x, int y, vec3 *val) {
int start = (width * y + x) * 3;
for (int i = 0; i < 3; i++) {
(*val)[i] = pixels[start + i];
}
}
decimal *pixels;
};
#endif