Browse Source

added FPS cap

IverMartinson 8 months ago
parent
commit
09ec28f87a

+ 2 - 1
.vscode/settings.json

@@ -8,6 +8,7 @@
         "functional": "c",
         "functional": "c",
         "tuple": "c",
         "tuple": "c",
         "utility": "c",
         "utility": "c",
-        "cl.h": "c"
+        "cl.h": "c",
+        "stdio.h": "c"
     }
     }
 }
 }

BIN
builds/final binaries/example.bin


BIN
builds/final binaries/librasteriver.so


+ 4 - 0
src/RasterIver/headers/rasteriver.h

@@ -13,6 +13,7 @@ typedef enum {
     RI_SUCCESS = 0,
     RI_SUCCESS = 0,
     RI_NOT_RUNNING = -2,
     RI_NOT_RUNNING = -2,
     RI_RUNNING = 1,
     RI_RUNNING = 1,
+    RI_INVALID_FLAG = -3,
 } RI_result_enum;
 } RI_result_enum;
 
 
 // RI_flag
 // RI_flag
@@ -20,6 +21,8 @@ typedef enum {
     RI_FLAG_DEBUG = 0,
     RI_FLAG_DEBUG = 0,
     RI_FLAG_DEBUG_VERBOSE = 1,
     RI_FLAG_DEBUG_VERBOSE = 1,
     RI_FLAG_SHOW_Z_BUFFER = 2,
     RI_FLAG_SHOW_Z_BUFFER = 2,
+    RI_FLAG_SHOW_FPS = 3,
+    RI_FLAG_DEBUG_FPS = 4,
 } RI_flag_enum;
 } RI_flag_enum;
 
 
 RI_result RI_Init();
 RI_result RI_Init();
@@ -30,5 +33,6 @@ RI_result RI_Tick();
 RI_result RI_SetBackground(RI_uint RI_BackgroundColor);
 RI_result RI_SetBackground(RI_uint RI_BackgroundColor);
 RI_result RI_ShowZBuffer(int RI_ShowZBufferFlag);
 RI_result RI_ShowZBuffer(int RI_ShowZBufferFlag);
 RI_result RI_SetFlag(RI_flag RI_FlagToSet, int RI_Value);
 RI_result RI_SetFlag(RI_flag RI_FlagToSet, int RI_Value);
+RI_result RI_SetFpsCap(int RI_FpsCap);
 
 
 #endif // RASTERIVER_H
 #endif // RASTERIVER_H

+ 57 - 3
src/RasterIver/source code/rasteriver.c

@@ -135,8 +135,6 @@ __kernel void raster_kernel(__global float* polygons, __global uint* frame_buffe
     }\
     }\
     frame_buffer[id_y * width + id_x] = frame_pixel; \
     frame_buffer[id_y * width + id_x] = frame_pixel; \
     \
     \
-    frame_buffer[polygon_count] = 0xFFFFFFFF;\
-    \
     if (!show_z_buffer){return;}\
     if (!show_z_buffer){return;}\
     \
     \
     float z = clamp(z_pixel, 0.0f, 800.0f);\
     float z = clamp(z_pixel, 0.0f, 800.0f);\
@@ -162,6 +160,15 @@ int frame = 0;
 
 
 int show_debug = 0;
 int show_debug = 0;
 int debug_verbose = 0;
 int debug_verbose = 0;
+int show_fps = 0;
+int debug_fps = 0;
+
+Uint64 start_time;
+double frame_time_ms;
+double fps;
+double elapsed_ticks;
+double delta_time;
+int fps_cap = -1;
 // ----- Internal Variables
 // ----- Internal Variables
 
 
 // ----- Rendering Vars
 // ----- Rendering Vars
@@ -245,6 +252,16 @@ RI_result RI_IsRunning()
         return RI_NOT_RUNNING;
         return RI_NOT_RUNNING;
     }
     }
 }
 }
