split .c and .h

This commit is contained in:
Christopher Chedeau
2014-04-18 16:00:53 -07:00
parent a650e77cb3
commit 75ab7b6f39
2 changed files with 94 additions and 75 deletions

View File

@@ -1,79 +1,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include "Layout.h"
#define CSS_UNDEFINED NAN #define CSS_UNDEFINED NAN
typedef enum {
CSS_FLEX_DIRECTION_COLUMN = 0,
CSS_FLEX_DIRECTION_ROW
} css_flex_direction_t;
typedef enum {
CSS_JUSTIFY_FLEX_START = 0,
CSS_JUSTIFY_CENTER,
CSS_JUSTIFY_FLEX_END,
CSS_JUSTIFY_SPACE_BETWEEN,
CSS_JUSTIFY_SPACE_AROUND
} css_justify_t;
// Note: auto is only a valid value for alignSelf. It is NOT a valid value for
// alignItems.
typedef enum {
CSS_ALIGN_AUTO = 0,
CSS_ALIGN_FLEX_START,
CSS_ALIGN_CENTER,
CSS_ALIGN_FLEX_END,
CSS_ALIGN_STRETCH
} css_align_t;
typedef enum {
CSS_FLEX_NONE = 0,
CSS_FLEX_ONE
} css_flex_t;
// Note: left and top are shared between position[2] and position[4], so
// they have to be before right and bottom.
typedef enum {
CSS_LEFT = 0,
CSS_TOP,
CSS_RIGHT,
CSS_BOTTOM
} css_position_t;
typedef enum {
CSS_WIDTH = 0,
CSS_HEIGHT
} css_dimension_t;
typedef struct {
float position[2];
float dimensions[2];
} css_layout_t;
typedef struct {
css_flex_direction_t flex_direction;
css_justify_t justify_content;
css_align_t align_items;
css_align_t align_self;
css_flex_t flex;
float margin[4];
float padding[4];
float position[4];
float dimensions[2];
css_layout_t layout;
} css_style_t;
typedef struct css_node {
css_style_t style;
css_layout_t layout;
struct css_node *children;
int children_count;
} css_node_t;
void init_css_node(css_node_t *node) { void init_css_node(css_node_t *node) {
memset(node, 0, sizeof(*node)); memset(node, 0, sizeof(*node));
node->style.align_items = CSS_ALIGN_FLEX_START; node->style.align_items = CSS_ALIGN_FLEX_START;
@@ -97,7 +32,7 @@ css_node_t *new_css_node() {
return node; return node;
} }
void new_css_node_children(css_node_t *node, int children_count) { void init_css_node_children(css_node_t *node, int children_count) {
node->children = malloc(children_count * sizeof(css_node_t)); node->children = malloc(children_count * sizeof(css_node_t));
for (int i = 0; i < children_count; ++i) { for (int i = 0; i < children_count; ++i) {
init_css_node(&node->children[i]); init_css_node(&node->children[i]);
@@ -117,25 +52,24 @@ void free_css_node(css_node_t *node) {
free(node); free(node);
} }
void layout_node(css_node_t *node) {
node->layout.dimensions[CSS_WIDTH] = node->style.dimensions[CSS_WIDTH];
}
void indent(int n) { void indent(int n) {
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
printf(" "); printf(" ");
} }
} }
void print_number_0(const char *str, float number) { void print_number_0(const char *str, float number) {
if (number != 0) { if (number != 0) {
printf("%s: %g, ", str, number); printf("%s: %g, ", str, number);
} }
} }
void print_number_nan(const char *str, float number) { void print_number_nan(const char *str, float number) {
if (!isnan(number)) { if (!isnan(number)) {
printf("%s: %g, ", str, number); printf("%s: %g, ", str, number);
} }
} }
void print_style(css_node_t *node, int level) { void print_style(css_node_t *node, int level) {
indent(level); indent(level);
printf("{"); printf("{");
@@ -431,7 +365,7 @@ int main()
node->style.dimensions[CSS_WIDTH] = 100; node->style.dimensions[CSS_WIDTH] = 100;
node->style.align_items = CSS_ALIGN_STRETCH; node->style.align_items = CSS_ALIGN_STRETCH;
new_css_node_children(node, 3); init_css_node_children(node, 3);
node->children[0].style.dimensions[CSS_HEIGHT] = 50; node->children[0].style.dimensions[CSS_HEIGHT] = 50;
layoutNode(node); layoutNode(node);
@@ -439,6 +373,5 @@ int main()
print_layout(node, 0); print_layout(node, 0);
free_css_node(node); free_css_node(node);
printf("Hello World\n");
} }

86
src/Layout.h Normal file
View File

@@ -0,0 +1,86 @@
#ifndef __LAYOUT_H
#define __LAYOUT_H
typedef enum {
CSS_FLEX_DIRECTION_COLUMN = 0,
CSS_FLEX_DIRECTION_ROW
} css_flex_direction_t;
typedef enum {
CSS_JUSTIFY_FLEX_START = 0,
CSS_JUSTIFY_CENTER,
CSS_JUSTIFY_FLEX_END,
CSS_JUSTIFY_SPACE_BETWEEN,
CSS_JUSTIFY_SPACE_AROUND
} css_justify_t;
// Note: auto is only a valid value for alignSelf. It is NOT a valid value for
// alignItems.
typedef enum {
CSS_ALIGN_AUTO = 0,
CSS_ALIGN_FLEX_START,
CSS_ALIGN_CENTER,
CSS_ALIGN_FLEX_END,
CSS_ALIGN_STRETCH
} css_align_t;
typedef enum {
CSS_FLEX_NONE = 0,
CSS_FLEX_ONE
} css_flex_t;
// Note: left and top are shared between position[2] and position[4], so
// they have to be before right and bottom.
typedef enum {
CSS_LEFT = 0,
CSS_TOP,
CSS_RIGHT,
CSS_BOTTOM
} css_position_t;
typedef enum {
CSS_WIDTH = 0,
CSS_HEIGHT
} css_dimension_t;
typedef struct {
float position[2];
float dimensions[2];
} css_layout_t;
typedef struct {
css_flex_direction_t flex_direction;
css_justify_t justify_content;
css_align_t align_items;
css_align_t align_self;
css_flex_t flex;
float margin[4];
float padding[4];
float position[4];
float dimensions[2];
css_layout_t layout;
} css_style_t;
typedef struct css_node {
css_style_t style;
css_layout_t layout;
struct css_node *children;
int children_count;
} css_node_t;
// Lifecycle of nodes and children
css_node_t *new_css_node();
void init_css_node_children(css_node_t *node, int children_count);
void free_css_node(css_node_t *node);
// Print utilities
void print_style(css_node_t *node, int level);
void print_layout(css_node_t *node, int level);
// Function that computes the layout!
void layoutNode(css_node_t *node);
#endif