Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Mar 26, 2023
1 parent 4e63600 commit a1d0a75
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 87 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ TARGET := orbvis
CC := gcc
WARNINGS := -Wall -Wextra -Wpedantic -Wdouble-promotion -Wstrict-prototypes -Wshadow -Wduplicated-cond -Wduplicated-branches -Wjump-misses-init -Wnull-dereference -Wrestrict -Wlogical-op -Walloc-zero -Wformat-security -Wformat-signedness -Winit-self -Wlogical-op -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wswitch-enum -Wundef -Wwrite-strings -Wno-discarded-qualifiers
CFLAGS := -O3 `pkg-config --cflags gtk+-3.0 cglm libcurl epoxy` -std=c11
LDFLAGS := -lm
LDFLAGS := -lm `pkg-config --libs gtk+-3.0 cglm libcurl epoxy`
COMMANDS := $(CC) pkg-config xxd sed
PACKAGES := gtk+-3.0 cglm libcurl epoxy

ifeq ($(MAKECMDGOALS),windows)
CFLAGS += -march=core2 -flto
LDFLAGS += -lopengl32 `pkg-config --libs gtk+-3.0 cglm libcurl epoxy` -mwindows -flto
LDFLAGS += -lopengl32 -mwindows -flto
else
LDFLAGS += -ldl `pkg-config --libs gl gtk+-3.0 cglm libcurl epoxy`
LDFLAGS += -ldl
ifeq ($(MAKECMDGOALS),debug)
CFLAGS += -march=native -fsanitize=address -fsanitize=undefined -g -ftrapv -fno-omit-frame-pointer -lrt -DDEBUG
LDFLAGS += -fsanitize=address -fsanitize=undefined -g -ftrapv -fno-omit-frame-pointer -lrt
Expand Down
1 change: 0 additions & 1 deletion res/ui/ui.glade
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,6 @@
<property name="can-focus">False</property>
<property name="has-alpha">True</property>
<property name="has-depth-buffer">True</property>
<property name="has-stencil-buffer">True</property>
<signal name="realize" handler="on_glarea_realize" swapped="no"/>
<signal name="render" handler="on_glarea_render" swapped="no"/>
<signal name="resize" handler="on_glarea_resize" swapped="no"/>
Expand Down
6 changes: 1 addition & 5 deletions src/core/type.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Wojciech Graj
* Copyright (c) 2022-2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -22,17 +22,13 @@
#define likely(x) __builtin_expect((x), TRUE)
#define unlikely(x) __builtin_expect((x), FALSE)

#define arrlen(a) (sizeof(a) / sizeof(a[0]))

#define SWAP(a, b) \
do { \
__typeof__(a) temp_ = a; \
a = b; \
b = temp_; \
} while (0)

#define sgn(x) (signbit(x) ? -1 : 1)

#define XSTR(s) STR(s)
#define STR(s) #s

Expand Down
9 changes: 1 addition & 8 deletions src/entity/entity.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Wojciech Graj
* Copyright (c) 2022-2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -22,9 +22,7 @@
void entity_init(void)
{
earth_init();
#ifndef NO_SATELLITE
satellite_init();
#endif
sky_init();
}

Expand All @@ -33,21 +31,16 @@ void entity_render(void)
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);

earth_render();
#ifndef NO_SATELLITE
satellites_render();
#endif
glCullFace(GL_FRONT);
sky_render();
}

void entity_deinit(void)
{
earth_deinit();
#ifndef NO_SATELLITE
satellite_deinit();
#endif
sky_deinit();
}
8 changes: 4 additions & 4 deletions src/entity/satellite.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ void *satellites_get_thrd(void *arguments)
return NULL;
}

static guint64 sc_hash(const void *item, guint64 seed0, guint64 seed1)
guint64 sc_hash(const void *item, guint64 seed0, guint64 seed1)
{
(void)seed0;
(void)seed1;
return ((struct SatCat *)item)->catnum;
}

static int sc_compare(const void *a, const void *b, void *udata)
int sc_compare(const void *a, const void *b, void *udata)
{
(void)udata;
return (int)(((struct SatCat *)a)->catnum) - (int)(((struct SatCat *)b)->catnum);
Expand Down Expand Up @@ -344,7 +344,7 @@ void satellites_get(void)
thread_pool_indices_sync[i] = SATELLITE_CALC_STEP * i;
}

static void set_satellite_color(char code, vec3 color)
void set_satellite_color(char code, vec3 color)
{
switch (code) {
case SCSTAT_OPERATIONAL:
Expand Down Expand Up @@ -494,7 +494,7 @@ void gen_satellite_orbit(guint32 idx)
}
}