+
+RI_result RI_ListFlags(){
+    printf("RI_FLAG_DEBUG: Turns debugging on or off\n");
+    printf("RI_FLAG_DEBUG_VERBOSE: If debugging and verbose is on, print extra data\n");
+    printf("RI_FLAG_SHOW_Z_BUFFER: Whether or not to render the Z buffer");
+    printf("RI_FLAG_SHOW_FPS: Render FPS on screen");
+    printf("RI_FLAG_DEBUG_FPS: Debug FPS into the console");
+
+    return RI_SUCCESS;
+}
 // ----- Value Return Functions
 // ----- Value Return Functions
 
 
 // ----- Set Value Functions
 // ----- Set Value Functions
@@ -264,8 +281,16 @@ RI_result RI_SetFlag(RI_flag RI_FlagToSet, int RI_Value)
         show_z_buffer = RI_Value;
         show_z_buffer = RI_Value;
         break;
         break;
 
 
-    default:
+    case RI_FLAG_SHOW_FPS:
+        show_fps = RI_Value;
         break;
         break;
+
+    case RI_FLAG_DEBUG_FPS:
+        debug_fps = RI_Value;
+        break;
+
+    default:
+        return RI_INVALID_FLAG;
     }
     }
 
 
     return RI_SUCCESS;
     return RI_SUCCESS;
@@ -314,11 +339,21 @@ RI_result RI_RequestPolygons(int RI_PolygonsToRequest){
     
     
     return erchk(error);
     return erchk(error);
 }
 }
+
+RI_result RI_SetFpsCap(int RI_FpsCap){
+    fps_cap = RI_FpsCap;
+
+    return RI_SUCCESS;
+}
 // ----- Set Value Functions
 // ----- Set Value Functions
 
 
 // ----- Renderer Action Functions
 // ----- Renderer Action Functions
 RI_result RI_Tick()
 RI_result RI_Tick()
 {
 {
+    if (show_fps || debug_fps){
+        start_time = SDL_GetPerformanceCounter();
+    }
+    
     debug(1, "Ticking...");
     debug(1, "Ticking...");
     
     
     if (running)
     if (running)
@@ -396,6 +431,25 @@ RI_result RI_Tick()
 
 
         debug(1, "Ticked");
         debug(1, "Ticked");
 
 
+        if (fps_cap > 0 && fps > fps_cap){
+            elapsed_ticks = SDL_GetPerformanceCounter() - start_time;
+            delta_time = elapsed_ticks / (double)SDL_GetPerformanceFrequency();
+            
+            double target_frame_time = 1.0 / fps_cap;
+
+            SDL_Delay((Uint32)((target_frame_time - delta_time) * 1000.0));
+        }
+        
+        if (show_fps || debug_fps){
+            elapsed_ticks = SDL_GetPerformanceCounter() - start_time;
+            delta_time = elapsed_ticks / (double)SDL_GetPerformanceFrequency();
+            fps = 1.0 / delta_time;
+        }
+        
+        if (debug_fps){
+            debug(0, "FPS: %lf (%d polygons, %d pixels)", fps, polygon_count, width * height);
+        }
+
         return RI_SUCCESS;
         return RI_SUCCESS;
     }
     }
     else
     else

+ 3 - 1
src/test programs/example.c

@@ -4,12 +4,14 @@
 int main(){
 int main(){
     RI_SetFlag(RI_FLAG_DEBUG, 1);
     RI_SetFlag(RI_FLAG_DEBUG, 1);
     RI_SetFlag(RI_FLAG_DEBUG_VERBOSE, 0);
     RI_SetFlag(RI_FLAG_DEBUG_VERBOSE, 0);
+    RI_SetFlag(RI_FLAG_DEBUG_FPS, 1);
+    RI_SetFpsCap(60);
     
     
     if (RI_Init(800, 800, "Rasteriver Test") == RI_ERROR){
     if (RI_Init(800, 800, "Rasteriver Test") == RI_ERROR){
         return 1;
         return 1;
     }
     }
 
 
-    RI_RequestPolygons(100000);
+    RI_RequestPolygons(1000);
 
 
     while (RI_IsRunning() == RI_RUNNING){
     while (RI_IsRunning() == RI_RUNNING){
         RI_Tick();
         RI_Tick();