Deuzivan

C++ and Raylib Plataform Exemple with Camera

Jan 20th, 2026
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.84 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #include <raylib.h>
  5. #include <raymath.h>
  6.  
  7. typedef struct
  8. {
  9.   Rectangle rect, texture_rect;
  10.   Texture2D texture;
  11. } Tile;
  12.  
  13. enum TileID
  14. {
  15.   GRASS_BLOCK,
  16.   DIRT,
  17.   STONE,
  18.   GRASS,
  19.   RED_FLOWER,
  20.   BLUE_FLOWER,
  21.   ESMERALD_ORE,
  22.   COPPER_ORE,
  23.   COAL_ORE,
  24.   DIAMOND_ORE
  25. };
  26.  
  27. Rectangle factoryTileRect(TileID id)
  28. {
  29.   switch (id)
  30.   {
  31.   case GRASS_BLOCK: return Rectangle { 8.f * 1.f, 8.f * 0.f, 8.f, 8.f }; break;
  32.   case DIRT: return Rectangle { 8.f * 1.f, 8.f * 1.f, 8.f, 8.f }; break;
  33.   case STONE: return Rectangle { 8.f * 6.f, 8.f * 2.f, 8.f, 8.f }; break;
  34.   case GRASS: return Rectangle { 8.f * 2.f, 8.f * 5.f, 8.f, 8.f }; break;
  35.   case RED_FLOWER: return Rectangle { 8.f * 5.f, 8.f * 2.f, 8.f, 8.f }; break;
  36.   case BLUE_FLOWER: return Rectangle { 8.f * 0.f, 8.f * 5.f, 8.f, 8.f }; break;
  37.   case ESMERALD_ORE: return Rectangle { 8.f * 7.f, 8.f * 0.f, 8.f, 8.f }; break;
  38.   case COPPER_ORE: return Rectangle { 8.f * 7.f, 8.f * 2.f, 8.f, 8.f }; break;
  39.   case DIAMOND_ORE: return Rectangle { 8.f * 6.f, 8.f * 3.f, 8.f, 8.f }; break;
  40.   case COAL_ORE: return Rectangle { 8.f * 4.f, 8.f * 5.f, 8.f, 8.f }; break;
  41.   default: return Rectangle { 8.f, 0.f, 8.f, 8.f }; break;
  42.   }
  43. }
  44.  
  45. std::vector<Tile> generateChunk(int chunk_at, Texture2D tilemap)
  46. {
  47.   std::vector<Tile> chunk;
  48.  
  49.   static ssize_t max_high = 64;
  50.   for(ssize_t x = chunk_at; x < chunk_at + 64; x++)
  51.   {
  52.     for(ssize_t y = 0; y < max_high; y++)
  53.     {
  54.       Tile tile;
  55.  
  56.       tile.rect = {
  57.         static_cast<float>(x) * 24.f,
  58.         static_cast<float>(y) * 24.f - static_cast<float>(max_high) * 24.f + 512.f,
  59.         24.f, 24.f
  60.       };
  61.       tile.texture = tilemap;
  62.      
  63.       if(y == 0)
  64.       {
  65.         tile.texture_rect = factoryTileRect(TileID::GRASS_BLOCK);
  66.  
  67.         if(GetRandomValue(0, 2) == 0)
  68.         {
  69.           Tile extra = tile;
  70.  
  71.           int id = GetRandomValue(0, 2);
  72.           extra.texture_rect = id == 0 ?
  73.             factoryTileRect(TileID::GRASS) : id == 1 ?
  74.             factoryTileRect(TileID::RED_FLOWER) :
  75.             factoryTileRect(TileID::BLUE_FLOWER);
  76.  
  77.           extra.rect.y -= extra.rect.height;
  78.  
  79.           chunk.push_back(extra);
  80.         }
  81.        
  82.       }
  83.       else if(y > 8)
  84.       {
  85.         tile.texture_rect = factoryTileRect(TileID::STONE);
  86.  
  87.         if(y > 16)
  88.         {
  89.           if(GetRandomValue(0, 25) == 0)
  90.             tile.texture_rect = factoryTileRect(TileID::COAL_ORE);
  91.           else if(GetRandomValue(0, 50) == 0)
  92.             tile.texture_rect = factoryTileRect(TileID::COPPER_ORE);
  93.           else if(GetRandomValue(0, 100) == 0)
  94.             tile.texture_rect = factoryTileRect(TileID::ESMERALD_ORE);
  95.           else if(GetRandomValue(0, 75) == 0)
  96.             tile.texture_rect = factoryTileRect(TileID::DIAMOND_ORE);
  97.         }
  98.        
  99.         if(y == max_high - 1)
  100.         {
  101.           tile.texture_rect = Rectangle { 8.f * 5.f, 8.f * 5.f, 8.f, 8.f };
  102.         }
  103.       }
  104.       else
  105.       {
  106.         tile.texture_rect = factoryTileRect(TileID::DIRT);
  107.       }
  108.  
  109.       chunk.push_back(tile);
  110.     }
  111.  
  112.     max_high += GetRandomValue(-1, 1);
  113.     if(max_high < 4) max_high = 4;
  114.   }
  115.  
  116.   return chunk;
  117. }
  118.  
  119. int main()
  120. {
  121.   InitWindow(720, 480, "World Gen");
  122.  
  123.   Texture2D default_tilemap = LoadTexture("data/tileset/default.png");
  124.   SetTargetFPS(60);
  125.  
  126.   int region_x = 0;
  127.   std::vector<Tile> world;
  128.  
  129.   world = generateChunk(region_x, default_tilemap);
  130.  
  131.   Vector2 camera_position = { 0.f, 0.f }, player_position = { 720.f / 2.f, 480.f / 2.f + 50.f * 24.f };
  132.   while(!WindowShouldClose())
  133.   {
  134.     if(IsKeyDown(KEY_W)) player_position.y += 10.f;
  135.     if(IsKeyDown(KEY_A)) player_position.x += 10.f;
  136.     if(IsKeyDown(KEY_S)) player_position.y -= 10.f;
  137.     if(IsKeyDown(KEY_D)) player_position.x -= 10.f;
  138.  
  139.     camera_position.x += (720.f / 2.f - player_position.x - camera_position.x) / 8.f;
  140.     camera_position.y += (480.f / 2.f - player_position.y - camera_position.y) / 8.f;
  141.  
  142.     BeginDrawing();
  143.     ClearBackground(Color {130, 200, 229});
  144.  
  145.     if(camera_position.x > region_x * 24.f)
  146.     {
  147.       region_x += 64;
  148.       std::vector<Tile> new_chunk = generateChunk(region_x, default_tilemap);
  149.       world.insert(world.end(), new_chunk.begin(), new_chunk.end());
  150.     }
  151.  
  152.     for(Tile tile : world)
  153.     {
  154.       tile.rect.x -= camera_position.x;
  155.       tile.rect.y -= camera_position.y;
  156.  
  157.       if(tile.rect.x > -tile.rect.width && tile.rect.x < 720.f && tile.rect.y > -tile.rect.height && tile.rect.y < 480.f)
  158.         DrawTexturePro(tile.texture, tile.texture_rect, tile.rect, Vector2Zero(), 0.f, WHITE );
  159.     }
  160.  
  161.     Vector2 player_scrolled = player_position;
  162.  
  163.     player_scrolled.x += camera_position.x;
  164.     player_scrolled.y += camera_position.y;
  165.  
  166.     DrawRectangleV(player_scrolled, Vector2 { 32.f, 32.f }, RED);
  167.      
  168.     EndDrawing();
  169.   }
  170.  
  171.   return EXIT_SUCCESS;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment