diff --git a/examples/textures/textures_fog_of_war.c b/examples/textures/textures_fog_of_war.c new file mode 100644 index 000000000..4cbebbb17 --- /dev/null +++ b/examples/textures/textures_fog_of_war.c @@ -0,0 +1,152 @@ +/******************************************************************************************* +* +* raylib [textures] example - Fog of war +* +* This example has been created using raylib 4.2 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2018-2022 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include // Required for: calloc(), free() + +#define MAP_TILE_SIZE 32 // Tiles size 32x32 pixels +#define PLAYER_SIZE 16 // Player size +#define PLAYER_TILE_VISIBILITY 2 // Player can see 2 tiles around its position + +// Map data type +typedef struct Map { + unsigned int tilesX; // Number of tiles in X axis + unsigned int tilesY; // Number of tiles in Y axis + unsigned char *tileIds; // Tile ids (tilesX*tilesY), defines type of tile to draw + unsigned char *tileFog; // Tile fog state (tilesX*tilesY), defines if a tile has fog or half-fog +} Map; + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [textures] example - fog of war"); + + Map map = { 0 }; + map.tilesX = 25; + map.tilesY = 15; + + // NOTE: We can have up to 256 values for tile ids and for tile fog state, + // probably we don't need that many values for fog state, it can be optimized + // to use only 2 bits per fog state (reducing size by 4) but logic will be a bit more complex + map.tileIds = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char)); + map.tileFog = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char)); + + // Load map tiles (generating 2 random tile ids for testing) + // NOTE: Map tile ids should be probably loaded from an external map file + for (int i = 0; i < map.tilesY*map.tilesX; i++) map.tileIds[i] = GetRandomValue(0, 1); + + // Player position on the screen (pixel coordinates, not tile coordinates) + Vector2 playerPosition = { 180, 130 }; + int playerTileX = 0; + int playerTileY = 0; + + // Render texture to render fog of war + // NOTE: To get an automatic smooth-fog effect we use a render texture to render fog + // at a smaller size (one pixel per tile) and scale it on drawing with bilinear filtering + RenderTexture2D fogOfWar = LoadRenderTexture(map.tilesX, map.tilesY); + SetTextureFilter(fogOfWar.texture, TEXTURE_FILTER_BILINEAR); + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // Move player around + if (IsKeyDown(KEY_RIGHT)) playerPosition.x += 5; + if (IsKeyDown(KEY_LEFT)) playerPosition.x -= 5; + if (IsKeyDown(KEY_DOWN)) playerPosition.y += 5; + if (IsKeyDown(KEY_UP)) playerPosition.y -= 5; + + // Check player position to avoid moving outside tilemap limits + if (playerPosition.x < 0) playerPosition.x = 0; + else if ((playerPosition.x + PLAYER_SIZE) > (map.tilesX*MAP_TILE_SIZE)) playerPosition.x = map.tilesX*MAP_TILE_SIZE - PLAYER_SIZE; + if (playerPosition.y < 0) playerPosition.y = 0; + else if ((playerPosition.y + PLAYER_SIZE) > (map.tilesY*MAP_TILE_SIZE)) playerPosition.y = map.tilesY*MAP_TILE_SIZE - PLAYER_SIZE; + + // Previous visited tiles are set to partial fog + for (int i = 0; i < map.tilesX*map.tilesY; i++) if (map.tileFog[i] == 1) map.tileFog[i] = 2; + + // Get current tile position from player pixel position + playerTileX = (int)((playerPosition.x + MAP_TILE_SIZE/2)/MAP_TILE_SIZE); + playerTileY = (int)((playerPosition.y + MAP_TILE_SIZE/2)/MAP_TILE_SIZE); + + // Check visibility and update fog + // NOTE: We check tilemap limits to avoid processing tiles out-of-array-bounds (it could crash program) + for (int y = (playerTileY - PLAYER_TILE_VISIBILITY); y < (playerTileY + PLAYER_TILE_VISIBILITY); y++) + for (int x = (playerTileX - PLAYER_TILE_VISIBILITY); x < (playerTileX + PLAYER_TILE_VISIBILITY); x++) + if ((x >= 0) && (x < map.tilesX) && (y >= 0) && (y < map.tilesY)) map.tileFog[y*map.tilesX + x] = 1; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + // Draw fog of war to a small render texture for automatic smoothing on scaling + BeginTextureMode(fogOfWar); + ClearBackground(BLANK); + for (int y = 0; y < map.tilesY; y++) + for (int x = 0; x < map.tilesX; x++) + if (map.tileFog[y*map.tilesX + x] == 0) DrawRectangle(x, y, 1, 1, BLACK); + else if (map.tileFog[y*map.tilesX + x] == 2) DrawRectangle(x, y, 1, 1, Fade(BLACK, 0.8f)); + EndTextureMode(); + + BeginDrawing(); + + ClearBackground(RAYWHITE); + + for (int y = 0; y < map.tilesY; y++) + { + for (int x = 0; x < map.tilesX; x++) + { + // Draw tiles from id (and tile borders) + DrawRectangle(x*MAP_TILE_SIZE, y*MAP_TILE_SIZE, MAP_TILE_SIZE, MAP_TILE_SIZE, + (map.tileIds[y*map.tilesX + x] == 0)? BLUE : Fade(BLUE, 0.9f)); + DrawRectangleLines(x*MAP_TILE_SIZE, y*MAP_TILE_SIZE, MAP_TILE_SIZE, MAP_TILE_SIZE, Fade(DARKBLUE, 0.5f)); + } + } + + // Draw player + DrawRectangleV(playerPosition, (Vector2){ PLAYER_SIZE, PLAYER_SIZE }, RED); + + + // Draw fog of war (scaled to full map, bilinear filtering) + DrawTexturePro(fogOfWar.texture, (Rectangle){ 0, 0, fogOfWar.texture.width, -fogOfWar.texture.height }, + (Rectangle){ 0, 0, map.tilesX*MAP_TILE_SIZE, map.tilesY*MAP_TILE_SIZE }, + (Vector2){ 0, 0 }, 0.0f, WHITE); + + // Draw player current tile + DrawText(TextFormat("Current tile: [%i,%i]", playerTileX, playerTileY), 10, 10, 20, LIME); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + free(map.tileIds); // Free allocated map tile ids + free(map.tileFog); // Free allocated map tile fog state + + UnloadRenderTexture(fogOfWar); // Unload render texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/textures/textures_fog_of_war.png b/examples/textures/textures_fog_of_war.png new file mode 100644 index 000000000..6a91316ee Binary files /dev/null and b/examples/textures/textures_fog_of_war.png differ diff --git a/projects/VS2022/examples/textures_fog_of_war.vcxproj b/projects/VS2022/examples/textures_fog_of_war.vcxproj new file mode 100644 index 000000000..aa93c01a8 --- /dev/null +++ b/projects/VS2022/examples/textures_fog_of_war.vcxproj @@ -0,0 +1,387 @@ + + + + + Debug.DLL + Win32 + + + Debug.DLL + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release.DLL + Win32 + + + Release.DLL + x64 + + + Release + Win32 + + + Release + x64 + + + + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0} + Win32Proj + textures_fog_of_war + 10.0 + textures_fog_of_war + + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\textures + WindowsLocalDebugger + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + /FS %(AdditionalOptions) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + Copy Debug DLL to output directory + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + Copy Debug DLL to output directory + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + + + Copy Release DLL to output directory + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + + + Copy Release DLL to output directory + + + + + + + + {e89d61ac-55de-4482-afd4-df7242ebc859} + + + + + + \ No newline at end of file diff --git a/projects/VS2022/raylib.sln b/projects/VS2022/raylib.sln index 66830ce14..ff6681f23 100644 --- a/projects/VS2022/raylib.sln +++ b/projects/VS2022/raylib.sln @@ -249,6 +249,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "text_codepoints_loading", " EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_window_should_close", "examples\core_window_should_close.vcxproj", "{3FE7E9B6-49AC-4246-A789-28DB4644567B}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "textures_fog_of_war", "examples\textures_fog_of_war.vcxproj", "{EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug.DLL|x64 = Debug.DLL|x64 @@ -2081,6 +2083,22 @@ Global {3FE7E9B6-49AC-4246-A789-28DB4644567B}.Release|x64.Build.0 = Release|x64 {3FE7E9B6-49AC-4246-A789-28DB4644567B}.Release|x86.ActiveCfg = Release|Win32 {3FE7E9B6-49AC-4246-A789-28DB4644567B}.Release|x86.Build.0 = Release|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug.DLL|x64.ActiveCfg = Debug.DLL|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug.DLL|x64.Build.0 = Debug.DLL|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug|x64.ActiveCfg = Debug|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug|x64.Build.0 = Debug|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug|x86.ActiveCfg = Debug|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Debug|x86.Build.0 = Debug|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release.DLL|x64.ActiveCfg = Release.DLL|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release.DLL|x64.Build.0 = Release.DLL|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release.DLL|x86.Build.0 = Release.DLL|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release|x64.ActiveCfg = Release|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release|x64.Build.0 = Release|x64 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release|x86.ActiveCfg = Release|Win32 + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2207,6 +2225,7 @@ Global {F5FC9279-DE63-4EF3-B31F-CFCEF9B11F71} = {AF5BEC5C-1F2B-4DA8-B12D-D09FE569237C} {F2DB2E59-76BF-4D81-859A-AFC289C046C0} = {8D3C83B7-F1E0-4C2E-9E34-EE5F6AB2502A} {3FE7E9B6-49AC-4246-A789-28DB4644567B} = {6C82BAAE-BDDF-457D-8FA8-7E2490B07035} + {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0} = {DA049009-21FF-4AC0-84E4-830DD1BCD0CE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E926C768-6307-4423-A1EC-57E95B1FAB29}