1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "SDL.h"
#include "sge.h"
int main(int argc, char** argv)
{
/* Init SDL */
if ( SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
exit(1);
}
/* Clean up on exit */
atexit(SDL_Quit);
/* Set window title */
SDL_WM_SetCaption("Collision", "collision");
/* Initialize the display */
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
sge_ClearSurface(screen,0);
//Allocate some buffers
SDL_Surface *img1,*buffer;
//The buffer for the ball
img1=SDL_AllocSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY,30,30,16, 0,0,0,0);
sge_ClearSurface(img1,0);
sge_AAFilledCircle(img1,15,15, 14, 150,200,50);
SDL_SetColorKey(img1, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0); //Set transparent color
//Draw a background (add more filled rectangles if you want)
sge_FilledRect(screen, 300,80, 340,400, 0,0,255);
sge_FilledRect(screen, 0,360, 200,400, 0,0,255);
sge_FilledRect(screen, 340,260, 550,300, 0,0,255);
sge_FilledRect(screen, 100,80, 140,360, 0,0,255);
sge_FilledRect(screen, 500,80, 550,130, 0,0,255);
buffer=SDL_DisplayFormat(screen); //The buffer for the background
//Make the collision maps
sge_cdata *cimg1, *cbg;
cimg1=sge_make_cmap(img1);
SDL_SetColorKey(buffer, SDL_SRCCOLORKEY, 0); //Must set transparent color on background
cbg=sge_make_cmap(buffer);
SDL_SetColorKey(buffer, 0, 0); //No transparency
// If the delay between two PollEvent is greater than 100 and all events are added to the
// event queue, the queue will grow *fast*. You're program will have no change to catch
// up. You *must* try to avoid adding events unnecessarily - mousemotions for example.
SDL_EventState(SDL_MOUSEMOTION,SDL_IGNORE);
Sint16 x=50,y=190;
Sint16 oldx=x,oldy=y;
bool right=true, down=false;
sge_Update_OFF();
// Sets start time
Uint32 tstart=SDL_GetTicks();
int loops=0;
// Main loop
SDL_Event event;
do{
SDL_Delay(10);
//Check borders
if( (x+img1->w)>=screen->w )
right=false;
else if(x<=0)
right=true;
if( (y+img1->h)>=screen->h )
down=false;
else if(y<=0)
down=true;
//Check for collision
if( sge_cmcheck(cbg,0,0, cimg1,x,y) ){
//SDL_Delay(200);
//Bounce the ball
if( abs(sge_get_cx()-(x+img1->w/2)) < abs(sge_get_cy()-(y+img1->h/2)) ){
if( sge_get_cy()-(y+img1->h/2) < 0 )
down=true;
else
down=false;
}
else{
if( sge_get_cx()-(x+img1->w/2) < 0 )
right=true;
else
right=false;
}
}
//Update pos
if(right)
x++;
else
x--;
if(down)
y++;
else
y--;
//Update last pos from buffer
sge_Blit(buffer,screen,oldx,oldy, oldx,oldy, img1->w,img1->h);
//Draw the circle
sge_Blit(img1,screen,0,0, x,y, img1->w,img1->h);
oldx=x;oldy=y;
//Update screen
sge_Update_ON();
sge_UpdateRect(screen,x-1,y-1,img1->w+2,img1->h+2);
sge_Update_OFF();
loops++;
// Check events
if(SDL_PollEvent(&event)==1){
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE){break;}
if(event.type==SDL_QUIT){break;}
}
}while(true);
/* Print FPS */
printf("%.2f FPS (target: 100).\n",((double)loops*1000)/(SDL_GetTicks()-tstart));
//Clean upp
SDL_FreeSurface(img1);
SDL_FreeSurface(buffer);
sge_destroy_cmap(cimg1);
sge_destroy_cmap(cbg);
return 0;
}
|