GameEngine/source/client.c
2026-02-12 19:50:33 +01:00

214 lines
5.9 KiB
C

#include <engine.h>
#include <rlgl.h>
#include <raymath.h>
//#define RAYGUI_IMPLEMENTATION
//#include <raygui.h>
Context g_ctx;
RenderCtx r_ctx;
/*
Audio
Video
Gameplay
Control
Accessibility
Filter
*/
void RenderOption() {
}
void RenderInventory() {
//DrawTextureRec();
}
void DebugInterface() {
DrawFPS(10, 10);
}
void UserInterface(const Player *player) {
//player stats
//player state and aliement
//health and other metric
//DrawTextureRec();
DrawText(player->entity.body.health, 0, 0, 16, RED);
}
void MainMenu() {
switch (g_ctx.state) {
case(0): {
break;
}
default:
break;
}
}
void LoadCameraMode(int mode) {
switch (mode) {
case(0): {
break;
}
default:
break;
}
}
void LoadPipeline() {
r_ctx.width = 800;
r_ctx.height = 480;
r_ctx.hud_on = true;
r_ctx.debug_on = true;
InitWindow(r_ctx.width, r_ctx.height, "GAME!!");
assert(IsWindowReady());
r_ctx.window = GetWindowHandle();
r_ctx.shader.deferred = LoadShader("source/shader/deferred.vs", "source/shader/deferred.fs");
r_ctx.shader.gbuffer = LoadShader("source/shader/gbuffer.vs", "source/shader/gbuffer.fs");
r_ctx.shader.filter = LoadShader("", "source/shader/Screen_filter.fs");
r_ctx.gbuffer.framebufferId = rlLoadFramebuffer();
assert(r_ctx.gbuffer.framebufferId);
rlEnableFramebuffer(r_ctx.gbuffer.framebufferId);
r_ctx.gbuffer.depth_tex_id = rlLoadTextureDepth(r_ctx.width, r_ctx.height, false);
// If Artefact appear on Normal, may need to change to RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32 and also need to be used in case of anti aliasing (MLAA or SMAA if needed)
r_ctx.gbuffer.normal_tex_id = rlLoadTexture(NULL, r_ctx.width, r_ctx.height, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1);
// The color in RGB, and the specular strength in the alpha channel
r_ctx.gbuffer.albedoSpec_tex_id = rlLoadTexture(NULL, r_ctx.width, r_ctx.height, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
rlActiveDrawBuffers(3);
rlFramebufferAttach(r_ctx.gbuffer.framebufferId, r_ctx.gbuffer.depth_tex_id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(r_ctx.gbuffer.framebufferId, r_ctx.gbuffer.normal_tex_id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(r_ctx.gbuffer.framebufferId, r_ctx.gbuffer.albedoSpec_tex_id, RL_ATTACHMENT_COLOR_CHANNEL1, RL_ATTACHMENT_TEXTURE2D, 0);
assert(rlFramebufferComplete(r_ctx.gbuffer.framebufferId));
rlEnableShader(r_ctx.shader.deferred.id);
int texUnitDepth = 0;
int texUnitNormal = 1;
int texUnitAlbedoSpec = 2;
SetShaderValue(r_ctx.shader.deferred, rlGetLocationUniform(r_ctx.shader.deferred.id, "gDepth"), &texUnitDepth, RL_SHADER_UNIFORM_SAMPLER2D);
SetShaderValue(r_ctx.shader.deferred, rlGetLocationUniform(r_ctx.shader.deferred.id, "gNormal"), &texUnitNormal, RL_SHADER_UNIFORM_SAMPLER2D);
SetShaderValue(r_ctx.shader.deferred, rlGetLocationUniform(r_ctx.shader.deferred.id, "gAlbedoSpec"), &texUnitAlbedoSpec, RL_SHADER_UNIFORM_SAMPLER2D);
rlDisableShader();
//now need to set this to things that use this Material, and add light used in the scene.
rlEnableDepthTest();
rlEnableBackfaceCulling();
TraceLog(LOG_INFO, "Render Pipeline Built");
SetTargetFPS(120);//may change
}
void UnloadPipeline() {
rlUnloadFramebuffer(r_ctx.gbuffer.framebufferId);
rlUnloadTexture(r_ctx.gbuffer.depth_tex_id);
rlUnloadTexture(r_ctx.gbuffer.normal_tex_id);
rlUnloadTexture(r_ctx.gbuffer.albedoSpec_tex_id);
UnloadShader(r_ctx.shader.deferred);
UnloadShader(r_ctx.shader.gbuffer);
UnloadShader(r_ctx.shader.filter);
CloseWindow();
}
void RenderingDeferred3D() {
BeginDrawing();
ClearBackground(BLACK);
rlEnableDepthMask();
rlEnableFramebuffer(r_ctx.gbuffer.framebufferId);
rlEnableShader(r_ctx.shader.gbuffer.id);
BeginMode3D(r_ctx.camera3d);
//MainGeamoetry;
EndMode3D();
rlDisableShader();
rlDisableFramebuffer();
rlDisableDepthMask();//may not be usefull
//rlDisableDepthTest();
//Light Pass and forward pass;
//I should build a shader that take every light in the scene and make a 3D heightmap of lighting then compute it once for all static light allowing to cheat computing everylight
//light is computed into a scalar field
//Light Pass
//Screen Filter Pass
BeginShaderMode(r_ctx.shader.filter);//may be good to have this disabled for accessibility
//DrawTextureRec();
EndShaderMode();
//render ui
EndDrawing();
}
Assets* LoadAssets() {
Assets *asset = (Assets *)calloc(sizeof(Assets), 1);
assert(asset);
//Font
asset->fonts[DENDRITIC_VOLTAGE] = LoadFont("assets/font/Dendritic_Voltage.ttf");
asset->fonts[LAZENBYCOMP_LIQUID] = LoadFont("assets/font/LazenbyCompLiquid.ttf");
asset->fonts[LAZENBYCOMP_SMOOTH] = LoadFont("assets/font/LazenbyCompSmooth.ttf");
asset->fonts[POTRA] = LoadFont("assets/font/Potra.ttf");
//Textures
//Models
//Sound
//Data
TraceLog(LOG_INFO, "Assets Loaded");
return (asset);
}
void UnloadAssets(Assets *asset) {
UnloadFont(asset->fonts[DENDRITIC_VOLTAGE]);
UnloadFont(asset->fonts[LAZENBYCOMP_LIQUID]);
UnloadFont(asset->fonts[LAZENBYCOMP_SMOOTH]);
UnloadFont(asset->fonts[POTRA]);
free(asset);
}
void UpdateInput() {
const Input input;
int key = GetKeyPressed();
Vector2 mouse_pos = GetMousePosition();
Vector2 mouse_delta = GetMouseDelta();
while (key) {
for (PLAYER_ACTION action = 0; action < 4; action++) {
if (input.key[action] == key) {
//emit("player_intent", action);
break;
}
}
key = GetKeyPressed();
}
}
//may be able to dispatch it
void GameLoop(const double tick_time) {
__thread static double time = 0;
time += GetFrameTime();
if (time >= tick_time) {
// interaction
// simulation
time = 0;
}
}
int main(void) {
double tick_time = 0.4;
SetTraceLogLevel(LOG_ALL);
LoadPipeline();
Assets *assets = LoadAssets();
SetExitKey(KEY_NULL);
while (!WindowShouldClose()) {
UpdateInput();
ComputeIntent();
GameLoop(tick_time);
Rendering();
}
UnloadAssets(assets);
assets = NULL;//to remember to not use it after
UnloadPipeline();
return (0);
}