109 lines
3.4 KiB
C++
Executable File
109 lines
3.4 KiB
C++
Executable File
#include <iostream>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
template<typename T, typename... Args>
|
|
void cleanup(T *t, Args&&... args)
|
|
{
|
|
cleanup(t);
|
|
cleanup(std::forward<Args>(args)...);
|
|
}
|
|
|
|
template<> inline void cleanup<SDL_Window>(SDL_Window *win) { if(!win){return;} SDL_DestroyWindow(win); }
|
|
template<> inline void cleanup<SDL_Renderer>(SDL_Renderer *ren) { if(!ren){return;}SDL_DestroyRenderer(ren); }
|
|
template<> inline void cleanup<SDL_Texture>(SDL_Texture *tex) { if(!tex){return;} SDL_DestroyTexture(tex); }
|
|
template<> inline void cleanup<SDL_Surface>(SDL_Surface *surf) { if(!surf){return;} SDL_FreeSurface(surf); }
|
|
template<> inline void cleanup<TTF_Font>(TTF_Font *font) { if(!font){return;} TTF_CloseFont(font); }
|
|
|
|
|
|
void logError(const std::string &msg)
|
|
{
|
|
std::cout << msg << " error: " << SDL_GetError() << std::endl;
|
|
}
|
|
|
|
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren)
|
|
{
|
|
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
|
|
if (texture == nullptr) logError("LoadTexture");
|
|
return texture;
|
|
}
|
|
|
|
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst, SDL_Rect *clip = nullptr)
|
|
{
|
|
SDL_RenderCopy(ren, tex, clip, &dst);
|
|
}
|
|
|
|
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, SDL_Rect *clip = nullptr)
|
|
{
|
|
SDL_Rect dst;
|
|
dst.x = x;
|
|
dst.y = y;
|
|
if (clip != nullptr) {
|
|
dst.w = clip->w;
|
|
dst.h = clip->h;
|
|
} else {
|
|
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
|
|
}
|
|
renderTexture(tex, ren, dst, clip);
|
|
}
|
|
|
|
SDL_Texture* renderText(const std::string &message, const std::string &file,
|
|
SDL_Color color, int size, SDL_Renderer *renderer)
|
|
{
|
|
TTF_Font *font = TTF_OpenFont(file.c_str(), size);
|
|
if(font == nullptr) { logError("TTF_OpenFont"); return nullptr; }
|
|
SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
|
|
if(surf == nullptr) { cleanup(font); logError("TTF_RenderText"); return nullptr; }
|
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
|
|
if(texture == nullptr) { cleanup(surf, font); logError("SDL_CreateTextureFromSurface"); return nullptr; }
|
|
|
|
cleanup(surf, font);
|
|
return texture;
|
|
}
|
|
|
|
const int SCREEN_WIDTH = 640;
|
|
const int SCREEN_HEIGHT = 480;
|
|
const int TILE_SIZE = 40;
|
|
|
|
int main(int, char**) {
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) { logError("SDL_Init"); return 1; }
|
|
if (TTF_Init() != 0) { logError("TTF_Init"); SDL_Quit(); return 1; }
|
|
|
|
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
|
if (win == nullptr) { logError("CreateWindow"); SDL_Quit(); return 1;}
|
|
|
|
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
if (ren == nullptr) { cleanup(win); logError("CreateRenderer"); SDL_Quit(); return 1; }
|
|
|
|
SDL_Color color = {255, 255, 255, 255};
|
|
SDL_Texture *image = renderText("TTF Font Test :3", "worksans.ttf", color, 64, ren);
|
|
if(image == nullptr) {
|
|
cleanup(ren, win);
|
|
TTF_Quit();
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
|
|
int iw, ih;
|
|
SDL_QueryTexture(image, NULL, NULL, &iw, &ih);
|
|
int x = SCREEN_WIDTH / 2 - iw / 2;
|
|
int y = SCREEN_HEIGHT / 2 - ih / 2;
|
|
|
|
SDL_Event e;
|
|
bool quit = false;
|
|
while (!quit) {
|
|
while(SDL_PollEvent(&e)) {
|
|
if(e.type == SDL_QUIT) { quit = true; }
|
|
}
|
|
SDL_RenderClear(ren);
|
|
renderTexture(image, ren, x, y);
|
|
SDL_RenderPresent(ren);
|
|
}
|
|
|
|
cleanup(ren, win);
|
|
TTF_Quit();
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|