From: Trent Huber Date: Tue, 15 Oct 2024 02:21:48 +0000 (-0400) Subject: Added new features and interface X-Git-Url: https://trenthuber.com/code?a=commitdiff_plain;h=51f3653eb870d254c5aba66c7083d3974653be09;p=xpmview.git Added new features and interface - New user interface/font - Exporting files to png - Reloading current file --- diff --git a/.gitignore b/.gitignore index 9b1518a..5106540 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.DS_Store *swp *cbs bin/ diff --git a/fonts/Comic_Sans_MS.ttf b/fonts/Comic_Sans_MS.ttf new file mode 100644 index 0000000..831e3d8 Binary files /dev/null and b/fonts/Comic_Sans_MS.ttf differ diff --git a/src/main.c b/src/main.c index 1640627..d826cd4 100644 --- a/src/main.c +++ b/src/main.c @@ -3,60 +3,80 @@ #include #include "parser.h" +#define SUPPORT_IMAGE_EXPORT #include "raylib.h" -#include "stb_c_lexer.h" #include "utils.h" #define FILE_PATH_CAP 2048 +#define FONT_SIZE 48 -// TODO: Implement writing to PNG file (stb image write) -// TODO: Add ability to reload current file -// TODO: Update visual interface +Image image; +Texture2D texture; +bool have_texture; + +void create_texture_from_xpm_file(char *xpm_file_path) { + if (!parse_xpm_file(&image, xpm_file_path)) + have_texture = false; + else { + UnloadTexture(texture); + texture = LoadTextureFromImage(image); + have_texture = true; + } +} int main(int argc, char **argv) { - char file_path[FILE_PATH_CAP] = {0}; - Texture2D texture = {0}; - bool have_texture = false; + char xpm_file_path[FILE_PATH_CAP] = {0}; // Check if a file was given on the command line if (argc >= 2) { - strncpy(file_path, argv[2], FILE_PATH_CAP); - Image image = {0}; - if (!parse_xpm_file(&image, file_path)) - have_texture = false; - else { - UnloadTexture(texture); - texture = LoadTextureFromImage(image); - have_texture = true; - } + strncpy(xpm_file_path, argv[2], FILE_PATH_CAP); + create_texture_from_xpm_file(xpm_file_path); } - int screenWidth = 800; - int screenHeight = 600; + int default_screen_width = 800; + int default_screen_height = 600; SetTraceLogLevel(LOG_WARNING); - InitWindow(screenWidth, screenHeight, "simplexpm"); + InitWindow(default_screen_width, default_screen_height, "simplexpm"); + SetWindowState(FLAG_WINDOW_RESIZABLE); + + Font font = LoadFontEx("./fonts/Comic_Sans_MS.ttf", FONT_SIZE, NULL, 0); while (!WindowShouldClose()) { if (IsFileDropped()) { FilePathList file_paths = LoadDroppedFiles(); - strncpy(file_path, file_paths.paths[0], FILE_PATH_CAP); + strncpy(xpm_file_path, file_paths.paths[0], FILE_PATH_CAP); UnloadDroppedFiles(file_paths); - Image image = {0}; - if (!parse_xpm_file(&image, file_path)) - have_texture = false; - else { - UnloadTexture(texture); - texture = LoadTextureFromImage(image); - have_texture = true; - } + create_texture_from_xpm_file(xpm_file_path); + } + if (IsKeyDown(KEY_R) && have_texture) + create_texture_from_xpm_file(xpm_file_path); + if (IsKeyDown(KEY_S) && have_texture) { + char png_file_path[FILE_PATH_CAP] = {0}; + strncpy(png_file_path, xpm_file_path, strlen(xpm_file_path) - strlen(".xpm")); + strncat(png_file_path, ".png", strlen(".png")); + ExportImage(image, png_file_path); } BeginDrawing(); - ClearBackground(RAYWHITE); - if (have_texture) - DrawTexture(texture, screenWidth / 2 - texture.width / 2, screenHeight / 2 - texture.height / 2, WHITE); - else - DrawText(error_message, GetScreenWidth() / 2, GetScreenHeight() / 2, 24, RED); + ClearBackground(CLITERAL(Color){0xEC, 0xEC, 0xEC, 0xFF}); + + int screen_width = GetScreenWidth(); + int screen_height = GetScreenHeight(); + if (have_texture) { + float scale = screen_width * texture.height > screen_height * texture.width + ? (float)screen_height / texture.height + : (float)screen_width / texture.width; + Vector2 position = CLITERAL(Vector2){(screen_width - (texture.width * scale)) / 2, + (screen_height - (texture.height * scale)) / 2}; + DrawTextureEx(texture, position, 0, scale, WHITE); + } else { + Vector2 message_dimensions = MeasureTextEx(font, error_message, FONT_SIZE, 0), + message_placement = { + .x = (screen_width - message_dimensions.x) / 2, + .y = (screen_height - message_dimensions.y) / 2, + }; + DrawTextEx(font, error_message, message_placement, FONT_SIZE, 0, BLACK); + } EndDrawing(); } diff --git a/src/utils.c b/src/utils.c index 9588307..96611e4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,7 +5,7 @@ #include "utils.h" -char error_message[ERROR_MESSAGE_CAP] = "Drag and drop .xpm files here"; +char error_message[ERROR_MESSAGE_CAP] = "Drag and drop an XPM file here"; jmp_buf env; char *strstrip(char **string) {