Created
September 16, 2024 19:17
-
-
Save Ssenseii/955a4de16c40d174ace38b4682717f71 to your computer and use it in GitHub Desktop.
RAYLIB: Image_to_ASCII Basic Implementation (with color)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "raylib.h" | |
| char asciiChars[] = {'@', '#', 'S', '%', '?', '*', '+', ';', ':', ',', '.'}; | |
| int windowWidth = 1024; | |
| int windowHeight = 780; | |
| int imageResizeValue = 100; | |
| int fontSize = 10; | |
| char getAsciiChar(Color color) { | |
| // Convert color to grayscale | |
| float grayscale = 0.299 * color.r + 0.587 * color.g + 0.114 * color.b; | |
| int index = (int)(grayscale / 255.0 * (sizeof(asciiChars) - 1)); | |
| return asciiChars[index]; | |
| } | |
| int main() { | |
| InitWindow(windowWidth, windowHeight, "SPACE MARINE IN ASCII"); | |
| Image image = LoadImage("space_marine.png"); // Use your own image | |
| ImageResize(&image, imageResizeValue, imageResizeValue); | |
| Color *pixels = LoadImageColors(image); | |
| while (!WindowShouldClose()) { | |
| BeginDrawing(); | |
| ClearBackground(ASCBLACK); | |
| int width = imageResizeValue; | |
| int height = imageResizeValue; | |
| for (int y = 0; y < height; y++) { | |
| for (int x = 0; x < width; x++) { | |
| Color pixelColor = pixels[y * width + x]; | |
| char asciiChar = getAsciiChar(pixelColor); | |
| char str[2] = {asciiChar, '\0'}; | |
| DrawText(str, x * fontSize, y * fontSize, fontSize, pixelColor); | |
| } | |
| } | |
| EndDrawing(); | |
| } | |
| UnloadImage(image); | |
| CloseWindow(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment