#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
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);
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 {
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 */
#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;
void collision_check_thread_function(){
for(;;){
+ double startime = glfwGetTime();
+
collision_check();
+ double endtime = glfwGetTime();
+
+ g.cctime = endtime - startime;
}
}
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) {
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));
}
}
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)){
/* Not for a particular object */
glfwSwapBuffers(window);
- // usleep(10000);
+ framecount++;
}
g.shutdown = true;
movement_thread.join();
class Projectile : public GameObject {
public:
vector<vec3> velocities;
+ vector<float> times_to_live;
+
Projectile() {
moving = true;
};
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]);