I'm fairly new to this and have been stuck on some error's for quite a while
now and was wondering if anyone would be able to help me with the following...
Making the direction increment and decrement by one using the direction keys Stop the mouse from interfering with the direction number Allow the 'jack' to enter from the centre of the screen Make the 'jack' only entered the screen once instead of a fast few times like it is doing
I know i'm asking for alot of help but even if you could point me in the right
direction to getting things sorted and point out obvious mistakes that would
be great appreciated.
Thanks
#include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <SDL/SDL.h>#include <SDL/SDL_Image.h>#include <SDL/SDL_ttf.h>#include <chipmunk/chipmunk.h>#define SLEEP_TICKS 16#define XSIZE 382#define YSIZE 472#define XMID XSIZE/2#define YMID YSIZE/2intticks=0;intsome_value=42;cpSpace*space;cpBody*staticBody;cpBody*marble;SDL_Surface*screen;//ThispointerwillreferencethebackbufferSDL_Surface*marble_image,*jack_image;//ThispointerwillreferenceourbitmapspriteSDL_Surface*hline_image,*vline_image;SDL_Surface*pitch_image;SDL_Surface*temp;//ThispointerwilltemporarilyreferenceourbitmapspriteSDL_Rectsrc,dest;//Theserectangleswilldescribethesourceanddestinationregionsofourblitintrunning=0;//Isthegamerunning?intnum_marbles=1;//Somemarblesintdirection=0;/*returnapseudo-randomnumberbetween0andlimitinclusive.*/intrandomize(intlimit){intdivisor=RAND_MAX/(limit+1);intrand_val;do{rand_val=rand()/divisor;}while(rand_val>limit);returnrand_val;}SDL_Surface*loadImage(char*name){/*LoadtheimageusingSDLImage*/SDL_Surface*temp=IMG_Load(name);SDL_Surface*image;if(temp==NULL){printf("Failed to load image %s\n",name);returnNULL;}/*Makethebackgroundtransparent*/SDL_SetColorKey(temp,(SDL_SRCCOLORKEY|SDL_RLEACCEL),SDL_MapRGB(temp->format,0,0,0));/*Converttheimagetothescreen's native format */image=SDL_DisplayFormat(temp);SDL_FreeSurface(temp);if(image==NULL){printf("Failed to convert image %s to native format\n",name);returnNULL;}/*Returntheprocessedimage*/returnimage;}voidupdate(intticks){inti;intsteps=2;cpFloatdt=1.0/60.0/(cpFloat)steps;for(i=0;i<steps;i++){cpSpaceStep(space,dt);}}staticvoidpostStepRemove(cpSpace*space,cpShape*shape,void*unused){cpSpaceRemoveBody(space,shape->body);cpBodyFree(shape->body);cpSpaceRemoveShape(space,shape);cpShapeFree(shape);}staticintbegin(cpArbiter*arb,cpSpace*space,void*ignore){cpShape*a,*b;cpArbiterGetShapes(arb,&a,&b);cpSpaceAddPostStepCallback(space,(cpPostStepFunc)postStepRemove,b,NULL);return0;}staticintcollFunc(cpShape*a,cpShape*b,cpContact*contacts,intnumContacts,cpFloatnormal_coef,void*data){int*some_ptr=(int*)data;//Dovariousthingswiththecontactinformation.//Makeparticleeffects,estimatetheimpactdamagefromtherelativevelocities,etc.//for(inti=0;i<numContacts;i++)//printf("Collision at %s. (%d - %d) %d\n",cpvstr(contacts[i].p),a->collision_type,b->collision_type,*some_ptr);//Returning0willcausethecollisiontobediscarded.Thisallowsyoutodoconditionalcollisions.return1;}voidinit(void){inti;unsignedintiseed=(unsignedint)time(NULL);cpFloatradius=25;cpFloatmarble_mass=0.4;cpFloatx,y;//Initializeastaticbodywithinfinitemassandmomentofinertia//toattachthestaticgeometryto.staticBody=cpBodyNew(INFINITY,INFINITY);//Optional.Readthedocstoseewhatthisreallydoes.cpResetShapeIdCounter();//Createaspaceandadjustsomeofit's parameters.space=cpSpaceNew();space->damping=0.9;//CreateashapepointercpShape*shape;//Createaborderaroundtheedgesofthescreen.shape=cpSegmentShapeNew(staticBody,cpv(0,0),cpv(50,0),0.0f);shape->e=1.0;shape->u=1.0;cpSpaceAddStaticShape(space,shape);shape=cpSegmentShapeNew(staticBody,cpv(0,0),cpv(0,480),0.0f);shape->e=1.0;shape->u=1.0;cpSpaceAddStaticShape(space,shape);shape=cpSegmentShapeNew(staticBody,cpv(640,0),cpv(640,480),0.0f);shape->e=1.0;shape->u=1.0;cpSpaceAddStaticShape(space,shape);shape=cpSegmentShapeNew(staticBody,cpv(0,480),cpv(640,480),0.0f);shape->e=1.0;shape->u=1.0;cpSpaceAddStaticShape(space,shape);srand(iseed);//Createsomemarblesfor(i=0;i<num_marbles;i++){marble=cpBodyNew(marble_mass,cpMomentForCircle(marble_mass,0.0,radius,cpvzero));x=XMID-200+(cpFloat)181;y=YMID-200+(cpFloat)70;marble->p=cpv(x,y);//marble->v=cpv(x,y);cpSpaceAddBody(space,marble);shape=cpCircleShapeNew(marble,radius,cpvzero);shape->e=0.0;shape->u=2.5;shape->data=(cpDataPointer)0;//shape->collision_type=1;cpSpaceAddShape(space,shape);}//Createajackmarble=cpBodyNew(marble_mass,cpMomentForCircle(marble_mass,0.0,radius,cpvzero));marble->p=cpv(XSIZE,YSIZE);//marble->v=cpv(-220,direction);//marble->v=cpv(-100,-160);cpSpaceAddBody(space,marble);shape=cpCircleShapeNew(marble,radius,cpvzero);shape->data=(cpDataPointer)1;shape->e=0.0;shape->u=6.5;//shape->collision_type=1;cpSpaceAddShape(space,shape);//Addacollisioncallback(begin).cpSpaceAddCollisionHandler(space,1,0,begin,NULL,NULL,NULL,NULL);}voiddestroy(void){cpSpaceFree(space);cpBodyFree(staticBody);}staticvoidrenderMarble(cpFloatx,cpFloaty,cpFloatr,cpFloata,cpShape*shape){SDL_Rectdest;/*Settheblittingrectangletothesizeofthesrcimage*/dest.x=x;dest.y=y;if(shape->data){dest.w=jack_image->w;dest.h=jack_image->h;/*Blittheentireimageontothescreenatcoordinatesxandy*/SDL_BlitSurface(jack_image,NULL,screen,&dest);}else{dest.w=marble_image->w;dest.h=marble_image->h;/*Blittheentireimageontothescreenatcoordinatesxandy*/SDL_BlitSurface(marble_image,NULL,screen,&dest);}}staticvoidrenderShape(cpFloatx,cpFloaty,cpFloatr,cpFloata,inttype){SDL_Rectdest;/*Settheblittingrectangletothesizeofthesrcimage*/if(type==0){dest.x=x;dest.y=y;dest.w=hline_image->w;dest.h=hline_image->h;/*Blittheentireimageontothescreenatcoordinatesxandy*/SDL_BlitSurface(hline_image,NULL,screen,&dest);}elseif(type==1){dest.x=x;dest.y=y;dest.w=vline_image->w;dest.h=vline_image->h;/*Blittheentireimageontothescreenatcoordinatesxandy*/SDL_BlitSurface(vline_image,NULL,screen,&dest);}elseif(type==2){dest.x=0;dest.y=0;dest.w=pitch_image->w;dest.h=pitch_image->h;/*Blittheentireimageontothescreenatcoordinatesxandy*/SDL_BlitSurface(pitch_image,NULL,screen,&dest);}}staticvoiddrawMarbleShapes(cpShape*shape){cpBody*body=shape->body;cpCircleShape*circle=(cpCircleShape*)shape;cpVectc=cpvadd(body->p,cpvrotate(circle->c,body->rot));renderMarble(c.x,c.y,circle->r,body->a,shape);}staticvoiddrawStaticShapes(cpShape*shape){cpBody*body=shape->body;cpSegmentShape*segment=(cpSegmentShape*)shape;cpVectc=cpvadd(body->p,cpvrotate(segment->a,body->rot));renderShape(0,0,segment->r,body->a,0);renderShape(0,475,segment->r,body->a,0);renderShape(0,0,segment->r,body->a,1);renderShape(635,0,segment->r,body->a,1);renderShape(100,30,segment->r,body->a,2);}staticvoiddrawMarbles(void*ptr,void*unused){cpShape*shape=(cpShape*)ptr;drawMarbleShapes(shape);}staticvoiddrawStaticObjects(void*ptr,void*unused){cpShape*shape=(cpShape*)ptr;drawStaticShapes(shape);}intmain(intargc,char*argv[]){inti;cpBody*body;cpShape*shape;SDL_Eventevent;//SDLeventsrunning=1;//InitialisethephysicsenginecpInitChipmunk();//Initialisethestuffweneedformarblephysicsinit();//WemustfirstinitializetheSDLvideocomponent,andcheckforsuccessif(SDL_Init(SDL_INIT_VIDEO)!=0){printf("Unable to initialize SDL: %s\n",SDL_GetError());return1;}//Whenthisprogramexits,SDL_Quitmustbecalled//atexit(SDL_Quit);//Setthevideomodeto640x480with16bitcolouranddouble-bufferingscreen=SDL_SetVideoMode(XSIZE,YSIZE,16,SDL_DOUBLEBUF);if(screen==NULL){printf("Unable to set video mode: %s\n",SDL_GetError());return1;}//Setupprintingif(TTF_Init()!=0){printf("Unable to initialize SDL_ttf: %s\n",TTF_GetError());return1;}TTF_Font*fntCourier=TTF_OpenFont("cour.ttf",24);SDL_ColorclrFg={255,255,255,0};//White("Fg"isforeground)SDL_Surface*textSurface;SDL_RecttextDest={0,0,0};chartextString[100];//Loadamarbleimagetemp=loadImage("marble.png");//Convertthesurfacetotheappropriatedisplayformatmarble_image=SDL_DisplayFormatAlpha(temp);//Loadajackimagetemp=loadImage("jack.png");//Convertthesurfacetotheappropriatedisplayformatjack_image=SDL_DisplayFormatAlpha(temp);//Loadastaticobjectimagetemp=loadImage("hline.png");//Convertthesurfacetotheappropriatedisplayformathline_image=SDL_DisplayFormatAlpha(temp);temp=loadImage("vline.png");//Convertthesurfacetotheappropriatedisplayformatvline_image=SDL_DisplayFormatAlpha(temp);temp=loadImage("pitch.png");//Convertthesurfacetotheappropriatedisplayformatpitch_image=SDL_DisplayFormatAlpha(temp);//ReleasethetemporarysurfaceSDL_FreeSurface(temp);while(1){while(SDL_PollEvent(&event)){switch(event.type){if(event.type==SDL_QUIT||(event.type==SDL_KEYDOWN&&event.key.keysym.sym==SDLK_ESCAPE))running=0;}if(event.key.keysym.sym==SDLK_SPACE){marble->p=cpv(XSIZE,YSIZE);marble->v=cpv(direction,-200);}if(event.key.keysym.sym==SDLK_RIGHT){direction=direction++;sprintf(textString,"Direction=%d",direction);textSurface=TTF_RenderText_Solid(fntCourier,textString,clrFg);}if(event.key.keysym.sym=SDLK_LEFT){direction--;sprintf(textString,"Direction=%d",direction);textSurface=TTF_RenderText_Solid(fntCourier,textString,clrFg);}}//while(handlingevents)if(running==0)break;//BlankthebackgroundSDL_FillRect(screen,NULL,0);//DrawtheotherstuffcpSpaceEachShape(space,(cpSpaceShapeIteratorFunc)drawStaticObjects,NULL);//DrawthemarblesandjackcpSpaceEachShape(space,(cpSpaceShapeIteratorFunc)drawMarbles,NULL);//DrawthetextSDL_BlitSurface(textSurface,NULL,screen,&textDest);//DoublebufferSDL_Flip(screen);ticks++;update(ticks);}//while(mainloop)//ReleasethesurfacesSDL_FreeSurface(textSurface);SDL_FreeSurface(marble_image);SDL_FreeSurface(jack_image);SDL_FreeSurface(hline_image);SDL_FreeSurface(vline_image);TTF_CloseFont(fntCourier);//Returnsuccess!SDL_Quit();destroy();return0;}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm fairly new to this and have been stuck on some error's for quite a while
now and was wondering if anyone would be able to help me with the following...
Making the direction increment and decrement by one using the direction keys
Stop the mouse from interfering with the direction number
Allow the 'jack' to enter from the centre of the screen
Make the 'jack' only entered the screen once instead of a fast few times like it is doing
I know i'm asking for alot of help but even if you could point me in the right
direction to getting things sorted and point out obvious mistakes that would
be great appreciated.
Thanks