]> isoptera.lcsc.edu Git - engine26/.git/commitdiff
Updates from Thursday's class
authorSeth Long <sslong@lcsc.edu>
Thu, 22 Oct 2026 17:19:42 +0000 (10:19 -0700)
committerSeth Long <sslong@lcsc.edu>
Thu, 22 Oct 2026 17:19:42 +0000 (10:19 -0700)
game.h
load_level.cpp
main.cpp
projectile.h

diff --git a/game.h b/game.h
index 71eedf9acd1d93c7e8ec03436d4fb4ea311a7a2e..72f585ba6669a3b2e733c328778f05d9149bbf90 100644 (file)
--- a/game.h
+++ b/game.h
@@ -1,7 +1,7 @@
 #pragma once
 
 /* Set this to WINDOWS for Windows, UNIX for Linux, BSD, etc. */
-#define WINDOWS
+#define UNIX
 
 /* This is needed to allow fopen, fread, etc. */
 #ifdef WINDOWS
@@ -66,6 +66,10 @@ struct globals {
        bool run = false;
        bool release_cursor = false;
        int on_count = 0;
+       unsigned last_fps = 0;
+       double cctime = 0;
+       double movement_time = 0;
+       double gmtime = 0;
 };
 
 vec3 get_lookat_point(int direction = 0, float r = 0.0);
