Created
November 13, 2016 13:03
-
-
Save simplexityx/dc3e5588797168920c0ddf4d7b56e13b 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 <stdio.h> | |
#include <stdlib.h> | |
#include "list.h" | |
/* | |
* List implementation | |
*/ | |
typedef struct listnode listnode_t; | |
struct listnode { | |
listnode_t *next; | |
void *item; | |
}; | |
struct list { | |
listnode_t *head; | |
int numitems; | |
}; | |
// Create new list | |
list_t *list_create(void) | |
{ | |
list_t *firstlist; | |
firstlist=malloc(sizeof(list_t)); | |
firstlist->head=NULL; | |
firstlist->numitems=0; | |
return firstlist; | |
} | |
// Free list. items not freed. | |
void list_destroy(list_t *list) | |
{ | |
free(list); | |
} | |
// Insert item first in list | |
int list_addfirst(list_t *list, void *item) | |
{ | |
listnode_t *node= (listnode_t*)malloc(sizeof(listnode_t)); | |
node->item = item; | |
node->next = list->head; | |
list->head = node; | |
list->numitems++; | |
return 0; | |
} | |
// Insert item last in list. | |
int list_addlast(list_t *list, void *item) | |
{ | |
listnode_t *node, *currentnode; | |
node=malloc(sizeof(listnode_t)); | |
node->next=NULL; | |
node->item=item; | |
currentnode = list->head; | |
if (currentnode == NULL) { | |
// Empty list, insert first | |
list->head = node; | |
} else if (currentnode->next == NULL) { | |
// List contains one item, insert after head | |
currentnode->next = node; | |
} else { | |
// Traverse until current points to node before last node | |
while (currentnode->next->next != NULL) { | |
currentnode = currentnode->next; | |
} | |
currentnode->next->next = node; | |
} | |
list->numitems++; | |
return 0; | |
} | |
// Remove item from list | |
void list_remove(list_t *list, void *item) | |
{ | |
listnode_t *node, *currentnode; | |
node = NULL; | |
if (list->head == NULL) { | |
return 0; | |
} else if (list->head->item == item) { | |
node = list->head; | |
list->head = node->next; | |
} else { | |
current = list->head; | |
while (currentnode->next != NULL && currentnode->next->item != item) | |
currentnode = currentnode->next; | |
if (currentnode->next == NULL) | |
node = currentnode->next; | |
currentnode->next = node->next; | |
} | |
free(node); | |
list->numitems--; | |
} | |
// Return # of items in list | |
int list_size(list_t *list) | |
{ | |
return list->numitems; | |
} | |
/* | |
* Iterator implementation | |
*/ | |
struct list_iterator { | |
listnode_t *next; | |
list_t *list; | |
}; | |
// Create new list iterator | |
list_iterator_t *list_createiterator(list_t *list) | |
{ | |
list_iterator_t *iter = malloc(sizeof(list_iterator_t)); | |
iter=malloc(sizeof(list_t)); | |
iter->next = list->head; | |
iter->list=list; | |
return iter; | |
} | |
// Free iterator | |
void list_destroyiterator(list_iterator_t *iter) | |
{ | |
free(iter); | |
} | |
// Move iterator to next item in list and return current. | |
void *list_next(list_iterator_t *iter) | |
{ | |
void *item; | |
item = NULL; | |
if (iter->next != NULL) { | |
item = iter->next->item; | |
iter->next = iter->next->next; | |
} | |
return item; | |
} | |
// Let iterator point to first item in list again | |
void list_resetiterator(list_iterator_t *iter) | |
{ | |
iter->next=iter->list->head; | |
} | |
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 listsize; | |
list_iterator_t *iter; | |
list_t *list=list_create(); | |
iter = list_createiterator(list); | |
listsize=list_size(list); | |
object_t *ball; | |
for(i = 0; i < 5; 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; | |
} | |
} | |
list_resetiterator(iter); | |
//animation loop | |
while((ball=list_next(iter))){ | |
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; | |
ball->ttl=0; | |
if(ball->speedy==0){ | |
SDL_Delay(500); | |
ball->ttl=ball->speedy; | |
} | |
if(ball->ttl==ball->speedy){ | |
list_remove(list,ball); | |
DestroyObject(ball); | |
} | |
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; | |
} |
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 "SDL.h" | |
#include "drawline.h" | |
#include "triangle.h" | |
#include "object.h" | |
#include <time.h> | |
#include <string.h> | |
// Create new object | |
object_t *CreateObject(SDL_Surface *screen, triangle_t *model, int numtriangles) | |
{ | |
object_t *new_object = malloc(sizeof(object_t)); | |
new_object->model = malloc(sizeof(triangle_t)*numtriangles); | |
memcpy(new_object->model, model, sizeof(triangle_t)*numtriangles); | |
new_object->screen = screen; | |
new_object->scale = 0.1; | |
new_object->radius = (1000 * new_object->scale) / 2; | |
new_object->rotation = 0.5; | |
new_object->tx = new_object->radius; | |
new_object->ty = new_object->radius; | |
new_object->speedx = 5+5*( (float)rand() / (float)RAND_MAX ); | |
new_object->speedy = 1.0; | |
new_object->ttl = 0; | |
new_object->numtriangles = numtriangles; | |
new_object->model = model; | |
new_object->screen = screen; | |
return new_object; | |
} | |
// Free memory used by object | |
void DestroyObject(object_t *object) | |
{ | |
free(object); | |
} | |
// Draw object on screen | |
void DrawObject(object_t *object) | |
{ | |
int i; | |
for(i = 0; i < object->numtriangles; i++) | |
{ | |
object->model[i].tx = object->tx; | |
object->model[i].ty = object->ty; | |
object->model[i].scale = object->scale; | |
object->model[i].rotation = object->rotation; | |
DrawTriangle(object->screen, &object->model[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment