tested, can display image.png, no text

This commit is contained in:
Ryan Tang 2023-12-14 17:53:47 -05:00
parent e4d93f6d51
commit 07be9e3a29
3 changed files with 44 additions and 23 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
display display
.gdb_history

17
statusDisplay/Makefile Normal file
View File

@ -0,0 +1,17 @@
# Compiler
CXX = g++
# Flags
CXXFLAGS = -Wall -Wextra -pedantic -std=c++11
# Libraries
LIBS = -lSDL2 -lSDL2_image -lSDL2_ttf
# Executable name
EXECUTABLE = display
all: $(EXECUTABLE)
$(EXECUTABLE): display.cpp
$(CXX) $(CXXFLAGS) -o $(EXECUTABLE) display.cpp $(LIBS)
clean:
rm -f $(EXECUTABLE)

View File

@ -11,7 +11,7 @@ int main() {
SDL_Window* window = SDL_CreateWindow("Image with Text Overlay", SDL_Window* window = SDL_CreateWindow("Image with Text Overlay",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, 0); 1577, 695, 0);
// Create a renderer // Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
@ -23,40 +23,42 @@ int main() {
// Font initialization for rendering text // Font initialization for rendering text
TTF_Init(); TTF_Init();
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/trutype/freefont/FreeMono.ttf", 24); TTF_Font* font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 24);
SDL_Color textColor = { 255, 255, 255 }; // White color SDL_Color textColor = { 255, 255, 255 }; // White color
SDL_Rect imageRect = { 0, 0, 640, 480 }; // Image position and size SDL_Rect imageRect = { 0, 0, 1577, 695 }; // Image position and size
SDL_Event event; SDL_Event event;
bool running = true; bool running = true;
while (running) { while (running) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
running = false; running = false;
} }
} }
// Clear the renderer // Clear the renderer
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
// Render the image // Render the image
SDL_RenderCopy(renderer, texture, NULL, &imageRect); SDL_RenderCopy(renderer, texture, NULL, &imageRect);
// Render text // Render text
std::string textToRender = "Dynamic Number: 123"; // Replace with your dynamic number std::string textToRender = "Dynamic Number: 123"; // Replace with your dynamic number
SDL_Surface* textSurface = TTF_RenderText_Solid(font, textToRender.c_str(), textColor); 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_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_DestroyTexture(textTexture);
// Update the screen SDL_Rect textRect = { 50, 50, textSurface->w, textSurface->h }; // Position of text
SDL_RenderPresent(renderer); SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
// Update the screen
SDL_RenderPresent(renderer);
} }
// Clean up // Clean up