#include <iostream>
#include "SDL.h"
#include "SDL_ttf.h"
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL Initialization Fail: %s\n", SDL_GetError());
return -1;
}
SDL_Window* window = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
if (!window) {
printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
if (TTF_Init() < 0) {
// Initialize SDL_ttf.
printf("SDL TTF Initialization Failed: %s\n", TTF_GetError());
return -1;
}
TTF_Font* font = TTF_OpenFont("C:\\Windows\\Fonts\\arial.ttf", 30);
// Create a font from a file, using a specified point size.
if (font == NULL) {
printf("Could not open font! (%s)\n", TTF_GetError());
return -1;
}
SDL_Color textColor = { 255, 0, 0 };
SDL_Surface* textSurface = TTF_RenderText_Blended(font, "Hello World!", textColor);
// Render Latin1 text at high quality to a new ARGB surface.
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
// Create a texture from an existing surface.
SDL_FreeSurface(textSurface);
// Free an RGB surface.
SDL_Rect destRect = { 10, 10, textSurface->w, textSurface->h };
TTF_CloseFont(font);
// Dispose of a previously-created font.
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
printf("Key pressed: %s\n", SDL_GetKeyName(event.key.keysym.sym));
if (event.key.keysym.sym == SDLK_ESCAPE)
quit = true;
break;
default:
break;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, textTexture, NULL, &destRect);
// Copy a portion of the texture to the current rendering target.
SDL_RenderPresent(renderer);
}
TTF_Quit();
// Deinitialize SDL_ttf.
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}