Permalink
Browse files

No commit message

  • Loading branch information...
1 parent 4227afc commit adbf0604a35c3e5610d68dff8ac3c7e15d82dbd0 namaste committed Jul 27, 2008
Showing with 161 additions and 102 deletions.
  1. +4 โˆ’0 soylent/GoD/GoD.vcproj
  2. +21 โˆ’0 soylent/GoD/avatar.h
  3. +136 โˆ’102 soylent/GoD/main.cpp
View
@@ -182,6 +182,10 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
+ <File
+ RelativePath=".\avatar.h"
+ >
+ </File>
</Filter>
<Filter
Name="Resource Files"
View
@@ -0,0 +1,21 @@
+#ifndef __AVATAR_H__
+#define __AVATAR_H__
+
+#include "chipmunk.h"
+
+class Avatar
+{
+public:
+ Avatar(cpSpace *space, cpBody *body, cpShape *shape)
+ {
+ cpSpaceAddBody(space, body);
+ cpSpaceAddShape(space, shape);
+ }
+ ~Avatar() {}
+
+
+private:
+
+};
+
+#endif
View
@@ -1,5 +1,5 @@
-#include <stdlib.h>
-#include <stdio.h>
+#include <stdlib.h>
+#include <stdio.h>
#include <math.h>
#include <windows.h>
@@ -8,14 +8,18 @@
#include "SDL.h"
#include "chipmunk.h"
+#include "avatar.h"
+
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
-int ticks = 0;
-cpSpace *space;
+int ticks = 0;
+cpSpace *space;
cpBody *staticBody;
+Avatar *avatar;
+
void init()
{
cpInitChipmunk();
@@ -45,9 +49,9 @@ void init()
void setup()
{
staticBody = cpBodyNew(INFINITY, INFINITY);
-
- cpResetShapeIdCounter();
- space = cpSpaceNew();
+
+ cpResetShapeIdCounter();
+ space = cpSpaceNew();
space->iterations = 10;//default is 10
space->gravity = cpv(0, -100);
@@ -56,32 +60,33 @@ void setup()
//count is the suggested minimum number of cells in the hash table. Bigger is better, but only to a point.
//Setting count to ~10x the number of objects in the hash is probably a good starting point.
//By default, dim is 100.0, and count is 1000.
- cpSpaceResizeStaticHash(space, 40.0, 999);
+ cpSpaceResizeStaticHash(space, 40.0, 999);
cpSpaceResizeActiveHash(space, 30.0, 2999);
- cpBody *body;
+ cpBody *body;
cpShape *shape;
//we are making polygons (pentagons)
- cpVect verts[NUM_VERTS];
- for(int i=0; i<NUM_VERTS; i++){
- cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
- verts[i] = cpv(10*cos(angle), 10*sin(angle));
+ cpVect verts[NUM_VERTS];
+ for(int i=0; i<NUM_VERTS; i++){
+ cpFloat angle = -2*(cpFloat)M_PI*i/((cpFloat) NUM_VERTS);
+ verts[i] = cpv(10*cos(angle), 10*sin(angle));
}
- for(int i=0; i<3; i++){
- body = cpBodyNew(1.0, cpMomentForPoly(1.0, NUM_VERTS, verts, cpvzero));
-// body = cpBodyNew(1.0, cpMomentForCircle(1.0, 0.0, 10.0, cpvzero));
- cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320;
- body->p = cpv(x, 350);
- cpSpaceAddBody(space, body);
- shape = cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero);
-// shape = cpCircleShapeNew(body, 10.0, cpvzero);
- shape->e = 0.0; shape->u = 0.04;
- cpSpaceAddShape(space, shape);
+ for(int i=0; i<3; i++){
+ body = cpBodyNew(1.0, cpMomentForPoly(1.0, NUM_VERTS, verts, cpvzero));
+// body = cpBodyNew(1.0, cpMomentForCircle(1.0, 0.0, 10.0, cpvzero));
+ cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320;
+ body->p = cpv(x, 350);
+ cpSpaceAddBody(space, body);
+ shape = cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero);
+// shape = cpCircleShapeNew(body, 10.0, cpvzero);
+ shape->e = 0.0; shape->u = 0.04;
+ cpSpaceAddShape(space, shape);
}
-
+ body->p = cpv(0, 0);
+ avatar = new Avatar(space, body, shape);
}
//Handle Keyboard and Mouse events.
@@ -90,10 +95,20 @@ int event_loop()
SDL_Event e;
while(SDL_PollEvent(&e))
{
+ SDLKey key = e.key.keysym.sym;
switch(e.type)
{
case SDL_KEYDOWN:
- if(e.key.keysym.sym == SDLK_ESCAPE) return 0;
+ if(key == SDLK_ESCAPE) return 0;
+ else if(key == SDLK_w)
+ {
+ }
+ else if(key == SDLK_s)
+ {
+ }
+ else if(key == SDLK_a)
+ {}
+ else if(key == SDLK_d) {}
break;
default:
break;
@@ -102,100 +117,105 @@ int event_loop()
return 1;
}
-static void
-eachBody(cpBody *body, void *unused)
-{
- if(body->p.y < -260 || fabsf(body->p.x) > 340){
- cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320;
- body->p = cpv(x, 260);
- }
+void logic ()
+{
+
+}
+
+static void
+eachBody(cpBody *body, void *unused)
+{
+ if(body->p.y < -260 || fabsf(body->p.x) > 340){
+ cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320;
+ body->p = cpv(x, 260);
+ }
}
void update()
{
- int steps = 1;
- cpFloat dt = 1.0/60.0/(cpFloat)steps;
-
- for(int i=0; i<steps; i++){
- cpSpaceStep(space, dt);
- cpSpaceEachBody(space, &eachBody, NULL);
+ int steps = 1;
+ cpFloat dt = 1.0/60.0/(cpFloat)steps;
+
+ for(int i=0; i<steps; i++){
+ cpSpaceStep(space, dt);
+ cpSpaceEachBody(space, &eachBody, NULL);
}
}
-void drawCircle(cpFloat x, cpFloat y, cpFloat r, cpFloat a)
-{
- const int segs = 15;
- const cpFloat coef = 2.0*M_PI/(cpFloat)segs;
-
- glBegin(GL_LINE_STRIP); {
- for(int n = 0; n <= segs; n++){
- cpFloat rads = n*coef;
- glVertex2f(r*cos(rads + a) + x, r*sin(rads + a) + y);
- }
- glVertex2f(x,y);
- } glEnd();
+void drawCircle(cpFloat x, cpFloat y, cpFloat r, cpFloat a)
+{
+ const int segs = 15;
+ const cpFloat coef = 2.0*M_PI/(cpFloat)segs;
+
+ glBegin(GL_LINE_STRIP); {
+ for(int n = 0; n <= segs; n++){
+ cpFloat rads = n*coef;
+ glVertex2f(r*cos(rads + a) + x, r*sin(rads + a) + y);
+ }
+ glVertex2f(x,y);
+ } glEnd();
}
-void drawCircleShape(cpShape *shape)
-{
- cpBody *body = shape->body;
- cpCircleShape *circle = (cpCircleShape *)shape;
- cpVect c = cpvadd(body->p, cpvrotate(circle->c, body->rot));
- drawCircle(c.x, c.y, circle->r, body->a);
-}
-
-void drawSegmentShape(cpShape *shape)
-{
- cpBody *body = shape->body;
- cpSegmentShape *seg = (cpSegmentShape *)shape;
- cpVect a = cpvadd(body->p, cpvrotate(seg->a, body->rot));
- cpVect b = cpvadd(body->p, cpvrotate(seg->b, body->rot));
-
- glBegin(GL_LINES); {
- glVertex2f(a.x, a.y);
- glVertex2f(b.x, b.y);
- } glEnd();
-}
-
-void drawPolyShape(cpShape *shape)
-{
- cpBody *body = shape->body;
- cpPolyShape *poly = (cpPolyShape *)shape;
-
- int num = poly->numVerts;
- cpVect *verts = poly->verts;
-
- glBegin(GL_LINE_LOOP);
- for(int i=0; i<num; i++){
- cpVect v = cpvadd(body->p, cpvrotate(verts[i], body->rot));
- glVertex2f(v.x, v.y);
- } glEnd();
+void drawCircleShape(cpShape *shape)
+{
+ cpBody *body = shape->body;
+ cpCircleShape *circle = (cpCircleShape *)shape;
+ cpVect c = cpvadd(body->p, cpvrotate(circle->c, body->rot));
+ drawCircle(c.x, c.y, circle->r, body->a);
}
-void drawObject(void *ptr, void *unused)
-{
- cpShape *shape = (cpShape *)ptr;
- switch(shape->type){
- case CP_CIRCLE_SHAPE:
- drawCircleShape(shape);
- break;
- case CP_SEGMENT_SHAPE:
- drawSegmentShape(shape);
- break;
- case CP_POLY_SHAPE:
- drawPolyShape(shape);
- break;
- default:
- printf("Bad enumeration in drawObject().\n");
- }
+void drawSegmentShape(cpShape *shape)
+{
+ cpBody *body = shape->body;
+ cpSegmentShape *seg = (cpSegmentShape *)shape;
+ cpVect a = cpvadd(body->p, cpvrotate(seg->a, body->rot));
+ cpVect b = cpvadd(body->p, cpvrotate(seg->b, body->rot));
+
+ glBegin(GL_LINES); {
+ glVertex2f(a.x, a.y);
+ glVertex2f(b.x, b.y);
+ } glEnd();
+}
+
+void drawPolyShape(cpShape *shape)
+{
+ cpBody *body = shape->body;
+ cpPolyShape *poly = (cpPolyShape *)shape;
+
+ int num = poly->numVerts;
+ cpVect *verts = poly->verts;
+
+ glBegin(GL_LINE_LOOP);
+ for(int i=0; i<num; i++){
+ cpVect v = cpvadd(body->p, cpvrotate(verts[i], body->rot));
+ glVertex2f(v.x, v.y);
+ } glEnd();
+}
+
+void drawObject(void *ptr, void *unused)
+{
+ cpShape *shape = (cpShape *)ptr;
+ switch(shape->type){
+ case CP_CIRCLE_SHAPE:
+ drawCircleShape(shape);
+ break;
+ case CP_SEGMENT_SHAPE:
+ drawSegmentShape(shape);
+ break;
+ case CP_POLY_SHAPE:
+ drawPolyShape(shape);
+ break;
+ default:
+ printf("Bad enumeration in drawObject().\n");
+ }
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0, 0.0, 0.0);
- cpSpaceHashEach(space->activeShapes, &drawObject, NULL);
+ glColor3f(0.0, 0.0, 0.0);
+ cpSpaceHashEach(space->activeShapes, &drawObject, NULL);
cpSpaceHashEach(space->staticShapes, &drawObject, NULL);
SDL_GL_SwapBuffers();
@@ -208,6 +228,9 @@ void run()
//process events
if(event_loop() == 0) return;
+ //perform game logic
+ logic();
+
//update the simulation
update();
@@ -216,10 +239,21 @@ void run()
}
}
+void quit()
+{
+ cpSpaceFreeChildren(space);
+ cpSpaceFree(space);
+
+ cpBodyFree(staticBody);
+
+ delete avatar;
+}
+
int main(int argc, char **argv)
{
init();
setup();
run();
+ //quit();
return 0;
}

0 comments on commit adbf060

Please sign in to comment.