Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Created April 17, 2024 13:38
Show Gist options
  • Save jdmichaud/d9278929058314c1f91e82bcdb002cbf to your computer and use it in GitHub Desktop.
Save jdmichaud/d9278929058314c1f91e82bcdb002cbf to your computer and use it in GitHub Desktop.
Easiest way to display pixels on a screen on Linux
//g++ main.cc -O3 -o test -lX11 && ./test
#include <chrono>
#include <iostream>
#include <string>
#include "CImg.h"
using namespace cimg_library;
int main(int argc,char **argv) {
const unsigned char white[] = { 255, 255, 255 };
const int width = 640;
const int height = 480;
// Create 3-channel RGB image
CImg<> img(width,height, 1, 3);
// Create main window
CImgDisplay main_window(img, "Random Data", 0);
float fps = 0;
while (!main_window.is_closed()) {
std::chrono::time_point<std::chrono::system_clock> startTime =
std::chrono::system_clock::now();
// Fill image with random noise
img.rand(0, 255);
// Draw in frame counter
std::string text = "Fps: " + std::to_string(fps);
img.draw_text(10, 10, text.c_str(), white, 0, 1, 32);
main_window.display(img);
float elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - startTime).count();
fps = fps * 0.9 + (1000 / elapsed) * 0.1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment