2023-12-14 17:22:43 -05:00
|
|
|
#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,
|
2023-12-14 17:53:47 -05:00
|
|
|
1577, 695, 0);
|
2023-12-14 17:22:43 -05:00
|
|
|
|
|
|
|
// 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();
|
2023-12-14 17:53:47 -05:00
|
|
|
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 24);
|
2023-12-14 17:22:43 -05:00
|
|
|
SDL_Color textColor = { 255, 255, 255 }; // White color
|
|
|
|
|
2023-12-14 17:53:47 -05:00
|
|
|
SDL_Rect imageRect = { 0, 0, 1577, 695 }; // Image position and size
|
2023-12-14 17:22:43 -05:00
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
bool running = true;
|
|
|
|
|
|
|
|
while (running) {
|
2023-12-14 17:53:47 -05:00
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
if (event.type == SDL_QUIT) {
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
}
|
2023-12-14 17:22:43 -05:00
|
|
|
|
2023-12-14 17:53:47 -05:00
|
|
|
// Clear the renderer
|
|
|
|
SDL_RenderClear(renderer);
|
2023-12-14 17:22:43 -05:00
|
|
|
|
2023-12-14 17:53:47 -05:00
|
|
|
// Render the image
|
|
|
|
SDL_RenderCopy(renderer, texture, NULL, &imageRect);
|
2023-12-14 17:22:43 -05:00
|
|
|
|
2023-12-14 17:53:47 -05:00
|
|
|
// Render text
|
|
|
|
std::string textToRender = "Dynamic Number: 123"; // Replace with your dynamic number
|
|
|
|
SDL_Surface* textSurface = TTF_RenderText_Solid(font, textToRender.c_str(), textColor);
|
2023-12-14 17:22:43 -05:00
|
|
|
|
|
|
|
|
2023-12-14 17:53:47 -05:00
|
|
|
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
|
2023-12-14 17:22:43 -05:00
|
|
|
|
2023-12-14 17:53:47 -05:00
|
|
|
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);
|
2023-12-14 17:22:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
SDL_DestroyRenderer(renderer);
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
TTF_Quit();
|
|
|
|
SDL_Quit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|