Last active
October 22, 2021 22:44
-
-
Save trya/119dfca6b332483168e39e3d92cb3504 to your computer and use it in GitHub Desktop.
Simple example of raylib rendering whatever is coming on stdin
This file contains 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 <stdlib.h> | |
#include <threads.h> | |
#include "raylib.h" | |
#include "rlgl.h" | |
int main(int argc, char **argv) | |
{ | |
if (argc != 4) { | |
fprintf(stderr, "usage: %s width height fps\n", argv[0]); | |
fprintf(stderr, "0 or negative number for fps means best effort\n"); | |
exit(EXIT_FAILURE); | |
} | |
int w = atoi(argv[1]); | |
int h = atoi(argv[2]); | |
int target_fps = atoi(argv[3]); | |
if (w <= 0 || h <= 0) { | |
fprintf(stderr, "error: invalid window dimensions\n"); | |
exit(EXIT_FAILURE); | |
} | |
InitWindow(w, h, "raylib sink"); | |
if (target_fps > 0) { | |
SetTargetFPS(target_fps); | |
fprintf(stderr, "Target FPS set to %d\n", target_fps); | |
} else { | |
fprintf(stderr, "Rendering with best effort\n"); | |
} | |
BeginDrawing(); | |
ClearBackground(DARKBLUE); | |
DrawTextEx(GetFontDefault(), "Please wait warmly...", (Vector2){2,0}, 20, 2, LIGHTGRAY); | |
EndDrawing(); | |
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); | |
size_t rgb_buf_sz = w*h*3; | |
char *rgb_buf = malloc(rgb_buf_sz); | |
if (!rgb_buf) { | |
fprintf(stderr, "error: memory allocation problem\n"); | |
exit(EXIT_FAILURE); | |
} | |
PixelFormat px_fmt = PIXELFORMAT_UNCOMPRESSED_R8G8B8; | |
Texture2D rgb_tex = { | |
.id = rlLoadTexture(rgb_buf, w, h, px_fmt, 1), | |
.width = w, | |
.height = h, | |
.mipmaps = 1, | |
.format = px_fmt, | |
}; | |
while (!WindowShouldClose()) { | |
// Update | |
if (fread(rgb_buf, rgb_buf_sz, 1, stdin) != 1) { | |
if (!feof(stdin)) { | |
fprintf(stderr, "error: reading problem\n"); | |
exit(EXIT_FAILURE); | |
} else { | |
fprintf(stderr, "EOF\n"); | |
exit(EXIT_SUCCESS); | |
} | |
} | |
UpdateTexture(rgb_tex, rgb_buf); | |
// Draw | |
BeginDrawing(); | |
DrawTexture(rgb_tex, 0, 0, WHITE); | |
DrawFPS(10, 10); | |
EndDrawing(); | |
} | |
free(rgb_buf); | |
UnloadTexture(rgb_tex); | |
CloseWindow(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment