math and things
This commit is contained in:
parent
247661fc13
commit
2fe23438b0
17 changed files with 243 additions and 35 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
||||||
/build
|
/build
|
||||||
|
*.swp
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,6 @@ project(af_client)
|
||||||
file(GLOB af_client_SRC *.c)
|
file(GLOB af_client_SRC *.c)
|
||||||
|
|
||||||
add_executable(af_client ${af_client_SRC})
|
add_executable(af_client ${af_client_SRC})
|
||||||
target_link_libraries(af_client PRIVATE GearBox)
|
target_link_libraries(af_client PRIVATE GearBox Mw)
|
||||||
|
|
||||||
install_target(af_client)
|
install_target(af_client)
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,58 @@
|
||||||
|
#define MW_OPENGL_NO_INCLUDE
|
||||||
|
#define _MILSKO
|
||||||
|
|
||||||
#include <GearBox/Foundation.h>
|
#include <GearBox/Foundation.h>
|
||||||
|
|
||||||
|
#include <Mw/Milsko.h>
|
||||||
|
#include <Mw/Widget/OpenGL.h>
|
||||||
|
|
||||||
|
MwWidget window, opengl;
|
||||||
|
|
||||||
|
static void* gl_getprocaddress(const char* name){
|
||||||
|
return MwOpenGLGetProcAddress(opengl, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gl_swapbuffer(void){
|
||||||
|
MwOpenGLSwapBuffer(opengl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ready(int width, int height){
|
||||||
|
MwSizeHints sh;
|
||||||
|
|
||||||
|
sh.min_width = sh.max_width = width;
|
||||||
|
sh.min_height = sh.max_height = height;
|
||||||
|
|
||||||
|
MwLibraryInit();
|
||||||
|
|
||||||
|
window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, width, height,
|
||||||
|
MwNtitle, "AssaultFish",
|
||||||
|
MwNsizeHints, &sh,
|
||||||
|
NULL);
|
||||||
|
opengl = MwCreateWidget(MwOpenGLClass, "opengl", window, 0, 0, width, height);
|
||||||
|
|
||||||
|
MwOpenGLMakeCurrent(opengl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tick(void){
|
||||||
|
while(!window->close && MwPending(window)) MwStep(window);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
GBEngineParam param;
|
GBEngineParam param;
|
||||||
GBEngine engine;
|
GBEngine engine;
|
||||||
|
|
||||||
param.server = 0;
|
GBEngineParamInit(¶m);
|
||||||
param.client = 1;
|
param.ready = ready;
|
||||||
|
param.tick = tick;
|
||||||
|
param.get_tick = MwTimeGetTick;
|
||||||
|
param.sleep = MwTimeSleep;
|
||||||
|
param.gl_getprocaddress = gl_getprocaddress;
|
||||||
|
param.gl_swapbuffer = gl_swapbuffer;
|
||||||
|
|
||||||
GBInit();
|
GBInit();
|
||||||
|
|
||||||
engine = GBEngineCreate(¶m);
|
if((engine = GBEngineCreate(¶m)) != NULL){
|
||||||
GBEngineLoop(engine);
|
GBEngineLoop(engine);
|
||||||
|
GBEngineDestroy(engine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,17 @@ macro(install_target name)
|
||||||
)
|
)
|
||||||
endmacro()
|
endmacro()
|
||||||
|
|
||||||
add_subdirectory(external/glad)
|
|
||||||
|
|
||||||
add_subdirectory(net)
|
add_subdirectory(net)
|
||||||
|
|
||||||
file(GLOB GearBox_SRC src/*.c)
|
file(GLOB GearBox_SRC src/*.c)
|
||||||
|
|
||||||
add_library(GearBox SHARED ${GearBox_SRC})
|
add_library(GearBox SHARED ${GearBox_SRC} external/glad/src/glad.c)
|
||||||
target_include_directories(GearBox PUBLIC include)
|
target_include_directories(GearBox PUBLIC include external/glad/include)
|
||||||
target_compile_definitions(GearBox PRIVATE _GEARBOX)
|
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)
|
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 GBClient GBClientCreate(GBEngine engine);
|
||||||
GBDECL void GBClientDestroy(GBClient client);
|
GBDECL void GBClientDestroy(GBClient client);
|
||||||
|
GBDECL void GBClientStep(GBClient client);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,14 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GBDECL void GBInit(void);
|
GBDECL void GBInit(void);
|
||||||
|
|
||||||
GBDECL GBEngine GBEngineCreate(GBEngineParam* param);
|
GBDECL GBEngine GBEngineCreate(GBEngineParam* param);
|
||||||
GBDECL void GBEngineDestroy(GBEngine engine);
|
GBDECL void GBEngineDestroy(GBEngine engine);
|
||||||
GBDECL void GBEngineStart(GBEngine engine);
|
GBDECL void GBEngineParamInit(GBEngineParam* param);
|
||||||
GBDECL void GBEngineLoop(GBEngine engine);
|
GBDECL void GBEngineLoop(GBEngine engine);
|
||||||
|
GBDECL GBClient GBEngineGetClient(GBEngine engine);
|
||||||
|
GBDECL GBServer GBEngineGetServer(GBEngine engine);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,6 @@
|
||||||
#include <GearBox/Log.h>
|
#include <GearBox/Log.h>
|
||||||
#include <GearBox/Client.h>
|
#include <GearBox/Client.h>
|
||||||
#include <GearBox/Server.h>
|
#include <GearBox/Server.h>
|
||||||
|
#include <GearBox/Math.h>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,13 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.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)
|
#define GBDECL extern __declspec(dllexport)
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
#define GBDECL extern __declspec(dllimport)
|
#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 GBServer GBServerCreate(GBEngine engine);
|
||||||
GBDECL void GBServerDestroy(GBServer server);
|
GBDECL void GBServerDestroy(GBServer server);
|
||||||
|
GBDECL void GBServerStep(GBServer server);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,13 @@ typedef void* GBServer;
|
||||||
typedef void* GBEngine;
|
typedef void* GBEngine;
|
||||||
#endif
|
#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
|
#ifdef _GEARBOX
|
||||||
struct _GBClient {
|
struct _GBClient {
|
||||||
};
|
};
|
||||||
|
|
@ -21,8 +28,9 @@ struct _GBServer {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GBEngine {
|
struct _GBEngine {
|
||||||
GBClient client;
|
GBEngineParam* param;
|
||||||
GBServer server;
|
GBClient client;
|
||||||
|
GBServer server;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -34,8 +42,14 @@ struct _GBVersion {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GBEngineParam {
|
struct _GBEngineParam {
|
||||||
int client;
|
int client;
|
||||||
int server;
|
int server;
|
||||||
|
GBGLGetProcAddressCallback gl_getprocaddress;
|
||||||
|
GBGLSwapBufferCallback gl_swapbuffer;
|
||||||
|
GBReadyCallback ready;
|
||||||
|
GBTickCallback tick;
|
||||||
|
GBGetTickCallback get_tick;
|
||||||
|
GBSleepCallback sleep;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,18 @@
|
||||||
#include <GearBox/Log.h>
|
#include <GearBox/Log.h>
|
||||||
|
|
||||||
GBClient GBClientCreate(GBEngine engine) {
|
GBClient GBClientCreate(GBEngine engine) {
|
||||||
|
GBClient client = malloc(sizeof(*client));
|
||||||
|
|
||||||
|
memset(client, 0, sizeof(*client));
|
||||||
|
|
||||||
GBLog(GBLogInfo, "Creating client");
|
GBLog(GBLogInfo, "Creating client");
|
||||||
|
|
||||||
return NULL;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GBClientDestroy(GBClient client) {
|
void GBClientDestroy(GBClient client) {
|
||||||
free(client);
|
free(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GBClientStep(GBClient client) {
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include <GearBox/Client.h>
|
#include <GearBox/Client.h>
|
||||||
#include <GearBox/Server.h>
|
#include <GearBox/Server.h>
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
void GBInit(void) {
|
void GBInit(void) {
|
||||||
GBVersion version;
|
GBVersion version;
|
||||||
|
|
||||||
|
|
@ -18,6 +20,17 @@ GBEngine GBEngineCreate(GBEngineParam* param) {
|
||||||
|
|
||||||
memset(engine, 0, sizeof(*engine));
|
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) {
|
if(engine != NULL && param->client && (engine->client = GBClientCreate(engine)) == NULL) {
|
||||||
GBLog(GBLogError, "Failed to create client");
|
GBLog(GBLogError, "Failed to create client");
|
||||||
|
|
||||||
|
|
@ -44,12 +57,63 @@ GBEngine GBEngineCreate(GBEngineParam* param) {
|
||||||
void GBEngineDestroy(GBEngine engine) {
|
void GBEngineDestroy(GBEngine engine) {
|
||||||
if(engine->server != NULL) GBServerDestroy(engine->server);
|
if(engine->server != NULL) GBServerDestroy(engine->server);
|
||||||
if(engine->client != NULL) GBClientDestroy(engine->client);
|
if(engine->client != NULL) GBClientDestroy(engine->client);
|
||||||
|
if(engine->param != NULL) free(engine->param);
|
||||||
|
|
||||||
free(engine);
|
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) {
|
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>
|
#include <GearBox/Log.h>
|
||||||
|
|
||||||
GBServer GBServerCreate(GBEngine engine) {
|
GBServer GBServerCreate(GBEngine engine) {
|
||||||
|
GBServer server = malloc(sizeof(*server));
|
||||||
|
|
||||||
|
memset(server, 0, sizeof(*server));
|
||||||
|
|
||||||
GBLog(GBLogInfo, "Creating server");
|
GBLog(GBLogInfo, "Creating server");
|
||||||
|
|
||||||
return NULL;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GBServerDestroy(GBServer server) {
|
void GBServerDestroy(GBServer server) {
|
||||||
|
|
||||||
free(server);
|
free(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GBServerStep(GBServer server) {
|
||||||
|
}
|
||||||
|
|
|
||||||
2
external/milsko
vendored
2
external/milsko
vendored
|
|
@ -1 +1 @@
|
||||||
Subproject commit a67a0c11f8e42033d246ae284e51ebd97c8e9672
|
Subproject commit debdaf998e0b5edfe42214934d8fcf4dfea3e7bd
|
||||||
Loading…
Add table
Add a link
Reference in a new issue