Create Helper files

This commit is contained in:
Mathias Wagner 2024-11-25 12:03:39 +01:00
parent 1c0dd4932d
commit 7f31271a8d
2 changed files with 46 additions and 0 deletions

36
Helper.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "Helper.h"
void drawTexture(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y, int w, int h) {
SDL_Rect src;
src.x = 0;
src.y = 0;
SDL_QueryTexture(texture, nullptr, nullptr, &src.w, &src.h);
SDL_Rect dist;
dist.x = x;
dist.y = y;
dist.w = w;
dist.h = h;
SDL_RenderCopy(renderer, texture, &src, &dist);
}
void drawText(SDL_Renderer *renderer, TTF_Font *font, const std::string text, int x, int y, int w, int h) {
SDL_Surface *surface = TTF_RenderText_Solid(font, text.c_str(), {255, 255, 255});
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect src;
src.x = 0;
src.y = 0;
SDL_QueryTexture(texture, nullptr, nullptr, &src.w, &src.h);
SDL_Rect dist;
dist.x = x;
dist.y = y;
dist.w = w;
dist.h = h;
SDL_RenderCopy(renderer, texture, &src, &dist);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}

10
Helper.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef CPP_LEARNING_HELPER_H
#define CPP_LEARNING_HELPER_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <string>
void drawTexture(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y, int w, int h);
void drawText(SDL_Renderer *renderer, TTF_Font *font, const std::string text, int x, int y, int w, int h);
#endif