Skip to content

Commit

Permalink
refactor: convert remaining stuff to C
Browse files Browse the repository at this point in the history
  • Loading branch information
slimit75 committed May 2, 2024
1 parent 2833534 commit a9fc255
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ build/*
# IDE files
.vscode/*
.fleet/*
.idea/*
.idea/*
*.cbp
9 changes: 2 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
cmake_minimum_required(VERSION 3.24)
project(xpdraw)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)

# CMake Settings
set(CMAKE_CXX_FLAGS "-O3 -fPIC -pedantic -Wall")
set(CMAKE_C_FLAGS "-O3 -fPIC -pedantic -Wall")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
Expand All @@ -26,16 +24,13 @@ include_directories(${FREETYPE_INCLUDE_DIRS})
add_library(xpdraw
${CMAKE_CURRENT_SOURCE_DIR}/src/xpdraw.c
${CMAKE_CURRENT_SOURCE_DIR}/src/xpdraw/xpdraw.h
${CMAKE_CURRENT_SOURCE_DIR}/src/windows.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/windows.c
${CMAKE_CURRENT_SOURCE_DIR}/src/xpdraw/windows.h
${CMAKE_CURRENT_SOURCE_DIR}/src/tools.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/tools.c
${CMAKE_CURRENT_SOURCE_DIR}/src/xpdraw/tools.h
${CMAKE_CURRENT_SOURCE_DIR}/src/fonts.c
${CMAKE_CURRENT_SOURCE_DIR}/src/xpdraw/fonts.h
)
target_link_libraries(xpdraw
stdc++
)

# Include directories for both xpdraw & its dependants
target_include_directories(xpdraw
Expand Down
21 changes: 12 additions & 9 deletions src/fonts.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "xpdraw/fonts.h"

#include "xpdraw/tools.h"

int fonts_init = 0;
Expand All @@ -21,17 +22,17 @@ void xpd_font_cache(xpd_font_face_t *font, int size) {

if (font->letters[size][7].letter == 0u) {
// Tell FreeType what font size we want
FT_Set_Pixel_Sizes(font->ftFace, 0, (int) (size * 1.5));
FT_Set_Pixel_Sizes(font->ftFace, 0, (int)(size * 1.5));

// Load data for each available character
for (int i = CHAR_MIN; i <= CHAR_MAX; i++) {
FT_Load_Char(font->ftFace, i, FT_LOAD_RENDER);

font->letters[size][i].letter = i;
font->letters[size][i].letter = i;
font->letters[size][i].metrics = font->ftFace->glyph->metrics;

xpd_load_buffer(&font->letters[size][i].bitmap, font->ftFace->glyph->bitmap.buffer,
font->ftFace->glyph->bitmap.width, font->ftFace->glyph->bitmap.rows, GL_ALPHA);
font->ftFace->glyph->bitmap.width, font->ftFace->glyph->bitmap.rows, GL_ALPHA);
}
}
}
Expand All @@ -43,17 +44,18 @@ int xpd_text_length(xpd_font_face_t *font, const char *text, const int size) {
for (int i = 0; i < strlen(text); i++) {
FT_Glyph_Metrics text_metrics = font->letters[size][text[i]].metrics;
if (i == strlen(text) - 1) {
width += (int) ((text_metrics.width + text_metrics.horiBearingX) / 64);
} else {
width += (int) (text_metrics.horiAdvance / 64);
width += (int)((text_metrics.width + text_metrics.horiBearingX) / 64);
}
else {
width += (int)(text_metrics.horiAdvance / 64);
}
}

return width;
}

void xpd_text_draw(xpd_font_face_t *font, const char *text, int x, int y, int size, xpd_text_align_t align,
xpd_color_t textColor) {
xpd_color_t textColor) {
xpd_assert(font != NULL, "ERROR: Font not loaded!");

xpd_font_cache(font, size);
Expand All @@ -62,7 +64,8 @@ void xpd_text_draw(xpd_font_face_t *font, const char *text, int x, int y, int si
// Handle text alignment
if (align == xpdAlignCenter) {
x -= xpd_text_length(font, text, size) / 2;
} else if (align == xpdAlignRight) {
}
else if (align == xpdAlignRight) {
x -= xpd_text_length(font, text, size);
}

Expand All @@ -76,7 +79,7 @@ void xpd_text_draw(xpd_font_face_t *font, const char *text, int x, int y, int si
// Fetch & draw texture
xpd_texture_t image = font->letters[size][text[i]].bitmap;
xpd_draw_texture(&image, x + (text_metrics.horiBearingX / 64), y + y_offset, image.width, image.height,
textColor);
textColor);

// Advance to the next character
x += text_metrics.horiAdvance / 64;
Expand Down
13 changes: 6 additions & 7 deletions src/tools.cpp → src/tools.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "xpdraw/tools.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <XPLMPlugin.h>
#include <XPLMUtilities.h>

Expand All @@ -20,10 +21,8 @@ char *xpd_tools_plugin_fp() {
XPLMEnableFeature("XPLM_USE_NATIVE_PATHS", 1);

if (strlen(pluginPath) == 0) {
XPLMGetPluginInfo(XPLMGetMyID(), nullptr, pluginPath, nullptr, nullptr);
std::string pluginPathTemp = pluginPath;
pluginPathTemp.erase(pluginPathTemp.end() - 10, pluginPathTemp.end());
strcpy(pluginPath, pluginPathTemp.c_str());
XPLMGetPluginInfo(XPLMGetMyID(), NULL, pluginPath, NULL, NULL);
pluginPath[strlen(pluginPath) - 10] = '\0';
}

return pluginPath;
Expand All @@ -41,7 +40,7 @@ char *xpd_tools_xp_fp() {

int xpd_tools_xp_ver() {
if (xpVersion == -1) {
XPLMGetVersions(&xpVersion, nullptr, nullptr);
XPLMGetVersions(&xpVersion, NULL, NULL);
xpVersion = xpVersion / 1000;
}
return xpVersion;
Expand All @@ -54,4 +53,4 @@ void xpd_assert(int exp, char *msg) {
XPLMDebugString(str);
abort();
}
}
}
51 changes: 27 additions & 24 deletions src/windows.cpp → src/windows.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "xpdraw/windows.h"

#include <stdlib.h>
#include <XPLMDisplay.h>

// "Dummy" callbacks. Adapted from https://developer.x-plane.com/code-sample/hello-world-sdk-3/
void base_draw(XPLMWindowID in_window_id, void *in_refcon) {
};
void base_draw(XPLMWindowID in_window_id, void *in_refcon) {}

int base_mouse(XPLMWindowID in_window_id, int x, int y, int is_down, void *in_refcon) {
return 0;
Expand All @@ -19,19 +19,18 @@ int base_wheel(XPLMWindowID in_window_id, int x, int y, int wheel, int clicks, v
}

void base_key(XPLMWindowID in_window_id, char key, XPLMKeyFlags flags, char virtual_key, void *in_refcon,
int losing_focus) {
}
int losing_focus) {}

// Real functions
void xpd_win_new(xpd_win_t *inWindow, int width, int height) {
inWindow->width = width;
inWindow->width = width;
inWindow->height = height;
inWindow->buffer = new unsigned char[width * height * 4];
inWindow->buffer = malloc(width * height * 4 * sizeof(unsigned char)); //new unsigned char[width * height * 4];

inWindow->drawFunc = base_draw;
inWindow->clickFunc = base_mouse;
inWindow->wheelFunc = base_wheel;
inWindow->keyFunc = base_key;
inWindow->drawFunc = base_draw;
inWindow->clickFunc = base_mouse;
inWindow->wheelFunc = base_wheel;
inWindow->keyFunc = base_key;
inWindow->cursorFunc = base_cursor;
}

Expand All @@ -55,25 +54,29 @@ void xpd_win_set_key_cb(xpd_win_t *inWindow, XPLMHandleKey_f new_cb) {
inWindow->keyFunc = new_cb;
}

void xpd_win_create(xpd_win_t *inWindow, const char *title, int winLeft, int winDown) {
inWindow->params.structSize = sizeof(inWindow->params);
inWindow->params.visible = 1;
inWindow->params.drawWindowFunc = inWindow->drawFunc;
inWindow->params.handleMouseClickFunc = inWindow->clickFunc;
inWindow->params.handleRightClickFunc = inWindow->clickFunc;
inWindow->params.handleMouseWheelFunc = inWindow->wheelFunc;
inWindow->params.handleKeyFunc = inWindow->keyFunc;
inWindow->params.handleCursorFunc = inWindow->cursorFunc;
inWindow->params.refcon = nullptr;
inWindow->params.layer = xplm_WindowLayerFloatingWindows;
void xpd_win_create(xpd_win_t *inWindow, const char *title) {
xpd_win_create2(inWindow, title, 50, 150);
}

void xpd_win_create2(xpd_win_t *inWindow, const char *title, int winLeft, int winDown) {
inWindow->params.structSize = sizeof(inWindow->params);
inWindow->params.visible = 1;
inWindow->params.drawWindowFunc = inWindow->drawFunc;
inWindow->params.handleMouseClickFunc = inWindow->clickFunc;
inWindow->params.handleRightClickFunc = inWindow->clickFunc;
inWindow->params.handleMouseWheelFunc = inWindow->wheelFunc;
inWindow->params.handleKeyFunc = inWindow->keyFunc;
inWindow->params.handleCursorFunc = inWindow->cursorFunc;
inWindow->params.refcon = NULL;
inWindow->params.layer = xplm_WindowLayerFloatingWindows;
inWindow->params.decorateAsFloatingWindow = xplm_WindowDecorationRoundRectangle;

int left, bottom, right, top;
XPLMGetScreenBoundsGlobal(&left, &top, &right, &bottom);
inWindow->params.left = left + winLeft;
inWindow->params.left = left + winLeft;
inWindow->params.bottom = bottom + winDown;
inWindow->params.right = inWindow->params.left + inWindow->width - 20;
inWindow->params.top = inWindow->params.bottom + inWindow->height - 20;
inWindow->params.right = inWindow->params.left + inWindow->width - 20;
inWindow->params.top = inWindow->params.bottom + inWindow->height - 20;

inWindow->windowID = XPLMCreateWindowEx(&inWindow->params);

Expand Down
2 changes: 1 addition & 1 deletion src/xpdraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int anchor_x = 0;
int anchor_y = 0;

void xpd_load_buffer(xpd_texture_t *texture, void *buffer, unsigned int width, unsigned int height, GLint format) {
texture->width = (int)width;
texture->width = (int)width;
texture->height = (int)height;

glGenTextures(1, &texture->gl_texture);
Expand Down
2 changes: 1 addition & 1 deletion src/xpdraw/fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int xpd_text_length(xpd_font_face_t *font, const char *text, int size);
* @param color Color of the text; defaults to white
*/
void xpd_text_draw(xpd_font_face_t *font, const char *text, int x, int y, int size, xpd_text_align_t align,
xpd_color_t color);
xpd_color_t color);

#ifdef __cplusplus
}
Expand Down
10 changes: 9 additions & 1 deletion src/xpdraw/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ void xpd_win_set_wheel_cb(xpd_win_t *inWindow, XPLMHandleMouseWheel_f new_cb);
*/
void xpd_win_set_key_cb(xpd_win_t *inWindow, XPLMHandleKey_f new_cb);

/**
* @brief Display a window created with xpd_win_new
*
* @param inWindow Window to start rendering
* @param title Title of the window
*/
void xpd_win_create(xpd_win_t *inWindow, const char *title);

/**
* @brief Display a window created with xpd_win_new
*
Expand All @@ -106,7 +114,7 @@ void xpd_win_set_key_cb(xpd_win_t *inWindow, XPLMHandleKey_f new_cb);
* @param winLeft Position of the left edge of the window
* @param winDown Position of the bottom edge of the window
*/
void xpd_win_create(xpd_win_t *inWindow, const char *title, int winLeft = 50, int winDown = 150);
void xpd_win_create2(xpd_win_t *inWindow, const char *title, int winLeft, int winDown);

/**
* @brief Set the minimum & maximum sizes of a window
Expand Down

0 comments on commit a9fc255

Please sign in to comment.