Browse Source

Implement the capability to make screenshots

rexim 3 years ago
parent
commit
9cb042e620
3 changed files with 38 additions and 8 deletions
  1. 2 0
      .gitignore
  2. 17 7
      README.md
  3. 19 1
      main.c

+ 2 - 0
.gitignore

@@ -2,3 +2,5 @@ main
 Dependencies
 Dependencies
 *.exe
 *.exe
 *.obj
 *.obj
+*.swp
+screenshot.png

+ 17 - 7
README.md

@@ -4,17 +4,27 @@ Just a simple OpenGL template that I use on my streams.
 
 
 ## Controls
 ## Controls
 
 
-| Shortcut                 | Description                                                                                                                                                               |
-|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| <kbd>q</kbd>             | Quit                                                                                                                                                                      |
-| <kbd>F5</kbd>            | Reload [main.frag](./main.frag) and [main.vert](./main.vert) shaders. Red screen indicates a compilation or linking error, check the output of the program if you see it. |
-| <kbd>SPACE</kbd>         | Pause/unpause the time uniform variable in shaders                                                                                                                        |
-| <kbd>←</kbd><kbd>→</kbd> | In pause mode step back/forth in time.                                                                                                                                    |
+| Shortcut                 | Description                                                                                                                                            |
+|--------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
+| <kbd>q</kbd>             | Quit                                                                                                                                                   |
+| <kbd>F5</kbd>            | Reload [render.conf](./render.conf) and all the resources refered by it. Red screen indicates an error, check the output of the program if you see it. |
+| <kbd>F6</kbd>            | Make a screenshot.                                                                                                                                     |
+| <kbd>SPACE</kbd>         | Pause/unpause the time uniform variable in shaders                                                                                                     |
+| <kbd>←</kbd><kbd>→</kbd> | In pause mode step back/forth in time.                                                                                                                 |
 
 
-## Uniforms
+## [render.conf](./render.conf) keys
+
+| Key     | Description                 |
+|---------|-----------------------------|
+| vert    | Path to the vertex shader   |
+| frag    | Path to the fragment shader |
+| texture | Path to the texture         |
+
+## Shader Uniforms
 
 
 | Name         | Type    | Description                                                                          |
 | Name         | Type    | Description                                                                          |
 |--------------|---------|--------------------------------------------------------------------------------------|
 |--------------|---------|--------------------------------------------------------------------------------------|
 | `resolution` | `vec2`  | Current resolution of the screen in pixels                                           |
 | `resolution` | `vec2`  | Current resolution of the screen in pixels                                           |
 | `time`       | `float` | Amount of time passed since the beginning of the application when it was not paused. |
 | `time`       | `float` | Amount of time passed since the beginning of the application when it was not paused. |
 | `mouse`      | `vec2`  | Position of the mouse on the screen in pixels                                        |
 | `mouse`      | `vec2`  | Position of the mouse on the screen in pixels                                        |
+

+ 19 - 1
main.c

@@ -25,6 +25,9 @@
 #define STB_IMAGE_IMPLEMENTATION
 #define STB_IMAGE_IMPLEMENTATION
 #include "stb_image.h"
 #include "stb_image.h"
 
 
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#include "stb_image_write.h"
+
 char *slurp_file_into_malloced_cstr(const char *file_path)
 char *slurp_file_into_malloced_cstr(const char *file_path)
 {
 {
     FILE *f = NULL;
     FILE *f = NULL;
@@ -374,7 +377,6 @@ void renderer_reload_shaders(Renderer *r)
 
 
 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
 {
 {
-    (void) window;
     (void) scancode;
     (void) scancode;
     (void) action;
     (void) action;
     (void) mods;
     (void) mods;
@@ -384,6 +386,22 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
             reload_render_conf("render.conf");
             reload_render_conf("render.conf");
             renderer_reload_textures(&global_renderer);
             renderer_reload_textures(&global_renderer);
             renderer_reload_shaders(&global_renderer);
             renderer_reload_shaders(&global_renderer);
+        } else if (key == GLFW_KEY_F6) {
+#define SCREENSHOT_PNG_PATH "screenshot.png"
+            printf("Saving the screenshot at %s\n", SCREENSHOT_PNG_PATH);
+            int width, height;
+            glfwGetWindowSize(window, &width, &height);
+            void *pixels = malloc(4 * width * height);
+            if (pixels == NULL) {
+                fprintf(stderr, "ERROR: could not allocate memory for pixels to make a screenshot: %s\n",
+                        strerror(errno));
+                return;
+            }
+            glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+            if (!stbi_write_png(SCREENSHOT_PNG_PATH, width, height, 4, pixels, width * 4)) {
+                fprintf(stderr, "ERROR: could not save %s: %s\n", SCREENSHOT_PNG_PATH, strerror(errno));
+            }
+            free(pixels);
         } else if (key == GLFW_KEY_SPACE) {
         } else if (key == GLFW_KEY_SPACE) {
             pause = !pause;
             pause = !pause;
         } else if (key == GLFW_KEY_Q) {
         } else if (key == GLFW_KEY_Q) {