Initial commit

This commit is contained in:
2022-05-19 20:07:15 +02:00
commit 41a75f7422
17 changed files with 881 additions and 0 deletions

17
src/AMI.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "AMI.h"
AMI::AMI(SDL_Renderer* renderer)
: renderer{renderer}, ft_logo{renderer,
{.x = 182, .y = 0, .w = 116, .h = 40},
FT_LOGO_PATH},
choose{renderer,
{.x = 0, .y = 45, .w = 480, .h = 40},
"resources/Avenir-Book.ttf",
"Choose a Mission:"} {}
void AMI::draw() {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
ft_logo.draw();
choose.draw();
}

89
src/App.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "App.h"
#include "AMI.h"
#include <SDL2/SDL_image.h>
#include <fmt/format.h>
#include <stdexcept>
App::App() : view{AppView::AMI} { init_sdl(); }
App::~App() {
// Destroy window
SDL_DestroyRenderer(renderer);
renderer = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
}
void App::init_sdl() {
window = SDL_CreateWindow("FT22 STW Display", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
throw std::runtime_error(
fmt::format("Couldn't create window: {}", SDL_GetError()));
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
throw std::runtime_error(
fmt::format("Couldn't create renderer: {}", SDL_GetError()));
}
}
int App::run() {
ami = std::make_unique<AMI>(renderer);
running = true;
while (running) {
handle_events();
render();
SDL_Delay(1);
}
return 0;
}
void App::handle_events() {
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
running = false;
}
}
}
void App::render() {
switch (view) {
case AppView::AMI:
ami->draw();
break;
default:
throw std::runtime_error(fmt::format("Unknown view: {}", (int)view));
}
SDL_RenderPresent(renderer);
}
SDLManager::SDLManager() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
throw std::runtime_error(
fmt::format("Couldn't initialize SDL: {}", SDL_GetError()));
}
if (TTF_Init() == -1) {
throw std::runtime_error(
fmt::format("Couldn't initialize SDL_ttf: {}", TTF_GetError()));
}
int img_flags = IMG_INIT_PNG;
if (!(IMG_Init(img_flags) & img_flags)) {
throw std::runtime_error(
fmt::format("Couldn't initialitze SDL_image: {}", IMG_GetError()));
}
}
SDLManager::~SDLManager() {
IMG_Quit();
TTF_Quit();
SDL_Quit();
}

6
src/main.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "App.h"
int main() {
App app;
return app.run();
}

34
src/util.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "util.h"
#include <SDL2/SDL_image.h>
#include <fmt/format.h>
#include <stdexcept>
namespace util {
SDL_Texture* load_img(SDL_Renderer* renderer, const std::string& path) {
SDL_Surface* loaded = IMG_Load(path.c_str());
if (loaded == nullptr) {
throw std::runtime_error(
fmt::format("Unable to load image {}: {}", path, IMG_GetError()));
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, loaded);
if (texture == nullptr) {
throw std::runtime_error(fmt::format(
"Unable to convert image {} to texture: {}", path, IMG_GetError()));
}
SDL_FreeSurface(loaded);
return texture;
}
TTF_Font* load_font(const std::string& path, int pt) {
TTF_Font* font = TTF_OpenFont(path.c_str(), pt);
if (font == nullptr) {
throw std::runtime_error(
fmt::format("Unable to load font {}: {}", path, TTF_GetError()));
}
return font;
}
} // namespace util

75
src/widgets.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "widgets.h"
#include "util.h"
#include <SDL2/SDL_image.h>
#include <fmt/format.h>
#include <stdexcept>
Widget::Widget(SDL_Renderer* renderer, SDL_Rect rect)
: renderer{renderer}, dest_rect{rect} {}
void Widget::update_rect(SDL_Rect new_rect) { dest_rect = new_rect; }
TextureWidget::TextureWidget(SDL_Renderer* renderer, SDL_Rect dest_rect,
std::optional<SDL_Texture*> texture)
: Widget{renderer, dest_rect}, texture{texture} {}
TextureWidget::~TextureWidget() {
if (texture) {
SDL_DestroyTexture(*texture);
texture = nullptr;
}
}
void TextureWidget::draw() {
if (texture) {
SDL_RenderCopy(renderer, *texture, NULL, &dest_rect);
}
}
void TextureWidget::update_texture(SDL_Texture* new_texture) {
if (texture) {
SDL_DestroyTexture(*texture);
}
texture = new_texture;
}
ImageWidget::ImageWidget(SDL_Renderer* renderer, SDL_Rect dest_rect,
const std::string& path)
: TextureWidget{renderer, dest_rect, util::load_img(renderer, path)} {}
TextWidget::TextWidget(SDL_Renderer* renderer, SDL_Rect dest_rect,
const std::string& font_path,
const std::string& initial_text)
: TextureWidget{renderer, dest_rect, std::nullopt},
font{util::load_font(font_path, 28)}, text{initial_text} {
if (text != "") {
update_texture(generate_text(text));
}
}
TextWidget::~TextWidget() { TTF_CloseFont(font); }
void TextWidget::update_text(const std::string& new_text) {
if (text != new_text) {
update_texture(generate_text(new_text));
text = new_text;
}
}
SDL_Texture* TextWidget::generate_text(const std::string& text) {
SDL_Surface* surf =
TTF_RenderText_Solid(font, text.c_str(), {0xFF, 0xFF, 0XFF, 0xFF});
if (surf == nullptr) {
throw std::runtime_error(
fmt::format("Unable to render text surface: {}", TTF_GetError()));
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surf);
if (texture == nullptr) {
throw std::runtime_error(fmt::format(
"Unable to create texture from text surface: {}", SDL_GetError()));
}
return texture;
}