Compare commits

...

2 Commits

Author SHA1 Message Date
Ulysse Cura 472a16dd2e Adding building instructions. 2026-09-29 13:16:39 +02:00
Ulysse Cura 3dfe19d64c Fixed compatibility with Windows. 2026-09-29 12:38:37 +02:00
5 changed files with 140 additions and 36 deletions

View File

@ -4,6 +4,85 @@
## Description ## Description
A multi-platform 2D_Engine made in C. A multi-platform 2D_Engine made in C.
## Summary
1. [Licenses](#licences)
2. [Building](#building)
## Licences ## Licences
- This program is open-source : you can redistribute and/or modify it under the term of the **GNU GPLv3**. - This program is open-source : you can redistribute and/or modify it under the term of the **GNU GPLv3**.
Copyright (C) 2025 - 2026 Ulysse Cura. See [LICENSE](LICENSE.txt) or [gnu.org/licenses/gpl-3.0.html](https://www.gnu.org/licenses/gpl-3.0.html). Copyright (C) 2025 - 2026 Ulysse Cura. See [LICENSE](LICENSE.txt) or [gnu.org/licenses/gpl-3.0.html](https://www.gnu.org/licenses/gpl-3.0.html).
## Building
### Setup
### Linux
The only tools needed are :
- gcc
- make
- pkg-config
With the librairies (tested with version) :
- sdl3 version >= 3.2.1O
- libpng version >= 1.6.48
- json-c version >= 0.18
On Debian, the command to install dependencies is :
```
sudo apt install gcc make pkg-config libsdl3-dev libpng-dev libjson-c-dev
```
On Nix, the shell.nix file manage the dependencies for you, but it might not be up to date, as support for Nix is not a priority.
### Windows
To build on windows you will need MSYS2 or Cygwin.
For the moment only MSYS2 is officialy supported.
The only tools needed are :
- x86_64-w64-mingw32-gcc
- make
- x86_64-w64-mingw32-pkg-config
With the librairies (tested with version) :
- sdl3 version >= 3.4.10
- libpng version >= 1.6.58
- json-c version >= 0.18
On MSYS2, the command to install dependencies is :
```
pacman -S mingw-w64-ucrt-x86_64-gcc make mingw-w64-ucrt-x86_64-pkg-config mingw-w64-ucrt-x86_64-sdl3 mingw-w64-ucrt-x86_64-libpng mingw-w64-ucrt-x86_64-json-c
```
### Cross compiling
For the moment cross compiling from linux to windows is the only supported cross compiling option.
To do that you will need the following tools :
- x86_64-w64-mingw32-gcc
- make
- x86_64-w64-mingw32-pkg-config
With the librairies (tested with version) :
- sdl3 version >= 3.4.10 (mingw devel)
- libpng version >= 1.6.58
- json-c version >= 0.18
On Debian the command to install dependencies is :
```
sudo apt install mingw-w64 mingw-w64-tools mingw-w64-ucrt64-dev make libpng-dev libjson-c-dev
```
For the sdl3 mingw devel package you will need to install it manually.
See [README-windows](https://wiki.libsdl.org/SDL3/README-windows).
Or [releases](https://github.com/libsdl-org/SDL/releases).
### Commands
The [configure](configure) script automaticaly test compilers and libs versions for your target host.
See `./configure --help` for usage.
The aim is to support linux, windows and the fxcg50 calculator.
However the option `--target-host` only officialy supports `linux` and `windows` but `fxcg50` is also an accepted option.

View File

@ -15,42 +15,42 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
return EXIT_FAILURE; return EXIT_FAILURE;
} }
png_structp png_ptr = NULL; png_struct *img = NULL;
png_infop info_ptr = NULL; png_info *img_info = NULL;
// Check png autenticity. // Check png autenticity.
png_byte sig[8]; uint8_t sig[8];
if(file_read(sig, sizeof(png_byte), sizeof(sig), input_file)) goto_failure; if(file_read(sig, sizeof(*sig), sizeof(sig), input_file)) goto_failure;
if(!png_check_sig(sig, 8)) if(!png_check_sig(sig, 8))
{ {
error_printf("Png file is not valid."); error_printf("Png file is not valid.");
goto_failure; goto failure;
} }
// PNG structs. // PNG structs.
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); img = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr) if(!img)
{ {
error_printf("Failed to allocate png ptr."); error_printf("Failed to allocate png ptr.");
goto_failure; goto failure;
} }
info_ptr = png_create_info_struct(png_ptr); img_info = png_create_info_struct(img);
if(!info_ptr) if(!img_info)
{ {
error_printf("Failed to allocate png info."); error_printf("Failed to allocate png info.");
goto_failure; goto failure;
} }
// Fill structs and load data. // Fill structs and load data.
png_init_io(png_ptr, input_file); png_init_io(img, input_file);
png_set_sig_bytes(png_ptr, 8); png_set_sig_bytes(img, 8);
png_read_info(png_ptr, info_ptr); png_read_info(img, img_info);
{ {
uint16_t width = png_get_image_width(png_ptr, info_ptr); uint16_t width = png_get_image_width(img, img_info);
uint16_t height = png_get_image_height(png_ptr, info_ptr); uint16_t height = png_get_image_height(img, img_info);
size_t row_length = png_get_rowbytes(png_ptr, info_ptr); size_t row_length = png_get_rowbytes(img, img_info);
// Write image size. // Write image size.
if(file_write(&width, sizeof(width), 1, output_file)) goto_failure; if(file_write(&width, sizeof(width), 1, output_file)) goto_failure;
@ -60,15 +60,37 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
*asset_size += sizeof(height); *asset_size += sizeof(height);
// Prepare storage. // Prepare storage.
png_byte src_pixels[height * row_length]; uint8_t *src_pixels = calloc(height * row_length, sizeof(*src_pixels));
png_bytep src_row_ptrs[height]; if(!src_pixels)
for(png_uint_32 i = 0; i < height; i++) {
error_printf("Failed to alloc memory : %d", errno);
goto failure;
}
uint8_t **src_row_ptrs = calloc(height, sizeof(*src_row_ptrs));
if(!src_row_ptrs)
{
error_printf("Failed to alloc memory : %d", errno);
free(src_pixels);
goto failure;
}
for(uint16_t i = 0; i < height; i++)
src_row_ptrs[i] = &src_pixels[i * row_length]; src_row_ptrs[i] = &src_pixels[i * row_length];
// Read pixels. // Read pixels.
png_read_image(png_ptr, src_row_ptrs); png_read_image(img, src_row_ptrs);
uint16_t dst_pixels[width * height]; free(src_row_ptrs);
png_destroy_read_struct(&img, &img_info, NULL);
uint16_t *dst_pixels = calloc(width * height, sizeof(*dst_pixels));
if(!dst_pixels)
{
error_printf("Failed to alloc memory : %d", errno);
free(src_pixels);
goto failure;
}
for(uint16_t i = 0; i < height; i++) for(uint16_t i = 0; i < height; i++)
{ {
@ -89,11 +111,18 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
} }
} }
if(file_write(&dst_pixels, sizeof(*dst_pixels), width * height, output_file)) goto_failure; free(src_pixels);
*asset_size += sizeof(dst_pixels);
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if(file_write(dst_pixels, sizeof(*dst_pixels), width * height, output_file))
{
free(dst_pixels);
goto_failure;
}
*asset_size += width * height * sizeof(*dst_pixels);
free(dst_pixels);
}
if(fclose(input_file)) if(fclose(input_file))
{ {
@ -104,8 +133,6 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
return EXIT_SUCCESS; return EXIT_SUCCESS;
failure: failure:
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(fclose(input_file)) if(fclose(input_file))
error_printf("Failed to close input file : %d", errno); error_printf("Failed to close input file : %d", errno);

View File

@ -7,7 +7,7 @@
#include "utils.h" #include "utils.h"
#include "errors.h" #include "errors.h"
static int json_get_object_of_type(const json_object *obj, const char *key, json_type type, json_object **result) static int json_get_object_of_type(json_object *obj, const char *key, json_type type, json_object **result)
{ {
if(!json_object_object_get_ex(obj, key, result)) if(!json_object_object_get_ex(obj, key, result))
{ {
@ -24,7 +24,7 @@ static int json_get_object_of_type(const json_object *obj, const char *key, json
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
static inline int json_get_idx_of_type(const json_object *obj, size_t idx, json_type type, json_object **result) static inline int json_get_idx_of_type(json_object *obj, size_t idx, json_type type, json_object **result)
{ {
*result = json_object_array_get_idx(obj, idx); *result = json_object_array_get_idx(obj, idx);

View File

@ -31,26 +31,26 @@ texture_t *load_texture(void)
if(!SDL_LockSurface(tmp_surface)) if(!SDL_LockSurface(tmp_surface))
{ {
error_printf("Failed to lock temporary surface : %s", SDL_GetError()); error_printf("Failed to lock temporary surface : %s", SDL_GetError());
goto_failure; goto failure;
} }
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height) if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height)
{ {
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
goto_failure; goto failure;
} }
if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR)) if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR))
{ {
error_printf("Failed to set color key of temporary surface : %s", SDL_GetError()); error_printf("Failed to set color key of temporary surface : %s", SDL_GetError());
goto_failure; goto failure;
} }
texture_t *texture = SDL_CreateTextureFromSurface(game.display.renderer, tmp_surface); texture_t *texture = SDL_CreateTextureFromSurface(game.display.renderer, tmp_surface);
if(!texture) if(!texture)
{ {
error_printf("Failed to create texture from temporary surface : %s", SDL_GetError()); error_printf("Failed to create texture from temporary surface : %s", SDL_GetError());
goto_failure; goto failure;
} }
SDL_DestroySurface(tmp_surface); SDL_DestroySurface(tmp_surface);

View File

@ -12,8 +12,6 @@ pkgs.mkShell {
libpng libpng
json_c json_c
sdl3 sdl3
lua
ncurses
]; ];
shellHook = '' shellHook = ''