shithub: tinygl

Download patch

ref: 32382f127cbd4c0c267012d1381083af86b4f353
parent: 1c67d76d6acdb928aca648274b5c53a11b8bada0
author: gek169 <gek169>
date: Sun May 9 09:20:48 EDT 2021

Automatic commit.

--- a/include-demo/chade.h
+++ b/include-demo/chade.h
@@ -42,97 +42,88 @@
 	return ret;
 }
 
-typedef struct{
-	GLuint dlframedata[256];
-	GLuint tx; /*if this is zero, texture will be unbound.*/
-} ChadAsset;
 
+static inline void drawModel(
+	vec3* points, GLuint npoints, vec3* colors, vec3* normals, vec3* texcoords) {
+	GLuint i;
+	if (!points)
+		return;
+	glBegin(GL_TRIANGLES);
+	for (i = 0; i < npoints; i++) {
+		if (colors) {
+			glColor3f(colors[i].d[0], colors[i].d[1], colors[i].d[2]);
+		}
+		if (texcoords)
+			glTexCoord2f(texcoords[i].d[0], texcoords[i].d[1]);
+		if (normals)
+			glNormal3f(normals[i].d[0], normals[i].d[1], normals[i].d[2]);
+		glVertex3f(points[i].d[0], points[i].d[1], points[i].d[2]);
+	}
+	glEnd();
+	return;
+}
+
 typedef struct {
 	phys_body body; /*body*/
-	void* pimpl; /*pointer to implementation*/
-	GLuint asset; /*Rendering details*/
-	unsigned char frame; /*the frame of animation to be used from the asset.*/
+	GLuint dl; /*Rendering details*/
 } ChadEntity;
 
 typedef struct{
 	phys_world world;
-	ChadEntity* ents;
-	ChadEntity* ents_phys; /*Where you place your objects.*/
-	ChadAsset* assets;
-	unsigned long n_ents; /*ents and ents_phys must contain the same number of elements.*/
-	unsigned long n_assets;
+	ChadEntity** ents;
+	long n_ents; /*ents and ents_phys must contain the same number of elements.*/
+	long max_ents;
 } ChadWorld;
 
-static inline void initChadWorld(ChadWorld* w){
+static inline void initChadWorld(ChadWorld* w, long max_entities){
 	*w = (ChadWorld){0};
-	w->world.ms = 9000;/*plenty fast*/
-	w->world.g = (vec3){{0,-10,0}};/*plenty realistic*/
+	w->world.ms = 1000;/*plenty fast*/
+	w->world.g = (vec3){{0,-1,0}};/*plenty realistic*/
+	w->ents = calloc(1, sizeof(void*) * max_entities);
+	if(w->ents == NULL)exit(1);
+	w->max_ents = max_entities;
+	w->n_ents = 0;
 }
 
-static inline void stepChadWorld(ChadWorld* world){
-	stepPhysWorld(&world->world, 1);
+static inline void stepChadWorld(ChadWorld* world, long iter){
+	stepPhysWorld(&world->world, iter);
 }
-/*Called at the beginning of every frame before the split.*/
-static inline void syncChadWorld(ChadWorld* world){
-	memcpy(world->ents, world->ents_phys, sizeof(ChadEntity) * world->n_ents);
-}
-/*invoked every single time that an object is added or removed*/
-static inline void prepChadWorld(ChadWorld* world){
-unsigned long i;
-	if(world->world.bodies) free(world->world.bodies);
-	world->world.bodies = calloc(1,sizeof(phys_body*) * world->n_ents);
-	/*Bodies is an array of pointers.*/
-	for(i = 0; i < world->n_ents; i++){
-		world->world.bodies[i] = &world->ents_phys[i].body;
+static inline void ChadWorld_AddEntity(ChadWorld* world, ChadEntity* ent){
+	/*safety check.
+	for(long i = 0; i < world->max_ents; i++)
+		if(world->ents[i] == ent) return;
+	*/		
+	for(long i = 0; i < world->max_ents; i++){
+		if(world->ents[i] == NULL){
+			world->ents[i] = ent; return;
+			world->world.bodies[i] = &ent->body;
+		}
 	}
-	syncChadWorld(world);
-}
 
-static inline void ChadWorld_AddEntity(ChadWorld* world, ChadEntity ent){
-	world->ents_phys = realloc(world->ents_phys, sizeof(ChadEntity) * (world->n_ents++));
-	world->ents_phys[world->n_ents-1] = ent;
-	prepChadWorld(world);
 }
 
-static inline void ChadWorld_RemoveEntity(ChadWorld* world, GLuint index){
-	ChadEntity* old_ents_phys = world->ents_phys;
-	if(world->n_ents <= index) return; /*Bad index*/
-	world->ents_phys = calloc(1,(--world->n_ents) * sizeof(ChadEntity));
-	for(unsigned long i = 0; i < world->n_ents+1; i++) /**/
-	{
-		
-		if(i < index)
-			world->ents_phys[i] = old_ents_phys[i];
-		else if(i==index) continue; /*Skip.*/
-		else if(i > index) /*Gets moved back a position.*/
-			world->ents_phys[i-1] = old_ents_phys[i];
-	}
-	free(old_ents_phys);
-	prepChadWorld(world);
+static inline void ChadWorld_RemoveEntity(ChadWorld* world, unsigned long index){
+	if(index < (unsigned long)world->max_ents && index > 0)
+		{world->ents[index] = NULL;world->world.bodies[index] = NULL;}
 }
 
 
 static inline void renderChadWorld(ChadWorld* world){
-	unsigned long i; 
+	long i; 
 	/*The user will already have set up the viewport, the camera, and the lights.*/
-	for(i = 0; i < world->n_ents; i++){
-		glPushMatrix();
-			/*build the transformation matrix*/
-			glTranslatef(world->ents[i].body.v.d[0],
-						world->ents[i].body.v.d[1],
-						world->ents[i].body.v.d[2]);
-			glMultMatrixf(world->ents[i].body.localt.d);
-			if(world->assets[world->ents[i].asset].tx){
-				glEnable(GL_TEXTURE_2D);
-				glBindTexture(GL_TEXTURE_2D, world->assets[world->ents[i].asset].tx);
-			} else {
-				glDisable(GL_TEXTURE_2D);
-			}
-			/*Render that shizzle!*/
-			glCallList(
-				world->assets[world->ents[i].asset].dlframedata[world->ents[i].frame]
-			);
-		glPopMatrix();
-	}
+	for(i = 0; i < world->n_ents; i++)
+		if(world->ents[i]){
+			glPushMatrix();
+				/*build the transformation matrix*/
+				glTranslatef(world->ents[i]->body.v.d[0],
+							world->ents[i]->body.v.d[1],
+							world->ents[i]->body.v.d[2]);
+				glMultMatrixf(world->ents[i]->body.localt.d);
+				/*Render that shizzle!*/
+				glCallList(
+					world->ents[i]->dl
+				);
+			glPopMatrix();
+		}
 }