2022-07-07 12:13:45 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [textures] example - Fog of war
|
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example originally created with raylib 4.2, last time updated with raylib 4.2
|
|
|
|
*
|
|
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
|
|
* BSD-like license that allows static linking with closed source software
|
2022-07-07 12:13:45 +03:00
|
|
|
*
|
|
|
|
* Copyright (c) 2018-2022 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
#include <stdlib.h> // Required for: calloc(), free()
|
|
|
|
|
|
|
|
#define MAP_TILE_SIZE 32 // Tiles size 32x32 pixels
|
|
|
|
#define PLAYER_SIZE 16 // Player size
|
2022-08-02 01:30:57 +03:00
|
|
|
#define PLAYER_TILE_VISIBILITY 2 // Player can see 2 tiles around its position
|
2022-07-07 12:13:45 +03:00
|
|
|
|
|
|
|
// 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");
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
Map map = { 0 };
|
|
|
|
map.tilesX = 25;
|
|
|
|
map.tilesY = 15;
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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));
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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);
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// Player position on the screen (pixel coordinates, not tile coordinates)
|
|
|
|
Vector2 playerPosition = { 180, 130 };
|
|
|
|
int playerTileX = 0;
|
|
|
|
int playerTileY = 0;
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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;
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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;
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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);
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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();
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
for (int y = 0; y < map.tilesY; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < map.tilesX; x++)
|
|
|
|
{
|
|
|
|
// Draw tiles from id (and tile borders)
|
2022-08-02 01:30:57 +03:00
|
|
|
DrawRectangle(x*MAP_TILE_SIZE, y*MAP_TILE_SIZE, MAP_TILE_SIZE, MAP_TILE_SIZE,
|
2022-07-07 12:13:45 +03:00
|
|
|
(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));
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// Draw player
|
|
|
|
DrawRectangleV(playerPosition, (Vector2){ PLAYER_SIZE, PLAYER_SIZE }, RED);
|
2022-08-02 01:30:57 +03:00
|
|
|
|
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// Draw fog of war (scaled to full map, bilinear filtering)
|
2022-08-02 01:30:57 +03:00
|
|
|
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 },
|
2022-07-07 12:13:45 +03:00
|
|
|
(Vector2){ 0, 0 }, 0.0f, WHITE);
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
// 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
|
2022-08-02 01:30:57 +03:00
|
|
|
|
2022-07-07 12:13:45 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|