瀏覽代碼

Move camera state inside of Simple_Renderer

rexim 2 年之前
父節點
當前提交
6757aaea5e
共有 3 個文件被更改,包括 20 次插入17 次删除
  1. 12 17
      src/main.c
  2. 2 0
      src/simple_renderer.c
  3. 6 0
      src/simple_renderer.h

+ 12 - 17
src/main.c

@@ -26,13 +26,6 @@
 #define FPS 60
 #define DELTA_TIME (1.0f / FPS)
 
-Editor editor = {0};
-Uint32 last_stroke = 0;
-Vec2f camera_pos = {0};
-float camera_scale = 3.0f;
-float camera_scale_vel = 0.0f;
-Vec2f camera_vel = {0};
-
 void usage(FILE *stream)
 {
     fprintf(stream, "Usage: te [FILE-PATH]\n");
@@ -65,6 +58,8 @@ void MessageCallback(GLenum source,
 
 static Free_Glyph_Atlas atlas = {0};
 static Simple_Renderer sr = {0};
+static Editor editor = {0};
+static Uint32 last_stroke = 0;
 
 #define FREE_GLYPH_FONT_SIZE 64
 #define ZOOM_OUT_GLYPH_THRESHOLD 30
@@ -82,8 +77,8 @@ void render_editor(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer
     simple_renderer_set_shader(sr, SHADER_FOR_EPICNESS);
     glUniform2f(sr->uniforms[UNIFORM_SLOT_RESOLUTION], (float) w, (float) h);
     glUniform1f(sr->uniforms[UNIFORM_SLOT_TIME], (float) SDL_GetTicks() / 1000.0f);
-    glUniform2f(sr->uniforms[UNIFORM_SLOT_CAMERA_POS], camera_pos.x, camera_pos.y);
-    glUniform1f(sr->uniforms[UNIFORM_SLOT_CAMERA_SCALE], camera_scale);
+    glUniform2f(sr->uniforms[UNIFORM_SLOT_CAMERA_POS], sr->camera_pos.x, sr->camera_pos.y);
+    glUniform1f(sr->uniforms[UNIFORM_SLOT_CAMERA_SCALE], sr->camera_scale);
     {
         for (size_t row = 0; row < editor->lines.count; ++row) {
             Line line = editor->lines.items[row];
@@ -121,8 +116,8 @@ void render_editor(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer
     simple_renderer_set_shader(sr, SHADER_FOR_COLOR);
     glUniform2f(sr->uniforms[UNIFORM_SLOT_RESOLUTION], (float) w, (float) h);
     glUniform1f(sr->uniforms[UNIFORM_SLOT_TIME], (float) SDL_GetTicks() / 1000.0f);
-    glUniform2f(sr->uniforms[UNIFORM_SLOT_CAMERA_POS], camera_pos.x, camera_pos.y);
-    glUniform1f(sr->uniforms[UNIFORM_SLOT_CAMERA_SCALE], camera_scale);
+    glUniform2f(sr->uniforms[UNIFORM_SLOT_CAMERA_POS], sr->camera_pos.x, sr->camera_pos.y);
+    glUniform1f(sr->uniforms[UNIFORM_SLOT_CAMERA_SCALE], sr->camera_scale);
     {
         float CURSOR_WIDTH = 5.0f;
         Uint32 CURSOR_BLINK_THRESHOLD = 500;
@@ -151,13 +146,13 @@ void render_editor(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer
             target_scale = 3.0f;
         }
 
-        camera_vel = vec2f_mul(
-                         vec2f_sub(cursor_pos, camera_pos),
-                         vec2fs(2.0f));
-        camera_scale_vel = (target_scale - camera_scale) * 2.0f;
+        sr->camera_vel = vec2f_mul(
+                             vec2f_sub(cursor_pos, sr->camera_pos),
+                             vec2fs(2.0f));
+        sr->camera_scale_vel = (target_scale - sr->camera_scale) * 2.0f;
 
-        camera_pos = vec2f_add(camera_pos, vec2f_mul(camera_vel, vec2fs(DELTA_TIME)));
-        camera_scale = camera_scale + camera_scale_vel * DELTA_TIME;
+        sr->camera_pos = vec2f_add(sr->camera_pos, vec2f_mul(sr->camera_vel, vec2fs(DELTA_TIME)));
+        sr->camera_scale = sr->camera_scale + sr->camera_scale_vel * DELTA_TIME;
     }
 }
 

+ 2 - 0
src/simple_renderer.c

@@ -17,6 +17,8 @@ void simple_renderer_init(Simple_Renderer *sr,
                           const char *image_frag_file_path,
                           const char *epic_frag_file_path)
 {
+    sr->camera_scale = 3.0f;
+
     {
         glGenVertexArrays(1, &sr->vao);
         glBindVertexArray(sr->vao);

+ 6 - 0
src/simple_renderer.h

@@ -40,6 +40,12 @@ typedef struct {
     GLint uniforms[COUNT_UNIFORM_SLOTS];
     Simple_Vertex verticies[SIMPLE_VERTICIES_CAP];
     size_t verticies_count;
+
+    Vec2f camera_pos;
+    float camera_scale;
+    float camera_scale_vel;
+    Vec2f camera_vel;
+
 } Simple_Renderer;
 
 void simple_renderer_init(Simple_Renderer *sr,