Permalink
Browse files

Add a generic linked list implementation.

  • Loading branch information...
1 parent 6ea1ac0 commit 83e743f1d6074af89ad0fbb6c24edc63aa28801c @batiste committed Jun 11, 2012
Showing with 172 additions and 25 deletions.
  1. +20 −0 common.c
  2. +112 −0 list.c
  3. +40 −25 my_app.c
View
@@ -3,6 +3,7 @@
#include "SDL.h"
#include "lodepng.h"
#include "lodepng.c"
+#include "list.c"
#define LOG_TAG "SDL"
@@ -42,6 +43,7 @@ float _mouse_x = 0.0;
float _mouse_y = 0.0;
static Uint32 next_time;
+
GLuint gvPositionHandle; // shader handler
GLuint gvTexCoordHandle;
GLuint gvSamplerHandle;
@@ -974,6 +976,13 @@ void playSound() {
}
+GenericList mouse_buffer = { 0, NULL, NULL };
+struct MousePosition {
+ int x;
+ int y;
+};
+typedef struct MousePosition MousePosition;
+
int getMouse(int* x, int* y) {
// instead of top left reference we use the center of
// the screen
@@ -988,6 +997,17 @@ int getMouse(int* x, int* y) {
*(x) = _x - hw;
*(y) = -(_y - hh);
#endif
+
+ MousePosition *pos = malloc(sizeof(MousePosition));
+ pos->x = *(x);
+ pos->y = *(y);
+
+ addToList(&mouse_buffer, pos);
+ while(mouse_buffer.length > 10) {
+ free(mouse_buffer.last->data);
+ removeFromList(&mouse_buffer, mouse_buffer.last);
+ }
+ //displayList(&mouse_buffer);
}
View
@@ -0,0 +1,112 @@
+#include <stdlib.h>
+
+void assert(int value) {
+ if(value)
+ return;
+ printf("assert fail\n");
+ exit(1);
+}
+
+//Generic list Element
+struct ListElement {
+ struct ListElement *next;
+ struct ListElement *prev;
+ void *data;
+};
+typedef struct ListElement ListElement;
+
+//Generic List Structure
+struct GenericList {
+ int length; //Number of elements in list
+ struct ListElement *first; //Ptr to first element in list
+ struct ListElement *last; //Ptr to last element in list
+};
+typedef struct GenericList GenericList;
+
+void createList(struct GenericList *list) {
+ list->length = 0;
+ list->first = NULL;
+ list->last = NULL;
+}
+
+ListElement *addToList(GenericList *list, void *item) {
+ //check inputs
+ assert(item!=NULL);
+ assert(list!=NULL);
+ //Create generic element to hold item ptr
+ ListElement *newElement;
+ newElement = (ListElement *)malloc(sizeof(newElement)); //create generic element
+ assert(newElement != NULL);
+ list->length = list->length + 1;
+ newElement->data = item;
+ if (list->length == 1)
+ {
+ list->last = newElement;
+ newElement->prev = NULL;
+ newElement->next = NULL;
+ }
+ else
+ {
+ newElement->prev = NULL;
+ newElement->next = list->first;
+ list->first->prev = newElement;
+ }
+ list->first = newElement;
+ return newElement;
+}
+
+int removeFromList(GenericList *list, ListElement *toRemove) {
+ // TODO: find a mechanisms to free deleted items
+
+ //check inputs
+ if(toRemove==NULL) {
+ return 0;
+ }
+ assert(list!=NULL);
+ ListElement *el;
+ if (list->length == 0) {
+ return 0;
+ }
+
+ if (list->length == 1) {
+ if(toRemove == list->first) {
+ list->length = 0;
+ list->first = NULL;
+ list->last = NULL;
+ return 1;
+ }
+ return 0;
+ }
+
+ // there is at least 2 items in the list
+ for(el = list->first; el != NULL; el=el->next) {
+ // found a matching item
+ if(el == toRemove) {
+ // this is the first item
+ if(el == list->first) {
+ list->first = el->next;
+ list->first->prev = NULL;
+ // this is the last item
+ } else if (el == list->last) {
+ list->last = el->prev;
+ list->last->next = NULL;
+ } else {
+ el->prev->next = el->next;
+ el->next->prev = el->prev;
+ }
+ list->length = list->length - 1;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+
+void displayList(GenericList *list) {
+ ListElement *el;
+ printf("List length %d\n", list->length);
+ int i = 1;
+ for(el = list->first; el != NULL; el=el->next, i=i+1) {
+ printf("Item %d address %d\n", i, (int)el->data);
+ }
+}
View
@@ -65,8 +65,7 @@ int main(int argc, char** argv)
int mouse_y_prev = 0;
int lastCut = 0;
- struct TextureInfos *pieces[100];
- int nbPieces = 0;
+ GenericList pieces = { 0, NULL, NULL };
transformTexture(&back2, 0, -back2.height, 0);
@@ -80,6 +79,7 @@ int main(int argc, char** argv)
}
}
+
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
CHECK_GL();
@@ -109,6 +109,23 @@ int main(int argc, char** argv)
getMouse(&mouse_x, &mouse_y);
+ ListElement *el;
+
+ MousePosition *pos1 = 0;
+ MousePosition *pos2 = 0;
+ for(el = mouse_buffer.first; el != NULL; el=el->next) {
+ pos2 = pos1;
+ pos1 = (MousePosition *) el->data;
+ if(pos1 && pos2) {
+ vertices[0] = pos1->x;
+ vertices[1] = pos1->y;
+ vertices[2] = pos2->x;
+ vertices[3] = pos2->y;
+ drawLines(vertices, 2);
+ }
+ }
+
+
vx = (vx / 1.5) + ((mouse_x - mouse_x_prev) / 30.0);
vy = (vy / 1.5) + ((mouse_y - mouse_y_prev) / 30.0);
mouse_y_prev = mouse_y_prev + vy;
@@ -118,21 +135,28 @@ int main(int argc, char** argv)
vertices[1] = mouse_y_prev;
vertices[2] = mouse_x;
vertices[3] = mouse_y;
- drawLines(vertices, 2);
+ //drawLines(vertices, 2);
+
+ MousePosition *first = 0;
+ MousePosition *last = 0;
+ first = (MousePosition *)mouse_buffer.first->data;
+ last = (MousePosition *)mouse_buffer.last->data;
struct Line line;
- line.ax = vertices[0];
- line.ay = vertices[1];
- line.bx = vertices[2];
- line.by = vertices[3];
GLfloat points[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+ if(first != last) {
+ line.ax = first->x;
+ line.ay = first->y;
+ line.bx = last->x;
+ line.by = last->y;
+ }
int nbPoints = 0;
if(nbPoints = find_intersect_points(&texture, &line, points)) {
useProgram(pointProgram);
drawLines(points, nbPoints);
- if(nbPoints > 1 && nbPieces < 95 && lastCut < frames) {
+ if(nbPoints > 1 && lastCut < frames) {
lastCut = frames + 10;
@@ -141,10 +165,8 @@ int main(int argc, char** argv)
split_vertex(&texture, &line, texture1, texture2);
- pieces[nbPieces] = texture1;
- nbPieces = nbPieces + 1;
- pieces[nbPieces] = texture2;
- nbPieces = nbPieces + 1;
+ addToList(&pieces, texture1);
+ addToList(&pieces, texture2);
texture1->vx = 2.0;
texture2->vx = -2.0;
@@ -154,10 +176,6 @@ int main(int argc, char** argv)
texture1->vr = 0.01;
texture2->vr = -0.01;
-
- /*useProgram(lineProgram);
- drawLinesFromVertices(texture1->vertices, texture1->verticesSize);
- drawLinesFromVertices(texture2->vertices, texture2->verticesSize);*/
}
@@ -166,15 +184,13 @@ int main(int argc, char** argv)
}
struct TextureInfos * myTexture;
- for(i=0; i<nbPieces; i++) {
-
- myTexture = pieces[i];
- if(myTexture == NULL)
- continue;
-
- // TODO: freee and cleanup
+ for(el = pieces.first; el != NULL; el=el->next) {
+ myTexture = (struct TextureInfos *) el->data;
+ // TODO: free and cleanup
if(myTexture->y < -screen.h) {
- pieces[i] = NULL;
+ removeFromList(&pieces, el);
+ free(myTexture);
+ continue;
}
// gravity
@@ -184,7 +200,6 @@ int main(int argc, char** argv)
useProgram(textureProgram);
drawTexture(myTexture, 0, 0, 0);
-
}

0 comments on commit 83e743f

Please sign in to comment.