implement engine structure
This commit is contained in:
parent
fdebdd3ca2
commit
2dbfa1b99e
6 changed files with 470 additions and 103 deletions
|
@ -69,4 +69,5 @@ target_include_directories(colysis PUBLIC include
|
|||
# put the assets folder path as a C preprocessor define
|
||||
target_compile_definitions(colysis PRIVATE ASSETS_PATH="${ASSETS_PATH}")
|
||||
|
||||
put_targets_into_folder(FOLDER "thid_party" TARGETS raylib flecs::flecs_static)
|
||||
put_targets_into_folder(FOLDER "vendor/raylib" TARGETS raylib uninstall)
|
||||
put_targets_into_folder(FOLDER "vendor/flecs" TARGETS flecs::flecs flecs::flecs_static)
|
||||
|
|
|
@ -16,14 +16,103 @@ extern "C" {
|
|||
|
||||
#include "types.h"
|
||||
|
||||
usx init(usx task);
|
||||
#define ALYSIS_ENGINE_WINDOW_TITLE "Alyson Engine"
|
||||
|
||||
void update(float dt);
|
||||
typedef struct {
|
||||
char type[4 + 1];
|
||||
char group[26 + 1];
|
||||
char* name;
|
||||
char* path;
|
||||
} alyson_path_t, *ALPath;
|
||||
|
||||
void render(float dt);
|
||||
/*
|
||||
* @function: demangle_path
|
||||
* @description: Demangles a path
|
||||
* @param path: path to resource
|
||||
* @return: ALPath
|
||||
*/
|
||||
ALPath al_demangle_path(const char* path);
|
||||
char* al_mangle_path(const ALPath path);
|
||||
ALPath al_create_path(
|
||||
const char* type,
|
||||
const char* path,
|
||||
const char* name,
|
||||
const char* group
|
||||
);
|
||||
void al_destroy_path(ALPath path);
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
} alyson_args_t, *ALArgs;
|
||||
|
||||
ALArgs al_args_create(int argc, char** argv);
|
||||
void al_args_destroy(ALArgs args);
|
||||
|
||||
typedef struct {
|
||||
usx id;
|
||||
alyson_path_t path;
|
||||
} alyson_resource_t, *ALResource;
|
||||
|
||||
ALResource al_resource_create(ALPath path);
|
||||
ALResource al_resource_create_from_file(char* name, char* path);
|
||||
void al_resource_destroy(ALResource resource);
|
||||
|
||||
/* TODO : implement
|
||||
typedef struct {
|
||||
ALResource* resources;
|
||||
usx count;
|
||||
usx capacity;
|
||||
} alyson_resource_manager_t, *ALResourceManager;
|
||||
|
||||
ALResourceManager al_resource_manager_create();
|
||||
void al_resource_manager_destroy(ALResourceManager manager);
|
||||
|
||||
void al_resource_manager_add(ALResourceManager manager, ALResource resource);
|
||||
void al_resource_manager_remove(ALResourceManager manager, usx id);
|
||||
void al_resource_manager_clear(ALResourceManager manager);
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
ALResourceManager resources_manager;
|
||||
usx id;
|
||||
char* name;
|
||||
usx width;
|
||||
usx height;
|
||||
} alyson_window_t, *ALWindow;
|
||||
|
||||
ALWindow al_window_create(char* name, int width, int height);
|
||||
void al_window_destroy(ALWindow window);
|
||||
|
||||
|
||||
#include <flecs.h>
|
||||
|
||||
typedef struct {
|
||||
ALWindow wnd;
|
||||
ecs_world_t* engine;
|
||||
ecs_world_t* ui;
|
||||
} alyson_engine_t, *ALEngine;
|
||||
|
||||
ALEngine al_engine_create(ALArgs args, ALWindow wnd = NULL);
|
||||
void al_engine_destroy(ALEngine engine);
|
||||
|
||||
void al_engine_run(ALEngine engine);
|
||||
void al_engine_stop(ALEngine engine);
|
||||
bool al_engine_is_running(ALEngine engine);
|
||||
bool al_engine_is_stopped(ALEngine engine);
|
||||
|
||||
void al_engine_update(ALEngine engine);
|
||||
void al_engine_draw(ALEngine engine);
|
||||
void al_engine_draw_ui(ALEngine engine);
|
||||
|
||||
void al_engine_set_window(ALEngine engine, ALWindow wnd);
|
||||
ALWindow al_engine_get_window(ALEngine engine);
|
||||
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // ALYSON_HPP
|
||||
#endif // ALYSON_HPP
|
||||
|
|
|
@ -1,86 +1,287 @@
|
|||
#include <alyson.h>
|
||||
|
||||
|
||||
#include <raylib.h>
|
||||
#include <stdio.h>
|
||||
#include <raymath.h>
|
||||
|
||||
#include <rlyson.h>
|
||||
|
||||
#include "../../cmake-build-minsizerel/_deps/raylib-src/src/raymath.h"
|
||||
|
||||
#define WINDOW_TITLE "Alyson Engine"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
InitWindow(900, 500, WINDOW_TITLE " -- 900x500");
|
||||
|
||||
|
||||
Texture2D logo = LoadTexture(MANGLE_RES_PATH("Raylib_logo.png"));
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/*
|
||||
* ALArgs
|
||||
*/
|
||||
ALArgs al_args_create(int argc, char** argv)
|
||||
{
|
||||
ALArgs args = malloc(sizeof(alyson_args_t));
|
||||
args->argc = argc;
|
||||
args->argv = argv;
|
||||
|
||||
Camera2D camera = { 0 };
|
||||
camera.target = (Vector2){
|
||||
(float)(GetScreenWidth()) / 2.f,
|
||||
(float)(GetScreenHeight()) / 2.f
|
||||
};
|
||||
camera.offset = (Vector2){ (float)(GetScreenWidth()) / 2.f,
|
||||
(float)(GetScreenHeight()) / 2.f };
|
||||
camera.rotation = 0.0f;
|
||||
camera.zoom = 1.0f;
|
||||
|
||||
init(0);
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
const float dt = GetFrameTime();
|
||||
|
||||
if (IsWindowResized()) {
|
||||
SetWindowTitle(TextFormat("%s -- %dx%d", WINDOW_TITLE, GetScreenWidth(), GetScreenHeight()));
|
||||
}
|
||||
|
||||
BeginDrawing();
|
||||
BeginMode2D(camera);
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
camera.rotation += dt * 10.f;
|
||||
if (camera.rotation > 360.f) {
|
||||
camera.rotation -= 360.f;
|
||||
}
|
||||
|
||||
DrawTexture(logo, GetScreenWidth() / 2 - logo.width / 2, GetScreenHeight() / 2 - logo.height / 2, WHITE);
|
||||
DrawTextFull(
|
||||
GetFontDefault(),
|
||||
WINDOW_TITLE,
|
||||
(Vector2) {
|
||||
(float)(GetScreenWidth()) / 2.f,
|
||||
(float)(GetScreenHeight()) - 60.f
|
||||
},
|
||||
TEXT_ORIENTATION_CENTER, 0.f,
|
||||
40.f, 5.f,
|
||||
0.f, BLACK
|
||||
);
|
||||
|
||||
update(dt);
|
||||
render(dt);
|
||||
|
||||
EndMode2D();
|
||||
|
||||
// log the delta time
|
||||
DrawTextFull(
|
||||
GetFontDefault(),
|
||||
TextFormat("%.3f", dt),
|
||||
(Vector2) {
|
||||
30.f,
|
||||
30.f
|
||||
},
|
||||
TEXT_ORIENTATION_RIGHT, 0.0f,
|
||||
20.f, 5.f,
|
||||
0.f, BLACK
|
||||
);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
UnloadTexture(logo);
|
||||
CloseWindow();
|
||||
return 0;
|
||||
return args;
|
||||
}
|
||||
|
||||
void al_args_destroy(ALArgs args)
|
||||
{
|
||||
free(args);
|
||||
}
|
||||
|
||||
/*
|
||||
* ALResource
|
||||
*/
|
||||
|
||||
|
||||
ALPath al_demangle_path(const char* path)
|
||||
{
|
||||
/*
|
||||
* mangled path format:
|
||||
* <type>://(<group>/)<name>:<path>
|
||||
*
|
||||
* e.g. res://test:/tests/test.png
|
||||
* type: "res"
|
||||
* group: ""
|
||||
* name: "test"
|
||||
* path: "/tests/test.png"
|
||||
*
|
||||
*
|
||||
* demangled path format:
|
||||
* ./<type_path>/<demangled_path>
|
||||
*
|
||||
* e.g.
|
||||
* type_path: res -> ./assets/
|
||||
* demangled_path: tests/test.png
|
||||
* -> ./assets/tests/test.png
|
||||
*
|
||||
* type max length: 4
|
||||
*
|
||||
* smallest possible path: ://:a (5 chars)
|
||||
*
|
||||
* groups max length: 26
|
||||
* groups:
|
||||
* e.g. res://image/test:/tests/test.png
|
||||
* type: "res"
|
||||
* group: "image"
|
||||
* name: "test"
|
||||
* path: "/tests/test.png"
|
||||
* -> ./assets/tests/test.png
|
||||
*/
|
||||
const ALPath res = malloc(sizeof(alyson_path_t));
|
||||
res->type[0] = '\0';
|
||||
res->group[0] = '\0';
|
||||
res->name = NULL;
|
||||
res->path = NULL;
|
||||
|
||||
const usx len = strlen(path);
|
||||
usx back_ptr = 0;
|
||||
|
||||
if (len < 5)
|
||||
{
|
||||
fprintf(stderr, "[ALResource] Invalid path: \"%s\"; too short\n", path);
|
||||
al_destroy_path(res);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// type
|
||||
for (usx i = 0; i < 4; i++)
|
||||
{
|
||||
if (path[i] == ':')
|
||||
{
|
||||
res->type[i] = '\0';
|
||||
back_ptr = i + 1;
|
||||
break;
|
||||
}
|
||||
res->type[i] = path[i];
|
||||
}
|
||||
if (back_ptr == 0)
|
||||
{
|
||||
fprintf(stderr, "[ALResource] Invalid path: \"%s\"; type not found\n", path);
|
||||
al_destroy_path(res);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (path[back_ptr] != '/' || path[back_ptr+1] != '/') {
|
||||
fprintf(stderr, "[ALResource] Invalid path: \"%s\"; type not followed by '://'\n", path);
|
||||
al_destroy_path(res);
|
||||
return NULL;
|
||||
}
|
||||
back_ptr += 2;
|
||||
|
||||
// group + name
|
||||
for (usx i = back_ptr; i < len; i++)
|
||||
{
|
||||
if (path[i] == '/')
|
||||
{
|
||||
if (res->group[0] != '\0')
|
||||
{
|
||||
fprintf(stderr, "[ALResource] Invalid path: \"%s\"; group already set\n", path);
|
||||
al_destroy_path(res);
|
||||
return NULL;
|
||||
}
|
||||
if (back_ptr == i) break;
|
||||
strncpy(res->group, (char*)path + back_ptr, i - back_ptr);
|
||||
res->group[i-back_ptr] = '\0';
|
||||
back_ptr = i + 1;
|
||||
}
|
||||
|
||||
if (path[i] == ':')
|
||||
{
|
||||
res->name = malloc(sizeof(char) * (i - back_ptr + 1));
|
||||
strncpy(res->name, (char*)path + back_ptr, i - back_ptr);
|
||||
res->name[i - back_ptr] = '\0';
|
||||
back_ptr = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// path
|
||||
if (back_ptr == len)
|
||||
{
|
||||
fprintf(stderr, "[ALResource] Invalid path: \"%s\"; path not found\n", path);
|
||||
al_destroy_path(res);
|
||||
return NULL;
|
||||
}
|
||||
res->path = malloc(sizeof(char) * (len - back_ptr + 1));
|
||||
strncpy(res->path, (char*)path + back_ptr, len - back_ptr);
|
||||
res->path[len - back_ptr] = '\0';
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
char* al_mangle_path(const ALPath path)
|
||||
{
|
||||
char* str = malloc(sizeof(char) * (
|
||||
strlen(path->type) +
|
||||
strlen(path->group) +
|
||||
strlen(path->name) +
|
||||
strlen(path->path) +
|
||||
5 ));
|
||||
if (path->group[0] != '\0')
|
||||
{
|
||||
sprintf(str, "%s://%s/%s:%s", path->type, path->group, path->name, path->path);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(str, "%s://%s:%s", path->type, path->name, path->path);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
ALPath al_create_path(const char* type, const char* path, const char* name, const char* group)
|
||||
{
|
||||
const ALPath res = malloc(sizeof(alyson_path_t));
|
||||
res->type[0] = '\0';
|
||||
res->name = NULL;
|
||||
res->path = NULL;
|
||||
|
||||
if (type != NULL)
|
||||
{
|
||||
if (strlen(type) > 4)
|
||||
{
|
||||
fprintf(stderr, "[ALResource] Type name too long: %s\n", type);
|
||||
return NULL;
|
||||
}
|
||||
strncpy(res->type, type, strlen(type));
|
||||
res->type[4] = '\0';
|
||||
}
|
||||
|
||||
if (path != NULL)
|
||||
{
|
||||
res->path = strdup(path);
|
||||
res->path[strlen(path)] = '\0';
|
||||
}
|
||||
|
||||
if (name != NULL)
|
||||
{
|
||||
res->name = malloc(sizeof(char) * (strlen(name) + 1));
|
||||
strncpy(res->name, name, strlen(name));
|
||||
res->name[strlen(name)] = '\0';
|
||||
}
|
||||
|
||||
if (group != NULL)
|
||||
{
|
||||
if (strlen(group) > 26)
|
||||
{
|
||||
fprintf(stderr, "[ALResource] Group name too long: %s\n", group);
|
||||
al_destroy_path(res);
|
||||
return NULL;
|
||||
}
|
||||
strncpy(res->group, group, strlen(group));
|
||||
res->group[strlen(group)] = '\0';
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void al_destroy_path(ALPath path)
|
||||
{
|
||||
if (path->name != NULL)
|
||||
{
|
||||
free(path->name);
|
||||
path->name = NULL;
|
||||
}
|
||||
if (path->path != NULL)
|
||||
{
|
||||
free(path->path);
|
||||
path->path = NULL;
|
||||
}
|
||||
free(path);
|
||||
path = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ALResource al_resource_create(ALPath path)
|
||||
{
|
||||
ALResource resource = malloc(sizeof(alyson_resource_t));
|
||||
resource->id = 0;
|
||||
memcpy(&resource->path, path, sizeof(alyson_path_t));
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
ALResource al_resource_create_from_file(char* name, char* path)
|
||||
{
|
||||
ALResource resource = malloc(sizeof(alyson_resource_t));
|
||||
resource->id = 0;
|
||||
|
||||
usx len = strlen(path);
|
||||
resource->path.path = malloc(sizeof(char) * (len + 1));
|
||||
memcpy(resource->path.path, path, sizeof(char) * len);
|
||||
resource->path.path[len] = '\0';
|
||||
|
||||
memcpy(&resource->path.type, "res", 4);
|
||||
resource->path.type[4] = '\0';
|
||||
|
||||
len = strlen(name);
|
||||
resource->path.name = malloc(sizeof(char) * len + 1);
|
||||
memcpy(resource->path.name, name, sizeof(char) * len);
|
||||
resource->path.name[len] = '\0';
|
||||
|
||||
memcpy(&resource->path.group, "runtime", 7);
|
||||
resource->path.group[7] = '\0';
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
void al_destroy_resource(ALResource resource)
|
||||
{
|
||||
if (resource->path.name != NULL)
|
||||
{
|
||||
free(resource->path.name);
|
||||
resource->path.name = NULL;
|
||||
}
|
||||
if (resource->path.path != NULL)
|
||||
{
|
||||
free(resource->path.path);
|
||||
resource->path.path = NULL;
|
||||
}
|
||||
|
||||
free(resource);
|
||||
resource = NULL;
|
||||
}
|
||||
|
|
86
alyson/tests/ALPath.c
Normal file
86
alyson/tests/ALPath.c
Normal file
|
@ -0,0 +1,86 @@
|
|||
//
|
||||
// Created by n0ffie on 10/02/2025.
|
||||
//
|
||||
|
||||
|
||||
#include <alyson.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ALPath path = al_demangle_path("res://test:/test.png");
|
||||
printf("1. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("res://image/test:/gay/test.png");
|
||||
printf("2. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("res:/test:/test.png");
|
||||
if (path != NULL)
|
||||
{
|
||||
printf("3. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("://test:/test.png");
|
||||
if (path != NULL)
|
||||
{
|
||||
printf("4. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("img://:/test.png");
|
||||
if (path != NULL)
|
||||
{
|
||||
printf("6. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("://:a");
|
||||
if (path != NULL)
|
||||
{
|
||||
printf("7. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("g://a:");
|
||||
if (path != NULL)
|
||||
{
|
||||
printf("8. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
path = al_demangle_path("image://gay/guy:/a/b");
|
||||
if (path != NULL)
|
||||
{
|
||||
printf("9. %s\n", al_mangle_path(path));
|
||||
al_destroy_path(path);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
return 0;
|
||||
}
|
11
src/main.c
Normal file
11
src/main.c
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <colysis.h>
|
||||
|
||||
#include <alyson.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ALArgs args = al_args_create(argc, argv);
|
||||
|
||||
|
||||
}
|
21
src/main.cpp
21
src/main.cpp
|
@ -1,21 +0,0 @@
|
|||
#include <colysis.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <alyson.h>
|
||||
|
||||
size_t init(size_t task) {
|
||||
if(task == 0)
|
||||
return 2;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void update(float dt) {
|
||||
|
||||
}
|
||||
|
||||
void render(float dt) {
|
||||
|
||||
}
|
Loading…
Reference in a new issue