diff options
author | Slendi <slendi@socopon.com> | 2024-03-05 23:42:54 +0200 |
---|---|---|
committer | Slendi <slendi@socopon.com> | 2024-03-05 23:42:54 +0200 |
commit | 8a70a42f859273cfb6dfaf608068856f7cdf17bd (patch) | |
tree | 591ee5ed766d11d73e02a549282296afd5423344 | |
parent | 117f0b15fb18abcd18d5b4bd972f438bbaec8836 (diff) |
Try and change indentation to spaces
Signed-off-by: Slendi <slendi@socopon.com>
-rw-r--r-- | main.cpp | 443 |
1 files changed, 219 insertions, 224 deletions
@@ -10,10 +10,10 @@ struct Config { enum class ProgressBarPosition { - TOP, - BOTTOM, - LEFT, - RIGHT, + TOP, + BOTTOM, + LEFT, + RIGHT, }; int width = 1280; @@ -34,8 +34,8 @@ struct Config { struct Slide { enum class Type { - TEXT, - IMAGE, + TEXT, + IMAGE, }; Type type; std::string text; @@ -47,213 +47,208 @@ using Slides = std::vector<Slide>; class Application { public: Application(Config config, Slides slides) - : m_config(config), m_slides(slides) { - int w = m_config.width; - int h = m_config.height; - if (m_config.fullscreen) { - w = GetMonitorWidth(GetCurrentMonitor()); - h = GetMonitorHeight(GetCurrentMonitor()); - } - - SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_MSAA_4X_HINT); - InitWindow(w, h, "Presentation"); - SetTargetFPS(30); - - auto time_begin = std::chrono::high_resolution_clock::now(); - m_font = LoadFontEx(m_config.font_path.c_str(), 128, nullptr, 0x2030); - auto time_end = std::chrono::high_resolution_clock::now(); - std::cout << "Loading font took " - << std::chrono::duration_cast<std::chrono::milliseconds>( - time_end - time_begin) - .count() - << "ms" << std::endl; - std::cout.flush(); + : m_config(config), m_slides(slides) { + + int w = m_config.width; + int h = m_config.height; + + if (m_config.fullscreen) { + w = GetMonitorWidth(GetCurrentMonitor()); + h = GetMonitorHeight(GetCurrentMonitor()); + } + + SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_MSAA_4X_HINT); + InitWindow(w, h, "Presentation"); + SetTargetFPS(30); + + auto time_begin = std::chrono::high_resolution_clock::now(); + m_font = LoadFontEx(m_config.font_path.c_str(), 128, nullptr, 0x2030); + auto time_end = std::chrono::high_resolution_clock::now(); + std::cout << "Loading font took " + << std::chrono::duration_cast<std::chrono::milliseconds>( + time_end - time_begin) + .count() + << "ms" << std::endl; + std::cout.flush(); SetTextureFilter(m_font.texture, TEXTURE_FILTER_TRILINEAR); - int display_width = GetMonitorWidth(GetCurrentMonitor()); - int display_height = GetMonitorHeight(GetCurrentMonitor()); + int display_width = GetMonitorWidth(GetCurrentMonitor()); + int display_height = GetMonitorHeight(GetCurrentMonitor()); - if (m_config.fullscreen) - ToggleFullscreen(); + if (m_config.fullscreen) + ToggleFullscreen(); - SetExitKey(KEY_Q); + SetExitKey(KEY_Q); } ~Application() { - if (m_texture.id != 0) - UnloadTexture(m_texture); - CloseWindow(); + if (m_texture.id != 0) + UnloadTexture(m_texture); + CloseWindow(); } void run() { - HideCursor(); - while (!WindowShouldClose()) { - float font_scale = - (GetMonitorWidth(GetCurrentMonitor()) / (float)GetScreenWidth() + - GetMonitorHeight(GetCurrentMonitor()) / (float)GetScreenHeight()) / - 2.0f; - - if (IsKeyPressed(KEY_F)) - ToggleFullscreen(); - - if (IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_SPACE) || - IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_J) || - IsKeyPressed(KEY_L) || IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) - advance_slide(); - - if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_K) || - IsKeyPressed(KEY_H) || IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) - previous_slide(); - - if (IsKeyPressed(KEY_HOME) || - (IsKeyPressed(KEY_G) && - !(IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))) || - IsKeyPressed(KEY_ZERO)) - beginning_slide(); - - if (IsKeyPressed(KEY_END) || - (IsKeyPressed(KEY_G) && - (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))) || - IsKeyPressed(KEY_NINE)) - end_slide(); - - BeginDrawing(); - ClearBackground(m_config.background_color); - - SetTextLineSpacing(font_scale * m_config.font_size); - if (m_slides[m_current_slide].type == Slide::Type::TEXT) { - // Draw centered - auto &txt = m_slides[m_current_slide].text; - auto measurement = MeasureTextEx(m_font, txt.c_str(), - m_config.font_size * font_scale, 0); - - float scale = 1.0f; - if (measurement.x > - GetScreenWidth() - m_config.padding_x * font_scale * 2) { - scale = (GetScreenWidth() - m_config.padding_x * font_scale * 2) / - measurement.x; - } - if (measurement.y > - GetScreenHeight() - m_config.padding_y * font_scale * 2) { - scale = (GetScreenHeight() - m_config.padding_y * font_scale * 2) / - measurement.y; - } - - measurement.x *= scale; - measurement.y *= scale; - SetTextLineSpacing(font_scale * m_config.font_size * scale); - - DrawTextEx(m_font, txt.c_str(), - { - .x = (GetScreenWidth() - measurement.x) / 2.0f, - .y = (GetScreenHeight() - measurement.y) / 2.0f, - }, - m_config.font_size * font_scale * scale, 0, - m_config.text_color); - } else if (m_slides[m_current_slide].type == Slide::Type::IMAGE && - m_texture.id != 0) { - float yoffset = 0; - - if (m_slides[m_current_slide].text.size() > 0) { - auto txt = m_slides[m_current_slide].text; - auto measurement = MeasureTextEx(m_font, txt.c_str(), - m_config.font_size * font_scale, 0); - yoffset = measurement.y + m_config.padding_y; - DrawTextEx(m_font, txt.c_str(), - { - .x = (GetScreenWidth() - measurement.x) / 2.0f, - .y = m_config.padding_y * font_scale, - }, - m_config.font_size * font_scale, 0, m_config.text_color); - } - - float scale = - std::min((GetScreenWidth() - m_config.padding_x * font_scale * 2) / - m_texture.width, - (GetScreenHeight() - yoffset * font_scale - - m_config.padding_y * font_scale * 2) / - m_texture.height); - - DrawTextureEx( - m_texture, - { - .x = (GetScreenWidth() - m_texture.width * scale) / 2.0f, - .y = (GetScreenHeight() - m_texture.height * scale) / 2.0f + - yoffset * font_scale * 0.5f, - }, - 0, scale, WHITE); - } - - float progress = m_current_slide / ((float)m_slides.size() - 1); - if (m_config.progress_bar_position == Config::ProgressBarPosition::TOP || - m_config.progress_bar_position == - Config::ProgressBarPosition::BOTTOM) { - DrawRectangle( - 0, - m_config.progress_bar_position == Config::ProgressBarPosition::TOP - ? 0 - : GetScreenHeight() - 8, - GetScreenWidth() * progress, 8, m_config.progress_bar_color); - } else if (m_config.progress_bar_position == - Config::ProgressBarPosition::LEFT) { - DrawRectangle(0, 0, 8, GetScreenHeight() * progress, - m_config.progress_bar_color); - } else if (m_config.progress_bar_position == - Config::ProgressBarPosition::RIGHT) { - DrawRectangle(GetScreenWidth() - 8, 0, 8, GetScreenHeight() * progress, - m_config.progress_bar_color); - } - - auto text = TextFormat("(%d/%d)", m_current_slide + 1, m_slides.size()); - auto measurement = MeasureTextEx(m_font, text, 20, 0); - DrawTextEx(m_font, text, - { - .x = (GetScreenWidth() - measurement.x) / 2.0f, - .y = GetScreenHeight() - 8 - measurement.y, - }, - 20, 0, m_config.progress_bar_color); - - EndDrawing(); - } + HideCursor(); + while (!WindowShouldClose()) { + float font_scale = + (GetMonitorWidth(GetCurrentMonitor()) / (float)GetScreenWidth() + + GetMonitorHeight(GetCurrentMonitor()) / (float)GetScreenHeight()) / + 2.0f; + + if (IsKeyPressed(KEY_F)) + ToggleFullscreen(); + + if (IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_SPACE) || + IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_J) || + IsKeyPressed(KEY_L) || IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + advance_slide(); + + if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_K) || + IsKeyPressed(KEY_H) || IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) + previous_slide(); + + if (IsKeyPressed(KEY_HOME) || + (IsKeyPressed(KEY_G) && + !(IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))) || + IsKeyPressed(KEY_ZERO)) + beginning_slide(); + + if (IsKeyPressed(KEY_END) || + (IsKeyPressed(KEY_G) && + (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))) || + IsKeyPressed(KEY_NINE)) + end_slide(); + + BeginDrawing(); + ClearBackground(m_config.background_color); + + SetTextLineSpacing(font_scale * m_config.font_size); + if (m_slides[m_current_slide].type == Slide::Type::TEXT) { + // Draw centered + auto &txt = m_slides[m_current_slide].text; + auto measurement = MeasureTextEx(m_font, txt.c_str(), + m_config.font_size * font_scale, 0); + + float scale = 1.0f; + if (measurement.x > + GetScreenWidth() - m_config.padding_x * font_scale * 2) { + scale = (GetScreenWidth() - m_config.padding_x * font_scale * 2) / measurement.x; + } + if (measurement.y > + GetScreenHeight() - m_config.padding_y * font_scale * 2) { + scale = (GetScreenHeight() - m_config.padding_y * font_scale * 2) / measurement.y; + } + + measurement.x *= scale; + measurement.y *= scale; + SetTextLineSpacing(font_scale * m_config.font_size * scale); + + DrawTextEx(m_font, txt.c_str(), + { + .x = (GetScreenWidth() - measurement.x) / 2.0f, + .y = (GetScreenHeight() - measurement.y) / 2.0f, + }, + m_config.font_size * font_scale * scale, 0, + m_config.text_color); + + } else if (m_slides[m_current_slide].type == Slide::Type::IMAGE && + m_texture.id != 0) { + float yoffset = 0; + + if (m_slides[m_current_slide].text.size() > 0) { + auto txt = m_slides[m_current_slide].text; + auto measurement = MeasureTextEx(m_font, txt.c_str(), + m_config.font_size * font_scale, 0); + yoffset = measurement.y + m_config.padding_y; + DrawTextEx(m_font, txt.c_str(), + { + .x = (GetScreenWidth() - measurement.x) / 2.0f, + .y = m_config.padding_y * font_scale, + }, + m_config.font_size * font_scale, 0, m_config.text_color); + } + + float scale = + std::min((GetScreenWidth() - m_config.padding_x * font_scale * 2) / + m_texture.width, + (GetScreenHeight() - yoffset * font_scale - + m_config.padding_y * font_scale * 2) / + m_texture.height); + + DrawTextureEx( + m_texture, + { + .x = (GetScreenWidth() - m_texture.width * scale) / 2.0f, + .y = (GetScreenHeight() - m_texture.height * scale) / 2.0f + yoffset * font_scale * 0.5f, + }, + 0, scale, WHITE); + } + + float progress = m_current_slide / ((float)m_slides.size() - 1); + if (m_config.progress_bar_position == Config::ProgressBarPosition::TOP || + m_config.progress_bar_position == Config::ProgressBarPosition::BOTTOM) { + DrawRectangle( + 0, + m_config.progress_bar_position == Config::ProgressBarPosition::TOP + ? 0 + : GetScreenHeight() - 8, + GetScreenWidth() * progress, 8, m_config.progress_bar_color); + } else if (m_config.progress_bar_position == Config::ProgressBarPosition::LEFT) { + DrawRectangle(0, 0, 8, GetScreenHeight() * progress, m_config.progress_bar_color); + } else if (m_config.progress_bar_position == Config::ProgressBarPosition::RIGHT) { + DrawRectangle(GetScreenWidth() - 8, 0, 8, GetScreenHeight() * progress, m_config.progress_bar_color); + } + + auto text = TextFormat("(%d/%d)", m_current_slide + 1, m_slides.size()); + auto measurement = MeasureTextEx(m_font, text, 20, 0); + DrawTextEx(m_font, text, + { + .x = (GetScreenWidth() - measurement.x) / 2.0f, + .y = GetScreenHeight() - 8 - measurement.y, + }, + 20, 0, m_config.progress_bar_color); + + EndDrawing(); + } } void advance_slide() { - m_current_slide++; - if (m_current_slide >= m_slides.size()) { - m_current_slide--; - return; - } + m_current_slide++; + if (m_current_slide >= m_slides.size()) { + m_current_slide--; + return; + } - handle_image_slide(); + handle_image_slide(); } void previous_slide() { - m_current_slide--; - if (m_current_slide < 0) { - m_current_slide++; - return; - } + m_current_slide--; + if (m_current_slide < 0) { + m_current_slide++; + return; + } - handle_image_slide(); + handle_image_slide(); } void handle_image_slide() { - if (m_slides[m_current_slide].type == Slide::Type::IMAGE) { - if (m_texture.id != 0) - UnloadTexture(m_texture); - m_texture = LoadTexture(m_slides[m_current_slide].image_path.c_str()); - } + if (m_slides[m_current_slide].type == Slide::Type::IMAGE) { + if (m_texture.id != 0) + UnloadTexture(m_texture); + m_texture = LoadTexture(m_slides[m_current_slide].image_path.c_str()); + } } void beginning_slide() { - m_current_slide = 0; - handle_image_slide(); + m_current_slide = 0; + handle_image_slide(); } void end_slide() { - m_current_slide = m_slides.size() - 1; - handle_image_slide(); + m_current_slide = m_slides.size() - 1; + handle_image_slide(); } private: @@ -273,41 +268,41 @@ Slides parse_input(std::istream *stream) { bool just_started_parsing = true; for (std::string line; std::getline(*stream, line);) { - if (line.empty()) { - if (!just_started_parsing) { - if (c_slide.text.size() > 0 || !c_slide.image_path.empty()) { - if (c_slide.text.size() > 0) - c_slide.text.pop_back(); - slides.push_back(c_slide); - c_slide = {}; - } - } - continue; - } - - if (line[0] == '#') - continue; - - just_started_parsing = false; - - if (line[0] == '@') { - c_slide.type = Slide::Type::IMAGE; - if (!c_slide.image_path.empty()) { - c_slide.text.pop_back(); - slides.push_back(c_slide); - c_slide = {}; - } - c_slide.image_path = line.substr(1); - } else { - c_slide.type = Slide::Type::TEXT; - c_slide.text += line + '\n'; - } - } + if (line.empty()) { + if (!just_started_parsing) { + if (c_slide.text.size() > 0 || !c_slide.image_path.empty()) { + if (c_slide.text.size() > 0) + c_slide.text.pop_back(); + slides.push_back(c_slide); + c_slide = {}; + } + } + continue; + } + + if (line[0] == '#') + continue; + + just_started_parsing = false; + + if (line[0] == '@') { + c_slide.type = Slide::Type::IMAGE; + if (!c_slide.image_path.empty()) { + c_slide.text.pop_back(); + slides.push_back(c_slide); + c_slide = {}; + } + c_slide.image_path = line.substr(1); + } else { + c_slide.type = Slide::Type::TEXT; + c_slide.text += line + '\n'; + } + } if (!just_started_parsing) { - if (c_slide.text.size() > 0 && '\n' == c_slide.text.back()) - c_slide.text.pop_back(); - slides.push_back(c_slide); + if (c_slide.text.size() > 0 && '\n' == c_slide.text.back()) + c_slide.text.pop_back(); + slides.push_back(c_slide); } return slides; @@ -316,12 +311,12 @@ Slides parse_input(std::istream *stream) { int main(int argc, char **argv) { std::istream *is = &std::cin; if (argc > 1) { - auto file = new std::ifstream(argv[1]); - if (!file->is_open()) { - std::cerr << "Could not open file: " << argv[1] << std::endl; - return 1; - } - is = file; + auto file = new std::ifstream(argv[1]); + if (!file->is_open()) { + std::cerr << "Could not open file: " << argv[1] << std::endl; + return 1; + } + is = file; } auto slides = parse_input(is); |