Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2016 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/07151a2542db16a0fe9bc453351387d6 to your computer and use it in GitHub Desktop.
Save anonymous/07151a2542db16a0fe9bc453351387d6 to your computer and use it in GitHub Desktop.
#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)
{
list->numitems++;
return 0;
}
// Remove item from list
void list_remove(list_t *list, void *item)
{
free(item);
list->numitems--;
}
// Return # of items in list
int list_size(list_t *list)
{
printf("number of items in list is %d", list->numitems);
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 *iterator = malloc(sizeof(list_iterator_t));
iterator=malloc(sizeof(list_t));
iterator->next = list->head;
return iterator;
}
// 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)
{
list_t *neste;
if(iter->next == NULL){
printf("end of list");
}else{
printf("searching through the list!!!!");
neste=iter->next;
}
}
// Let iterator point to first item in list again
void list_resetiterator(list_iterator_t *iter)
{
iter->next=iter->list->head;
}
#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"
#define MIN(x,y) (x < y ? x : y)
#define MAX(x,y) (x > y ? x : y)
object_t *object;
// 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, object_t *ball)
{
SDL_Event event;
int quitting = 0;
//object_t *ball = CreateObject(screen, sphere_model, SPHERE_NUMTRIANGLES);
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;
}
}
DrawObject(ball);
ball->tx += ball->speedx;
if(ball->tx + ball->radius >= screen->w || ball->tx - ball->radius <= 0)
ball->speedx *= -1;
ball->ty += ball->speedy;
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;
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);
}
// Start bouncing some balls
list_t *list=list_create();
object_t *ball=CreateObject(screen, sphere_model, SPHERE_NUMTRIANGLES);
int a,b;
a=0;
while(a!=1){
printf("press 1 to add another ball to list\n");
scanf("%d", &b);
if(b==1){
list_addfirst(list,ball);
}else{
printf("do you wish to continue?\n");
scanf("%d", &a);
}
}
// 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