Remove line breaks

This commit is contained in:
Ray 2022-08-02 00:30:57 +02:00
parent b20d416131
commit fe9e82b2e6
4 changed files with 101 additions and 101 deletions

View file

@ -36,11 +36,11 @@ int main(void)
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - codepoints loading");
// Get codepoints from text
int codepointCount = 0;
int *codepoints = LoadCodepoints(text, &codepointCount);
// Removed duplicate codepoints to generate smaller font atlas
int codepointsNoDupsCount = 0;
int *codepointsNoDups = CodepointRemoveDuplicates(codepoints, codepointCount, &codepointsNoDupsCount);
@ -49,13 +49,13 @@ int main(void)
// Load font containing all the provided codepoint glyphs
// A texture font atlas is automatically generated
Font font = LoadFontEx("resources/DotGothic16-Regular.ttf", 36, codepointsNoDups, codepointsNoDupsCount);
// Set bilinear scale filter for better font scaling
SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);
// Free codepoints, atlas has already been generated
free(codepointsNoDups);
bool showFontAtlas = false;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
@ -74,7 +74,7 @@ int main(void)
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangle(0, 0, GetScreenWidth(), 70, BLACK);
DrawText(TextFormat("Total codepoints contained in provided text: %i", codepointCount), 10, 10, 20, GREEN);
DrawText(TextFormat("Total codepoints required for font atlas (duplicates excluded): %i", codepointsNoDupsCount), 10, 40, 20, GREEN);
@ -90,20 +90,20 @@ int main(void)
// Draw provided text with laoded font, containing all required codepoint glyphs
DrawTextEx(font, text, (Vector2) { 160, 110 }, 48, 5, BLACK);
}
DrawText("Press SPACE to toggle font atlas view!", 10, GetScreenHeight() - 30, 20, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadFont(font); // Unload font
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
@ -130,9 +130,9 @@ static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *
}
}
// NOTE: The size of codepointsNoDups is the same as original array but
// NOTE: The size of codepointsNoDups is the same as original array but
// only required positions are filled (codepointsNoDupsCount)
*codepointsResultCount = codepointsNoDupsCount;
return codepointsNoDups;
}

View file

@ -17,7 +17,7 @@
#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
#define PLAYER_TILE_VISIBILITY 2 // Player can see 2 tiles around its position
// Map data type
typedef struct Map {
@ -38,26 +38,26 @@ int main(void)
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
@ -77,7 +77,7 @@ int main(void)
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;
@ -86,11 +86,11 @@ int main(void)
// 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++)
@ -108,31 +108,31 @@ int main(void)
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,
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 },
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);
@ -146,7 +146,7 @@ int main(void)
free(map.tileFog); // Free allocated map tile fog state
UnloadRenderTexture(fogOfWar); // Unload render texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View file

@ -27,22 +27,22 @@ int main(void)
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - gif playing");
int animFrames = 0;
// Load all GIF animation frames into a single Image
// NOTE: GIF data is always loaded as RGBA (32bit) by default
// NOTE: Frames are just appended one after another in image.data memory
Image imScarfyAnim = LoadImageAnim("resources/scarfy_run.gif", &animFrames);
// Load texture from image
// NOTE: We will update this texture when required with next frame data
// WARNING: It's not recommended to use this technique for sprites animation,
// use spritesheets instead, like illustrated in textures_sprite_anim example
Texture2D texScarfyAnim = LoadTextureFromImage(imScarfyAnim);
unsigned int nextFrameDataOffset = 0; // Current byte offset to next frame in image.data
int currentAnimFrame = 0; // Current animation frame to load and draw
int frameDelay = 8; // Frame delay to switch between animation frames
int frameCounter = 0; // General frames counter
@ -56,23 +56,23 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
frameCounter++;
if (frameCounter >= frameDelay)
if (frameCounter >= frameDelay)
{
// Move to next frame
// NOTE: If final frame is reached we return to first frame
currentAnimFrame++;
if (currentAnimFrame >= animFrames) currentAnimFrame = 0;
// Get memory offset position for next frame data in image.data
nextFrameDataOffset = imScarfyAnim.width*imScarfyAnim.height*4*currentAnimFrame;
// Update GPU texture data with next frame image data
// WARNING: Data size (frame size) and pixel format must match already created texture
UpdateTexture(texScarfyAnim, ((unsigned char *)imScarfyAnim.data) + nextFrameDataOffset);
frameCounter = 0;
}
// Control frames delay
if (IsKeyPressed(KEY_RIGHT)) frameDelay++;
else if (IsKeyPressed(KEY_LEFT)) frameDelay--;
@ -90,7 +90,7 @@ int main(void)
DrawText(TextFormat("TOTAL GIF FRAMES: %02i", animFrames), 50, 30, 20, LIGHTGRAY);
DrawText(TextFormat("CURRENT FRAME: %02i", currentAnimFrame), 50, 60, 20, GRAY);
DrawText(TextFormat("CURRENT FRAME IMAGE.DATA OFFSET: %02i", nextFrameDataOffset), 50, 90, 20, GRAY);
DrawText("FRAMES DELAY: ", 100, 305, 10, DARKGRAY);
DrawText(TextFormat("%02i frames", frameDelay), 620, 305, 10, DARKGRAY);
DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 350, 10, DARKGRAY);
@ -113,7 +113,7 @@ int main(void)
//--------------------------------------------------------------------------------------
UnloadTexture(texScarfyAnim); // Unload texture
UnloadImage(imScarfyAnim); // Unload image (contains all frames)
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------