Advertisement
largeNumberGoeshere

raylib dynamic camera window resizing

Mar 15th, 2025
195
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <raylib.h>
  2. #define RAYGUI_IMPLEMENTATION
  3. #include <raygui.h>
  4. #include <stdint.h>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. const int DEFAULT_WIDTH = 800;
  9. const int DEFAULT_HEIGHT = 600;
  10.  
  11.  
  12. const int BOUNDS_ASSERTIONS = false;
  13.  
  14. static void todoImpl(uint64_t line, const char* file) {
  15.     std::cerr << file << ":" << line << " is todo" << std::endl;
  16.     exit(1);
  17. }
  18.  
  19. #define TODO() \
  20.     todoImpl(__LINE__, __FILE__)
  21.  
  22. void initWindow() {
  23.     SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
  24.     InitWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, "sand experiment 3");
  25.     BeginDrawing();
  26.     {
  27.         ClearBackground(BLACK);
  28.         DrawText("Loading sand...", 10, 10, 20, RAYWHITE);
  29.     }
  30.     EndDrawing();
  31. }
  32.  
  33. const int COLUMN_WIDTH = 800;
  34. const int ROW_HEIGHT = 600;
  35.  
  36. // class Grid {
  37. //     public:
  38.  
  39. //         std::vector<bool> cellIsSand = std::vector<bool>();
  40. //         uint64_t grid_width = 0;
  41. //         uint64_t grid_height = 0;
  42.  
  43. //         inline uint64_t cellPosV(Vector2 pos) {
  44. //             uint64_t pos = (pos.y * COLUMN_WIDTH) + pos.x;
  45. //             return pos;
  46. //         }
  47.  
  48. //         inline bool cellHasSandV(Vector2 cellPos) {
  49.            
  50. //         }
  51.  
  52. //         inline Grid() {
  53.  
  54. //         }
  55.  
  56.  
  57.  
  58. // };
  59.  
  60. static int CAMERA_ZOOM = 1;
  61. float screen_ratio(int current_size, int default_size) {
  62.     return (float)current_size / (float)default_size;
  63. }
  64.  
  65. int main() {
  66.    
  67.     initWindow();
  68.  
  69.     Rectangle menu_bounds = Rectangle{0, 0, COLUMN_WIDTH, 50};
  70.     Rectangle sim_bounds = Rectangle {0, 60, COLUMN_WIDTH, ROW_HEIGHT-60};
  71.     Camera2D camera = Camera2D {};
  72.     camera.offset = Vector2{0, 60};
  73.     camera.rotation = 0;
  74.     camera.target = Vector2{0, 60};
  75.     // camera.target = Vector2{COLUMN_WIDTH/2, ROW_HEIGHT/2};
  76.     camera.zoom = 1;
  77.  
  78.     bool firstRun = true;
  79.     while (!WindowShouldClose())
  80.     {
  81.         BeginDrawing();
  82.         {
  83.             ClearBackground(BLACK);
  84.  
  85.             DrawRectangleRec(menu_bounds, BROWN);
  86.             for (int i = 0; i < 3; i++) {
  87.                 Rectangle bounds = menu_bounds;
  88.                 bounds.y += 5;
  89.                 bounds.x += 5;
  90.                 bounds.width = 50;
  91.                 bounds.height = 40;
  92.                 bounds.x = (60* i) + 10;
  93.                 GuiButton(bounds, "meow");
  94.  
  95.             }
  96.  
  97.             BeginMode2D(camera);
  98.             {  
  99.                 DrawRectangleRec(sim_bounds, GREEN);
  100.             }
  101.             EndMode2D();
  102.         }
  103.         EndDrawing();
  104.  
  105.         // update camera if window resized
  106.         if (IsWindowResized() || firstRun) {
  107.             int new_h = GetScreenHeight();
  108.             int new_w = GetScreenWidth();
  109.            
  110.             float ratio_h = screen_ratio(new_h, ROW_HEIGHT); // (float)new_h / (float)HEIGHT;
  111.             float ratio_w = screen_ratio(new_w, COLUMN_WIDTH); // (float)new_w / (float)WIDTH;
  112.            
  113.             // smallest ratio means window is smallest along that dimension
  114.             if (ratio_w < ratio_h) {
  115.                 camera.zoom = ratio_w * 0.99;
  116.             } else {
  117.                 camera.zoom = ratio_h * 0.99;
  118.             }
  119.  
  120.             CAMERA_ZOOM = camera.zoom;
  121.         }
  122.  
  123.         PollInputEvents();
  124.  
  125.         firstRun = false;
  126.     }
  127.  
  128.     CloseWindow();
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement