Browse Source

basic phys, debug renderer

Iver 2 weeks ago
parent
commit
b6c6c6ac01
4 changed files with 227 additions and 8 deletions
  1. 7 7
      Makefile
  2. 5 0
      changelog.txt
  3. 37 0
      src/launch program/main.c
  4. 178 1
      src/main/main.c

+ 7 - 7
Makefile

@@ -1,16 +1,16 @@
 COMPILER=gcc
 COMPILER=gcc
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
-FLAGS_EXAMPLE=-Lbuilds/ -lNAMEHERE -lLIBNAMEHERE -Wl,-rpath=builds/ -lm
-FLAGS_LIB=-fPIC -shared -lc -lm 
+FLAGS_EXAMPLE=-Lbuilds/ -lfizzix -Wl,-rpath=builds/ -lm
+FLAGS_LIB=-fPIC -shared -lc -lm -lSDL2
 
 
-main.bin: NAMEHERE.so LIBNAMEHERE.so
+main.bin: libfizzix.so # LIBNAMEHERE.so
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 
 
-NAMEHERE.so:
-	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/librasteriver.so $(FLAGS_LIB) 
+libfizzix.so:
+	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/libfizzix.so $(FLAGS_LIB) 
 
 
-LIBNAMEHERE.so:
-	cp src/libraries/libLIBNAMEHERE.so builds/
+# LIBNAMEHERE.so:
+# 	cp src/libraries/libLIBNAMEHERE.so builds/
 
 
 clean:
 clean:
 	rm builds/*
 	rm builds/*

+ 5 - 0
changelog.txt

@@ -0,0 +1,5 @@
+-defualt constructors for new_something funcs
+-basic nessecary functions
+-headers
+-debug renderer
+-basic 2d euler physics (velocity angular velocity)

+ 37 - 0
src/launch program/main.c

@@ -1,3 +1,40 @@
+#include "../headers/fizzix.h"
+
 int main(){
 int main(){
+    FZ_init();
+
+    FZ_context* context = FZ_get_context();
+
+    FZ_scene* scene = FZ_new_scene();
+
+    FZ_shape* shape_1 = FZ_new_shape();
+    FZ_shape* shape_2 = FZ_new_shape();
+
+    scene->shapes = malloc(sizeof(FZ_shape*) * 2);
+
+    scene->shapes[0] = shape_1;
+    scene->shapes[1] = shape_2;
+
+    shape_2->flags = FZ_SHAPE_IS_STATIC;
+
+    shape_1->scale = (FZ_vector_2){50, 50};
+    shape_2->scale = (FZ_vector_2){190, 40};
+
+    shape_1->position = (FZ_vector_2){0, 150};
+    shape_2->position = (FZ_vector_2){0, -150};
+
+
+    scene->shape_count = 2;
+
+    shape_1->velocity = (FZ_vector_2){0.01, .02};
+    
+    shape_1->angular_veclocity = 0.001;
+    shape_2->angular_veclocity = 0.002;
+
+    while (context->is_running){
+        FZ_tick(scene);
+        FZ_render_debug(scene);
+    }
+
     return 0;
     return 0;
 }
 }

+ 178 - 1
src/main/main.c

@@ -1,3 +1,180 @@
-int main(){
+#include "../headers/fizzix.h"
+#include <stdio.h>
+
+FZ_context context;
+
+FZ_shape* FZ_new_shape(){
+    FZ_shape* new_shape = malloc(sizeof(FZ_shape));
+
+    // default transforms
+    new_shape->position = (FZ_vector_2){0, 0};
+    new_shape->velocity = (FZ_vector_2){0, 0};
+    new_shape->angle = 0;
+    new_shape->angular_veclocity = 0;
+
+    // default otha stuff
+    new_shape->flags = 0;
+    new_shape->mass = 1;
+    new_shape->moment_of_intertia = 1;
+    new_shape->point_count = 4;
+    new_shape->points = malloc(sizeof(FZ_vector_2) * 4);
+    new_shape->transformed_points = malloc(sizeof(FZ_vector_2) * 4);
+    new_shape->points[0] = (FZ_vector_2){-1, -1};
+    new_shape->points[1] = (FZ_vector_2){-1, 1};
+    new_shape->points[2] = (FZ_vector_2){1, 1};
+    new_shape->points[3] = (FZ_vector_2){1, -1};
+    new_shape->scale = (FZ_vector_2){1, 1};
+    new_shape->tick_transform_function = NULL;
+    new_shape->tick_points_function = NULL;
+
+    return new_shape;
+}
+
+FZ_scene* FZ_new_scene(){
+    FZ_scene* new_scene = malloc(sizeof(FZ_scene));
+
+    new_scene->shape_count = 0;
+    new_scene->shapes = NULL;
+    new_scene->gravity = (FZ_vector_2){0, -9.81};
+
+    return new_scene;
+}
+
+int FZ_tick(FZ_scene* scene){
+    for (int i = 0; i < scene->shape_count; i++){
+        FZ_shape* shape = scene->shapes[i];
+
+        if (!(shape->flags & FZ_SHAPE_IS_STATIC)){
+            shape->velocity = v2_ew_add(shape->velocity, scene->gravity);
+
+            shape->position = v2_ew_add(shape->position, shape->velocity);
+            shape->angle += shape->angular_veclocity;
+        }
+
+        for (int j = 0; j < shape->point_count; j++){
+            shape->transformed_points[j] = v2_ew_add(
+                v2_rot(v2_ew_mul(shape->points[j], shape->scale), (FZ_vector_2){0, 0}, shape->angle),
+                shape->position
+            );
+        }
+
+        // printf("%f %f\n", shape->transformed_points[0].x, shape->transformed_points[0].y);
+    }
+
+    
+    return 0;
+}
+
+void draw_line(FZ_vector_2 point_a, FZ_vector_2 point_b, uint32_t color){
+    int num_pixels = (int)distance_2(point_a, point_b);
+
+    for (int i = 0; i < num_pixels; ++i){
+        FZ_vector_2 point_2_draw = vector_2_lerp(point_a, point_b, (double)i / (double)num_pixels);
+
+        point_2_draw.x += (double)context.sdl.half_width;
+        point_2_draw.y += (double)context.sdl.half_height;
+
+        if (point_2_draw.x < 0 || point_2_draw.x >= (double)context.sdl.width || point_2_draw.y < 0 || point_2_draw.y >= (double)context.sdl.height) continue;
+
+        context.sdl.frame_buffer[(int)((int)(context.sdl.height - point_2_draw.y) * (double)context.sdl.width + point_2_draw.x)] = color;
+    }
+}
+
+int FZ_render_debug(FZ_scene* scene){
+    for (int i = 0; i < context.sdl.width * context.sdl.height; i++){
+        context.sdl.frame_buffer[i] = 0x333333FF;
+    }
+    
+    for (int i = 0; i < scene->shape_count; i++){
+        FZ_shape* shape = scene->shapes[i];
+
+        
+        for (int j = 0; j < shape->point_count; j++){
+            draw_line(shape->transformed_points[j], j < shape->point_count - 1 ? shape->transformed_points[j + 1] : shape->transformed_points[0], 0x00FF00FF);
+        }
+        
+        for (int j = 0; j < shape->point_count; j++){
+            double screen_x = shape->transformed_points[j].x + context.sdl.half_width;
+            double screen_y = shape->transformed_points[j].y + context.sdl.half_height;
+            
+            if (screen_x < 0 || screen_x >= context.sdl.width || 
+                screen_y < 0 || screen_y >= context.sdl.height) continue;
+            
+            int index = (int)(context.sdl.width * (int)(context.sdl.height - screen_y) + screen_x);
+            context.sdl.frame_buffer[index] = 0xFFFFFFFF;
+        }
+    }
+
+    SDL_Event event;
+
+    while (SDL_PollEvent(&event)){
+        switch (event.type){
+            case SDL_QUIT: {
+                context.is_running = 0;
+
+                break;
+            }
+
+            default: {
+                break;
+            }
+        }
+    }
+
+    int pitch = 500;
+
+    SDL_LockTexture(
+        context.sdl.frame_buffer_texture, 
+        NULL, 
+        (void*)&context.sdl.frame_buffer, 
+        &pitch
+    );
+
+    SDL_UnlockTexture(context.sdl.frame_buffer_texture);
+
+    SDL_RenderCopy(context.sdl.renderer, context.sdl.frame_buffer_texture, NULL, NULL);
+    SDL_RenderPresent(context.sdl.renderer);
+
+    return 0;
+}
+
+FZ_context* FZ_get_context(){
+    return &context;
+}
+
+int FZ_init(){
+    SDL_Init(0);
+
+    context.flags = 0;
+
+    context.sdl.width = 500;
+    context.sdl.height = 500;
+
+    context.sdl.half_width = context.sdl.width / 2;
+    context.sdl.half_height = context.sdl.height / 2;
+    
+    // init SDL
+    context.sdl.window = SDL_CreateWindow(
+        "FIZZIX DEBUG", 
+        SDL_WINDOWPOS_CENTERED, 
+        SDL_WINDOWPOS_CENTERED, 
+        context.sdl.width, 
+        context.sdl.height, 0
+    );
+    context.sdl.renderer = SDL_CreateRenderer(context.sdl.window, -1, SDL_RENDERER_ACCELERATED);
+    context.sdl.frame_buffer_texture = SDL_CreateTexture(
+        context.sdl.renderer, 
+        SDL_PIXELFORMAT_BGRA8888, 
+        SDL_TEXTUREACCESS_STREAMING, 
+        context.sdl.width, 
+        context.sdl.height
+    );
+
+    context.sdl.frame_buffer = malloc(
+        sizeof(uint32_t) * context.sdl.width * context.sdl.height
+    );
+
+    context.is_running = 1;
+
     return 0;
     return 0;
 }
 }