rendered paste body/* SDL BOUNCEY BALL!!
*
* TonyB <tonyb@tonybox.net>
* Based on VB Code, suppose it would be good for teaching a VB user C.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL/SDL.h>
#ifdef _WIN32
#include <windows.h>
#define usleep(x) Sleep((x)/1000)
#else
#include <pthread.h>
#endif
#include <time.h>
int BallPosTop;
int BallPosLeft;
int VertAccel;
int HorizAccel;
SDL_Surface *screen;
void DrawFrame(SDL_Surface* block, SDL_Surface* thescreen );
void Timer1() { // Vertical Acceleration
BallPosTop = BallPosTop + VertAccel;
if(BallPosTop >= (600 - 7)) { VertAccel = (-1 * VertAccel) - 5;}
if(BallPosTop <= 0) { VertAccel = VertAccel * -1; }
}
void Timer2() { // Horizontal Acceleration
BallPosLeft = BallPosLeft + HorizAccel;
if(BallPosLeft >= (800 - 7)) { HorizAccel = (-1 * HorizAccel) - 5; }
if(BallPosLeft <= 0) { HorizAccel = HorizAccel * -1; }
}
void Timer3() { // Acceleration Limit
if(HorizAccel >= 50) { HorizAccel = 50; }
if(VertAccel >= 50) { VertAccel = 50; }
}
void Timer4() { // Gravity
if(BallPosTop <= (600 - 7) ) { VertAccel = VertAccel + 5; }
}
#ifdef _WIN32
DWORD WINAPI TimerThread( LPVOID lpParam ) {
#else
void *TimerThread(void *threadid) {
#endif
while(1) {
Timer1();
Timer2();
Timer3();
Timer4(); SDL_Delay(30);
}
}
void drawSprite(SDL_Surface* imageSurface, SDL_Surface* screenSurface, int srcX, int srcY, int dstX, int dstY, int width, int height)
{
SDL_Rect srcRect;
srcRect.x = srcX;
srcRect.y = srcY;
srcRect.w = width;
srcRect.h = height;
SDL_Rect dstRect;
dstRect.x = dstX;
dstRect.y = dstY;
dstRect.w = width;
dstRect.h = height;
SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}
#ifdef _WIN32
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
#else
int main(int argc, char *argv[]) {
#endif
SDL_Event event;
int done = 0;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 1;
}
screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
if (!screen) {
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
return 1;
}
SDL_WM_SetCaption("Bouncey!", "Bouncey!");
printf("SDL_LoadBMP [ block.bmp ]\r\n");
SDL_Surface* bitmap = SDL_LoadPNG("block.bmp");
printf("SDL_LoadBMP [ back.bmp ]\r\n");
SDL_Surface* back = SDL_LoadBMP("back.bmp");
//printf("SDL_LoadBMP [ lost_back.bmp ]\r\n");
//SDL_Surface* lost_back = SDL_LoadBMP("lost_back.bmp");
printf("Seeding Random Number Generator\r\n");
srand(time(NULL));
SDL_ShowCursor(SDL_ENABLE);
#ifdef _WIN32
printf("Loading Game Thread.\r\n");
CreateThread(NULL, 0, TimerThread, NULL, 0, NULL);
#else
pthread_t ithread;
pthread_t gthread;
printf("Loading Game Thread.\r\n");
pthread_create(&ithread, NULL, TimerThread, NULL);
#endif
VertAccel = 20; HorizAccel = 20;
while(1) {
SDL_PollEvent(&event);
if(event.type==SDL_QUIT) { exit(0); }
SDL_Flip(screen);
drawSprite(back, screen, 0, 0, 0, 0, back->w, back->h); // just-lost background
DrawFrame(bitmap, screen);
SDL_Delay(20);
}
}
void DrawFrame(SDL_Surface* block, SDL_Surface* thescreen) {
drawSprite(block, thescreen, 0, 0, BallPosLeft, BallPosTop, block->w, block->h); // draw it.
}