This commit is contained in:
AnonymusRaccoon
2020-03-12 16:46:25 +01:00
4 changed files with 121 additions and 72 deletions
+20
View File
@@ -0,0 +1,20 @@
/*
** EPITECH PROJECT, 2019
** Gamacon
** File description:
** vertex_component
*/
#ifndef MY3D_CREATE_MAP_FUNCTIONS_H
#define MY3D_CREATE_MAP_FUNCTIONS_H
#include <stdbool.h>
#include "vertex_component.h"
bool get_vertices(struct vertex_component *this, node *n);
char *get_texture_for_coords(gc_vector2i coords, node *n);
bool init_tile(struct vertex_component *this, int *inc, void **d, \
gc_vector2i c);
bool get_tiles(struct vertex_component *this, gc_scene *scene, node *n);
#endif //MY3D_CREATE_MAP_FUNCTIONS_H
-9
View File
@@ -9,15 +9,6 @@
#include <stdbool.h>
#include "vertex_component.h"
enum modes_on_tile {
VERTEX_0 = 2,
VERTEX_1 = 4,
VERTEX_2 = 8,
VERTEX_3 = 16,
ALL_VERTICES = 30,
INVERT_ADD_VALUE = 1
};
#define ANGLE_X 45
#define ANGLE_Y 35
#define ADD_VALUE 10
@@ -0,0 +1,98 @@
/*
** EPITECH PROJECT, 2019
** Gamacon
** File description:
** vertex_component
*/
#include "xml.h"
#include <malloc.h>
#include <stdint.h>
#include "create_map_functions.h"
bool get_vertices(struct vertex_component *this, node *n)
{
int i = 0;
int j = 0;
int size[3] = {sizeof(struct vertex), sizeof(struct vertex *), 0};
this->vertices = malloc(size[1] * (xml_getchildcount(n) + 1));
if (!this->vertices)
return (false);
for (node *line = n->child; line; line = line->next) {
this->vertices[i] = malloc(size[0] * (xml_getchildcount(line) + 1));
if (!this->vertices[i])
return (false);
for (node *row = line->child; row; row = row->next) {
size[2] = xml_getintprop(row, "height");
this->vertices[i][j] = (struct vertex){i, j++, size[2]};
}
this->vertices[i++][j] = (struct vertex){0, 0, INT32_MIN};
j = 0;
}
this->vertices[i] = NULL;
return (true);
}
char *get_texture_for_coords(gc_vector2i coords, node *n)
{
char *texture = NULL;
for (node *text = n->child; text; text = text->next) {
if (xml_getintprop(text, "x") != coords.x)
continue;
if (xml_getintprop(text, "y") != coords.y)
continue;
texture = xml_getproperty(text, "name");
break;
}
return (texture);
}
bool init_tile(struct vertex_component *this, int *inc, void **d, \
gc_vector2i c)
{
char *t;
void *tmp;
gc_vector2i arr[4] = {c, (gc_vector2i){c.x, c.y + 1}, \
(gc_vector2i){c.x + 1, c.y + 1}, (gc_vector2i){c.x + 1, c.y}};
for (int i = 0; i < 4; i++)
if (this->vertices[arr[i].x][arr[i].y].z == INT32_MIN)
return (false);
for (int i = 0; i < 4; i++)
this->map[*inc].corners[i] = &this->vertices[arr[i].x][arr[i].y];
this->map[*inc].data = 0;
t = get_texture_for_coords(c, d[0]);
if (t) {
tmp = ((gc_scene *)d[1])->get_data(d[1], "sprite", t);
this->map[*inc].texture = tmp;
free(t);
}
else
this->map[*inc].texture = NULL;
return (true);
}
bool get_tiles(struct vertex_component *this, gc_scene *scene, node *n)
{
int inc = 0;
int v_x = xml_getchildcount(n);
void *dodge[2] = {n->next, scene};
int vy = xml_getchildcount(n->child->child);
for (node *line = n->child; line; line = line->next)
vy = (xml_getchildcount(line) > vy) ? xml_getchildcount(line) : vy;
this->map = malloc(sizeof(struct tile) * (v_x * vy + 1));
if (!this->map)
return (false);
for (v_x = 0; this->vertices[v_x + 1]; v_x++) {
for (vy = 0; this->vertices[v_x][vy].z != INT32_MIN; vy++) {
if (!init_tile(this, &inc, dodge, (gc_vector2i){v_x, vy}))
break;
inc++;
}
}
this->map[inc].corners[0] = NULL;
return (true);
}
+3 -63
View File
@@ -2,75 +2,15 @@
** EPITECH PROJECT, 2019
** Gamacon
** File description:
** movable_component
** vertex_component
*/
#include "xml.h"
#include "component.h"
#include "components/vertex_component.h"
#include "create_map_functions.h"
#include "utility.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
static bool get_vertices(void *component, node *n)
{
struct vertex_component *this = (struct vertex_component *)component;
int i = 0;
int j = 0;
int size[3] = {sizeof(struct vertex), sizeof(struct vertex *), 0};
this->vertices = malloc(size[1] * (xml_getchildcount(n) + 1));
if (!this->vertices)
return (false);
for (node *line = n->child; line; line = line->next) {
this->vertices[i] = malloc(size[0] * (xml_getchildcount(line) + 1));
if (!this->vertices[i])
return (false);
for (node *row = line->child; row; row = row->next) {
size[2] = xml_getintprop(row, "height");
this->vertices[i][j] = (struct vertex){i, j++, size[2]};
}
this->vertices[i++][j] = (struct vertex){0, 0, INT32_MIN};
j = 0;
}
this->vertices[i] = NULL;
return (true);
}
static bool get_tiles(struct vertex_component *this, gc_scene *scene, node *n)
{
int inc = 0;
int v_x = xml_getchildcount(n);
int vy = xml_getchildcount(n->child);
for (node *line = n->child; line; line = line->next)
vy = (xml_getchildcount(line) > vy) ? xml_getchildcount(line) : vy;
this->map = malloc(sizeof(struct tile) * (v_x * vy + 1));
if (!this->map)
return (false);
for (v_x = 0; this->vertices[v_x + 1]; v_x++) {
for (vy = 0; this->vertices[v_x][vy].z != INT32_MIN; vy++) {
if (this->vertices[v_x][vy].z == INT32_MIN || \
this->vertices[v_x][vy + 1].z == INT32_MIN || \
this->vertices[v_x + 1][vy].z == INT32_MIN || \
this->vertices[v_x + 1][vy + 1].z == INT32_MIN)
break;
this->map[inc].corners[0] = &this->vertices[v_x][vy];
this->map[inc].corners[1] = &this->vertices[v_x][vy + 1];
this->map[inc].corners[3] = &this->vertices[v_x + 1][vy];
this->map[inc].corners[2] = &this->vertices[v_x + 1][vy + 1];
this->map[inc].data = 0;
this->map[inc++].texture = scene->get_data(scene, "sprite", (inc % 2) ? "cobblestone" : "mossy_cobblestone");
if (inc == 60)
this->map[inc - 1].texture = scene->get_data(scene, "sprite", "comparator_on");
if (inc > 31 && inc < 56)
this->map[inc - 1].texture = scene->get_data(scene, "sprite", "crafting_table");
}
}
this->map[inc].corners[0] = NULL;
return (true);
}
static void ctr(void *component, va_list args)
{
@@ -95,7 +35,7 @@ static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n)
my_printf("gamacon: unable to find a valid tilemap at %s\n", tilemap);
return;
}
if (!get_vertices(component, n) || !get_tiles(this, scene, n)) {
if (!get_vertices(this, n->child) || !get_tiles(this, scene, n->child)) {
my_printf("Unable to malloc during verticies/tile parsing\n");
return;
}