math and things
This commit is contained in:
parent
247661fc13
commit
2fe23438b0
17 changed files with 243 additions and 35 deletions
|
|
@ -14,15 +14,17 @@ macro(install_target name)
|
|||
)
|
||||
endmacro()
|
||||
|
||||
add_subdirectory(external/glad)
|
||||
|
||||
add_subdirectory(net)
|
||||
|
||||
file(GLOB GearBox_SRC src/*.c)
|
||||
|
||||
add_library(GearBox SHARED ${GearBox_SRC})
|
||||
target_include_directories(GearBox PUBLIC include)
|
||||
add_library(GearBox SHARED ${GearBox_SRC} external/glad/src/glad.c)
|
||||
target_include_directories(GearBox PUBLIC include external/glad/include)
|
||||
target_compile_definitions(GearBox PRIVATE _GEARBOX)
|
||||
target_link_libraries(GearBox PRIVATE gbnet)
|
||||
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
|
||||
target_link_libraries(GearBox PRIVATE m)
|
||||
endif()
|
||||
|
||||
install_target(GearBox)
|
||||
|
||||
|
|
|
|||
14
engine/external/glad/CMakeLists.txt
vendored
14
engine/external/glad/CMakeLists.txt
vendored
|
|
@ -1,14 +0,0 @@
|
|||
cmake_minimum_required(VERSION 3.11)
|
||||
project(glad)
|
||||
|
||||
add_library(
|
||||
glad
|
||||
STATIC
|
||||
src/glad.c
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
glad
|
||||
PUBLIC
|
||||
include
|
||||
)
|
||||
|
|
@ -10,6 +10,7 @@ extern "C" {
|
|||
|
||||
GBDECL GBClient GBClientCreate(GBEngine engine);
|
||||
GBDECL void GBClientDestroy(GBClient client);
|
||||
GBDECL void GBClientStep(GBClient client);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,14 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
GBDECL void GBInit(void);
|
||||
GBDECL void GBInit(void);
|
||||
|
||||
GBDECL GBEngine GBEngineCreate(GBEngineParam* param);
|
||||
GBDECL void GBEngineDestroy(GBEngine engine);
|
||||
GBDECL void GBEngineStart(GBEngine engine);
|
||||
GBDECL void GBEngineParamInit(GBEngineParam* param);
|
||||
GBDECL void GBEngineLoop(GBEngine engine);
|
||||
GBDECL GBClient GBEngineGetClient(GBEngine engine);
|
||||
GBDECL GBServer GBEngineGetServer(GBEngine engine);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,5 +9,6 @@
|
|||
#include <GearBox/Log.h>
|
||||
#include <GearBox/Client.h>
|
||||
#include <GearBox/Server.h>
|
||||
#include <GearBox/Math.h>
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -5,8 +5,13 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <math.h>
|
||||
|
||||
#if defined(_MILSKO) && defined(_WIN32)
|
||||
#ifdef _GEARBOX
|
||||
#include <gbnet.h>
|
||||
#endif
|
||||
|
||||
#if defined(_GEARBOX) && defined(_WIN32)
|
||||
#define GBDECL extern __declspec(dllexport)
|
||||
#elif defined(_WIN32)
|
||||
#define GBDECL extern __declspec(dllimport)
|
||||
|
|
|
|||
19
engine/include/GearBox/Math.h
Normal file
19
engine/include/GearBox/Math.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef __GEARBOX_MATH_H__
|
||||
#define __GEARBOX_MATH_H__
|
||||
|
||||
#include <GearBox/MachDep.h>
|
||||
#include <GearBox/TypeDefs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
GBDECL void GBMathCross(double* out, double v00, double v01, double v02, double v10, double v11, double v12);
|
||||
GBDECL void GBMathNormal3(double* out, double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22);
|
||||
GBDECL void GBMathNormal4(double* out, double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22, double v30, double v31, double v32);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -10,6 +10,7 @@ extern "C" {
|
|||
|
||||
GBDECL GBServer GBServerCreate(GBEngine engine);
|
||||
GBDECL void GBServerDestroy(GBServer server);
|
||||
GBDECL void GBServerStep(GBServer server);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,13 @@ typedef void* GBServer;
|
|||
typedef void* GBEngine;
|
||||
#endif
|
||||
|
||||
typedef void* (*GBGLGetProcAddressCallback)(const char* name);
|
||||
typedef void (*GBGLSwapBufferCallback)(void);
|
||||
typedef void (*GBReadyCallback)(int width, int height);
|
||||
typedef void (*GBTickCallback)(void);
|
||||
typedef long (*GBGetTickCallback)(void);
|
||||
typedef void (*GBSleepCallback)(int ms);
|
||||
|
||||
#ifdef _GEARBOX
|
||||
struct _GBClient {
|
||||
};
|
||||
|
|
@ -21,8 +28,9 @@ struct _GBServer {
|
|||
};
|
||||
|
||||
struct _GBEngine {
|
||||
GBClient client;
|
||||
GBServer server;
|
||||
GBEngineParam* param;
|
||||
GBClient client;
|
||||
GBServer server;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
|
@ -34,8 +42,14 @@ struct _GBVersion {
|
|||
};
|
||||
|
||||
struct _GBEngineParam {
|
||||
int client;
|
||||
int server;
|
||||
int client;
|
||||
int server;
|
||||
GBGLGetProcAddressCallback gl_getprocaddress;
|
||||
GBGLSwapBufferCallback gl_swapbuffer;
|
||||
GBReadyCallback ready;
|
||||
GBTickCallback tick;
|
||||
GBGetTickCallback get_tick;
|
||||
GBSleepCallback sleep;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -3,11 +3,18 @@
|
|||
#include <GearBox/Log.h>
|
||||
|
||||
GBClient GBClientCreate(GBEngine engine) {
|
||||
GBClient client = malloc(sizeof(*client));
|
||||
|
||||
memset(client, 0, sizeof(*client));
|
||||
|
||||
GBLog(GBLogInfo, "Creating client");
|
||||
|
||||
return NULL;
|
||||
return client;
|
||||
}
|
||||
|
||||
void GBClientDestroy(GBClient client) {
|
||||
free(client);
|
||||
}
|
||||
|
||||
void GBClientStep(GBClient client) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
#include <GearBox/Client.h>
|
||||
#include <GearBox/Server.h>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
void GBInit(void) {
|
||||
GBVersion version;
|
||||
|
||||
|
|
@ -18,6 +20,17 @@ GBEngine GBEngineCreate(GBEngineParam* param) {
|
|||
|
||||
memset(engine, 0, sizeof(*engine));
|
||||
|
||||
engine->param = malloc(sizeof(*engine->param));
|
||||
memcpy(engine->param, param, sizeof(*param));
|
||||
|
||||
if(param->ready != NULL) {
|
||||
param->ready(1024, 768);
|
||||
}
|
||||
|
||||
if(param->gl_getprocaddress != NULL) {
|
||||
gladLoadGLLoader(param->gl_getprocaddress);
|
||||
}
|
||||
|
||||
if(engine != NULL && param->client && (engine->client = GBClientCreate(engine)) == NULL) {
|
||||
GBLog(GBLogError, "Failed to create client");
|
||||
|
||||
|
|
@ -44,12 +57,63 @@ GBEngine GBEngineCreate(GBEngineParam* param) {
|
|||
void GBEngineDestroy(GBEngine engine) {
|
||||
if(engine->server != NULL) GBServerDestroy(engine->server);
|
||||
if(engine->client != NULL) GBClientDestroy(engine->client);
|
||||
if(engine->param != NULL) free(engine->param);
|
||||
|
||||
free(engine);
|
||||
}
|
||||
|
||||
void GBEngineStart(GBEngine engine) {
|
||||
void GBEngineParamInit(GBEngineParam* param) {
|
||||
memset(param, 0, sizeof(*param));
|
||||
|
||||
param->server = 0;
|
||||
param->client = 1;
|
||||
param->gl_getprocaddress = NULL;
|
||||
param->gl_swapbuffer = NULL;
|
||||
param->ready = NULL;
|
||||
param->sleep = NULL;
|
||||
param->get_tick = NULL;
|
||||
}
|
||||
|
||||
void GBEngineLoop(GBEngine engine) {
|
||||
long t = 0;
|
||||
int wait_actually = 0;
|
||||
int more = 0;
|
||||
long wait = 1000 / 50.0;
|
||||
|
||||
if(engine->param->sleep == NULL || engine->param->get_tick == NULL) {
|
||||
GBLog(GBLogWarn, "sleep and/or get_tick parameter are missing!");
|
||||
GBLog(GBLogWarn, "This will result game to be crazy fast!!!");
|
||||
} else {
|
||||
t = engine->param->get_tick();
|
||||
|
||||
wait_actually = 1;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
GBClientStep(engine->client);
|
||||
GBServerStep(engine->server);
|
||||
|
||||
if(engine->param->tick != NULL) engine->param->tick();
|
||||
|
||||
if(wait_actually) {
|
||||
int diff = (wait - more) - (engine->param->get_tick() - t);
|
||||
|
||||
if(diff > 0) {
|
||||
engine->param->sleep(diff);
|
||||
}
|
||||
|
||||
more -= diff;
|
||||
if(more < 0) more = 0;
|
||||
|
||||
t = engine->param->get_tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GBClient GBEngineGetClient(GBEngine engine) {
|
||||
return engine->client;
|
||||
}
|
||||
|
||||
GBServer GBEngineGetServer(GBEngine engine) {
|
||||
return engine->server;
|
||||
}
|
||||
|
|
|
|||
54
engine/src/math.c
Normal file
54
engine/src/math.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <GearBox/Math.h>
|
||||
|
||||
void GBMathCross(double* out, double v00, double v01, double v02, double v10, double v11, double v12) {
|
||||
out[0] = v01 * v12 - v02 * v11;
|
||||
out[1] = v02 * v10 - v00 * v12;
|
||||
out[2] = v00 * v11 - v01 * v10;
|
||||
}
|
||||
|
||||
/*
|
||||
* v0-v2
|
||||
* | /
|
||||
* v1/
|
||||
*/
|
||||
void GBMathNormal3(double* out, double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22) {
|
||||
double v[3];
|
||||
double l = 0;
|
||||
int i;
|
||||
|
||||
GBMathCross(&v[0], /**/
|
||||
v10 - v00, v11 - v01, v12 - v02, /**/
|
||||
v20 - v00, v21 - v01, v22 - v02 /**/
|
||||
);
|
||||
|
||||
l = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||
|
||||
for(i = 0; i < 3; i++) {
|
||||
out[i] = v[i] / l;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* v0-v3
|
||||
* | |
|
||||
* v1-v2
|
||||
*/
|
||||
void GBMathNormal4(double* out, double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22, double v30, double v31, double v32) {
|
||||
double v0[3];
|
||||
double v1[3];
|
||||
int i;
|
||||
double l = 0;
|
||||
|
||||
GBMathNormal3(&v0[0], v00, v01, v02, v10, v11, v12, v30, v31, v32);
|
||||
GBMathNormal3(&v1[0], v10, v11, v12, v20, v21, v22, v30, v31, v32);
|
||||
|
||||
for(i = 0; i < 3; i++) {
|
||||
out[i] = v0[i] + v1[i];
|
||||
}
|
||||
|
||||
l = sqrt(out[0] * out[0] + out[1] * out[1] + out[2] * out[2]);
|
||||
|
||||
for(i = 0; i < 3; i++) {
|
||||
out[i] = out[i] / l;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,12 +3,18 @@
|
|||
#include <GearBox/Log.h>
|
||||
|
||||
GBServer GBServerCreate(GBEngine engine) {
|
||||
GBServer server = malloc(sizeof(*server));
|
||||
|
||||
memset(server, 0, sizeof(*server));
|
||||
|
||||
GBLog(GBLogInfo, "Creating server");
|
||||
|
||||
return NULL;
|
||||
return server;
|
||||
}
|
||||
|
||||
void GBServerDestroy(GBServer server) {
|
||||
|
||||
free(server);
|
||||
}
|
||||
|
||||
void GBServerStep(GBServer server) {
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue