Changeset 5527206 in opengl-game


Ignore:
Timestamp:
Jun 22, 2018, 5:07:15 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
dba67b2
Parents:
c94a699
Message:

Add a random number generating function and continuously spawn asteroids at random positions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rc94a699 r5527206  
    2525
    2626#include <cstdio>
     27#include <cstdlib>
     28#include <ctime>
    2729#include <iostream>
    2830#include <fstream>
     
    7274};
    7375
    74 #define NUM_KEYS (512)
    75 #define ONE_DEG_IN_RAD ((2.0f * M_PI) / 360.0f) // 0.017444444 (maybe make this a const instead)
    76 
    77 const int KEY_STATE_UNCHANGED = -1;
    78 const bool FULLSCREEN = false;
    79 const bool SHOW_FPS = false;
    80 const bool DISABLE_VSYNC = false; // disable vsync to see real framerate
    81 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
    82 
    83 int key_state[NUM_KEYS];
    84 bool key_pressed[NUM_KEYS];
    85 
    86 int width = 640;
    87 int height = 480;
    88 
    89 double fps;
    90 
    91 vec3 cam_pos;
    92 
    93 mat4 view_mat;
    94 mat4 proj_mat;
    95 
    96 // TODO: Consider using a list instead since it will make element deletion more efficient
    97 vector<SceneObject> objects;
    98 queue<Event> events;
    99 
    100 SceneObject* clickedObject = NULL;
    101 SceneObject* selectedObject = NULL;
    102 
    103 float NEAR_CLIP = 0.1f;
    104 float FAR_CLIP = 100.0f;
    105 
    106 // Should really have some array or struct of UI-related variables
    107 bool isRunning = true;
    108 
    109 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
    110 
    11176void glfw_error_callback(int error, const char* description);
    11277
     
    188153                  GLuint model_mat_idx_vbo);
    189154
     155float getRandomNum(float low, float high);
     156
     157#define NUM_KEYS (512)
     158#define ONE_DEG_IN_RAD ((2.0f * M_PI) / 360.0f) // 0.017444444 (maybe make this a const instead)
     159
     160const int KEY_STATE_UNCHANGED = -1;
     161const bool FULLSCREEN = false;
     162const bool SHOW_FPS = false;
     163const bool DISABLE_VSYNC = false; // disable vsync to see real framerate
     164unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
     165
     166int key_state[NUM_KEYS];
     167bool key_pressed[NUM_KEYS];
     168
     169int width = 640;
     170int height = 480;
     171
     172double fps;
     173
     174vec3 cam_pos;
     175
     176mat4 view_mat;
     177mat4 proj_mat;
     178
     179// TODO: Consider using a list instead since it will make element deletion more efficient
     180vector<SceneObject> objects;
     181queue<Event> events;
     182
     183SceneObject* clickedObject = NULL;
     184SceneObject* selectedObject = NULL;
     185
     186float NEAR_CLIP = 0.1f;
     187float FAR_CLIP = 100.0f;
     188
     189// Should really have some array or struct of UI-related variables
     190bool isRunning = true;
     191
     192ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
     193
    190194int main(int argc, char* argv[]) {
    191195   cout << "New OpenGL Game" << endl;
     
    231235   glewExperimental = GL_TRUE;
    232236   glewInit();
     237
     238   srand(time(0));
    233239
    234240   /*
     
    816822      model_mat_idx_vbo);
    817823
     824   /*
    818825   spawnAsteroid(vec3(0.0f, -1.2f, -21.5f), color_sp,
    819826      shaderBufferInfo,
     
    861868      ubo,
    862869      model_mat_idx_vbo);
     870   */
    863871
    864872   GLuint vao = 0;
     
    964972   int frame_count = 0;
    965973   double elapsed_seconds_fps = 0.0f;
     974   double elapsed_seconds_spawn = 0.0f;
    966975   double previous_seconds = glfwGetTime();
    967976
     
    9911000
    9921001         frame_count++;
     1002      }
     1003
     1004      elapsed_seconds_spawn += elapsed_seconds;
     1005      if (elapsed_seconds_spawn > 0.5f) {
     1006         spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp,
     1007            shaderBufferInfo,
     1008            points_vbo,
     1009            colors_vbo,
     1010            selected_colors_vbo,
     1011            texcoords_vbo,
     1012            normals_vbo,
     1013            ubo,
     1014            model_mat_idx_vbo);
     1015
     1016         elapsed_seconds_spawn = 0.0f;
    9931017      }
    9941018
     
    10491073         }
    10501074
    1051          if (key_pressed[GLFW_KEY_DOWN]) {
    1052             transformObject(objects[1], translate(mat4(), vec3(0.0f, 0.0f, 0.03f)), ubo);
    1053             transformObject(objects[2], translate(mat4(), vec3(0.0f, 0.0f, 0.06f)), ubo);
    1054             transformObject(objects[3], translate(mat4(), vec3(0.0f, 0.0f, 0.04f)), ubo);
    1055             transformObject(objects[4], translate(mat4(), vec3(0.0f, 0.0f, 0.06f)), ubo);
    1056             transformObject(objects[5], translate(mat4(), vec3(0.0f, 0.0f, 0.04f)), ubo);
     1075         for (int i = 1; i < objects.size(); i++) {
     1076            if (!objects[i].deleted) {
     1077               transformObject(objects[i], translate(mat4(), vec3(0.0f, 0.0f, 0.04f)), ubo);
     1078            }
    10571079         }
    10581080
     
    18711893                  model_mat_idx_vbo);
    18721894}
     1895
     1896float getRandomNum(float low, float high) {
     1897   return low + ((float)rand()/RAND_MAX) * (high-low);
     1898}
Note: See TracChangeset for help on using the changeset viewer.