@@ -111,6 +115,7 @@ class GameObject {
                virtual vec3 closest_point(vec3 other, size_t instance) {return vec3(0, 0, 0);}
                virtual vec3 get_center(size_t instance) {return vec3(0, 0, 0);}
                virtual void report_collision(GameObject *other, size_t other_instance, size_t our_instance) {};
+               virtual bool inside_instance(vec3 other, size_t instance) {return false;}
 };
 
 class SolidObject : public GameObject {
index 82d4b5f1e3375ec4ecdf40275b47ff5c1212e3c7..6576018def9246655d82c3a5fe9f1e972636922e 100644 (file)
@@ -6,8 +6,8 @@ void load_level(){
 
        FloorPanel *floor = (FloorPanel*)add_object(new FloorPanel, "The Floor", true);
        floor->texture_file = "textures/stones.png";
-       for(int i = -90; i < 90; i+= 5)
-               for(int j = -90; j < 90; j+= 5)
+       for(float i = -90; i < 90; i+= 5)
+               for(float j = -90; j < 90; j+= 5)
                        floor->add_location_size(vec3(i, -2, j), vec3(5, 1, 5));
 
        /* New Way */
index 8a8517af9413cb85b0e50a4d1074a030ca6bdf36..18147ae610eca3927aba11a8b232b2516b5d8bea 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1,8 +1,9 @@
 #define MAIN
 #include "game.h"
 
-int  movement_count = 0;
+int movement_count = 0;
 
+void fire();
 void GLAPIENTRY debug_callback(GLenum, GLenum type, GLuint,    GLenum severity, GLsizei, const GLchar* message, const void*){
        if(severity != GL_DEBUG_SEVERITY_HIGH && severity != GL_DEBUG_SEVERITY_MEDIUM )
                return;
@@ -64,7 +65,12 @@ void collision_check(){
 
 void collision_check_thread_function(){
        for(;;){
+               double startime = glfwGetTime();
+
                collision_check();
+               double endtime = glfwGetTime();
+               
+               g.cctime = endtime - startime;
        }
 }
 
@@ -118,6 +124,7 @@ void do_move_if_clear(vec3 step_to_point){
 
 void general_management_function(){
        while(!g.shutdown) { 
+               double start_time = glfwGetTime();
                /* Move the player due to keys, physics, etc */
                vec3 step_to_point = vec3(1000000, 1000000, 1000000);
                if(g.forward) {
@@ -153,22 +160,49 @@ void general_management_function(){
                        g.on_count++;
                }
 
-               this_thread::sleep_for(chrono::milliseconds(10));       
+               double end_time = glfwGetTime();
+               double sleep_seconds = 0.01 - (end_time - start_time);
+               unsigned long sleep_microseconds = sleep_seconds * 1000000.0;
+               g.gmtime = end_time - start_time;
+               this_thread::sleep_for(chrono::microseconds(sleep_microseconds));       
        }
 
 }
 
 void movement_function() {
        /* We might need a shutdown procedure that ends this loop */
+       double last_start = glfwGetTime();
        while(!g.shutdown) { 
+               double start_time = glfwGetTime();
                for(auto o : game_objects)
-                       o->movement(10);
+                       o->movement(1000 * (start_time - last_start));
+               last_start = start_time;
                
+               double end_time = glfwGetTime();
                movement_count++;
 
+               double sleep_seconds = 0.01 - (end_time - start_time);
+               unsigned long sleep_microseconds = sleep_seconds * 1000000.0;
+               this_thread::sleep_for(chrono::microseconds(sleep_microseconds));       
+       }
+}
 
-               /* TODO:  Sleep for 10 ms - the time this iteration took */
-               this_thread::sleep_for(chrono::milliseconds(10));       
+unsigned framecount = 0;
+void stats_function() {
+       for(;;){
+               g.last_fps = framecount;
+               framecount = 0;
+               g.movement_time = 1.0 / (double)movement_count;
+               movement_count = 0;
+               printf("FPS:  %u        cctime = %lf    movement_time = %lf     gmtime = %lf\n", g.last_fps, g.cctime, g.movement_time, g.gmtime);
+               for(auto proj : game_objects) {
+                       if(proj->name == "Projectiles") {
+                               printf("Projectile count:  %lu\n", ((Projectile*)proj)->models.size());
+                               break;
+                       }
+               }
+               
+               this_thread::sleep_for(chrono::milliseconds(1000));     
        }
 }
 
@@ -303,6 +337,7 @@ int main(int argc, char ** argv){
        thread movement_thread(movement_function);
        thread general_management_thread(general_management_function);
        thread collision_check_thread(collision_check_thread_function);
+       thread stats_thread(stats_function);
 
        glEnable(GL_DEPTH_TEST);
        while(!glfwWindowShouldClose(window)){
@@ -323,7 +358,7 @@ int main(int argc, char ** argv){
 
                /* Not for a particular object */
                glfwSwapBuffers(window);
-               //              usleep(10000);
+               framecount++;
        }
        g.shutdown = true;
        movement_thread.join();
index 033d1bd8f10ff15365469808d9d655a720a95205..7e03a83e622ecec7ec460c7000b142f39eec4f18 100644 (file)
@@ -5,6 +5,8 @@
 class Projectile : public GameObject {
 public:
        vector<vec3> velocities;
+       vector<float> times_to_live;
+       
        Projectile() {
                moving = true;
        };
@@ -17,17 +19,34 @@ public:
                float vy = sin(g.elevation);
                /* The number we multiply the vector by on the next line is the speed */
                velocities.push_back(vec3(vx, vy, vz) * 0.8f);
+               times_to_live.push_back(2000);
        }
        void movement(float time) {
-               for(size_t i = 0; i < models.size(); i++)
+               for(size_t i = 0; i < models.size(); i++) {
                        models[i] = translate(models[i], velocities[i]);
+                       times_to_live[i] -= time;
+                       printf("times to live[i] = %f\n", times_to_live[i]);
+                       if(times_to_live[i] < 0){
+                               models.erase(models.begin() + i);
+                               velocities.erase(velocities.begin() + i);
+                               times_to_live.erase(times_to_live.begin() + i);
+                       }
+               }
        }
        /* Required for collision detection */
        float fast_check_distance(size_t instance) {return 1;}
        vec3 closest_point(vec3 other, size_t instance) {
                vec3 cmpos = get_center(instance);
+               if(distance(cmpos, other) < 1) // Contains sqrt
+                       return other;
                return cmpos - normalize(cmpos - other);
        }
+       vec3 inside_instance(vec3 other, size_t instance) {
+               vec3 cmpos = get_center(instance);
+               if(distance(cmpos, other) < 1) // Contains sqrt
+                       return true;
+               return false;
+       }
        vec3 get_center(size_t instance) {
                mat4 m = models[instance]; // The optimizer can deal with it
                return vec3(m[3][0], m[3][1], m[3][2]);