Changeset a2f62d7 in opengl-game for new-game.cpp
- Timestamp:
- Dec 16, 2020, 2:54:13 AM (4 years ago)
- Branches:
- feature/imgui-sdl, master
- Children:
- c324d6a
- Parents:
- e68d549
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
re68d549 ra2f62d7 172 172 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); 173 173 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 174 void window_size_callback(GLFWwindow* window, int width, int height);174 void framebuffer_size_callback(GLFWwindow* window, int width, int height); 175 175 176 176 void APIENTRY debugGlCallback( … … 262 262 263 263 /*** START OF REFACTORED CODE ***/ 264 int windowWidth = 640;265 int windowHeight = 480;264 unsigned int windowWidth = 640; 265 unsigned int windowHeight = 480; 266 266 267 267 vec3 cam_pos; … … 333 333 } 334 334 335 // Currently, this is just for IMGUI 335 336 string glsl_version = "#version 410"; 336 337 … … 345 346 #endif 346 347 347 GLFWwindow* window = NULL;348 348 GLFWmonitor* mon = NULL; 349 350 glfwWindowHint(GLFW_SAMPLES, 16);351 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);352 349 353 350 if (FULLSCREEN) { … … 359 356 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl; 360 357 } 361 window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL); 362 363 if (!window) { 358 359 glfwWindowHint(GLFW_SAMPLES, 16); 360 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); 361 362 GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL); 363 364 if (window == NULL) { 364 365 gl_log_err("ERROR: could not open window with GLFW3"); 365 366 cerr << "ERROR: could not open window with GLFW3" << endl; … … 370 371 371 372 glfwMakeContextCurrent(window); 372 glViewport(0, 0, windowWidth, windowHeight);373 373 374 374 glewExperimental = GL_TRUE; … … 378 378 cout << "FOUND GLEW debug extension" << endl; 379 379 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); 380 glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, nullptr);380 glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, NULL); 381 381 cout << "Bound debug callback" << endl; 382 382 } else { 383 383 cout << "OpenGL debug message callback is not supported" << endl; 384 384 } 385 386 glfwSetMouseButtonCallback(window, mouse_button_callback); 387 glfwSetKeyCallback(window, key_callback); 388 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 385 389 386 390 srand(time(0)); … … 418 422 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 419 423 424 // Setup Platform/Renderer backends 425 ImGui_ImplGlfw_InitForOpenGL(window, true); 426 ImGui_ImplOpenGL3_Init(glsl_version.c_str()); 427 420 428 // Setup Dear ImGui style 421 429 ImGui::StyleColorsDark(); 422 430 //ImGui::StyleColorsClassic(); 423 431 424 // Setup Platform/Renderer backends425 ImGui_ImplGlfw_InitForOpenGL(window, true);426 ImGui_ImplOpenGL3_Init(glsl_version.c_str());427 428 glfwSetMouseButtonCallback(window, mouse_button_callback);429 glfwSetKeyCallback(window, key_callback);430 glfwSetWindowSizeCallback(window, window_size_callback);431 432 /*** END OF REFACTORED CODE ***/ 432 433 … … 1000 1001 } 1001 1002 1003 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) { 1004 delete* it; 1005 } 1006 1002 1007 ImGui_ImplOpenGL3_Shutdown(); 1003 1008 ImGui_ImplGlfw_Shutdown(); … … 1008 1013 1009 1014 close_log(); 1010 1011 // free memory1012 1013 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {1014 delete *it;1015 }1016 1015 1017 1016 return 0; … … 1084 1083 1085 1084 /*** START OF REFACTORED CODE ***/ 1086 void window_size_callback(GLFWwindow* window, int width, int height) {1085 void framebuffer_size_callback(GLFWwindow* window, int width, int height) { 1087 1086 cout << "Window resized to (" << width << ", " << height << ")" << endl; 1088 1087 1089 1088 windowWidth = width; 1090 1089 windowHeight = height; 1090 1091 // make sure the viewport matches the new window dimensions; note that width and 1092 // height will be significantly larger than specified on retina displays. 1093 glViewport(0, 0, width, height); 1091 1094 1092 1095 // TODO: Ideally, remove the window title bar when the window is maximized … … 1241 1244 1242 1245 unsigned char* loadImage(string file_name, int* x, int* y) { 1243 int n; 1244 int force_channels = 4; // This forces RGBA (4 bytes per pixel) 1245 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels); 1246 1247 int width_in_bytes = *x * 4; 1248 unsigned char *top = NULL; 1249 unsigned char *bottom = NULL; 1250 unsigned char temp = 0; 1251 int half_height = *y / 2; 1252 1253 // flip image upside-down to account for OpenGL treating lower-left as (0, 0) 1254 for (int row = 0; row < half_height; row++) { 1255 top = image_data + row * width_in_bytes; 1256 bottom = image_data + (*y - row - 1) * width_in_bytes; 1257 for (int col = 0; col < width_in_bytes; col++) { 1258 temp = *top; 1259 *top = *bottom; 1260 *bottom = temp; 1261 top++; 1262 bottom++; 1263 } 1264 } 1265 1266 if (!image_data) { 1267 gl_log_err("ERROR: could not load %s", file_name.c_str()); 1268 cerr << "ERROR: could not load " << file_name << endl; 1269 get_log() << "ERROR: could not load " << file_name << endl; 1270 } 1271 1272 // Not Power-of-2 check 1273 if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) { 1274 gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str()); 1275 cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl; 1276 get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl; 1277 } 1278 1279 return image_data; 1246 int n; 1247 int force_channels = 4; // This forces RGBA (4 bytes per pixel) 1248 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels); 1249 1250 int width_in_bytes = *x * 4; 1251 unsigned char *top = NULL; 1252 unsigned char *bottom = NULL; 1253 unsigned char temp = 0; 1254 int half_height = *y / 2; 1255 1256 // TODO: I should be able to use the line below instead of doing the flip 1257 // stbi_set_flip_vertically_on_load(true); 1258 1259 // flip image upside-down to account for OpenGL treating lower-left as (0, 0) 1260 for (int row = 0; row < half_height; row++) { 1261 top = image_data + row * width_in_bytes; 1262 bottom = image_data + (*y - row - 1) * width_in_bytes; 1263 for (int col = 0; col < width_in_bytes; col++) { 1264 temp = *top; 1265 *top = *bottom; 1266 *bottom = temp; 1267 top++; 1268 bottom++; 1269 } 1270 } 1271 1272 if (!image_data) { 1273 gl_log_err("ERROR: could not load %s", file_name.c_str()); 1274 cerr << "ERROR: could not load " << file_name << endl; 1275 get_log() << "ERROR: could not load " << file_name << endl; 1276 } 1277 1278 // Not Power-of-2 check 1279 if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) { 1280 gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str()); 1281 cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl; 1282 get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl; 1283 } 1284 1285 return image_data; 1280 1286 } 1281 1287 /*** END OF REFACTORED CODE ***/ … … 2373 2379 2374 2380 void renderScene(map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) { 2375 2376 2381 glUseProgram(modelGroups[ObjectType::SHIP].shaderProgram); 2377 2382 glBindVertexArray(modelGroups[ObjectType::SHIP].vao);
Note:
See TracChangeset
for help on using the changeset viewer.