From 9db2f09e80b894e1e04038a37c6dcf769b77347c Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 3 Aug 2025 16:51:52 +0200 Subject: Initial commit --- include/glamac_events.h | 27 +++++++++++++++ include/glamac_render.h | 40 ++++++++++++++++++++++ include/glamac_view.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ include/glamacdef.h | 38 +++++++++++++++++++++ include/glass_data.h | 40 ++++++++++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 include/glamac_events.h create mode 100644 include/glamac_render.h create mode 100644 include/glamac_view.h create mode 100644 include/glamacdef.h create mode 100644 include/glass_data.h (limited to 'include') diff --git a/include/glamac_events.h b/include/glamac_events.h new file mode 100644 index 0000000..c4145c0 --- /dev/null +++ b/include/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 +#include "glamacdef.h" +#include "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 */ diff --git a/include/glamac_render.h b/include/glamac_render.h new file mode 100644 index 0000000..f18d53f --- /dev/null +++ b/include/glamac_render.h @@ -0,0 +1,40 @@ +/** + * glamac_render.h - header file from glamac_render.c. + */ +#ifndef GLAMAC_RENDER_H +#define GLAMAC_RENDER_H + +#include +#include +#include "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, const 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); + +// Free all fonts +void free_fonts(FontSet *fonts); +void clear_text_cache(void); + +#endif /* GLAMAC_RENDER_H */ diff --git a/include/glamac_view.h b/include/glamac_view.h new file mode 100644 index 0000000..8f0b984 --- /dev/null +++ b/include/glamac_view.h @@ -0,0 +1,90 @@ +/** + * glamac_view.h - header file from glamac_view.c. + */ +#ifndef GLAMAC_VIEW_H +#define GLAMAC_VIEW_H + +#include +#include "glamacdef.h" + +// Constants for view +#define PADDING_PERCENT 0.08f // 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 + +// 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); + +// 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 = (i32)(view->windowWidth * PADDING_PERCENT); + + // 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 = (i32)(view->windowWidth * PADDING_PERCENT); + + // 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/glamacdef.h b/include/glamacdef.h new file mode 100644 index 0000000..563df70 --- /dev/null +++ b/include/glamacdef.h @@ -0,0 +1,38 @@ +/** + * 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 +#include +#include +/* 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; +typedef char byte; +typedef ptrdiff_t size; +typedef size_t usize; +/* Utility macros */ +#define countof(a) (size)(sizeof(a) / sizeof(*(a))) +#define lengthof(s) (countof(s) - 1) +// #define new(a, t, n) (t *)alloc(a, sizeof(t), _Alignof(t), n) //From Nullprogram, not using currently +#define new(t, n) (t *)malloc(n*sizeof(t)) +#define new_arr(t,arr) (t)malloc(sizeof(arr)) +#endif /* OPDECDEF_H */ diff --git a/include/glass_data.h b/include/glass_data.h new file mode 100644 index 0000000..a5eb3c6 --- /dev/null +++ b/include/glass_data.h @@ -0,0 +1,40 @@ +/** + * 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 "glamacdef.h" // For type definitions + +// Structure to represent an optical glass +typedef struct { + byte name[50]; + f32 abbeNumber; // X-axis + f32 refractiveIndex; // Y-axis +} 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); + +#endif /* GLASS_DATA_H */ -- cgit v1.2.3