70 lines
2.0 KiB
C++
70 lines
2.0 KiB
C++
#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;
|
|
} |