I'm trying to create a 3D rendering engine in C++. It works well, but I got stuck on one point
I have a struct called screenDrawInfo, defined as:
typedef struct {
float *rectangle;
unsigned char color[3];
} screenDrawInfo;
I also have an array of the struct.
screenDrawInfo *polygon_buffer;
int polygon_buffer_size = 0;
But whenever I try to assign an address to the pointer, like
float vertex_buffer_ssf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
polygon_buffer[polygon_buffer_size].rectangle = &vertex_buffer_ssf[0];
The program hangs forever. It compiles fine though.
Full code below:
#include <iostream>
using namespace std;
typedef struct {
float *rectangle;
unsigned char color[3];
} screenDrawInfo;
screenDrawInfo *polygon_buffer;
int polygon_buffer_size = 0;
float vertex_buffer_wsf[12] = {
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0
};
int vertex_buffer_ws[12] = {
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0
};
float vertex_buffer_ssf[8] = {
0, 0,
0, 0,
0, 0,
0, 0
};
int vertex_buffer_ss[8] = {
0, 0,
0, 0,
0, 0,
0, 0
};
int vertex_buffer_count[4] = {0, 0, 0, 0};
void regVertF(float x, float y) {
int count = vertex_buffer_count[2];
if(count >= 4) {
cerr << "[ERROR] Pixel Buffer full. Please request a polygon draw. Ignoring call." << endl;
}
vertex_buffer_ssf[count * 2] = x;
vertex_buffer_ssf[count * 2 + 1] = y;
count++;
}
void displayShapeF(int n) {
if(polygon_buffer_size == n) return;
polygon_buffer[polygon_buffer_size].rectangle = &vertex_buffer_ssf[0];
vertex_buffer_count[2] = 0;
}
screenDrawInfo *get_polygon_buffer_addr() {
return polygon_buffer;
}
int get_polygon_buffer_size() {
return polygon_buffer_size;
}
Minimal reproducible example:
#include <iostream>
typedef struct {
float *pointer;
} structure;
int main(int argc, char **argv) {
float number = 4.5;
structure *bug;
std::cout << "You should be able to see this in the log" << std::endl;
bug[2].pointer = &number;
std::cout << "You shouldn't be able to see this in the log" << std::endl;
}
EDIT:
- There is not supposed to be any relation between the minimal reproducible code and the actual code. The full code is four files, so I tried to keep it brief.
- polygon_buffer_size increments only a few times, though I will make some exception handling code later on. For the moment, polygon_buffer_size maximum value is 1.
- I am much more accustomed to C syntax, and I would like to refrain from using STL classes.