Created
November 15, 2016 22:17
-
-
Save simplexityx/3d0bdac3bf48257364512c34b670a7b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
#include "SDL.h" | |
#include "drawline.h" | |
#include "triangle.h" | |
#include "list.h" | |
#include "teapot_data.h" | |
#include "sphere_data.h" | |
#include "object.h" | |
#include <time.h> | |
#define MIN(x,y) (x < y ? x : y) | |
#define MAX(x,y) (x > y ? x : y) | |
// Clear screen by filling it with 0 | |
void ClearScreen(SDL_Surface *screen) | |
{ | |
SDL_Rect rect; | |
// Define a rectangle covering the entire screen | |
rect.x = 0; | |
rect.y = 0; | |
rect.w = screen->w; | |
rect.h = screen->h; | |
// And fill screen with 0 | |
SDL_FillRect(screen, &rect, 0); | |
} | |
// Add some speed to an object | |
void AccelerateObject(object_t *a, float boost, float maxspeed) | |
{ | |
float s; | |
float news; | |
// Calculate lenght of speed vector | |
s = sqrtf(a->speedx * a->speedx + a->speedy * a->speedy); | |
// Boost speed | |
news = s * boost; | |
if (news < 0.0) | |
news = 0.0; | |
if (news > maxspeed) | |
news = maxspeed; | |
a->speedx *= news/s; | |
a->speedy *= news/s; | |
} | |
void BouncingBalls(SDL_Surface *screen) | |
{ | |
//initializing lists and setting balls into list | |
SDL_Event event; | |
int quitting = 0; | |
int i; | |
int time; | |
list_iterator_t *iter; | |
list_t *list=list_create(); | |
iter = list_createiterator(list); | |
object_t *ball; | |
for(i = 0; i < 1; i++){ | |
ball = CreateObject(screen,sphere_model,SPHERE_NUMTRIANGLES); | |
printf("SpeedX: %f\tSpeedy: %f\tTX: %f\t TY: %f\n", ball->speedx, ball->speedy,ball->tx, ball->ty); | |
list_addlast(list,ball); | |
} | |
//initiliazing animation | |
while(!quitting) | |
{ | |
ClearScreen(screen); | |
/* Event-loop: goes through the event queue | |
* responding to the events we care about. | |
*/ | |
while (SDL_PollEvent(&event)) | |
{ | |
switch (event.type) | |
{ | |
case SDL_QUIT: // Quit events include Alt-F4, Ctrl-C, closing the window | |
quitting = 1; | |
break; | |
case SDL_KEYDOWN: | |
if (event.key.keysym.sym == SDLK_ESCAPE) // Quit with escape | |
quitting = 1; | |
else if ( event.key.keysym.sym == SDLK_SPACE ) { | |
ball = CreateObject(screen,sphere_model,SPHERE_NUMTRIANGLES); | |
printf("SpeedX: %f\tSpeedy: %f\tTX: %f\t TY: %f\n", ball->speedx, ball->speedy,ball->tx, ball->ty); | |
list_addlast(list,ball); | |
} | |
} | |
} | |
int iteratortime; | |
list_resetiterator(iter); | |
time=SDL_GetTicks(); | |
printf("Time: %d\n", time); | |
//animation loop | |
while((ball=list_next(iter))){ | |
iteratortime=SDL_GetTicks()-time; | |
printf("IteratorTime: %d\n", iteratortime); | |
DrawObject(ball); | |
ball->tx += ball->speedx; | |
ball->ty += ball->speedy; | |
ball->rotation+=ball->speedx; | |
if(ball->tx + ball->radius >= screen->w || ball->tx - ball->radius <= 0) | |
ball->speedx *= -1; | |
if(ball->ty + ball->radius >= screen->h || ball->ty - ball->radius <= 0){ | |
ball->speedy *= -1; | |
if(ball->speedx > 0.3 || ball->speedx < -0.3) { | |
if(ball->speedx > 0) | |
ball->speedx -= 0.5; | |
else if(ball->speedx < 0) | |
ball->speedx += 0.5; | |
}else | |
ball->speedx = 0.0; | |
if((ball->speedy < 0.5 && ball->speedy > -0.5) || (ball->speedy > -0.5 && ball->speedy < -0.5)) | |
ball->speedy = 0; | |
} | |
if(ball->speedy > 0.0) | |
ball->speedy += 0.5; | |
else if(ball->speedy < 0.0) | |
ball->speedy += 0.7; | |
if(ball->speedx > 0) | |
ball->speedx -= 0.015; | |
else if(ball->speedx < 0) | |
ball->speedx += 0.015; | |
if(ball->speedy==0){ | |
ball->ttl-=time; | |
if(ball->ttl<0) | |
{ | |
DestroyObject(ball); | |
list_remove(list,ball); | |
} | |
} | |
printf("TTL: %d\n", ball->ttl); | |
printf("SpeedX: %f\tSpeedY: %f\t\n", ball->speedx,ball->speedy); | |
} | |
SDL_UpdateRect(screen, 0, 0, 0, 0); | |
} | |
} | |
// First function run in your program | |
int main(int argc, char **argv) | |
{ | |
int retval; | |
SDL_Surface *screen; | |
// Initialize SDL | |
retval = SDL_Init(SDL_INIT_VIDEO); | |
if (retval == -1) { | |
printf("Unable to initialize SDL\n"); | |
exit(1); | |
} | |
// Create a 1024x768x32 window | |
screen = SDL_SetVideoMode(1024, 568, 32, 0); | |
if (screen == NULL) { | |
printf("Unable to get video surface: %s\n", SDL_GetError()); | |
exit(1); | |
} | |
BouncingBalls(screen); | |
// Shut down SDL | |
SDL_Quit(); | |
// Wait a little bit jic something went wrong (so that printfs can be read) | |
SDL_Delay(5000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment