Skip to content

Commit

Permalink
Replace array size and looping boilerplate with macros
Browse files Browse the repository at this point in the history
  • Loading branch information
aklomp committed Sep 13, 2017
1 parent a70df74 commit bf28631
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
11 changes: 6 additions & 5 deletions gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "matrix.h"
#include "model.h"
#include "program.h"
#include "util.h"
#include "view.h"

// Hold init data for GTK signals:
Expand Down Expand Up @@ -145,9 +146,9 @@ on_scroll (GtkWidget* widget, GdkEventScroll *event)
static void
connect_signals (GtkWidget *widget, struct signal *signals, size_t members)
{
for (size_t i = 0; i < members; i++) {
gtk_widget_add_events(widget, signals[i].mask);
g_signal_connect(widget, signals[i].signal, signals[i].handler, NULL);
FOREACH_NELEM (signals, members, s) {
gtk_widget_add_events(widget, s->mask);
g_signal_connect(widget, s->signal, s->handler, NULL);
}
}

Expand All @@ -158,7 +159,7 @@ connect_window_signals (GtkWidget *window)
{ "destroy", G_CALLBACK(gtk_main_quit), 0 },
};

connect_signals(window, signals, sizeof(signals) / sizeof(signals[0]));
connect_signals(window, signals, NELEM(signals));
}

static void
Expand All @@ -174,7 +175,7 @@ connect_glarea_signals (GtkWidget *glarea)
{ "motion-notify-event", G_CALLBACK(on_motion_notify), GDK_BUTTON1_MOTION_MASK },
};

connect_signals(glarea, signals, sizeof(signals) / sizeof(signals[0]));
connect_signals(glarea, signals, NELEM(signals));
}

bool
Expand Down
7 changes: 4 additions & 3 deletions model.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "matrix.h"
#include "program.h"
#include "util.h"

struct point {
float x;
Expand Down Expand Up @@ -222,10 +223,10 @@ model_init (void)
} ,
};

for (size_t i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
GLint loc = program_cube_loc(map[i].loc);
FOREACH (map, m) {
GLint loc = program_cube_loc(m->loc);
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, sizeof(struct vertex), map[i].ptr);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, sizeof(struct vertex), m->ptr);
}

// Upload vertex data:
Expand Down
13 changes: 6 additions & 7 deletions program.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "model.h"
#include "view.h"
#include "program.h"
#include "util.h"

// Define inline data:
#define DATA_DEF(x) \
Expand Down Expand Up @@ -76,13 +77,13 @@ programs[] = {
.shader.vert = SHADER (bkgd_vertex),
.shader.frag = SHADER (bkgd_fragment),
.loc = loc_bkgd,
.nloc = sizeof (loc_bkgd) / sizeof (loc_bkgd[0]),
.nloc = NELEM(loc_bkgd),
},
[CUBE] = {
.shader.vert = SHADER (cube_vertex),
.shader.frag = SHADER (cube_fragment),
.loc = loc_cube,
.nloc = sizeof (loc_cube) / sizeof (loc_cube[0]),
.nloc = NELEM(loc_cube),
},
};

Expand Down Expand Up @@ -154,9 +155,7 @@ program_init (struct program *p)
glDeleteShader(vert->id);
glDeleteShader(frag->id);

for (size_t j = 0; j < p->nloc; j++) {
struct loc *l = &p->loc[j];

FOREACH_NELEM (p->loc, p->nloc, l) {
switch (l->type)
{
case UNIFORM:
Expand All @@ -173,8 +172,8 @@ program_init (struct program *p)
void
programs_init (void)
{
for (size_t i = 0; i < sizeof(programs) / sizeof(programs[0]); i++)
program_init(&programs[i]);
FOREACH (programs, p)
program_init(p);
}

void
Expand Down
12 changes: 12 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Get number of elements in an array:
#define NELEM(array) (sizeof(array) / sizeof(*(array)))

// Loop over an array of given size:
#define FOREACH_NELEM(array, nelem, iter) \
for (__typeof__(*(array)) *iter = (array); \
iter < (array) + (nelem); \
iter++)

// Loop over an array of known size:
#define FOREACH(array, iter) \
FOREACH_NELEM(array, NELEM(array), iter)

0 comments on commit bf28631

Please sign in to comment.