commit 84a31fb7042db237390325e6d3b33ea02ed491ce
parent ff8527854d6689c4d2ec20d4160bfba782da0905
Author: Edea Kramer <edea@lunarcry.home>
Date: Thu, 24 Oct 2024 22:04:03 +0300
*Finally understood why SDL_PollEvent takes so much time.
In fact it takes time but not so much.
The problem is that it's really slow to run it every instruction
the cpu decodes, so we loop 1 mil times before checking for input
* Added playground
* Rendering the triangle still requires work
Diffstat:
6 files changed, 100 insertions(+), 179 deletions(-)
diff --git a/makefile b/makefile
@@ -1,2 +1,2 @@
build:
- cc -O3 -g src/*.c -o bin/ultimecia -I/usr/local/include -L/usr/local/lib -lSDL2
+ cc -O3 -g src/*.c -o bin/ultimecia -I/usr/local/include -L/usr/local/lib -lSDL2
diff --git a/src/main.c b/src/main.c
@@ -12,45 +12,44 @@
#include "gpu.h"
#include "sr.h"
-//FB fb;
-//SDL_Window* win;
-//SDL_Renderer* ren;
-//vec3f **vertices;
-//i32 **faces;
SDL_Event ev;
int
main(int argc, char **argv)
{
+ int c;
REN *ren;
CPU *cpu;
Interconnect *inter;
+ SDL_Init(SDL_INIT_VIDEO);
inter = new_interconnect();
cpu = new_cpu(inter);
ren = inter->gpu->ren;
+ SDL_SetRenderDrawColor(ren->renderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderClear(ren->renderer);
+ SDL_RenderPresent(ren->renderer);
while(1) {
- CPU_run_next_instruction(cpu);
+ /* Because it's too slow to run events every instr */
+ for (c = 0; c < 1e5; c++)
+ CPU_run_next_instruction(cpu);
- //while(SDL_PollEvent(&ev) != 0) {
- // switch(ev.type) {
- // case SDL_QUIT:
- // SDL_Quit();
- // exit(1);
- //
- // case SDL_KEYDOWN:
- // if (ev.key.keysym.sym == SDLK_q) {
- // SDL_Quit();
- // exit(1);
- // } else if (ev.key.keysym.sym == SDLK_a) {
- // SDL_Log("A pressed");
- // }
- // default:
- // break;
- // }
- //}
+ while(SDL_PollEvent(&ev) != 0) {
+ switch(ev.type) {
+ case SDL_QUIT:
+ SDL_Quit();
+ exit(1);
+ case SDL_KEYDOWN:
+ if (ev.key.keysym.sym == SDLK_q) {
+ SDL_Quit();
+ exit(1);
+ } else if (ev.key.keysym.sym == SDLK_a) {
+ SDL_Log("A pressed");
+ break;
+ }
+ }
+ }
}
free(inter->bios->data);
diff --git a/src/playground/main.c b/src/playground/main.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <SDL2/SDL.h>
+
+void
+hi()
+{
+ int a = 1;
+ int b = 2;
+ printf("Haha\n");
+}
+
+int
+main()
+{
+
+ int *array;
+ array = malloc(sizeof(int)*100);
+ for (int i = 0; i < 100; i++)
+ *(array + i) = i;
+
+ int array2[2];
+
+ printf("%d\n", *(array+1));
+ SDL_Renderer *ren;
+ SDL_Event ev;
+ SDL_Window *win;
+
+ SDL_Init(SDL_INIT_VIDEO);
+ win = SDL_CreateWindow("Ultimecia", 400, 400, 1024, 1024, SDL_WINDOW_SHOWN);
+ ren = SDL_CreateRenderer(win, -1, 0);
+
+ SDL_SetRenderDrawColor(ren, 0xff, 0xff,
+0xff, 0xff);
+ SDL_RenderClear(ren);
+ SDL_RenderPresent(ren);
+
+ while(1) {
+ while(SDL_PollEvent(&ev) != 0) {
+ switch(ev.type) {
+ case SDL_QUIT:
+ SDL_Quit();
+ exit(1);
+
+ case SDL_KEYDOWN:
+ if (ev.key.keysym.sym == SDLK_q) {
+ SDL_Quit();
+ exit(1);
+ } else if (ev.key.keysym.sym == SDLK_a) {
+ SDL_Log("A pressed");
+ }
+ default:
+ goto CPU;
+ }
+ }
+CPU:
+ hi();
+ SDL_Delay(33);
+ }
+
+
+}
diff --git a/src/sr.c b/src/sr.c
@@ -10,13 +10,6 @@
#include "types.h"
#include "sr.h"
-FB fb;
-SDL_Window* win;
-SDL_Renderer* ren;
-vec3f **vertices;
-i32 **faces;
-//SDL_Event ev;
-
ivec2
POSITION_from_gp0(u32 val)
@@ -39,22 +32,6 @@ COLOR_from_gp0(u32 val)
return c;
}
-void
-RENDERER_push_triangle(REN* ren, ivec2 positions[3], C colors[3])
-{
- u8 i;
-
- if (ren->nvertices + 3 > 1) {
- printf("Vertex attribute buffer full, forcing draw...\n");
- //RENDERER_draw();
- }
-
- for (i = 0; i < 3; i++) {
- ren->nvertices++;
- }
-
-}
-
void
FB_flip_vert(u32 *data)
{
@@ -132,8 +109,6 @@ C_new(u32 b)
return c;
};
-v new_fb() { fb.data = malloc(W*H*sizeof(u32));}
-void FB_set(i32 x, i32 y, C c) { (!fb.data || x<0 || y<0 || x>=W || y>=H) ? 0 : memcpy(fb.data + ((x + y * W)), &c, 4); }
void REN_FB_set(REN* ren, i32 x, i32 y, C c) { (!ren->fb || x<0 || y<0 || x>=W || y>=H) ? 0 : memcpy(ren->fb + ((x + y * W)), &c, 4); }
//C*
@@ -143,42 +118,10 @@ void REN_FB_set(REN* ren, i32 x, i32 y, C c) { (!ren->fb || x<0 || y<0 || x>=W |
// return (C*)c;
//}
-void
-line(ivec2 v, ivec2 v1, C color)
-{
-
- i32 x,y,x0,x1,y0,y1,dx,dy,sx,sy,e,e2,p;
- x0 = v.x;
- y0 = v.y;
- x1 = v1.x;
- y1 = v1.y;
-
- dx = abs(x1-x0);
- sx = x0 < x1 ? 1 : -1;
- dy = -abs(y1-y0);
- sy = y0 < y1 ? 1 : -1;
- e = dx + dy;
-
- while (1) {
- FB_set(x0, y0, color);
- if (x0 == x1 && y0 == y1) break;
- e2 = 2 * e;
- if (e2 >= dy) {
- e += dy;
- x0 += sx;
- }
- if (e2 <= dx) {
- e += dx;
- y0 += sy;
- }
- }
-}
-
REN*
REN_new()
{
REN* ren;
- SDL_Init(SDL_INIT_VIDEO);
ren = (REN*)malloc(sizeof(REN));
ren->window = SDL_CreateWindow("Ultimecia", 400, 400, WIN_W, WIN_H, SDL_WINDOW_SHOWN);
ren->renderer = SDL_CreateRenderer(ren->window, -1, 0);
@@ -188,91 +131,15 @@ REN_new()
ren->fb = malloc(W*H*sizeof(u32));
memset(ren->fb, 0, (u32)(W*H)*sizeof(u32));
ren->nvertices = 0;
+ printf("FFFFFFFFFFFFFFFFUCK");
return ren;
}
-i32
-parse_obj(i32 *fcount, i32 *ecount)
-{
- FILE *f;
- char *line;
- char *tokens[128], *p;
- size_t len;
- ssize_t read;
- long pos;
- i32 edge_count, face_count;
-
- f = fopen("african.obj", "rb");
-
- fseek(f, 0, SEEK_END);
- pos = ftell(f);
- fseek(f, 0, SEEK_SET);
-
- line = NULL;
- edge_count = 0;
- face_count = 0;
-
- while ((read = getline(&line, &len, f)) != -1) {
- i32 i;
- i = 0;
- if (strstr(line, "v ") != NULL) {
- for ((p = strtok(line, " ")); p; (p = strtok(NULL, " ")))
- tokens[i++] = p;
-
- vertices = realloc(vertices, (edge_count + 1) * sizeof(vec3f*));
- vertices[edge_count] = malloc(sizeof(vec3f));
-
- vertices[edge_count]->x = atof(tokens[1]);
- vertices[edge_count]->y = atof(tokens[2]);
- vertices[edge_count++]->z = atof(tokens[3]);
- }
-
- if (strstr(line, "f ") != NULL) {
- for ((p = strtok(line, " ")); p; (p = strtok(NULL, " ")))
- tokens[i++] = p;
-
- faces = realloc(faces, (face_count + 1) * sizeof(i32*));
- faces[face_count] = malloc(sizeof(i32)*3);
-
- faces[face_count][0] = atoi(tokens[1]) - 1;
- faces[face_count][1] = atoi(tokens[2]) - 1;
- faces[face_count++][2] = atoi(tokens[3]) - 1;
- }
- }
-
- fclose(f);
- if (line)
- free(line);
-
- *fcount = face_count;
- *ecount = edge_count;
-
- return 1;
-
-}
-
-void
-draw_obj(i32 fcount)
-{
- for (i32 i=0; i<fcount-1; i++) {
- i32 *face = faces[i];
-
- for (i32 j=0; j<3; j++) {
- vec3f *v0 = vertices[face[j]];
- vec3f *v1 = vertices[face[(j+1)%3]];
- i32 x0 = (v0->x+1.)*W/2.;
- i32 y0 = W - ((v0->y+1.)*W/2.);
- i32 x1 = (v1->x+1.)*W/2.;
- i32 y1 = W - ((v1->y+1.)*W/2.);
- line((ivec2){x0, y0}, (ivec2){x1, y1}, C_new(0x3333ffff));
- }
- }
-}
-
void
-triangle(REN* ren, ivec2 t0, ivec2 t1, ivec2 t2, C color) {
+REN_triangle(REN* ren, ivec2 verts[3], C colors[3]) {
// SORT the vertices, t0, t1, t2 lower−to−upper (bubblesort yay!)
- ivec2 temp;
+ ivec2 temp, t0, t1, t2;
+ t0 = verts[0]; t1 = verts[1]; t2 = verts[2];
if (t0.y>t1.y) VEC2I_SWAP(t0, t1);
if (t0.y>t2.y) VEC2I_SWAP(t0, t2);
@@ -304,7 +171,7 @@ triangle(REN* ren, ivec2 t0, ivec2 t1, ivec2 t2, C color) {
if (A.x > B.x) VEC2I_SWAP(A, B);
// Draw horizontal span between A and B
for (i32 x = A.x; x <= B.x; x++) {
- REN_FB_set(ren, x, A.y, color);
+ REN_FB_set(ren, x, A.y, colors[0]);
}
}
}
@@ -314,19 +181,17 @@ REN_push_triangle(REN* ren, ivec2 verts[3], C colors[3])
{
u8 i;
long VERTEX_BUFFER_LEN = 64*1024;
- ivec2 t0[3] = {{10, 70}, {50, 160}, {70, 80}};
+ ivec2 tmp[3];
- if (ren->nvertices + 3 > 1000) {
- printf("Vertex attribute buffer full, forcing draw...\n");
- for(int i = 0; i < ren->nvertices-2; i++) {
- triangle(ren, ren->verts[i], ren->verts[i+1], ren->verts[i+2], ren->colors[i]);
- triangle(ren, ren->verts[i], ren->verts[i+1], ren->verts[i+2], ren->colors[i+1]);
- triangle(ren, ren->verts[i], ren->verts[i+1], ren->verts[i+2], ren->colors[i+2]);
- //triangle(ren, t0[0],t0[1], t0[2], C_new(0xffffffff));//ren->colors[i]);
- //printf("Vert 1 -> x: %d, y: %d\nVert 2 -> x: %d, y: %d\nVert 3 -> x: %d, y: %d\n", ren->verts[i].x, ren->verts[i].y, ren->verts[i+1].x, ren->verts[i+1].y, ren->verts[i+2].x, ren->verts[i+3].y);
- }
+ if (ren->nvertices + 3 > 1000) {
+ printf("Vertex attribute buffer full, forcing draw... and NVERTICES IS %d\n", ren->nvertices);
+ for(int i = 0; i < ren->nvertices-3; i++) {
+ REN_triangle(ren, ren->verts+(i*2), ren->colors);
+ //triangle(ren, t0[0],t0[1], t0[2], C_new(0xffffffff));//ren->colors[i]);
+ //printf("Vert 1 -> x: %d, y: %d\nVert 2 -> x: %d, y: %d\nVert 3 -> x: %d, y: %d\n", ren->verts[i].x, ren->verts[i].y, ren->verts[i+1].x, ren->verts[i+1].y, ren->verts[i+2].x, ren->verts[i+3].y);
+ }
- }
+ }
ren->verts = realloc(ren->verts, sizeof(ivec2) * (ren->nvertices + 3));
ren->colors = realloc(ren->colors, sizeof(C) * (ren->nvertices + 3));
for (i = 0; i < 3; i++) {
diff --git a/src/sr.h b/src/sr.h
@@ -11,8 +11,6 @@
#define VEC2I_SWAP(x, y) { ivec2 temp = x; x = y; y = temp; }
#define I_SWAP(x, y) { int temp = x; x = y; y = temp; }
-typedef void v; typedef uint8_t u8;typedef uint16_t u16;typedef uint32_t u32;typedef uint64_t u64;typedef int8_t i8;typedef int16_t i16;typedef int32_t i32;typedef int64_t i64;typedef uint8_t Bool;
-typedef struct { u32 *data; } FB;
typedef struct {u8 b;u8 g;u8 r; u8 a;} C;
typedef struct { double x, y, z; } point;
typedef struct { int x, y; } ivec2;
@@ -34,9 +32,7 @@ ivec2 IVEC2_op(ivec2, ivec2, enum mop);
ivec2 IVEC2_ops(ivec2, i32, enum mop);
C C_new(u32);
-v new_fb(); //{ fb.data = malloc(W*H*sizeof(u32));}
-void FB_set(i32, i32, C);
-void triangle(REN*, ivec2, ivec2, ivec2, C color);
+void REN_triangle(REN*, ivec2[3], C[3]);
REN* REN_new();
ivec2 POSITION_from_gp0(u32 val);
C COLOR_from_gp0(u32 val);
diff --git a/src/types.h b/src/types.h
@@ -13,4 +13,3 @@ typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
-typedef uint8_t Bool;