summaryrefslogtreecommitdiff
path: root/include/glamac
diff options
context:
space:
mode:
Diffstat (limited to 'include/glamac')
-rw-r--r--include/glamac/core/glamac_errors.h38
-rw-r--r--include/glamac/core/glamacdef.h72
-rw-r--r--include/glamac/core/security.h41
-rw-r--r--include/glamac/data/glass_data.h56
-rw-r--r--include/glamac/graphics/glamac_render.h50
-rw-r--r--include/glamac/graphics/glamac_view.h109
-rw-r--r--include/glamac/input/glamac_events.h27
7 files changed, 393 insertions, 0 deletions
diff --git a/include/glamac/core/glamac_errors.h b/include/glamac/core/glamac_errors.h
new file mode 100644
index 0000000..2065600
--- /dev/null
+++ b/include/glamac/core/glamac_errors.h
@@ -0,0 +1,38 @@
+/**
+ * glamac_errors.h - Unified error handling for GlaMaC
+ *
+ * Copyright (C) 2025 https://optics-design.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * See the COPYING file for the full license text.
+ */
+#ifndef GLAMAC_ERRORS_H
+#define GLAMAC_ERRORS_H
+
+#include "glamacdef.h"
+#include "security.h"
+
+// Error codes for GlaMaC operations
+typedef enum {
+ GLAMAC_SUCCESS = 0,
+ GLAMAC_ERROR_MEMORY = -1,
+ GLAMAC_ERROR_FILE_NOT_FOUND = -2,
+ GLAMAC_ERROR_FILE_TOO_LARGE = -3,
+ GLAMAC_ERROR_INVALID_JSON = -4,
+ GLAMAC_ERROR_INVALID_PATH = -5,
+ GLAMAC_ERROR_BUFFER_OVERFLOW = -6,
+ GLAMAC_ERROR_MANUFACTURER_NOT_FOUND = -7,
+ GLAMAC_ERROR_NO_GLASSES_FOUND = -8,
+ GLAMAC_ERROR_INVALID_ARGUMENT = -9,
+ GLAMAC_ERROR_INVALID_GLASS_DATA = -10,
+ GLAMAC_ERROR_INVALID_MANUFACTURER = -11
+} GlamacResult;
+
+// Convert error code to human-readable string
+const char* glamac_error_string(GlamacResult error);
+
+#endif /* GLAMAC_ERRORS_H */ \ No newline at end of file
diff --git a/include/glamac/core/glamacdef.h b/include/glamac/core/glamacdef.h
new file mode 100644
index 0000000..46cbf82
--- /dev/null
+++ b/include/glamac/core/glamacdef.h
@@ -0,0 +1,72 @@
+/**
+ * glamacdef.h - header file containing various definitions for the GlaMaC.
+ *
+ * Copyright (C) 2025 https://optics-design.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * See the COPYING file for the full license text.
+ */
+#ifndef OPDECDEF_H
+#define OPDECDEF_H
+
+#include <stdint.h>
+#include <uchar.h>
+#include <stddef.h>
+#include <stdlib.h>
+/* Type definitions for consistent sizing across platforms. Idea taken from https://nullprogram.com/blog/2023/10/08/ (archive link: ) */
+typedef uint8_t u8;
+typedef char16_t c16;
+typedef int32_t b32;
+typedef int32_t i32;
+typedef uint32_t u32;
+typedef uint64_t u64;
+typedef float f32;
+typedef double f64;
+typedef uintptr_t uptr;
+#ifndef _WIN32
+typedef char byte;
+#else
+// On Windows, use unsigned char to match Windows headers
+typedef unsigned char byte;
+#endif
+typedef ptrdiff_t size;
+typedef size_t usize;
+/* Utility macros */
+#define countof(a) (size)(sizeof(a) / sizeof(*(a)))
+#define lengthof(s) (countof(s) - 1)
+
+/* Safe allocation macros with overflow protection */
+static inline void* safe_malloc(size_t element_size, size_t count) {
+ // Check for integer overflow in multiplication
+ if (count == 0 || element_size == 0) {
+ return NULL;
+ }
+ if (count > SIZE_MAX / element_size) {
+ return NULL; // Would overflow
+ }
+ return malloc(element_size * count);
+}
+
+static inline void* safe_calloc(size_t element_size, size_t count) {
+ // Check for integer overflow - calloc handles this internally but be explicit
+ if (count == 0 || element_size == 0) {
+ return NULL;
+ }
+ if (count > SIZE_MAX / element_size) {
+ return NULL; // Would overflow
+ }
+ return calloc(count, element_size);
+}
+
+// Safe allocation macros - use these instead of malloc directly
+#define new(t, n) ((t*)safe_malloc(sizeof(t), (size_t)(n)))
+#define new_zero(t, n) ((t*)safe_calloc(sizeof(t), (size_t)(n)))
+#define new_arr(t, arr) ((t*)safe_malloc(sizeof(arr), 1))
+
+// Legacy unsafe macro - deprecated, use new() or new_zero() instead
+// #define old_new(t, n) (t *)malloc(n*sizeof(t)) // DEPRECATED
+#endif /* OPDECDEF_H */
diff --git a/include/glamac/core/security.h b/include/glamac/core/security.h
new file mode 100644
index 0000000..6b8c811
--- /dev/null
+++ b/include/glamac/core/security.h
@@ -0,0 +1,41 @@
+/**
+ * security.h - Centralized security constants and limits for GlaMaC
+ *
+ * Copyright (C) 2025 https://optics-design.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * See the COPYING file for the full license text.
+ */
+#ifndef GLAMAC_SECURITY_H
+#define GLAMAC_SECURITY_H
+
+// File and data size limits
+#define MAX_JSON_FILE_SIZE (10 * 1024 * 1024) // 10MB limit for JSON file size
+#define MAX_JSON_STRING_LEN 1024 // Max string field length in JSON
+#define MAX_PATH_LEN 4096 // Max file path length
+#define MAX_MANUFACTURER_NAME_LEN 64 // Max manufacturer name length
+
+// Glass search utility limits
+#define FGLA_MAX_SEARCH_TERM_LEN 256 // Max search term length
+#define FGLA_MAX_GLASS_NAME_LEN 256 // Max glass name length
+#define FGLA_MAX_CATALOG_COUNT 10 // Max number of catalogs to search
+#define FGLA_MAX_NORMALIZED_LEN 512 // Max normalized string length
+#define FGLA_GLASS_CODE_LEN 6 // Fixed glass code length
+
+// Buffer limits for input validation
+#define MAX_INPUT_BUFFER_SIZE 8192 // General input buffer limit
+#define MAX_COMMAND_LINE_ARGS 32 // Max command line arguments
+
+// Memory allocation limits
+#define MAX_GLASS_ENTRIES 100000 // Max glass entries in database
+#define MAX_TEXTURE_CACHE_SIZE 1000 // Max cached textures for rendering
+
+// Security validation constants
+#define MIN_GLASS_CODE_DIGITS 6 // Minimum digits in glass code
+#define MAX_GLASS_CODE_DIGITS 6 // Maximum digits in glass code
+
+#endif /* GLAMAC_SECURITY_H */ \ No newline at end of file
diff --git a/include/glamac/data/glass_data.h b/include/glamac/data/glass_data.h
new file mode 100644
index 0000000..00f30c0
--- /dev/null
+++ b/include/glamac/data/glass_data.h
@@ -0,0 +1,56 @@
+/**
+ * glass_data.h - header file from glass_data.c.
+ *
+ * Copyright (C) 2025 https://optics-design.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * See the COPYING file for the full license text.
+ */
+#ifndef GLASS_DATA_H
+#define GLASS_DATA_H
+
+#include "../core/glamacdef.h" // For type definitions
+#include "../core/glamac_errors.h" // For error handling
+
+// Structure to represent an optical glass
+typedef struct {
+ byte name[50];
+ f32 abbeNumber; // X-axis (vd)
+ f32 refractiveIndex; // Y-axis (nd)
+ byte glass_code[20]; // Glass code from manufacturer
+ byte manufacturer[20]; // Manufacturer name
+} Glass;
+
+// Get number of glasses in the catalog
+u32 get_glass_count(void);
+
+// Get glass at index
+const Glass* get_glass(u32 index);
+
+// Get glass name
+const byte* get_glass_name(u32 index);
+
+// Find data range in glass catalog
+void find_glass_data_range(f32 *minAbbe, f32 *maxAbbe, f32 *minRI, f32 *maxRI);
+
+// Initialize glass data - call at program start
+void initialize_glass_data(void);
+
+// Load glasses from JSON file
+b32 load_glasses_from_json(const byte* json_path, const byte* manufacturer_filter);
+
+// Cleanup glass data resources
+void cleanup_glass_data(void);
+
+// Catalog management
+u32 get_catalog_count(void);
+const char* get_catalog_name(u32 catalog_index);
+const char* get_current_catalog_name(void);
+void set_current_catalog(u32 catalog_index);
+void cycle_catalog(i32 direction); // +1 for next, -1 for previous
+
+#endif /* GLASS_DATA_H */
diff --git a/include/glamac/graphics/glamac_render.h b/include/glamac/graphics/glamac_render.h
new file mode 100644
index 0000000..4c20751
--- /dev/null
+++ b/include/glamac/graphics/glamac_render.h
@@ -0,0 +1,50 @@
+/**
+ * glamac_render.h - header file from glamac_render.c.
+ */
+#ifndef GLAMAC_RENDER_H
+#define GLAMAC_RENDER_H
+
+#include <SDL3/SDL.h>
+#include <SDL3_ttf/SDL_ttf.h>
+#include "../core/glamacdef.h"
+#include "glamac_view.h"
+
+// Drawing primitives
+void draw_text(SDL_Renderer *renderer, TTF_Font *font, const char *text, i32 x, i32 y, SDL_Color color);
+void draw_filled_circle(SDL_Renderer *renderer, i32 centerX, i32 centerY, i32 radius);
+
+// UI element rendering
+void draw_axes(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view);
+void draw_grid(SDL_Renderer *renderer, const ViewState* view);
+void draw_glass_points(SDL_Renderer *renderer, TTF_Font *labelFont, const ViewState* view);
+void draw_glass_properties(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view);
+void draw_help_window(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view);
+
+// Main render function
+void render(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, TTF_Font *labelFont, ViewState* view);
+
+// Font management
+typedef struct {
+ TTF_Font *regular;
+ TTF_Font *title;
+ TTF_Font *label;
+} FontSet;
+
+// Load all required fonts
+b32 load_fonts(FontSet *fonts);
+
+// Load fonts with DPI-aware sizing
+b32 load_adaptive_fonts(FontSet *fonts, i32 windowWidth, i32 windowHeight, f32 dpi);
+
+// Free all fonts
+void free_fonts(FontSet *fonts);
+void clear_text_cache(void);
+
+// Simple glass property display helper
+void calculate_smart_window_position(i32 glassX, i32 glassY, i32 windowWidth, i32 windowHeight,
+ const ViewState* view, i32* windowX, i32* windowY);
+
+// Debug mode check
+b32 is_debug_mode(void);
+
+#endif /* GLAMAC_RENDER_H */
diff --git a/include/glamac/graphics/glamac_view.h b/include/glamac/graphics/glamac_view.h
new file mode 100644
index 0000000..58de2b1
--- /dev/null
+++ b/include/glamac/graphics/glamac_view.h
@@ -0,0 +1,109 @@
+/**
+ * glamac_view.h - header file from glamac_view.c.
+ */
+#ifndef GLAMAC_VIEW_H
+#define GLAMAC_VIEW_H
+
+#include <SDL3/SDL.h>
+#include "../core/glamacdef.h"
+
+// Constants for view
+#define MIN_PADDING 20 // Minimum padding in pixels
+#define MAX_PADDING_PERCENT 0.04f // Maximum padding as percentage of window size
+#define PAN_STEP 0.05f // Step size for keyboard panning
+#define ZOOM_FACTOR 1.2f // Zoom factor for zoom operations
+#define MIN_ZOOM 0.5f // Minimum zoom level
+#define MAX_ZOOM 10.0f // Maximum zoom level
+
+// Glass label positioning constants (adjustable parameters)
+#define LABEL_OFFSET_X 12 // Horizontal offset from glass point (pixels)
+#define LABEL_OFFSET_Y -8 // Vertical offset from glass point (pixels)
+#define LABEL_SIZE_SCALE 1.4f // Scale factor for glass name labels
+
+
+
+// State for zooming and panning
+typedef struct {
+ f32 zoomLevel;
+ f32 offsetX;
+ f32 offsetY;
+ i32 windowWidth;
+ i32 windowHeight;
+ f32 minAbbe;
+ f32 maxAbbe;
+ f32 minRI;
+ f32 maxRI;
+ b32 showHelp; // Flag to show/hide help window
+ b32 gKeyPressed; // Flag to track if 'g' was pressed
+ u32 gKeyTime; // Time when 'g' was pressed for sequence timing
+ i32 selectedGlass; // Index of selected glass (-1 if none)
+} ViewState;
+
+// Initialize a view state with default values
+void init_view_state(ViewState* view, i32 windowWidth, i32 windowHeight);
+
+// Refresh view state data range when catalog changes
+void refresh_view_data_range(ViewState* view);
+
+// Helper function to calculate adaptive padding
+static inline i32 get_adaptive_padding(const ViewState* view) {
+ i32 padding = (i32)(view->windowWidth * MAX_PADDING_PERCENT);
+ return padding > MIN_PADDING ? padding : MIN_PADDING;
+}
+
+// Convert glass data to screen coordinates with zoom and offset
+static inline void data_to_screen_coords(f32 abbeNumber, f32 refractiveIndex,
+ const ViewState* view, i32 *x, i32 *y) {
+ const i32 padding = get_adaptive_padding(view);
+
+ // Apply zoom and offset transformation
+ // FLIPPED: Use 1.0f - normalized to flip the Abbe number axis
+ f32 normalizedX = 1.0f - (abbeNumber - view->minAbbe) / (view->maxAbbe - view->minAbbe);
+ f32 normalizedY = (refractiveIndex - view->minRI) / (view->maxRI - view->minRI);
+
+ // Transform with zoom and offset
+ normalizedX = (normalizedX - 0.5f) * view->zoomLevel + 0.5f + view->offsetX;
+ normalizedY = (normalizedY - 0.5f) * view->zoomLevel + 0.5f + view->offsetY;
+
+ // Convert to screen coordinates
+ *x = padding + (i32)(normalizedX * (view->windowWidth - 2 * padding));
+ *y = view->windowHeight - padding - (i32)(normalizedY * (view->windowHeight - 2 * padding));
+}
+
+// Convert screen coordinates to data values
+static inline void screen_to_data_coords(i32 x, i32 y, const ViewState* view,
+ f32 *abbeNumber, f32 *refractiveIndex) {
+ const i32 padding = get_adaptive_padding(view);
+
+ // Convert to normalized coordinates
+ f32 normalizedX = (f32)(x - padding) / (view->windowWidth - 2 * padding);
+ f32 normalizedY = (f32)(view->windowHeight - y - padding) / (view->windowHeight - 2 * padding);
+
+ // Reverse transform with zoom and offset
+ normalizedX = (normalizedX - view->offsetX - 0.5f) / view->zoomLevel + 0.5f;
+ normalizedY = (normalizedY - view->offsetY - 0.5f) / view->zoomLevel + 0.5f;
+
+ // Convert to data values - FLIPPED axis logic for Abbe
+ *abbeNumber = view->maxAbbe - normalizedX * (view->maxAbbe - view->minAbbe);
+ *refractiveIndex = view->minRI + normalizedY * (view->maxRI - view->minRI);
+}
+
+// Find the nearest glass to a given screen position
+i32 find_nearest_glass(i32 x, i32 y, const ViewState* view, f32 maxDistance);
+
+// Calculate visible data range based on current view
+void get_visible_data_range(const ViewState* view, f32 *visibleMinAbbe, f32 *visibleMaxAbbe,
+ f32 *visibleMinRI, f32 *visibleMaxRI);
+
+// Handle mouse wheel zoom
+void handle_mouse_wheel_zoom(i32 wheelY, i32 mouseX, i32 mouseY, ViewState* view);
+
+// Toggle fullscreen
+void toggle_fullscreen(SDL_Window* window);
+
+// Reset view to default
+void reset_view(ViewState* view);
+
+
+
+#endif /* GLAMAC_VIEW_H */
diff --git a/include/glamac/input/glamac_events.h b/include/glamac/input/glamac_events.h
new file mode 100644
index 0000000..f06a703
--- /dev/null
+++ b/include/glamac/input/glamac_events.h
@@ -0,0 +1,27 @@
+/**
+ * glamac_events.h - header file from glamac_events.c.
+ */
+#ifndef GLAMAC_EVENTS_H
+#define GLAMAC_EVENTS_H
+
+#include <SDL3/SDL.h>
+#include "../core/glamacdef.h"
+#include "../graphics/glamac_view.h"
+
+// Process key event
+b32 process_key_event(SDL_KeyboardEvent *key, ViewState *view, SDL_Window *window, b32 *quit);
+
+// Process mouse button event
+b32 process_mouse_button(SDL_MouseButtonEvent *button, ViewState *view, i32 *lastMouseX, i32 *lastMouseY, b32 *dragging);
+
+// Process mouse motion event
+b32 process_mouse_motion(SDL_MouseMotionEvent *motion, ViewState *view, i32 *lastMouseX, i32 *lastMouseY, b32 dragging);
+
+// Process window event
+b32 process_window_event(SDL_WindowEvent *window_event, ViewState *view);
+
+// Process all events on queue
+b32 process_events(SDL_Event *event, ViewState *view, SDL_Window *window,
+ i32 *lastMouseX, i32 *lastMouseY, b32 *dragging, b32 *quit);
+
+#endif /* GLAMAC_EVENTS_H */
Back to https://optics-design.com