]> Trent Huber's Code - xpmview.git/commitdiff
Added new features and interface
authorTrent Huber <trentmhuber@gmail.com>
Tue, 15 Oct 2024 02:21:48 +0000 (22:21 -0400)
committerTrent Huber <trentmhuber@gmail.com>
Tue, 15 Oct 2024 02:21:48 +0000 (22:21 -0400)
 - New user interface/font
 - Exporting files to png
 - Reloading current file

.gitignore
fonts/Comic_Sans_MS.ttf [new file with mode: 0644]
src/main.c
src/utils.c

index 9b1518a2d30214e02cf9e0778abc2193e7cf2fa6..5106540120c6514c96e8ff376750250dabd62c73 100644 (file)
@@ -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 (file)
index 0000000..831e3d8
Binary files /dev/null and b/fonts/Comic_Sans_MS.ttf differ
index 16406273ae1eb14f013c87c2e14a35c2d7d28717..d826cd4c666189e687b6c865a3d65d13d92c48d9 100644 (file)
@@ -3,60 +3,80 @@
 #include <string.h>
 
 #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();
    }
 
index 9588307662541e195a449ea12fdf2c04a0f0afce..96611e4edf2e8af406d85dbce73dfec213e33f77 100644 (file)
@@ -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) {