added statusDisplay, not finish, fail to run

This commit is contained in:
Ryan Tang 2023-12-14 17:22:43 -05:00
parent 775626a2c4
commit e4d93f6d51
4 changed files with 78 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
display

7
statusDisplay/README.md Normal file
View File

@ -0,0 +1,7 @@
The Status Display on Raspberry Pi use the SDL C++ library
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev
To Compile
g++ -o display display.cpp -lSDL2 -lSDL2_image -lSDL2_ttf

70
statusDisplay/display.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <string>
int main() {
SDL_Init(SDL_INIT_VIDEO);
// Create a window
SDL_Window* window = SDL_CreateWindow("Image with Text Overlay",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480, 0);
// Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Load an image
SDL_Surface* imageSurface = IMG_Load("image.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_FreeSurface(imageSurface);
// Font initialization for rendering text
TTF_Init();
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/trutype/freefont/FreeMono.ttf", 24);
SDL_Color textColor = { 255, 255, 255 }; // White color
SDL_Rect imageRect = { 0, 0, 640, 480 }; // Image position and size
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
// Clear the renderer
SDL_RenderClear(renderer);
// Render the image
SDL_RenderCopy(renderer, texture, NULL, &imageRect);
// Render text
std::string textToRender = "Dynamic Number: 123"; // Replace with your dynamic number
SDL_Surface* textSurface = TTF_RenderText_Solid(font, textToRender.c_str(), textColor);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = { 50, 50, textSurface->w, textSurface->h }; // Position of text
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
// Update the screen
SDL_RenderPresent(renderer);
}
// Clean up
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}

BIN
statusDisplay/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB