#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();
}