summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2016-03-30 21:20:07 +0200
committerPaweł Redman <pawel.redman@gmail.com>2016-03-30 21:20:07 +0200
commit523f6b460dcf1f864656a9a6ccc2a54d52f385c6 (patch)
treedd457448a2518ce45ac36ba427c12491f01d7e76
parent745d5a072d8669d1bc35e7c1f28b75b3484542e8 (diff)
Split common.h.
-rw-r--r--Makefile3
-rw-r--r--TODO5
-rw-r--r--src/common.c21
-rw-r--r--src/common.h141
-rw-r--r--src/itc.c3
-rw-r--r--src/itc.h26
-rw-r--r--src/main.c23
-rw-r--r--src/physics.c2
-rw-r--r--src/physics.h46
-rw-r--r--src/renderer.c2
-rw-r--r--src/renderer.h62
-rw-r--r--src/ui.c2
-rw-r--r--src/ui.h16
13 files changed, 189 insertions, 163 deletions
diff --git a/Makefile b/Makefile
index 75d59bb..5113b8f 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,8 @@ PP_CC := $(PP_BOLD)$(shell tput setf 6)CC$(PP_RESET)
PP_LD := $(PP_BOLD)$(shell tput setf 2)LD$(PP_RESET)
PP_RM := $(PP_BOLD)$(shell tput setf 4)RM$(PP_RESET)
-SRC := src/main.c src/physics.c src/renderer.c src/ui.c src/itc.c
+SRC := src/main.c src/physics.c src/renderer.c src/ui.c src/itc.c \
+ src/common.c
OBJ := $(SRC:src/%.c=obj/%.o)
OUT := cem
diff --git a/TODO b/TODO
index a5963f6..d5bb629 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1 @@
-refactor
-
-new shit:
- phy_control and shit
+itc_chan_destroy: free queue
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..71484e5
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,21 @@
+#include "common.h"
+#include <time.h>
+
+char *va(const char *fmt, ...)
+{
+ va_list vl;
+ static char buffer[4096];
+
+ va_start(vl, fmt);
+ vsnprintf(buffer, sizeof(buffer), fmt, vl);
+ va_end(vl);
+
+ return buffer;
+}
+
+int64_t get_time(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec * 1000000000 + ts.tv_nsec;
+}
diff --git a/src/common.h b/src/common.h
index 1b8e719..8933749 100644
--- a/src/common.h
+++ b/src/common.h
@@ -1,3 +1,7 @@
+#ifndef _COMMON_H
+#define _COMMON_H
+
+#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
@@ -8,21 +12,16 @@
#define PROGRAM_NAME "Symulator pól elektromagnetycznych"
-#include <stdio.h>
-#define con_printf printf
+#define con_printf printf // TODO?
#define max2(a, b) ((a) > (b) ? (a) : (b))
#define max3(a, b, c) (max2(max2(a, b), c))
#define min2(a, b) ((a) < (b) ? (a) : (b))
#define min3(a, b, c) (min2(min2(a, b), c))
-// main
-
char *va(const char *fmt, ...);
int64_t get_time(void);
-// math
-
typedef float vec_t;
typedef vec_t vec2_t[2];
typedef vec_t vec3_t[3];
@@ -149,132 +148,4 @@ static inline void mst2_dump(mst2_t M)
printf("[%f\t%f\t%f]\n", 0.0, 0.0, 1.0);
}
-// itc
-
-typedef struct itc_message_s itc_message;
-struct itc_message_s {
- int number;
- void *data;
-
- itc_message *older, *newer;
-};
-
-typedef struct {
- itc_message *newest, *oldest;
- SDL_mutex *mutex;
- SDL_sem *sem;
-} itc_chan;
-
-void itc_chan_create(itc_chan *chan);
-void itc_chan_destroy(itc_chan *chan);
-void itc_chan_push(itc_chan *chan, int number, void *data);
-int itc_chan_pop(itc_chan *chan, int *number, void **data);
-int itc_chan_pop_block(itc_chan *chan, int *number, void **data);
-
-// physics
-
-typedef struct {
- size_t width, height, depth;
- size_t zstr, ystr, xstr, size; // strides in no. of points (not bytes)
- size_t zstr1, ystr1, xstr1, size1; // same as above but for associated
- // scalar fields
- float spacing; // in meters
-} phy_field_info;
-
-typedef struct {
- float *E;
- float *H;
-} phy_field_em;
-
-enum {
- PHY_CMD_QUIT,
- PHY_CMD_PAUSE,
- PHY_CMD_RESUME,
- PHY_CMD_STEP
-};
-
-typedef struct {
- phy_field_info field_info;
- phy_field_em fields[3];
- float *field_eps, *field_mu; //permittivity and permeability
- float time;
-
- // UI stuff
- bool running;
- itc_chan ctl;
- SDL_mutex *rotate_lock;
-} phy_sim;
-
-void phy_sim_destroy(phy_sim *sim);
-int phy_sim_create(phy_sim *sim);
-void phy_sim_compute_const_fields(phy_sim *sim);
-void phy_sim_step(phy_sim *sim, float dt);
-
-int phy_thread(phy_sim *sim);
-
-// renderer
-
-extern float r_color_white[4];
-extern float r_color_black[4];
-extern float r_color_red[4];
-extern float r_color_blue[4];
-
-typedef struct r_window_s r_window;
-struct r_window_s {
- SDL_Window *window;
- SDL_Renderer *renderer;
- SDL_Texture *font_texture;
-
- r_window *prev, *next;
-};
-
-int r_init(void);
-void r_quit(void);
-
-r_window *r_window_create(void);
-void r_window_destroy(r_window *rw);
-
-void r_clear(r_window *rw, float *color);
-void r_flip(r_window *rw);
-
-void r_clip_enable(r_window *rw, float x, float y, float w, float h);
-void r_clip_disable(r_window *rw);
-void r_draw_line(r_window *rw, float x0, float y0, float x1, float y1,
- float *color);
-void r_draw_rect(r_window *rw, float x, float y, float w, float h,
- float *color);
-
-#define TEXT_CENTERX 0x0001
-#define TEXT_RIGHTX 0x0002
-float r_text_width(float h, char *text);
-void r_draw_text(r_window *rw, float x, float y, float h, char *text,
- float *color, int flags);
-
-typedef enum {
- XSECTION_XY,
- XSECTION_XZ,
- XSECTION_YZ
-} r_xsection_type;
-
-typedef struct {
- SDL_Texture *texture;
- float aspect_ratio;
-} r_xsection;
-
-void r_xsection_create(r_xsection *xsection);
-void r_xsection_destroy(r_xsection *xsection);
-int r_xsection_update(r_window *rw, r_xsection *xsection,
- phy_field_info *fi, float *field,
- r_xsection_type type, float frac);
-void r_xsection_draw(r_window *rw, r_xsection *xsection,
- float x, float y, float w, float h);
-
-// ui
-
-int ui_init(void);
-void ui_quit(void);
-
-void ui_renderer_window_register(r_window *rw);
-
-void ui_event(SDL_Event *event);
-void ui_draw(phy_sim *sim);
+#endif // _COMMON_H
diff --git a/src/itc.c b/src/itc.c
index ac13018..a1fde52 100644
--- a/src/itc.c
+++ b/src/itc.c
@@ -1,5 +1,4 @@
-#include "common.h"
-#include <unistd.h>
+#include "itc.h"
void itc_chan_create(itc_chan *chan)
{
diff --git a/src/itc.h b/src/itc.h
new file mode 100644
index 0000000..485b1bb
--- /dev/null
+++ b/src/itc.h
@@ -0,0 +1,26 @@
+#ifndef _ITC_H
+#define _ITC_H
+
+#include "common.h"
+
+typedef struct itc_message_s itc_message;
+struct itc_message_s {
+ int number;
+ void *data;
+
+ itc_message *older, *newer;
+};
+
+typedef struct {
+ itc_message *newest, *oldest;
+ SDL_mutex *mutex;
+ SDL_sem *sem;
+} itc_chan;
+
+void itc_chan_create(itc_chan *chan);
+void itc_chan_destroy(itc_chan *chan);
+void itc_chan_push(itc_chan *chan, int number, void *data);
+int itc_chan_pop(itc_chan *chan, int *number, void **data);
+int itc_chan_pop_block(itc_chan *chan, int *number, void **data);
+
+#endif // _ITC_H
diff --git a/src/main.c b/src/main.c
index 4cd8a6f..1bcc236 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,25 +1,10 @@
-#include "common.h"
#include <signal.h>
#include <time.h>
-char *va(const char *fmt, ...)
-{
- va_list vl;
- static char buffer[4096];
-
- va_start(vl, fmt);
- vsnprintf(buffer, sizeof(buffer), fmt, vl);
- va_end(vl);
-
- return buffer;
-}
-
-int64_t get_time(void)
-{
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return ts.tv_sec * 1000000000 + ts.tv_nsec;
-}
+#include "common.h"
+#include "physics.h"
+#include "renderer.h"
+#include "ui.h"
void itc_test(void);
diff --git a/src/physics.c b/src/physics.c
index c8a7cbd..5b904c6 100644
--- a/src/physics.c
+++ b/src/physics.c
@@ -1,4 +1,6 @@
#include "common.h"
+#include "itc.h"
+#include "physics.h"
void phy_sim_destroy(phy_sim *sim) {
size_t i;
diff --git a/src/physics.h b/src/physics.h
new file mode 100644
index 0000000..533d8a4
--- /dev/null
+++ b/src/physics.h
@@ -0,0 +1,46 @@
+#ifndef _PHYSICS_H
+#define _PHYSICS_H
+
+#include "common.h"
+#include "itc.h"
+
+typedef struct {
+ size_t width, height, depth;
+ size_t zstr, ystr, xstr, size; // strides in no. of points (not bytes)
+ size_t zstr1, ystr1, xstr1, size1; // same as above but for associated
+ // scalar fields
+ float spacing; // in meters
+} phy_field_info;
+
+typedef struct {
+ float *E;
+ float *H;
+} phy_field_em;
+
+enum {
+ PHY_CMD_QUIT,
+ PHY_CMD_PAUSE,
+ PHY_CMD_RESUME,
+ PHY_CMD_STEP
+};
+
+typedef struct {
+ phy_field_info field_info;
+ phy_field_em fields[3];
+ float *field_eps, *field_mu; //permittivity and permeability
+ float time;
+
+ // UI stuff
+ bool running;
+ itc_chan ctl;
+ SDL_mutex *rotate_lock;
+} phy_sim;
+
+void phy_sim_destroy(phy_sim *sim);
+int phy_sim_create(phy_sim *sim);
+void phy_sim_compute_const_fields(phy_sim *sim);
+void phy_sim_step(phy_sim *sim, float dt);
+
+int phy_thread(phy_sim *sim);
+
+#endif // _PHYSICS_H
diff --git a/src/renderer.c b/src/renderer.c
index 11c3706..3c403a0 100644
--- a/src/renderer.c
+++ b/src/renderer.c
@@ -1,5 +1,5 @@
-#define _RENDERER_C
#include "common.h"
+#include "renderer.h"
#include <SDL2/SDL_image.h>
#define FONT_FILE "assets/font.png"
diff --git a/src/renderer.h b/src/renderer.h
new file mode 100644
index 0000000..0c38e6d
--- /dev/null
+++ b/src/renderer.h
@@ -0,0 +1,62 @@
+#ifndef _RENDERER_H
+#define _RENDERER_H
+
+#include "common.h"
+#include "physics.h"
+
+extern float r_color_white[4];
+extern float r_color_black[4];
+extern float r_color_red[4];
+extern float r_color_blue[4];
+
+typedef struct r_window_s r_window;
+struct r_window_s {
+ SDL_Window *window;
+ SDL_Renderer *renderer;
+ SDL_Texture *font_texture;
+
+ r_window *prev, *next;
+};
+
+int r_init(void);
+void r_quit(void);
+
+r_window *r_window_create(void);
+void r_window_destroy(r_window *rw);
+
+void r_clear(r_window *rw, float *color);
+void r_flip(r_window *rw);
+
+void r_clip_enable(r_window *rw, float x, float y, float w, float h);
+void r_clip_disable(r_window *rw);
+void r_draw_line(r_window *rw, float x0, float y0, float x1, float y1,
+ float *color);
+void r_draw_rect(r_window *rw, float x, float y, float w, float h,
+ float *color);
+
+#define TEXT_CENTERX 0x0001
+#define TEXT_RIGHTX 0x0002
+float r_text_width(float h, char *text);
+void r_draw_text(r_window *rw, float x, float y, float h, char *text,
+ float *color, int flags);
+
+typedef enum {
+ XSECTION_XY,
+ XSECTION_XZ,
+ XSECTION_YZ
+} r_xsection_type;
+
+typedef struct {
+ SDL_Texture *texture;
+ float aspect_ratio;
+} r_xsection;
+
+void r_xsection_create(r_xsection *xsection);
+void r_xsection_destroy(r_xsection *xsection);
+int r_xsection_update(r_window *rw, r_xsection *xsection,
+ phy_field_info *fi, float *field,
+ r_xsection_type type, float frac);
+void r_xsection_draw(r_window *rw, r_xsection *xsection,
+ float x, float y, float w, float h);
+
+#endif // _RENDERER_H
diff --git a/src/ui.c b/src/ui.c
index f55f18b..c79669d 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -1,4 +1,4 @@
-#include "common.h"
+#include "ui.h"
struct {
float font_size;
diff --git a/src/ui.h b/src/ui.h
new file mode 100644
index 0000000..73b355b
--- /dev/null
+++ b/src/ui.h
@@ -0,0 +1,16 @@
+#ifndef _UI_H
+#define _UI_H
+
+#include "common.h"
+#include "renderer.h"
+#include "physics.h"
+
+int ui_init(void);
+void ui_quit(void);
+
+void ui_renderer_window_register(r_window *rw);
+
+void ui_event(SDL_Event *event);
+void ui_draw(phy_sim *sim);
+
+#endif // _UI_H