static void satellite_toggle_orbit(guint32 idx)
void satellite_toggle_orbit(guint32 idx)
{
if (satellites[idx].orbit_idx == UINT32_MAX) { /* Enable orbit */
/* For similar-length line segments, apply Kepler's Third Law (T^2 prop. r^3) */
Expand Down
25 changes: 5 additions & 20 deletions src/gfx/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

#include "camera.h"
#include "error.h"
#include "info.h"
#include "perf.h"
#include "render.h"
#include "thread.h"
#include "toolbar.h"
#include "mainloop.h"

struct GLCtx e_gl_ctx;
static GtkGLArea *glarea;

static void on_glarea_realize(GtkWidget *widget, gpointer user_data);
static void on_glarea_resize(GtkGLArea *area, int width, int height, gpointer user_data);
Expand All @@ -32,7 +27,8 @@ static gboolean on_glarea_render(GtkGLArea *area, GdkGLContext *context, gpointe
void on_glarea_realize(GtkWidget *widget, gpointer user_data)
{
(void)user_data;
(void)widget;

GtkGLArea *glarea = GTK_GL_AREA(widget);

error_check(!gtk_gl_area_get_error(glarea), "Failed to create OpenGL context");

Expand All @@ -57,24 +53,13 @@ void on_glarea_resize(GtkGLArea *area, int width, int height, gpointer user_data
camera_proj_update(&e_camera);
}

/* Main Loop */
gboolean on_glarea_render(GtkGLArea *area, GdkGLContext *context, gpointer user_data)
{
(void)area;
(void)context;
(void)user_data;

thread_dispatch(THRD_PHYS, NULL);
satellites_tic();
render_process();
toolbar_tic();
info_tic();
perf_tic();
thread_join(THRD_PHYS);
satellites_tic_sync();
#ifndef NO_SATELLITE
thread_join_if_finished(THRD_SATELLITES_GET);
#endif
mainloop();

return TRUE;
}
Expand All @@ -87,6 +72,6 @@ void gfx_init(GtkBuilder *builder)
"on_glarea_realize", G_CALLBACK(on_glarea_realize),
NULL);

glarea = GTK_GL_AREA(gtk_builder_get_object(builder, "glarea"));
GtkGLArea *glarea = GTK_GL_AREA(gtk_builder_get_object(builder, "glarea"));
gtk_gl_area_set_required_version(glarea, 4, 1);
}
21 changes: 7 additions & 14 deletions src/gfx/render.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Wojciech Graj
* Copyright (c) 2022-2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -14,21 +14,13 @@

#include "render.h"

#include "bo.h"
#include "camera.h"
#include "entity.h"
#include "error.h"
#include "gfx.h"
#include "icosphere.h"
#include "idx_obj.h"
#include "model.h"
#include "shader.h"
#include "system.h"
#include "texture.h"
#include "ui.h"
#include "vao.h"
#include "type.h"

#include <cglm/cglm.h>
#include <cglm/vec3.h>

#define DEFAULT_CAMERA_POS ((vec3){ 0.f, 5.f, 0.f })
#define DEFAULT_CAMERA_RAD 5.f
Expand All @@ -50,18 +42,19 @@ void render_init(void)
camera_view_update(&e_camera);
camera_proj_update(&e_camera);

textures_init();

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_STENCIL_TEST);
}

void render_process(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

entity_render();
}
4 changes: 2 additions & 2 deletions src/gfx/shader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Wojciech Graj
* Copyright (c) 2022-2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -20,7 +20,7 @@

static GLint shader_compile(char *str, unsigned int len, GLenum type);

static GLint shader_compile(char *str, unsigned int len, GLenum type)
GLint shader_compile(char *str, unsigned int len, GLenum type)
{
GLuint handle = glCreateShader(type);
glShaderSource(handle, 1, (const GLchar *const *)&str, (const GLint *)&len);
Expand Down
8 changes: 6 additions & 2 deletions src/gfx/texture.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Wojciech Graj
* Copyright (c) 2022-2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -18,6 +18,11 @@

#include "error.h"

void textures_init(void)
{
stbi_set_flip_vertically_on_load(TRUE);
}

void texture_init(struct Texture *texture, GLenum unit, GLenum type)
{
texture->unit = unit;
Expand All @@ -30,7 +35,6 @@ void texture_init(struct Texture *texture, GLenum unit, GLenum type)
void texture_init_from_image(struct Texture *texture, unsigned char *buffer, unsigned int len, GLenum unit, GLenum type)
{
int width, height, n_channels;
stbi_set_flip_vertically_on_load(TRUE); /* TODO: Move to init */
unsigned char *data = stbi_load_from_memory(buffer, len, &width, &height, &n_channels, 0);
error_check(data, "Failed to open texture.");

Expand Down
2 changes: 2 additions & 0 deletions src/gfx/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct Texture {
GLenum type;
};

void textures_init(void);

void texture_init(struct Texture *texture, GLenum unit, GLenum type);

void texture_init_from_image(struct Texture *texture, unsigned char *buffer, unsigned int len, GLenum unit, GLenum type);
Expand Down
5 changes: 1 addition & 4 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Wojciech Graj
* Copyright (c) 2022-2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -33,10 +33,7 @@ int main(int argc, char *argv[])
render_init();
phys_init();
entity_init();

#ifndef NO_SATELLITE
thread_dispatch(THRD_SATELLITES_GET, NULL);
#endif

gtk_main();

Expand Down
35 changes: 35 additions & 0 deletions src/mainloop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
**/

#include "mainloop.h"

#include "info.h"
#include "perf.h"
#include "render.h"
#include "satellite.h"
#include "thread.h"
#include "toolbar.h"

void mainloop(void)
{
thread_dispatch(THRD_PHYS, NULL);
satellites_tic();
render_process();
toolbar_tic();
info_tic();
perf_tic();
thread_join(THRD_PHYS);
satellites_tic_sync();
thread_join_if_finished(THRD_SATELLITES_GET);
}
20 changes: 20 additions & 0 deletions src/mainloop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 Wojciech Graj
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
**/

#ifndef __MAINLOOP_H__
#define __MAINLOOP_H__

void mainloop(void);

#endif /* __MAINLOOP_H__ */
6 changes: 1 addition & 5 deletions src/phys/phys.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ void phys_init(void)
phys_phys_sync();
}

static void phys(void)
void phys(void)
{
phys_phys();
#ifndef NO_SATELLITE
satellites_phys();
#endif
}

void *phys_thrd(void *arguments)
Expand All @@ -117,7 +115,5 @@ void phys_phys_sync(void)
void phys_sync(void)
{
phys_phys_sync();
#ifndef NO_SATELLITE
satellites_phys_sync();
#endif
}
Loading

0 comments on commit a1d0a75

Please sign in to comment.