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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| // COMPIL THIS FILE WITH : gcc -Wall -g -std=c99 sdlanim.c `sdl-config --cflags --libs` -lm -o sdlanim
#include "math.h"
#include "SDL.h"
#define PI 3.14159265359
#define SCREEN_WIDTH 720 // horizontal size of the frame
#define SCREEN_HEIGHT 540 // vertical size of the frame
#define BOARD_LEFT 200 // x-coord of the left border of the game board
#define BOARD_RIGHT 520 // x-coord of the right border of the game board
#define BOARD_TOP 31 // y-coord of the top border of the game board
#define BOARD_BOTTOM 422 // y-coord of the bottom border of the game board (will be useful in next levels)
#define LAUNCHER_WIDTH 187 // width of the launcher surface
#define LAUNCHER_HEIGHT 157 // height of the launcher surface
#define LAUNCHER_CENTER 94 // y-coord of the launcher center
#define LAUNCHER_DIV 48 // PI/LAUNCHER_DIV is the angular distance between each of the 45 launcher orientations
#define BUB_SIZE 40 // horizontal and vertical size of the bubble surface
#define VELOCITY 1 // bubbles velocity
#define BUB_NX 8 // max. number of bubbles in horizontal direction
#define BUB_NY 11 // max. number of bubbles in vertical direction
int gameover, launched, reset, launcherDirection, bub_array[BUB_NY][BUB_NX];
float angle, dX, dY, BPosX, BPosY;
SDL_Rect launcherRect, bubblePosition;
void centralizeBubble()
{
bubblePosition.x = SCREEN_WIDTH / 2 - BUB_SIZE / 2;
bubblePosition.y = SCREEN_HEIGHT - LAUNCHER_HEIGHT + LAUNCHER_CENTER - BUB_SIZE / 2;
BPosX = bubblePosition.x;
BPosY = bubblePosition.y;
reset = 1;
}
void HandleEvent(SDL_Event event)
{
switch (event.type)
{
case SDL_QUIT:
gameover = 1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
case SDLK_SPACE:
if (!launched)
{
if (reset)
{
launched = 1;
reset = 0;
angle = PI - launcherDirection * PI / LAUNCHER_DIV;
dX = VELOCITY * cos(angle);
dY = -VELOCITY * sin(angle);
}
else
centralizeBubble();
}
break;
case SDLK_LEFT:
if (launcherRect.y >= LAUNCHER_HEIGHT)
{
launcherRect.y -= LAUNCHER_HEIGHT;
launcherDirection-=1;
}
break;
case SDLK_RIGHT:
if (launcherRect.y <= 43 * LAUNCHER_HEIGHT)
{
launcherRect.y += LAUNCHER_HEIGHT;
launcherDirection+=1;
}
break;
}
break;
}
}
int main(int argc, char *argv[])
{
SDL_Surface *screen, *temp, *lcher, *bckg, *bub;
SDL_Rect backgroundPosition, lcherPos, bubSrc;
int colorkey;
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("SDL Animation", "SDL Animation");
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
SDL_EnableKeyRepeat(70, 70);
temp = SDL_LoadBMP("frame_launcher.bmp");
lcher = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
SDL_SetColorKey(lcher, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
temp = SDL_LoadBMP("frame_1p.bmp");
bckg = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
SDL_SetColorKey(bckg, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
temp = SDL_LoadBMP("bub_blue.bmp");
bub = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
SDL_SetColorKey(bub, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
lcherPos.x = SCREEN_WIDTH / 2 - LAUNCHER_WIDTH / 2;
lcherPos.y = SCREEN_HEIGHT - LAUNCHER_HEIGHT;
centralizeBubble();
launcherRect.x = 0;
launcherRect.y = 0;
launcherRect.w = LAUNCHER_WIDTH;
launcherRect.h = LAUNCHER_HEIGHT;
bubSrc.x = 0;
bubSrc.y = 0;
bubSrc.w = BUB_SIZE;
bubSrc.h = BUB_SIZE;
gameover = 0;
launched = 0;
launcherDirection = 2;
angle = PI - launcherDirection * PI / LAUNCHER_DIV;
dX = VELOCITY * cos(angle);
dY = -VELOCITY * sin(angle);
while (!gameover)
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
HandleEvent(event);
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
backgroundPosition.x = 0;
backgroundPosition.y = 0;
SDL_BlitSurface(bckg, NULL, screen, &backgroundPosition);
if (launched)
{
if (BPosY <= BOARD_TOP + 1)
{
launched = 0;
}
if (BPosX <= BOARD_LEFT + 1)
{
BPosX = BPosX + 1;
dX = -dX;
}
if (BPosX + BUB_SIZE >= BOARD_RIGHT - 1)
{
BPosX = BPosX - 1;
dX = -dX;
}
BPosX += dX;
BPosY += dY;
bubblePosition.x = (int)BPosX;
bubblePosition.y = (int)BPosY;
}
SDL_BlitSurface(lcher, &launcherRect, screen, &lcherPos);
SDL_BlitSurface(bub, &bubSrc, screen, &bubblePosition);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
/* clean up */
SDL_FreeSurface(lcher);
SDL_FreeSurface(bckg);
SDL_Quit();
return 0;
} |
Partager